From cf3ba7c4adc5888d7a5ee43f523eb76bc392ccd1 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Wed, 9 Nov 2011 10:46:42 -0500 Subject: [PATCH] Bug fixes Ratchet Exception now accepts SocketInterface for better troubleshooting WebSocket protocol calls onOpen on child app after handshake Misc little bugs found --- lib/Ratchet/Exception.php | 11 ++++++++-- lib/Ratchet/Protocol/WebSocket.php | 9 ++++---- .../Protocol/WebSocket/AppInterface.php | 8 +++++++ lib/Ratchet/Protocol/WebSocket/Util/HTTP.php | 1 + .../Protocol/WebSocket/Version/HyBi10.php | 2 +- lib/Ratchet/Server.php | 19 ++++++++++------- lib/Ratchet/Socket.php | 21 ++++++++++++------- 7 files changed, 49 insertions(+), 22 deletions(-) diff --git a/lib/Ratchet/Exception.php b/lib/Ratchet/Exception.php index c1f217a..9d0087f 100644 --- a/lib/Ratchet/Exception.php +++ b/lib/Ratchet/Exception.php @@ -5,12 +5,19 @@ namespace Ratchet; * Uses internal php methods to fill an Exception class (no parameters required) */ class Exception extends \Exception { - public function __construct() { + protected $_socket; + + public function __construct(SocketInterface $socket) { $int = socket_last_error(); $msg = socket_strerror($int); - // todo, replace {$msg: $int} to {$msg} + $this->_socket = $socket; + //@socket_clear_error($socket->getResource()); parent::__construct($msg, $int); } + + public function getSocket() { + return $this->_socket; + } } \ No newline at end of file diff --git a/lib/Ratchet/Protocol/WebSocket.php b/lib/Ratchet/Protocol/WebSocket.php index 1713569..f294e12 100644 --- a/lib/Ratchet/Protocol/WebSocket.php +++ b/lib/Ratchet/Protocol/WebSocket.php @@ -66,8 +66,6 @@ class WebSocket implements ProtocolInterface { public function onOpen(SocketInterface $conn) { $this->_clients[$conn] = new Client; - $cmds = $this->_app->onOpen($conn); - return $this->prepareCommand($cmds); } public function onRecv(SocketInterface $from, $msg) { @@ -89,8 +87,11 @@ class WebSocket implements ProtocolInterface { $header = $response; } - // here, need to send headers/handshake to application, let it have the cookies, etc - return $this->_factory->newCommand('SendMessage', $from)->setMessage($header); + $comp = $this->_factory->newComposite(); + $comp->enqueue($this->_factory->newCommand('SendMessage', $from)->setMessage($header)); + $comp->enqueue($this->prepareCommand($this->_app->onOpen($from, $msg))); // Need to send headers/handshake to application, let it have the cookies, etc + + return $comp; } try { diff --git a/lib/Ratchet/Protocol/WebSocket/AppInterface.php b/lib/Ratchet/Protocol/WebSocket/AppInterface.php index f96a9a2..0b613a7 100644 --- a/lib/Ratchet/Protocol/WebSocket/AppInterface.php +++ b/lib/Ratchet/Protocol/WebSocket/AppInterface.php @@ -1,6 +1,7 @@ _master->bind($address, (int)$port))) { - throw new Exception; + throw new Exception($this->_master); } if (false === ($this->_master->listen())) { - throw new Exception; + throw new Exception($this->_master); } $this->_master->set_nonblock(); + declare(ticks = 1); do { try { @@ -112,14 +113,18 @@ class Server implements SocketObserver, \IteratorAggregate { } } } catch (Exception $se) { - $this->_log->error($se->getMessage()); + // Instead of logging error, will probably add/trigger onIOError/onError or something in SocketObserver + $this->_log->error($se->getCode() . ' - ' . $se->getMessage()); + + // temporary, move to application + if ($se->getCode() != 35) { + $close = new \Ratchet\Command\Action\CloseConnection($se->getSocket()); + $close->execute($this); + } } catch (\Exception $e) { $this->_log->error('Big uh oh: ' . $e->getMessage()); } } while (true); - -// $this->_master->set_nonblock(); -// declare(ticks = 1); } public function onOpen(SocketInterface $conn) { @@ -133,7 +138,7 @@ class Server implements SocketObserver, \IteratorAggregate { } public function onRecv(SocketInterface $from, $msg) { - $this->_log->note('New message "' . trim($msg) . '"'); +// $this->_log->note('New message "' . trim($msg) . '"'); return $this->_app->onRecv($from, $msg); } diff --git a/lib/Ratchet/Socket.php b/lib/Ratchet/Socket.php index f24f97a..0ee614d 100644 --- a/lib/Ratchet/Socket.php +++ b/lib/Ratchet/Socket.php @@ -30,7 +30,7 @@ class Socket implements SocketInterface { $this->_resource = @socket_create($domain, $type, $protocol); if (!is_resource($this->_resource)) { - throw new Exception; + throw new Exception($this); } } @@ -49,7 +49,7 @@ class Socket implements SocketInterface { $this->_resource = @socket_accept($this->_resource); if (false === $this->_resource) { - throw new Exception; + throw new Exception($this); } } @@ -72,7 +72,7 @@ class Socket implements SocketInterface { $num = socket_select($read, $write, $except, $tv_sec, $tv_usec); if (false === $num) { - throw new Exception; + throw new Exception($this); } return $num; @@ -88,13 +88,18 @@ class Socket implements SocketInterface { /** * @see http://ca3.php.net/manual/en/function.socket-recv.php - * @param string + * @param string Variable to write data to + * @param int Number of bytes to read * @param int - * @param int - * @return int + * @return int Number of bytes received + * @throws Exception */ public function recv(&$buf, $len, $flags) { - return socket_recv($this->_resource, $buf, $len, $flags); + if (false === ($bytes = @socket_recv($this->_resource, $buf, $len, $flags))) { + throw new Exception($this); + } + + return $bytes; } /** @@ -176,7 +181,7 @@ class Socket implements SocketInterface { $result = @call_user_func_array('socket_' . $method, $arguments); if (false === $result) { - throw new Exception; + throw new Exception($this); } // onAfterMethod