From 33bf91dcea953ff2c94674ff84c5e78715da4c66 Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Wed, 1 Feb 2012 20:52:10 -0500 Subject: [PATCH] Socket refactor Moved SocketInterface and Socket to Resource namespace --- README.md | 24 +++++----- .../Component/Server/IOServerComponent.php | 4 +- src/Ratchet/Resource/Connection.php | 4 +- .../Socket/BSDSocket.php} | 44 +++++++++---------- .../Socket/BSDSocketException.php} | 8 ++-- .../{ => Resource/Socket}/SocketInterface.php | 3 +- tests/Ratchet/Tests/Mock/FakeSocket.php | 4 +- .../Socket/BSDSocketTest.php} | 10 ++--- 8 files changed, 51 insertions(+), 50 deletions(-) rename src/Ratchet/{Socket.php => Resource/Socket/BSDSocket.php} (86%) rename src/Ratchet/{Exception.php => Resource/Socket/BSDSocketException.php} (72%) rename src/Ratchet/{ => Resource/Socket}/SocketInterface.php (96%) rename tests/Ratchet/Tests/{SocketTest.php => Resource/Socket/BSDSocketTest.php} (87%) diff --git a/README.md b/README.md index e6f762e..704f6d7 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ A PHP 5.3 (PSR-0 compliant) component library for serving sockets and building socket based applications. Build up your application through simple interfaces using the decorator and command patterns. Re-use your application without changing any of its code just by wrapping it in a different protocol. -Ratchet's primary intention is to be used as a WebSocket server. +Ratchet's primary intention is to be used as a WebSocket server (and a client in 0.6). ##WebSockets @@ -38,11 +38,11 @@ I'm looking into a couple daemonized servers written in PHP to run Ratchet on to ```php _clients = new \SplObjectStorage; } - public function onOpen(Connection $conn) { + public function onOpen(ConnectionInterface $conn) { $this->_clients->attach($conn); } - public function onMessage(Connection $from, $msg) { + public function onMessage(ConnectionInterface $from, $msg) { $commands = new Cmds; foreach ($this->_clients as $client) { @@ -77,11 +77,11 @@ class Chat implements ComponentInterface { return $commands; } - public function onClose(Connection $conn) { + public function onClose(ConnectionInterface $conn) { $this->_clients->detach($conn); } - public function onError(Connection $conn, \Exception $e) { + public function onError(ConnectionInterface $conn, \Exception $e) { return new CloseConnection($conn); } } diff --git a/src/Ratchet/Component/Server/IOServerComponent.php b/src/Ratchet/Component/Server/IOServerComponent.php index 47479e0..cd0d403 100644 --- a/src/Ratchet/Component/Server/IOServerComponent.php +++ b/src/Ratchet/Component/Server/IOServerComponent.php @@ -1,7 +1,7 @@ * @link http://ca2.php.net/manual/en/book.sockets.php */ -class Socket implements SocketInterface { +class BSDSocket implements SocketInterface { /** * @type resource */ @@ -23,7 +22,7 @@ class Socket implements SocketInterface { * @param int Specifies the protocol family to be used by the socket. * @param int The type of communication to be used by the socket * @param int Sets the specific protocol within the specified domain to be used when communicating on the returned socket - * @throws Ratchet\Exception + * @throws BSDSocketException */ public function __construct($domain = null, $type = null, $protocol = null) { list($domain, $type, $protocol) = static::getConfig($domain, $type, $protocol); @@ -31,7 +30,7 @@ class Socket implements SocketInterface { $this->_resource = @socket_create($domain, $type, $protocol); if (!is_resource($this->_resource)) { - throw new Exception($this); + throw new BSDSocketException($this); } } @@ -55,7 +54,7 @@ class Socket implements SocketInterface { $this->_resource = @socket_accept($this->_resource); if (false === $this->_resource) { - throw new Exception($this); + throw new BSDSocketException($this); } } @@ -71,7 +70,7 @@ class Socket implements SocketInterface { public function bind($address, $port = 0) { if (false === @socket_bind($this->getResource(), $address, $port)) { - throw new Exception($this); + throw new BSDSocketException($this); } return $this; @@ -84,7 +83,7 @@ class Socket implements SocketInterface { public function connect($address, $port = 0) { if (false === @socket_connect($this->getResource(), $address, $port)) { - throw new Exception($this); + throw new BSDSocketException($this); } return $this; @@ -93,7 +92,7 @@ class Socket implements SocketInterface { public function getRemoteAddress() { $address = $port = ''; if (false === @socket_getpeername($this->getResource(), $address, $port)) { - throw new Exception($this); + throw new BSDSocketException($this); } return $address; @@ -101,7 +100,7 @@ class Socket implements SocketInterface { public function get_option($level, $optname) { if (false === ($res = @socket_get_option($this->getResource(), $level, $optname))) { - throw new Exception($this); + throw new BSDSocketException($this); } return $res; @@ -109,7 +108,7 @@ class Socket implements SocketInterface { public function listen($backlog = 0) { if (false === @socket_listen($this->getResource(), $backlog)) { - throw new Exception($this); + throw new BSDSocketException($this); } return $this; @@ -117,7 +116,7 @@ class Socket implements SocketInterface { public function read($length, $type = PHP_BINARY_READ) { if (false === ($res = @socket_read($this->getResource(), $length, $type))) { - throw new Exception($this); + throw new BSDSocketException($this); } return $res; @@ -129,11 +128,11 @@ class Socket implements SocketInterface { * @param int Number of bytes to read * @param int * @return int Number of bytes received - * @throws Exception + * @throws BSDSocketException */ public function recv(&$buf, $len, $flags) { if (false === ($bytes = @socket_recv($this->_resource, $buf, $len, $flags))) { - throw new Exception($this); + throw new BSDSocketException($this); } return $bytes; @@ -148,7 +147,7 @@ class Socket implements SocketInterface { * @param int The tv_sec and tv_usec together form the timeout parameter. The timeout is an upper bound on the amount of time elapsed before socket_select() return. tv_sec may be zero , causing socket_select() to return immediately. This is useful for polling. If tv_sec is NULL (no timeout), socket_select() can block indefinitely. * @param int * @throws \InvalidArgumentException - * @throws Exception + * @throws BSDSocketException */ public function select(&$read, &$write, &$except, $tv_sec, $tv_usec = 0) { $read = static::mungForSelect($read); @@ -158,7 +157,7 @@ class Socket implements SocketInterface { $num = socket_select($read, $write, $except, $tv_sec, $tv_usec); if (false === $num) { - throw new Exception($this); + throw new BSDException($this); } return $num; @@ -166,7 +165,7 @@ class Socket implements SocketInterface { public function set_block() { if (false === @socket_set_block($this->getResource())) { - throw new Exception($this); + throw new BSDSocketException($this); } return $this; @@ -174,7 +173,7 @@ class Socket implements SocketInterface { public function set_nonblock() { if (false === @socket_set_nonblock($this->getResource())) { - throw new Exception($this); + throw new BSDSocketException($this); } return $this; @@ -182,7 +181,7 @@ class Socket implements SocketInterface { public function set_option($level, $optname, $optval) { if (false === @socket_set_option($this->getResource(), $level, $optname, $optval)) { - throw new Exception($this); + throw new BSDSocketException($this); } return $this; @@ -190,7 +189,7 @@ class Socket implements SocketInterface { public function shutdown($how = 2) { if (false === @socket_shutdown($this->getResource(), $how)) { - throw new Exception($this); + throw new BSDSocketException($this); } return $this; @@ -198,7 +197,7 @@ class Socket implements SocketInterface { public function write($buffer, $length = 0) { if (false === ($res = @socket_write($this->getResource(), $buffer, $length))) { - throw new Exception($this); + throw new BSDSocketException($this); } return $res; @@ -238,7 +237,8 @@ class Socket implements SocketInterface { $return = array(); foreach ($collection as $key => $socket) { - $return[$key] = ($socket instanceof \Ratchet\Socket ? $socket->getResource() : $socket); +die("Checking if sock is instance of this: " . (int)($socket instanceof $this) . "\n"); + $return[$key] = ($socket instanceof $this ? $socket->getResource() : $socket); } return $return; diff --git a/src/Ratchet/Exception.php b/src/Ratchet/Resource/Socket/BSDSocketException.php similarity index 72% rename from src/Ratchet/Exception.php rename to src/Ratchet/Resource/Socket/BSDSocketException.php index 3fc2b16..b3ef30e 100644 --- a/src/Ratchet/Exception.php +++ b/src/Ratchet/Resource/Socket/BSDSocketException.php @@ -1,16 +1,16 @@ setExpectedException('\\Ratchet\\Exception'); + $this->setExpectedException('\\Ratchet\\Resource\\Socket\\BSDSocketException'); $socket = new RealSocket('invalid', 'param', 'derp'); }