diff --git a/lib/Ratchet/Application/Server/App.php b/lib/Ratchet/Application/Server/App.php index 5ecd404..179cefa 100644 --- a/lib/Ratchet/Application/Server/App.php +++ b/lib/Ratchet/Application/Server/App.php @@ -27,8 +27,12 @@ class App implements ApplicationInterface { protected $_app; /** - * @param Ratchet\Application\ApplicationInterface + * Number of bytes to read in the TCP buffer at a time + * Default is (currently) 4kb + * @var int */ + protected $_buffer_size = 4096; + public function __construct(ApplicationInterface $application = null) { if (null === $application) { throw new \UnexpectedValueException("Server requires an application to run off of"); @@ -37,6 +41,14 @@ class App implements ApplicationInterface { $this->_app = $application; } + public function setBufferSize($recv_bytes) { + if ((int)$recv_bytes < 1) { + throw new \InvalidArgumentException('Invalid number of bytes set, must be more than 0'); + } + + $this->_buffer_size = (int)$recv_bytes; + } + /* * @param Ratchet\SocketInterface * @param mixed The address to listen for incoming connections on. "0.0.0.0" to listen from anywhere @@ -63,7 +75,7 @@ class App implements ApplicationInterface { } while (true); } - protected function loop(SocketInterface $host, $recv_bytes = 1024) { + protected function loop(SocketInterface $host) { $changed = $this->_resources; try { @@ -81,7 +93,7 @@ class App implements ApplicationInterface { $res = $this->onOpen($conn); } else { $data = $buf = ''; - $bytes = $conn->getSocket()->recv($buf, $recv_bytes, 0); + $bytes = $conn->getSocket()->recv($buf, $this->_buffer_size, 0); if ($bytes > 0) { $data = $buf; diff --git a/lib/Ratchet/Application/WebSocket/Version/Hixie76.php b/lib/Ratchet/Application/WebSocket/Version/Hixie76.php index e83e9b4..22574a2 100644 --- a/lib/Ratchet/Application/WebSocket/Version/Hixie76.php +++ b/lib/Ratchet/Application/WebSocket/Version/Hixie76.php @@ -49,10 +49,6 @@ class Hixie76 implements VersionInterface { return new Hixie76\Frame; } - public function unframe($message) { - return substr($message, 1, strlen($message) - 2); - } - public function frame($message) { return chr(0) . $message . chr(255); } diff --git a/lib/Ratchet/Application/WebSocket/Version/HyBi10.php b/lib/Ratchet/Application/WebSocket/Version/HyBi10.php index e4bba6d..f6cb4ea 100644 --- a/lib/Ratchet/Application/WebSocket/Version/HyBi10.php +++ b/lib/Ratchet/Application/WebSocket/Version/HyBi10.php @@ -41,113 +41,6 @@ class HyBi10 implements VersionInterface { return new HyBi10\Frame; } - /** - * Unframe a message received from the client - * Thanks to @lemmingzshadow for the code on decoding a HyBi-10 frame - * @link https://github.com/lemmingzshadow/php-websocket - * @param string - * @return array - * @throws UnexpectedValueException - * @todo return a common interface instead of array - */ - public function unframe($message) { - $data = $message; - $mask = $payloadLength = $unmaskedPayload = ''; - $decodedData = array(); - - // estimate frame type: - $firstByteBinary = sprintf('%08b', ord($data[0])); - $finIndicator = bindec(substr($firstByteBinary, 0, 1)); - $opcode = bindec(substr($firstByteBinary, 4, 4)); - - $secondByteBinary = sprintf('%08b', ord($data[1])); - $isMasked = ($secondByteBinary[0] == '1') ? true : false; - $payloadLength = ord($data[1]) & 127; - - // close connection if unmasked frame is received: - if($isMasked === false) { - throw new \UnexpectedValueException('Masked byte is false'); - } - - switch($opcode) { - // continuation frame - case 0: - throw new \UnexpectedValueException("Opcode CONTINUATION not accepted yet"); - $decodedData['type'] = 'text'; // incomplete - break; - - // text frame: - case 1: - $decodedData['type'] = 'text'; - break; - - // binary data frame - case 2: - throw new \UnexpectedValueException("Opcode BINARY not accepted yet"); - $decodedData['type'] = 'binary'; - break; - - // connection close frame: - case 8: - throw new \UnexpectedValueException("Opcode CLOSE not accepted yet"); - $decodedData['type'] = 'close'; - break; - - // ping frame: - case 9: - throw new \UnexpectedValueException("Opcode PING not accepted yet"); - $decodedData['type'] = 'ping'; - break; - - // pong frame: - case 10: - throw new \UnexpectedValueException("Opcode PONG not accepted yet"); - $decodedData['type'] = 'pong'; - break; - - default: - // Close connection on unknown opcode: - throw new \UnexpectedValueException("Unknown opcode ({$opcode})"); - break; - } - - if($payloadLength === 126) { - $msg_len = bindec(sprintf('%08b', ord($data[2])) . sprintf('%08b', ord($data[3]))); - $mask = substr($data, 4, 4); - $payloadOffset = 8; - } elseif($payloadLength === 127) { - $msg_len = bindec( - sprintf('%08b', ord($data[2])) - . sprintf('%08b', ord($data[3])) - . sprintf('%08b', ord($data[4])) - . sprintf('%08b', ord($data[5])) - . sprintf('%08b', ord($data[6])) - . sprintf('%08b', ord($data[7])) - . sprintf('%08b', ord($data[8])) - . sprintf('%08b', ord($data[9])) - ); - - $mask = substr($data, 10, 4); - $payloadOffset = 14; - } else { - $msg_len = $payloadLength; - $mask = substr($data, 2, 4); - $payloadOffset = 6; - } - - $dataLength = strlen($data); - - if($isMasked === true) { // This will always pass... - for($i = $payloadOffset; $i < $dataLength; $i++) { - $j = $i - $payloadOffset; - $unmaskedPayload .= $data[$i] ^ $mask[$j % 4]; - } - $decodedData['payload'] = $unmaskedPayload; - } - - return $decodedData; - } - /** * Thanks to @lemmingzshadow for the code on decoding a HyBi-10 frame * @link https://github.com/lemmingzshadow/php-websocket diff --git a/lib/Ratchet/Application/WebSocket/Version/VersionInterface.php b/lib/Ratchet/Application/WebSocket/Version/VersionInterface.php index 4d4df15..1e34a4f 100644 --- a/lib/Ratchet/Application/WebSocket/Version/VersionInterface.php +++ b/lib/Ratchet/Application/WebSocket/Version/VersionInterface.php @@ -4,6 +4,8 @@ namespace Ratchet\Application\WebSocket\Version; /** * Despite the version iterations of WebInterface the actions they go through are similar * This standardizes how the server handles communication with each protocol version + * @todo Need better naming conventions...newMessage and newFrame are for reading incoming framed messages (action is unframing) + * The current method names suggest you could create a new message/frame to send, which they can not do */ interface VersionInterface { /** @@ -23,15 +25,6 @@ interface VersionInterface { */ function newFrame(); - /** - * Get a framed message as per the protocol and return the decoded message - * @param string - * @return string - * @todo Return a frame object with message, type, masked? - * @deprecated - */ - function unframe($message); - /** * @param string * @return string