Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for sreamed output #13

Merged
merged 4 commits into from
Mar 21, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 28 additions & 12 deletions src/Frame.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
* @psalm-type FrameType = Frame::CONTROL | Frame::ERROR
* @psalm-type FrameCodec = Frame::CODEC_*
* @psalm-type FrameCodecValue = int-mask-of<FrameCodec>
* @psalm-type FrameByte10 = Frame::BYTE10_*
* @psalm-type FrameByte10Value = int-mask-of<FrameByte10>
*/
final class Frame
{
Expand Down Expand Up @@ -48,6 +50,15 @@ final class Frame
public const CODEC_PROTO = 0x80;
/**#@-*/

/**#@+
* BYTE10 flags, it means, that we can set multiply flags from this group
* using bitwise OR.
*
* @var positive-int Flags for {@see $byte10}
*/
public const BYTE10_STREAM = 0x01;
/**#@-*/

/**
* @var string|null
*/
Expand All @@ -58,21 +69,26 @@ final class Frame
*/
public array $options = [];

public int $flags;

/**
* @var int
* @psalm-var FrameByte10Value
*/
public int $flags;
public int $byte10;

public int $byte11 = 0;

/**
* @param string|null $body
* @param array<int> $options
* @param int $flags
*
* @psalm-param FrameByte10Value $byte10
*/
public function __construct(?string $body, array $options = [], int $flags = 0)
public function __construct(?string $body, array $options = [], int $flags = 0, int $byte10 = 0)
{
$this->payload = $body;
$this->options = $options;
$this->flags = $flags;
$this->byte10 = $byte10;
}

/**
Expand All @@ -85,7 +101,7 @@ public function setFlag(int ...$flag): void
throw new InvalidArgumentException('Flags can be byte only');
}

$this->flags = $this->flags | $f;
$this->flags |= $f;
}
}

Expand Down Expand Up @@ -117,17 +133,17 @@ public function setOptions(int ...$options): void
*/
public static function packFrame(Frame $frame): string
{
$header = pack(
$header = \pack(
'CCL',
self::VERSION << 4 | (count($frame->options) + 3),
self::VERSION << 4 | (\count($frame->options) + 3),
$frame->flags,
strlen((string)$frame->payload)
\strlen((string)$frame->payload)
);

if ($frame->options !== []) {
$header .= pack('LCCL*', crc32($header), 0, 0, ...$frame->options);
$header .= \pack('LCCL*', \crc32($header), $frame->byte10, $frame->byte11, ...$frame->options);
} else {
$header .= pack('LCC', crc32($header), 0, 0);
$header .= \pack('LCC', \crc32($header), $frame->byte10, $frame->byte11);
}

return $header . (string)$frame->payload;
Expand Down Expand Up @@ -157,7 +173,7 @@ public static function readHeader(string $header): array
*/
public static function initFrame(array $header, string $body): Frame
{
assert(count($header) >= 2);
\assert(\count($header) >= 2);

/**
* optimize?
Expand Down