From 8e30b861c7f2a6f080545c0c914f787c80f21c3b Mon Sep 17 00:00:00 2001 From: Chris Boden Date: Sat, 2 Jun 2012 22:08:27 -0400 Subject: [PATCH] [WebSocket] Messaging Fluent interface on MessageInterface::addFrame RFC6455 Message unit tests RFC handling TCP concatenation (refs #31) --- MessageParser.php | 6 +++++- Version/Hixie76/Message.php | 2 ++ Version/MessageInterface.php | 1 + Version/RFC6455/Frame.php | 5 ----- Version/RFC6455/Message.php | 5 ++++- 5 files changed, 12 insertions(+), 7 deletions(-) diff --git a/MessageParser.php b/MessageParser.php index 017c4f5..b28592d 100644 --- a/MessageParser.php +++ b/MessageParser.php @@ -21,13 +21,17 @@ class MessageParser { return; } + // Check frame // If is control frame, do your thing // Else, add to message // Control frames (ping, pong, close) can be sent in between a fragmented message + $nextFrame = $from->WebSocket->version->newFrame(); + $nextFrame->addBuffer($from->WebSocket->frame->extractOverflow()); + $from->WebSocket->message->addFrame($from->WebSocket->frame); - unset($from->WebSocket->frame); + $from->WebSocket->frame = $nextFrame; } if ($from->WebSocket->message->isCoalesced()) { diff --git a/Version/Hixie76/Message.php b/Version/Hixie76/Message.php index f783e8b..818d95d 100644 --- a/Version/Hixie76/Message.php +++ b/Version/Hixie76/Message.php @@ -36,6 +36,8 @@ class Message implements MessageInterface { } $this->_frame = $fragment; + + return $this; } /** diff --git a/Version/MessageInterface.php b/Version/MessageInterface.php index 90d0179..4c21114 100644 --- a/Version/MessageInterface.php +++ b/Version/MessageInterface.php @@ -17,6 +17,7 @@ interface MessageInterface { /** * @param FragmentInterface + * @return MessageInterface */ function addFrame(FrameInterface $fragment); diff --git a/Version/RFC6455/Frame.php b/Version/RFC6455/Frame.php index af8e961..ec73393 100644 --- a/Version/RFC6455/Frame.php +++ b/Version/RFC6455/Frame.php @@ -35,7 +35,6 @@ class Frame implements FrameInterface { * @param bool Mask the payload * @return Frame * @throws InvalidArgumentException If the payload is not a valid UTF-8 string - * @throws BadMethodCallException If there is a problem with miss-matching parameters * @throws LengthException If the payload is too big */ public static function create($payload, $final = true, $opcode = 1, $mask = false) { @@ -45,10 +44,6 @@ class Frame implements FrameInterface { throw new \InvalidArgumentException("Payload is not a valid UTF-8 string"); } - if (false === (boolean)$final && $opcode !== static::OP_CONTINUE) { - throw new \BadMethodCallException("opcode MUST be 'continue' if the frame is not final"); - } - $raw = (int)(boolean)$final . sprintf('%07b', (int)$opcode); $plLen = strlen($payload); diff --git a/Version/RFC6455/Message.php b/Version/RFC6455/Message.php index 1071115..53bb8dd 100644 --- a/Version/RFC6455/Message.php +++ b/Version/RFC6455/Message.php @@ -39,6 +39,8 @@ class Message implements MessageInterface { */ public function addFrame(FrameInterface $fragment) { $this->_frames->push($fragment); + + return $this; } /** @@ -62,6 +64,7 @@ class Message implements MessageInterface { try { $len += $frame->getPayloadLength(); } catch (\UnderflowException $e) { + // Not an error, want the current amount buffered } } @@ -73,7 +76,7 @@ class Message implements MessageInterface { */ public function getPayload() { if (!$this->isCoalesced()) { - throw new \UnderflowMessage('Message has not been put back together yet'); + throw new \UnderflowException('Message has not been put back together yet'); } $buffer = '';