Skip to content

Commit

Permalink
fix fatal err
Browse files Browse the repository at this point in the history
  • Loading branch information
walkor committed Sep 16, 2015
1 parent 38a2004 commit 7fb397d
Show file tree
Hide file tree
Showing 9 changed files with 87 additions and 29 deletions.
38 changes: 28 additions & 10 deletions src/Engine/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ public function handleRequest($req, $res)
{
$this->prepare($req);
$req->res = $res;
$this->verify($req, false, array($this, 'dealRequest'));
$this->verify($req, $res, false, array($this, 'dealRequest'));
}

public function dealRequest($err, $success, $req)
public function dealRequest($err, $success, $req, $res)
{
if (!$success)
{
Expand Down Expand Up @@ -98,33 +98,33 @@ protected function sendErrorMessage($req, $res, $code)
)));
}

protected function verify($req, $upgrade, $fn)
protected function verify($req, $res, $upgrade, $fn)
{
if(!isset($req->_query['transport']) || !isset(self::$allowTransports[$req->_query['transport']]))
{
return call_user_func($fn, self::ERROR_UNKNOWN_TRANSPORT, false, $req);
return call_user_func($fn, self::ERROR_UNKNOWN_TRANSPORT, false, $req, $res);
}
$transport = $req->_query['transport'];
$sid = isset($req->_query['sid']) ? $req->_query['sid'] : '';
if($sid)
{
if(!isset($this->clients[$sid]))
{
return call_user_func($fn, self::ERROR_UNKNOWN_SID, false, $req);
return call_user_func($fn, self::ERROR_UNKNOWN_SID, false, $req, $res);
}
if(!$upgrade && $this->clients[$sid]->transport->name !== $transport)
{
return call_user_func($fn, self::ERROR_BAD_REQUEST, false, $req);
return call_user_func($fn, self::ERROR_BAD_REQUEST, false, $req, $res);
}
}
else
{
if('GET' !== $req->method)
{
return call_user_func($fn, self::ERROR_BAD_HANDSHAKE_METHOD, false, $req);
return call_user_func($fn, self::ERROR_BAD_HANDSHAKE_METHOD, false, $req, $res);
}
}
call_user_func($fn, null, true, $req);
call_user_func($fn, null, true, $req, $res);
}

protected function prepare($req)
Expand Down Expand Up @@ -211,7 +211,25 @@ public function onConnect($connection)
public function onWebSocketConnect($connection, $req, $res)
{
$this->prepare($req);
$transport = new WebSocket($req);
$this->clients[$req->_query['sid']]->maybeUpgrade($transport);
$this->verify($req, $res, true, array($this, 'dealWebSocketConnect'));
}

public function dealWebSocketConnect($err, $success, $req, $res)
{
if (!$success)
{
self::sendErrorMessage($req, $res, $err);
return;
}

if(isset($req->_query['sid']))
{
$transport = new WebSocket($req);
$this->clients[$req->_query['sid']]->maybeUpgrade($transport);
}
else
{
$this->handshake($req->_query['transport'], $req);
}
}
}
8 changes: 7 additions & 1 deletion src/Engine/Protocols/Http/Response.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ class Response

public $headersSent = false;

public $writable = true;

public function __construct($connection)
{
Expand All @@ -32,7 +33,6 @@ public function writeHead($status_code, $reason_phrase = '', $headers = null)
echo "header has already send\n";
return false;
}

$this->statusCode = $status_code;
if($reason_phrase)
{
Expand Down Expand Up @@ -112,6 +112,11 @@ public function write($chunk)

public function end($data = null)
{
if(!$this->writable)
{
echo new \Exception('unwirtable');
return false;
}
if(!isset($this->_headers['Content-Length']))
{
if(null !== $data)
Expand Down Expand Up @@ -145,6 +150,7 @@ public function destroy()
}
$this->_connection->httpResponse = $this->_connection->httpRequest = null;
$this->_connection = null;
$this->writable = false;
}

public static $codes = array(
Expand Down
4 changes: 2 additions & 2 deletions src/Engine/Protocols/SocketIO.php
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ public static function cleanup($connection)

public static function emitData($connection, $req, $data)
{
if($req->onData)
if(isset($req->onData))
{
try
{
Expand All @@ -170,7 +170,7 @@ public static function emitData($connection, $req, $data)

public static function emitEnd($connection, $req)
{
if($req->onEnd)
if(isset($req->onEnd))
{
try
{
Expand Down
2 changes: 1 addition & 1 deletion src/Engine/Protocols/WebSocket.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ public static function dealHandshake($connection, $req, $res)
{
if(isset($req->headers['sec-websocket-key1']))
{
$res->wirteHead(400);
$res->writeHead(400);
$res->end("Not support");
return 0;
}
Expand Down
4 changes: 4 additions & 0 deletions src/Engine/Protocols/WebSocket/RFC6455.php
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,10 @@ public static function dealHandshake($connection, $req, $res)
{
echo $e;
}
if(!$res->writable)
{
return false;
}
}

if(isset($req->headers['sec-websocket-key']))
Expand Down
53 changes: 42 additions & 11 deletions src/Engine/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,46 +36,78 @@ public function maybeUpgrade($transport)
);
$this->upgradeTransport = $transport;
$transport->on('packet', array($this, 'onUpgradePacket'));
$transport->once('close', array($this, 'onUpgradeTransportClose'));
$transport->once('error', array($this, 'onUpgradeTransportError'));
$this->once('close', array($this, 'onUpgradeTransportClose'));
}

public function onUpgradePacket($packet)
{
if('ping' === $packet['type'] && 'probe' === $packet['data'])
if(empty($this->upgradeTransport))
{
$this->onError('upgradeTransport empty');
return;
}
if('ping' === $packet['type'] && (isset($packet['data']) && 'probe' === $packet['data']))
{
$this->upgradeTransport->send(array(array('type'=> 'pong', 'data'=> 'probe')));
Timer::del($this->checkIntervalTimer);
$this->checkIntervalTimer = Timer::add(0.1, array($this, 'check'));
}
else if ('upgrade' === $packet['type'] && $this->readyState !== 'closed')
{
$this->cleanup();
$this->upgraded = true;
$this->clearTransport();
$this->upgradeTransport->removeAllListeners('packet');
$this->setTransport($this->upgradeTransport);
$this->upgradeTransport = null;
$this->emit('upgrade', $this->upgradeTransport);
$this->upgradeTransport = null;
$this->setPingTimeout();
$this->flush();
Timer::del($this->checkIntervalTimer);
$this->checkIntervalTimer = null;
Timer::del($this->upgradeTimeoutTimer);
if($this->readyState === 'closing')
{
$this->transport->close(array($this, 'onClose'));
}
}
else
{
$this->upgradeTransport->close();
$this->upgradeTransport = null;
if(!empty($this->upgradeTransport))
{
$this->cleanup();
$this->upgradeTransport->close();
$this->upgradeTransport = null;
}
}

}

public function cleanup()
{
Timer::del($this->checkIntervalTimer);
Timer::del($this->upgradeTimeoutTimer);
$this->upgradeTransport->removeListener('packet', array($this, 'onUpgradePacket'));
$this->upgradeTransport->removeListener('close', array($this, 'onUpgradeTransportClose'));
$this->upgradeTransport->removeListener('error', array($this, 'onUpgradeTransportError'));
$this->removeListener('close', array($this, 'onUpgradeTransportClose'));
}

public function onUpgradeTransportClose()
{
$this->onUpgradeTransportError('transport closed');
}

public function onUpgradeTransportError($err)
{
echo $err;
$this->cleanup();
$this->upgradeTransport->close();
$this->upgradeTransport = null;
}

public function upgradeTimeoutCallback($transport)
{
echo("client did not complete upgrade - closing transport\n");
$this->checkIntervalTimer = null;
$this->cleanup();
if('open' == $transport->readyState)
{
$transport->close();
Expand Down Expand Up @@ -166,6 +198,7 @@ public function setPingTimeout()

public function clearTransport()
{
$this->transport->close();
Timer::del($this->pingTimeoutTimer);
}

Expand Down Expand Up @@ -282,8 +315,6 @@ public function closeTransport()
{
//todo onClose.bind(this, 'forced close'));
$this->transport->close(array($this, 'onClose'));
$this->transport->removeAllListeners();
$this->transport = null;
}

public function setupSendCallback()
Expand Down
2 changes: 1 addition & 1 deletion src/Engine/Transport.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public function onRequest($req)
$this->req = $req;
}

public function close($fn)
public function close($fn = null)
{
$this->readyState = 'closing';
$fn = $fn ? $fn : array($this, 'noop');
Expand Down
3 changes: 1 addition & 2 deletions src/Engine/Transports/Polling.php
Original file line number Diff line number Diff line change
Expand Up @@ -191,8 +191,7 @@ public function write($data)

public function doClose($fn)
{

if($this->dataReq)
if(!empty($this->dataReq))
{
echo('aborting ongoing data request');
$this->dataReq->destroy();
Expand Down
2 changes: 1 addition & 1 deletion src/Socket.php
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ public function send()
return $this;
}

public function wirte()
public function write()
{
$args = func_get_args();
array_unshift($args, 'message');
Expand Down

0 comments on commit 7fb397d

Please sign in to comment.