From 8770c361bc4cca64318fb60483b346207e0b2d3d Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Thu, 14 Jun 2012 15:07:52 -0400 Subject: [PATCH] [WebSocket] Hixie refactoring Created parent interface for messages and frames Created Hixie Connection Applied updated interfaces to Hixie versions Removed __toString on msgs/frames since there could be 2-3 types returned --- Version/DataInterface.php | 28 ++++++++++++++ Version/FrameInterface.php | 28 +++----------- Version/Hixie76.php | 61 ++++++++++++++++++++++-------- Version/Hixie76/Connection.php | 16 ++++++++ Version/Hixie76/Frame.php | 8 ++++ Version/Hixie76/Message.php | 68 ---------------------------------- Version/MessageInterface.php | 20 +--------- Version/RFC6455/Connection.php | 4 +- Version/RFC6455/Message.php | 24 ++++++++---- Version/VersionInterface.php | 13 +++++-- WsServer.php | 2 +- 11 files changed, 133 insertions(+), 139 deletions(-) create mode 100644 Version/DataInterface.php create mode 100644 Version/Hixie76/Connection.php delete mode 100644 Version/Hixie76/Message.php diff --git a/Version/DataInterface.php b/Version/DataInterface.php new file mode 100644 index 0000000..ef44565 --- /dev/null +++ b/Version/DataInterface.php @@ -0,0 +1,28 @@ +getHeader('Sec-WebSocket-Key2', true)); } + /** + * {@inheritdoc} + */ public function getVersionNumber() { return 0; } @@ -46,25 +53,47 @@ class Hixie76 implements VersionInterface { return $response; } - /** - * @return Hixie76\Message - */ - public function newMessage() { - return new Hixie76\Message; - } - - /** - * @return Hixie76\Frame - */ - public function newFrame() { - return new Hixie76\Frame; - } - /** * {@inheritdoc} */ - public function frame($message, $mask = true) { - return chr(0) . $message . chr(255); + public function upgradeConnection(ConnectionInterface $conn, MessageInterface $coalescedCallback) { + $upgraded = new Connection($conn); + + if (!isset($upgraded->WebSocket)) { + $upgraded->WebSocket = new \StdClass; + } + + $upgraded->WebSocket->coalescedCallback = $coalescedCallback; + + return $upgraded; + } + + public function onMessage(ConnectionInterface $from, $data) { + $overflow = ''; + + if (!isset($from->WebSocket->frame)) { + $from->WebSocket->frame = $this->newFrame(); + } + + $from->WebSocket->frame->addBuffer($data); + if ($from->WebSocket->frame->isCoalesced()) { + $overflow = $from->WebSocket->frame->extractOverflow(); + + $parsed = $from->WebSocket->frame->getPayload(); + unset($from->WebSocket->frame); + + $from->WebSocket->coalescedCallback->onMessage($from, $parsed); + + unset($from->WebSocket->frame); + } + + if (strlen($overflow) > 0) { + $this->onMessage($from, $overflow); + } + } + + public function newFrame() { + return new Frame; } public function generateKeyNumber($key) { diff --git a/Version/Hixie76/Connection.php b/Version/Hixie76/Connection.php new file mode 100644 index 0000000..2011ac1 --- /dev/null +++ b/Version/Hixie76/Connection.php @@ -0,0 +1,16 @@ +getConnection()->send(chr(0) . $msg . chr(255)); + } + + public function close() { + return $this->getConnection()->close(); + } +} \ No newline at end of file diff --git a/Version/Hixie76/Frame.php b/Version/Hixie76/Frame.php index b9af87d..a172207 100644 --- a/Version/Hixie76/Frame.php +++ b/Version/Hixie76/Frame.php @@ -75,4 +75,12 @@ class Frame implements FrameInterface { return substr($this->_data, 1, strlen($this->_data) - 2); } + + public function getContents() { + return $this->_data; + } + + public function extractOverflow() { + return ''; + } } \ No newline at end of file diff --git a/Version/Hixie76/Message.php b/Version/Hixie76/Message.php deleted file mode 100644 index 818d95d..0000000 --- a/Version/Hixie76/Message.php +++ /dev/null @@ -1,68 +0,0 @@ -getPayload(); - } - - /** - * {@inheritdoc} - */ - public function isCoalesced() { - if (!($this->_frame instanceof FrameInterface)) { - return false; - } - - return $this->_frame->isCoalesced(); - } - - /** - * {@inheritdoc} - */ - public function addFrame(FrameInterface $fragment) { - if (null !== $this->_frame) { - throw new \OverflowException('Hixie76 does not support multiple framing of messages'); - } - - $this->_frame = $fragment; - - return $this; - } - - /** - * {@inheritdoc} - */ - public function getOpcode() { - // Hixie76 only supported text messages - return 1; - } - - /** - * {@inheritdoc} - */ - public function getPayloadLength() { - throw new \DomainException('Please sir, may I have some code? (' . __FUNCTION__ . ')'); - } - - /** - * {@inheritdoc} - */ - public function getPayload() { - if (!$this->isCoalesced()) { - throw new \UnderflowException('Message has not been fully buffered yet'); - } - - return $this->_frame->getPayload(); - } -} \ No newline at end of file diff --git a/Version/MessageInterface.php b/Version/MessageInterface.php index 1dc91fc..dc126d6 100644 --- a/Version/MessageInterface.php +++ b/Version/MessageInterface.php @@ -1,15 +1,7 @@ _frames = new \SplDoublyLinkedList; } - /** - * {@inheritdoc} - */ - public function __toString() { - return $this->getPayload(); - } - /** * {@inheritdoc} */ @@ -87,4 +80,21 @@ class Message implements MessageInterface { return $buffer; } + + /** + * {@inheritdoc} + */ + public function getContents() { + if (!$this->isCoalesced()) { + throw new \UnderflowException("Message has not been put back together yet"); + } + + $buffer = ''; + + foreach ($this->_frames as $frame) { + $buffer .= $frame->getContents(); + } + + return $buffer; + } } \ No newline at end of file diff --git a/Version/VersionInterface.php b/Version/VersionInterface.php index 630c62a..9a3c29e 100644 --- a/Version/VersionInterface.php +++ b/Version/VersionInterface.php @@ -1,6 +1,7 @@ versioner ->enableVersion(new Version\RFC6455($component)) ->enableVersion(new Version\HyBi10($component)) - //->enableVersion(new Version\Hixie76) + ->enableVersion(new Version\Hixie76) ; $this->_decorating = $component;