Skip to content

Instantly share code, notes, and snippets.

@benedmunds
Created June 7, 2015 18:09
Show Gist options
  • Save benedmunds/b6f532e331cf9577aaec to your computer and use it in GitHub Desktop.
Save benedmunds/b6f532e331cf9577aaec to your computer and use it in GitHub Desktop.
public function onData($data, MessageInterface $message = null, callable $onMessage, callable $onControl = null, $context = null) {
RECURSE:
$overflow = '';
$onControl ?: $this->noop;
$message ?: $message = $this->newMessage();
$prevFrame = null;
$frameCount = count($message);
if ($frameCount > 0) {
$frame = $message[$frameCount - 1];
if ($frame->isCoalesced()) {
$prevFrame = $frame;
$frame = $this->newFrame();
$message->addFrame($frame);
$frameCount++;
} elseif ($frameCount > 1) {
$prevFrame = $message[$frameCount - 2];
}
} else {
$frame = $this->newFrame();
$message->addFrame($frame);
$frameCount++;
}
$frame->addBuffer($data);
if ($frame->isCoalesced()) {
$frame = $this->frameCheck($frame, $prevFrame);
$opcode = $frame->getOpcode();
if ($opcode > 2) {
$onControl($frame, $context);
unset($message[$frameCount - 1]);
$overflow = $frame->extractOverflow();
if (strlen($overflow) > 0) {
$message = $this->onData($overflow, $message, $onMessage, $onControl, $context);
}
return $message;
}
$overflow = $frame->extractOverflow();
$frame->unMaskPayload();
}
if ($message->isCoalesced()) {
$msgCheck = $this->checkMessage($message);
if (true !== $msgCheck) {
$onControl($this->newCloseFrame($msgCheck), $context);
return $this->newMessage();
}
$onMessage($message, $context);
$message = $this->newMessage();
}
if (strlen($overflow) > 0) {
goto RECURSE;
//$this->onData($overflow, $message, $onMessage, $onControl, $context);
}
return $message;
}
@cboden
Copy link

cboden commented Jun 7, 2015

    public function onData($data, MessageInterface $message = null, callable $onMessage, callable $onControl = null, $context = null) {
        recurse:
        $overflow = '';

        $onControl ?: $this->noop;
        $message   ?: $message = $this->newMessage();

        $prevFrame  = null;
        $frameCount = count($message);

        if ($frameCount > 0) {
            $frame = $message[$frameCount - 1];

            if ($frame->isCoalesced()) {
                $prevFrame = $frame;
                $frame = $this->newFrame();
                $message->addFrame($frame);
                $frameCount++;
            } elseif ($frameCount > 1) {
                $prevFrame = $message[$frameCount - 2];
            }
        } else {
            $frame = $this->newFrame();
            $message->addFrame($frame);
            $frameCount++;
        }

        $frame->addBuffer($data);
        if ($frame->isCoalesced()) {
            $frame = $this->frameCheck($frame, $prevFrame);

            $opcode = $frame->getOpcode();
            if ($opcode > 2) {
                $onControl($frame, $context);
                unset($message[$frameCount - 1]);

                $overflow = $frame->extractOverflow();

                if ('' === $overflow) {
                    return $message;
                }

                $data = $overflow;
                goto recurse;
                //return $this->onData($overflow, $message, $onMessage, $onControl, $context);
            }

            $overflow = $frame->extractOverflow();

            $frame->unMaskPayload();
        }

        if ($message->isCoalesced()) {
            $msgCheck = $this->checkMessage($message);
            if (true !== $msgCheck) {
                $onControl($this->newCloseFrame($msgCheck), $context);

                return $this->newMessage();
            }

            $onMessage($message, $context);
            $message = $this->newMessage();
        }

        if ('' === $overflow) {
            return $message;
        }

        $data = $overflow;
        goto recurse;
//        return $this->onData($overflow, $message, $onMessage, $onControl, $context);
    }

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment