Socket refactor
Moved SocketInterface and Socket to Resource namespace
This commit is contained in:
parent
e26dc0ccde
commit
33bf91dcea
24
README.md
24
README.md
@ -3,7 +3,7 @@
|
|||||||
A PHP 5.3 (PSR-0 compliant) component library for serving sockets and building socket based applications.
|
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.
|
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.
|
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
|
##WebSockets
|
||||||
|
|
||||||
@ -38,11 +38,11 @@ I'm looking into a couple daemonized servers written in PHP to run Ratchet on to
|
|||||||
```php
|
```php
|
||||||
<?php
|
<?php
|
||||||
namespace MyApps;
|
namespace MyApps;
|
||||||
use Ratchet\Component\ComponentInterface;
|
use Ratchet\Component\MessageComponentInterface;
|
||||||
use Ratchet\Resource\Connection;
|
use Ratchet\Resource\ConnectionInterface;
|
||||||
use Ratchet\Socket;
|
use Ratchet\Resource\BSDSocket as Socket;
|
||||||
use Ratchet\Component\Server\App as Server;
|
use Ratchet\Component\Server\IOServerComponent as Server;
|
||||||
use Ratchet\Component\WebSocket\App as WebSocket;
|
use Ratchet\Component\WebSocket\WebSocketComponent as WebSocket;
|
||||||
use Ratchet\Resource\Command\Composite as Cmds;
|
use Ratchet\Resource\Command\Composite as Cmds;
|
||||||
use Ratchet\Resource\Command\Action\SendMessage;
|
use Ratchet\Resource\Command\Action\SendMessage;
|
||||||
use Ratchet\Resource\Command\Action\CloseConnection;
|
use Ratchet\Resource\Command\Action\CloseConnection;
|
||||||
@ -51,18 +51,18 @@ use Ratchet\Resource\Command\Action\CloseConnection;
|
|||||||
* chat.php
|
* chat.php
|
||||||
* Send any incoming messages to all connected clients (except sender)
|
* Send any incoming messages to all connected clients (except sender)
|
||||||
*/
|
*/
|
||||||
class Chat implements ComponentInterface {
|
class Chat implements MessageComponentInterface {
|
||||||
protected $_clients;
|
protected $_clients;
|
||||||
|
|
||||||
public function __construct(ComponentInterface $app = null) {
|
public function __construct(MessageComponentInterface $app = null) {
|
||||||
$this->_clients = new \SplObjectStorage;
|
$this->_clients = new \SplObjectStorage;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onOpen(Connection $conn) {
|
public function onOpen(ConnectionInterface $conn) {
|
||||||
$this->_clients->attach($conn);
|
$this->_clients->attach($conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onMessage(Connection $from, $msg) {
|
public function onMessage(ConnectionInterface $from, $msg) {
|
||||||
$commands = new Cmds;
|
$commands = new Cmds;
|
||||||
|
|
||||||
foreach ($this->_clients as $client) {
|
foreach ($this->_clients as $client) {
|
||||||
@ -77,11 +77,11 @@ class Chat implements ComponentInterface {
|
|||||||
return $commands;
|
return $commands;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onClose(Connection $conn) {
|
public function onClose(ConnectionInterface $conn) {
|
||||||
$this->_clients->detach($conn);
|
$this->_clients->detach($conn);
|
||||||
}
|
}
|
||||||
|
|
||||||
public function onError(Connection $conn, \Exception $e) {
|
public function onError(ConnectionInterface $conn, \Exception $e) {
|
||||||
return new CloseConnection($conn);
|
return new CloseConnection($conn);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet\Component\Server;
|
namespace Ratchet\Component\Server;
|
||||||
use Ratchet\Component\MessageComponentInterface;
|
use Ratchet\Component\MessageComponentInterface;
|
||||||
use Ratchet\SocketInterface;
|
use Ratchet\Resource\Socket\SocketInterface;
|
||||||
use Ratchet\Resource\ConnectionInterface;
|
use Ratchet\Resource\ConnectionInterface;
|
||||||
use Ratchet\Resource\Connection;
|
use Ratchet\Resource\Connection;
|
||||||
use Ratchet\Resource\Command\CommandInterface;
|
use Ratchet\Resource\Command\CommandInterface;
|
||||||
@ -63,7 +63,7 @@ class IOServerComponent implements MessageComponentInterface {
|
|||||||
|
|
||||||
/*
|
/*
|
||||||
* Run the server infinitely
|
* 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 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)
|
* @param int The port to listen to connections on (make sure to run as root if < 1000)
|
||||||
* @throws Ratchet\Exception
|
* @throws Ratchet\Exception
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet\Resource;
|
namespace Ratchet\Resource;
|
||||||
use Ratchet\SocketInterface;
|
use Ratchet\Resource\Socket\SocketInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A proxy object representing a connection to the application
|
* A proxy object representing a connection to the application
|
||||||
@ -10,7 +10,7 @@ class Connection implements ConnectionInterface {
|
|||||||
protected $_data = array();
|
protected $_data = array();
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @var Ratchet\SocketInterface
|
* @var Ratchet\Resource\Socket\SocketInterface
|
||||||
*/
|
*/
|
||||||
protected $_socket;
|
protected $_socket;
|
||||||
|
|
||||||
|
@ -1,13 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet;
|
namespace Ratchet\Resource\Socket;
|
||||||
use Ratchet\Component\ProtocolInterface;
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A wrapper for the PHP socket_ functions
|
* A wrapper for the PHP socket_ functions
|
||||||
* @author Chris Boden <shout at chrisboden dot ca>
|
* @author Chris Boden <shout at chrisboden dot ca>
|
||||||
* @link http://ca2.php.net/manual/en/book.sockets.php
|
* @link http://ca2.php.net/manual/en/book.sockets.php
|
||||||
*/
|
*/
|
||||||
class Socket implements SocketInterface {
|
class BSDSocket implements SocketInterface {
|
||||||
/**
|
/**
|
||||||
* @type resource
|
* @type resource
|
||||||
*/
|
*/
|
||||||
@ -23,7 +22,7 @@ class Socket implements SocketInterface {
|
|||||||
* @param int Specifies the protocol family to be used by the socket.
|
* @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 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
|
* @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) {
|
public function __construct($domain = null, $type = null, $protocol = null) {
|
||||||
list($domain, $type, $protocol) = static::getConfig($domain, $type, $protocol);
|
list($domain, $type, $protocol) = static::getConfig($domain, $type, $protocol);
|
||||||
@ -31,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($this);
|
throw new BSDSocketException($this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -55,7 +54,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($this);
|
throw new BSDSocketException($this);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -71,7 +70,7 @@ class Socket implements SocketInterface {
|
|||||||
|
|
||||||
public function bind($address, $port = 0) {
|
public function bind($address, $port = 0) {
|
||||||
if (false === @socket_bind($this->getResource(), $address, $port)) {
|
if (false === @socket_bind($this->getResource(), $address, $port)) {
|
||||||
throw new Exception($this);
|
throw new BSDSocketException($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
@ -84,7 +83,7 @@ class Socket implements SocketInterface {
|
|||||||
|
|
||||||
public function connect($address, $port = 0) {
|
public function connect($address, $port = 0) {
|
||||||
if (false === @socket_connect($this->getResource(), $address, $port)) {
|
if (false === @socket_connect($this->getResource(), $address, $port)) {
|
||||||
throw new Exception($this);
|
throw new BSDSocketException($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
@ -93,7 +92,7 @@ class Socket implements SocketInterface {
|
|||||||
public function getRemoteAddress() {
|
public function getRemoteAddress() {
|
||||||
$address = $port = '';
|
$address = $port = '';
|
||||||
if (false === @socket_getpeername($this->getResource(), $address, $port)) {
|
if (false === @socket_getpeername($this->getResource(), $address, $port)) {
|
||||||
throw new Exception($this);
|
throw new BSDSocketException($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $address;
|
return $address;
|
||||||
@ -101,7 +100,7 @@ class Socket implements SocketInterface {
|
|||||||
|
|
||||||
public function get_option($level, $optname) {
|
public function get_option($level, $optname) {
|
||||||
if (false === ($res = @socket_get_option($this->getResource(), $level, $optname))) {
|
if (false === ($res = @socket_get_option($this->getResource(), $level, $optname))) {
|
||||||
throw new Exception($this);
|
throw new BSDSocketException($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $res;
|
return $res;
|
||||||
@ -109,7 +108,7 @@ class Socket implements SocketInterface {
|
|||||||
|
|
||||||
public function listen($backlog = 0) {
|
public function listen($backlog = 0) {
|
||||||
if (false === @socket_listen($this->getResource(), $backlog)) {
|
if (false === @socket_listen($this->getResource(), $backlog)) {
|
||||||
throw new Exception($this);
|
throw new BSDSocketException($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
@ -117,7 +116,7 @@ class Socket implements SocketInterface {
|
|||||||
|
|
||||||
public function read($length, $type = PHP_BINARY_READ) {
|
public function read($length, $type = PHP_BINARY_READ) {
|
||||||
if (false === ($res = @socket_read($this->getResource(), $length, $type))) {
|
if (false === ($res = @socket_read($this->getResource(), $length, $type))) {
|
||||||
throw new Exception($this);
|
throw new BSDSocketException($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $res;
|
return $res;
|
||||||
@ -129,11 +128,11 @@ class Socket implements SocketInterface {
|
|||||||
* @param int Number of bytes to read
|
* @param int Number of bytes to read
|
||||||
* @param int
|
* @param int
|
||||||
* @return int Number of bytes received
|
* @return int Number of bytes received
|
||||||
* @throws Exception
|
* @throws BSDSocketException
|
||||||
*/
|
*/
|
||||||
public function recv(&$buf, $len, $flags) {
|
public function recv(&$buf, $len, $flags) {
|
||||||
if (false === ($bytes = @socket_recv($this->_resource, $buf, $len, $flags))) {
|
if (false === ($bytes = @socket_recv($this->_resource, $buf, $len, $flags))) {
|
||||||
throw new Exception($this);
|
throw new BSDSocketException($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $bytes;
|
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 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
|
* @param int
|
||||||
* @throws \InvalidArgumentException
|
* @throws \InvalidArgumentException
|
||||||
* @throws Exception
|
* @throws BSDSocketException
|
||||||
*/
|
*/
|
||||||
public function select(&$read, &$write, &$except, $tv_sec, $tv_usec = 0) {
|
public function select(&$read, &$write, &$except, $tv_sec, $tv_usec = 0) {
|
||||||
$read = static::mungForSelect($read);
|
$read = static::mungForSelect($read);
|
||||||
@ -158,7 +157,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($this);
|
throw new BSDException($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $num;
|
return $num;
|
||||||
@ -166,7 +165,7 @@ class Socket implements SocketInterface {
|
|||||||
|
|
||||||
public function set_block() {
|
public function set_block() {
|
||||||
if (false === @socket_set_block($this->getResource())) {
|
if (false === @socket_set_block($this->getResource())) {
|
||||||
throw new Exception($this);
|
throw new BSDSocketException($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
@ -174,7 +173,7 @@ class Socket implements SocketInterface {
|
|||||||
|
|
||||||
public function set_nonblock() {
|
public function set_nonblock() {
|
||||||
if (false === @socket_set_nonblock($this->getResource())) {
|
if (false === @socket_set_nonblock($this->getResource())) {
|
||||||
throw new Exception($this);
|
throw new BSDSocketException($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
@ -182,7 +181,7 @@ class Socket implements SocketInterface {
|
|||||||
|
|
||||||
public function set_option($level, $optname, $optval) {
|
public function set_option($level, $optname, $optval) {
|
||||||
if (false === @socket_set_option($this->getResource(), $level, $optname, $optval)) {
|
if (false === @socket_set_option($this->getResource(), $level, $optname, $optval)) {
|
||||||
throw new Exception($this);
|
throw new BSDSocketException($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
@ -190,7 +189,7 @@ class Socket implements SocketInterface {
|
|||||||
|
|
||||||
public function shutdown($how = 2) {
|
public function shutdown($how = 2) {
|
||||||
if (false === @socket_shutdown($this->getResource(), $how)) {
|
if (false === @socket_shutdown($this->getResource(), $how)) {
|
||||||
throw new Exception($this);
|
throw new BSDSocketException($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
@ -198,7 +197,7 @@ class Socket implements SocketInterface {
|
|||||||
|
|
||||||
public function write($buffer, $length = 0) {
|
public function write($buffer, $length = 0) {
|
||||||
if (false === ($res = @socket_write($this->getResource(), $buffer, $length))) {
|
if (false === ($res = @socket_write($this->getResource(), $buffer, $length))) {
|
||||||
throw new Exception($this);
|
throw new BSDSocketException($this);
|
||||||
}
|
}
|
||||||
|
|
||||||
return $res;
|
return $res;
|
||||||
@ -238,7 +237,8 @@ class Socket implements SocketInterface {
|
|||||||
|
|
||||||
$return = array();
|
$return = array();
|
||||||
foreach ($collection as $key => $socket) {
|
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;
|
return $return;
|
@ -1,16 +1,16 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet;
|
namespace Ratchet\Resource\Socket;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 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 BSDSocketException extends \Exception {
|
||||||
/**
|
/**
|
||||||
* @var SocketInterface
|
* @var BSDSocket
|
||||||
*/
|
*/
|
||||||
protected $_socket;
|
protected $_socket;
|
||||||
|
|
||||||
public function __construct(SocketInterface $socket) {
|
public function __construct(BSDSocket $socket) {
|
||||||
$int = socket_last_error();
|
$int = socket_last_error();
|
||||||
$msg = socket_strerror($int);
|
$msg = socket_strerror($int);
|
||||||
|
|
@ -1,8 +1,9 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet;
|
namespace Ratchet\Resource\Socket;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An object-oriented container for a single socket connection
|
* An object-oriented container for a single socket connection
|
||||||
|
* @todo Major refactor when socket streams are implemented against this interface
|
||||||
*/
|
*/
|
||||||
interface SocketInterface {
|
interface SocketInterface {
|
||||||
/**
|
/**
|
@ -1,7 +1,7 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet\Tests\Mock;
|
namespace Ratchet\Tests\Mock;
|
||||||
use Ratchet\SocketInterface;
|
use Ratchet\Resource\Socket\SocketInterface;
|
||||||
use Ratchet\Socket as RealSocket;
|
use Ratchet\Resource\Socket\BSDSocket as RealSocket;
|
||||||
|
|
||||||
class FakeSocket implements SocketInterface {
|
class FakeSocket implements SocketInterface {
|
||||||
public $_arguments = array();
|
public $_arguments = array();
|
||||||
|
@ -1,12 +1,12 @@
|
|||||||
<?php
|
<?php
|
||||||
namespace Ratchet\Tests;
|
namespace Ratchet\Tests\Resource\Socket;
|
||||||
use Ratchet\Tests\Mock\FakeSocket as 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 $_socket;
|
||||||
|
|
||||||
protected static function getMethod($name) {
|
protected static function getMethod($name) {
|
||||||
@ -31,7 +31,7 @@ class SocketTest extends \PHPUnit_Framework_TestCase {
|
|||||||
/**/
|
/**/
|
||||||
|
|
||||||
public function testInvalidConstructorArguments() {
|
public function testInvalidConstructorArguments() {
|
||||||
$this->setExpectedException('\\Ratchet\\Exception');
|
$this->setExpectedException('\\Ratchet\\Resource\\Socket\\BSDSocketException');
|
||||||
$socket = new RealSocket('invalid', 'param', 'derp');
|
$socket = new RealSocket('invalid', 'param', 'derp');
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user