[WebSocket] Messaging

Fluent interface on MessageInterface::addFrame
RFC6455 Message unit tests
RFC handling TCP concatenation (refs #31)
This commit is contained in:
Chris Boden 2012-06-02 22:08:27 -04:00
parent 0326ac99bd
commit 8e30b861c7
5 changed files with 12 additions and 7 deletions

View File

@ -21,13 +21,17 @@ class MessageParser {
return; return;
} }
// Check frame // Check frame
// If is control frame, do your thing // If is control frame, do your thing
// Else, add to message // Else, add to message
// Control frames (ping, pong, close) can be sent in between a fragmented 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); $from->WebSocket->message->addFrame($from->WebSocket->frame);
unset($from->WebSocket->frame); $from->WebSocket->frame = $nextFrame;
} }
if ($from->WebSocket->message->isCoalesced()) { if ($from->WebSocket->message->isCoalesced()) {

View File

@ -36,6 +36,8 @@ class Message implements MessageInterface {
} }
$this->_frame = $fragment; $this->_frame = $fragment;
return $this;
} }
/** /**

View File

@ -17,6 +17,7 @@ interface MessageInterface {
/** /**
* @param FragmentInterface * @param FragmentInterface
* @return MessageInterface
*/ */
function addFrame(FrameInterface $fragment); function addFrame(FrameInterface $fragment);

View File

@ -35,7 +35,6 @@ class Frame implements FrameInterface {
* @param bool Mask the payload * @param bool Mask the payload
* @return Frame * @return Frame
* @throws InvalidArgumentException If the payload is not a valid UTF-8 string * @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 * @throws LengthException If the payload is too big
*/ */
public static function create($payload, $final = true, $opcode = 1, $mask = false) { 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"); 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); $raw = (int)(boolean)$final . sprintf('%07b', (int)$opcode);
$plLen = strlen($payload); $plLen = strlen($payload);

View File

@ -39,6 +39,8 @@ class Message implements MessageInterface {
*/ */
public function addFrame(FrameInterface $fragment) { public function addFrame(FrameInterface $fragment) {
$this->_frames->push($fragment); $this->_frames->push($fragment);
return $this;
} }
/** /**
@ -62,6 +64,7 @@ class Message implements MessageInterface {
try { try {
$len += $frame->getPayloadLength(); $len += $frame->getPayloadLength();
} catch (\UnderflowException $e) { } catch (\UnderflowException $e) {
// Not an error, want the current amount buffered
} }
} }
@ -73,7 +76,7 @@ class Message implements MessageInterface {
*/ */
public function getPayload() { public function getPayload() {
if (!$this->isCoalesced()) { 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 = ''; $buffer = '';