Skip to content

Commit

Permalink
Support for sreamed output #13
Browse files Browse the repository at this point in the history
  • Loading branch information
roxblnfk authored Mar 21, 2022
2 parents 2c50b64 + 9767044 commit 6813452
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 12 deletions.
35 changes: 24 additions & 11 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,15 +69,17 @@ final class Frame
*/
public array $options = [];

public int $flags;

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

public int $byte11 = 0;

/**
* @param string|null $body
* @param array<int> $options
* @param int $flags
*/
public function __construct(?string $body, array $options = [], int $flags = 0)
{
Expand All @@ -85,7 +98,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 +130,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 +170,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
29 changes: 29 additions & 0 deletions tests/Goridge/FrameTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Goridge;

use PHPUnit\Framework\TestCase;
use Spiral\Goridge\Frame;

class FrameTest extends TestCase
{
public function testByte10DefaultValue(): void
{
$frame = new Frame('');
$this->assertSame(0, $frame->byte10);
}

public function testByte10DefaultValuePacked(): void
{
$string = Frame::packFrame(new Frame(''));
$this->assertSame(\chr(0), $string[10]);
}

public function testByte10StreamedOutputPacked(): void
{
$frame = new Frame('');
$frame->byte10 = Frame::BYTE10_STREAM;
$string = Frame::packFrame($frame);
$this->assertSame(Frame::BYTE10_STREAM, \ord($string[10]));
}
}
1 change: 0 additions & 1 deletion tests/Goridge/MsgPackRPCTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
namespace Spiral\Goridge\Tests;

use Spiral\Goridge\RPC\Codec\MsgpackCodec;
use Spiral\Goridge\RPC\Exception\CodecException;
use Spiral\Goridge\RPC\Exception\ServiceException;
use Spiral\Goridge\RPC\RPC;

Expand Down

0 comments on commit 6813452

Please sign in to comment.