Socket refactor

Moved SocketInterface and Socket to Resource namespace
This commit is contained in:
Chris Boden 2012-02-01 20:52:10 -05:00
parent e26dc0ccde
commit 33bf91dcea
8 changed files with 51 additions and 50 deletions

View File

@ -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
<?php
namespace MyApps;
use Ratchet\Component\ComponentInterface;
use Ratchet\Resource\Connection;
use Ratchet\Socket;
use Ratchet\Component\Server\App as Server;
use Ratchet\Component\WebSocket\App as WebSocket;
use Ratchet\Component\MessageComponentInterface;
use Ratchet\Resource\ConnectionInterface;
use Ratchet\Resource\BSDSocket as Socket;
use Ratchet\Component\Server\IOServerComponent as Server;
use Ratchet\Component\WebSocket\WebSocketComponent as WebSocket;
use Ratchet\Resource\Command\Composite as Cmds;
use Ratchet\Resource\Command\Action\SendMessage;
use Ratchet\Resource\Command\Action\CloseConnection;
@ -51,18 +51,18 @@ use Ratchet\Resource\Command\Action\CloseConnection;
* chat.php
* Send any incoming messages to all connected clients (except sender)
*/
class Chat implements ComponentInterface {
class Chat implements MessageComponentInterface {
protected $_clients;
public function __construct(ComponentInterface $app = null) {
public function __construct(MessageComponentInterface $app = null) {
$this->_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);
}
}

View File

@ -1,7 +1,7 @@
<?php
namespace Ratchet\Component\Server;
use Ratchet\Component\MessageComponentInterface;
use Ratchet\SocketInterface;
use Ratchet\Resource\Socket\SocketInterface;
use Ratchet\Resource\ConnectionInterface;
use Ratchet\Resource\Connection;
use Ratchet\Resource\Command\CommandInterface;
@ -63,7 +63,7 @@ class IOServerComponent implements MessageComponentInterface {
/*
* Run the server infinitely
* @param Ratchet\SocketInterface
* @param Ratchet\Resource\Socket\SocketInterface
* @param mixed The address to listen for incoming connections on. "0.0.0.0" to listen from anywhere
* @param int The port to listen to connections on (make sure to run as root if < 1000)
* @throws Ratchet\Exception

View File

@ -1,6 +1,6 @@
<?php
namespace Ratchet\Resource;
use Ratchet\SocketInterface;
use Ratchet\Resource\Socket\SocketInterface;
/**
* A proxy object representing a connection to the application
@ -10,7 +10,7 @@ class Connection implements ConnectionInterface {
protected $_data = array();
/**
* @var Ratchet\SocketInterface
* @var Ratchet\Resource\Socket\SocketInterface
*/
protected $_socket;

View File

@ -1,13 +1,12 @@
<?php
namespace Ratchet;
use Ratchet\Component\ProtocolInterface;
namespace Ratchet\Resource\Socket;
/**
* A wrapper for the PHP socket_ functions
* @author Chris Boden <shout at chrisboden dot ca>
* @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;

View File

@ -1,16 +1,16 @@
<?php
namespace Ratchet;
namespace Ratchet\Resource\Socket;
/**
* Uses internal php methods to fill an Exception class (no parameters required)
*/
class Exception extends \Exception {
class BSDSocketException extends \Exception {
/**
* @var SocketInterface
* @var BSDSocket
*/
protected $_socket;
public function __construct(SocketInterface $socket) {
public function __construct(BSDSocket $socket) {
$int = socket_last_error();
$msg = socket_strerror($int);

View File

@ -1,8 +1,9 @@
<?php
namespace Ratchet;
namespace Ratchet\Resource\Socket;
/**
* An object-oriented container for a single socket connection
* @todo Major refactor when socket streams are implemented against this interface
*/
interface SocketInterface {
/**

View File

@ -1,7 +1,7 @@
<?php
namespace Ratchet\Tests\Mock;
use Ratchet\SocketInterface;
use Ratchet\Socket as RealSocket;
use Ratchet\Resource\Socket\SocketInterface;
use Ratchet\Resource\Socket\BSDSocket as RealSocket;
class FakeSocket implements SocketInterface {
public $_arguments = array();

View File

@ -1,12 +1,12 @@
<?php
namespace Ratchet\Tests;
namespace Ratchet\Tests\Resource\Socket;
use Ratchet\Tests\Mock\FakeSocket as Socket;
use Ratchet\Socket as RealSocket;
use Ratchet\Resource\Socket\BSDSocket as RealSocket;
/**
* @covers Ratchet\Socket
* @covers Ratchet\Resource\Socket\BSDSocket
*/
class SocketTest extends \PHPUnit_Framework_TestCase {
class BSDSocketTest extends \PHPUnit_Framework_TestCase {
protected $_socket;
protected static function getMethod($name) {
@ -31,7 +31,7 @@ class SocketTest extends \PHPUnit_Framework_TestCase {
/**/
public function testInvalidConstructorArguments() {
$this->setExpectedException('\\Ratchet\\Exception');
$this->setExpectedException('\\Ratchet\\Resource\\Socket\\BSDSocketException');
$socket = new RealSocket('invalid', 'param', 'derp');
}