Bug fixes

Ratchet Exception now accepts SocketInterface for better troubleshooting
WebSocket protocol calls onOpen on child app after handshake
Misc little bugs found
This commit is contained in:
Chris Boden 2011-11-09 10:46:42 -05:00
parent c8a0911452
commit cf3ba7c4ad
7 changed files with 49 additions and 22 deletions

View File

@ -5,12 +5,19 @@ namespace Ratchet;
* Uses internal php methods to fill an Exception class (no parameters required) * Uses internal php methods to fill an Exception class (no parameters required)
*/ */
class Exception extends \Exception { class Exception extends \Exception {
public function __construct() { protected $_socket;
public function __construct(SocketInterface $socket) {
$int = socket_last_error(); $int = socket_last_error();
$msg = socket_strerror($int); $msg = socket_strerror($int);
// todo, replace {$msg: $int} to {$msg} $this->_socket = $socket;
//@socket_clear_error($socket->getResource());
parent::__construct($msg, $int); parent::__construct($msg, $int);
} }
public function getSocket() {
return $this->_socket;
}
} }

View File

@ -66,8 +66,6 @@ class WebSocket implements ProtocolInterface {
public function onOpen(SocketInterface $conn) { public function onOpen(SocketInterface $conn) {
$this->_clients[$conn] = new Client; $this->_clients[$conn] = new Client;
$cmds = $this->_app->onOpen($conn);
return $this->prepareCommand($cmds);
} }
public function onRecv(SocketInterface $from, $msg) { public function onRecv(SocketInterface $from, $msg) {
@ -89,8 +87,11 @@ class WebSocket implements ProtocolInterface {
$header = $response; $header = $response;
} }
// here, need to send headers/handshake to application, let it have the cookies, etc $comp = $this->_factory->newComposite();
return $this->_factory->newCommand('SendMessage', $from)->setMessage($header); $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 { try {

View File

@ -1,6 +1,7 @@
<?php <?php
namespace Ratchet\Protocol\WebSocket; namespace Ratchet\Protocol\WebSocket;
use Ratchet\SocketObserver; use Ratchet\SocketObserver;
use Ratchet\SocketInterface;
/** /**
* @todo App interfaces this (optionally) if is meant for WebSocket * @todo App interfaces this (optionally) if is meant for WebSocket
@ -11,4 +12,11 @@ interface AppInterface extends SocketObserver {
* @return string * @return string
*/ */
function getSubProtocol(); function getSubProtocol();
/**
* @param Ratchet\SocketInterface
* @param string
* @return Ratchet\Command\CommandInterface|null
*/
function onOpen(SocketInterface $conn, $headers);
} }

View File

@ -6,6 +6,7 @@ namespace Ratchet\Protocol\WebSocket\Util;
*/ */
class HTTP { class HTTP {
/** /**
* @todo Probably should iterate through the array, strtolower all the things, then return it
* @param string * @param string
* @return array * @return array
*/ */

View File

@ -74,7 +74,7 @@ class HyBi10 implements VersionInterface {
default: default:
// Close connection on unknown opcode: // Close connection on unknown opcode:
throw new UnexpectedValueException('Unknown opcode'); throw new \UnexpectedValueException('Unknown opcode');
break; break;
} }

View File

@ -78,14 +78,15 @@ class Server implements SocketObserver, \IteratorAggregate {
ob_implicit_flush(); ob_implicit_flush();
if (false === ($this->_master->bind($address, (int)$port))) { if (false === ($this->_master->bind($address, (int)$port))) {
throw new Exception; throw new Exception($this->_master);
} }
if (false === ($this->_master->listen())) { if (false === ($this->_master->listen())) {
throw new Exception; throw new Exception($this->_master);
} }
$this->_master->set_nonblock(); $this->_master->set_nonblock();
declare(ticks = 1);
do { do {
try { try {
@ -112,14 +113,18 @@ class Server implements SocketObserver, \IteratorAggregate {
} }
} }
} catch (Exception $se) { } 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) { } catch (\Exception $e) {
$this->_log->error('Big uh oh: ' . $e->getMessage()); $this->_log->error('Big uh oh: ' . $e->getMessage());
} }
} while (true); } while (true);
// $this->_master->set_nonblock();
// declare(ticks = 1);
} }
public function onOpen(SocketInterface $conn) { public function onOpen(SocketInterface $conn) {
@ -133,7 +138,7 @@ class Server implements SocketObserver, \IteratorAggregate {
} }
public function onRecv(SocketInterface $from, $msg) { 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); return $this->_app->onRecv($from, $msg);
} }

View File

@ -30,7 +30,7 @@ class Socket implements SocketInterface {
$this->_resource = @socket_create($domain, $type, $protocol); $this->_resource = @socket_create($domain, $type, $protocol);
if (!is_resource($this->_resource)) { 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); $this->_resource = @socket_accept($this->_resource);
if (false === $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); $num = socket_select($read, $write, $except, $tv_sec, $tv_usec);
if (false === $num) { if (false === $num) {
throw new Exception; throw new Exception($this);
} }
return $num; return $num;
@ -88,13 +88,18 @@ class Socket implements SocketInterface {
/** /**
* @see http://ca3.php.net/manual/en/function.socket-recv.php * @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
* @param int * @return int Number of bytes received
* @return int * @throws Exception
*/ */
public function recv(&$buf, $len, $flags) { 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); $result = @call_user_func_array('socket_' . $method, $arguments);
if (false === $result) { if (false === $result) {
throw new Exception; throw new Exception($this);
} }
// onAfterMethod // onAfterMethod