Skip to content
Open
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
}
],
"require": {
"ext-encoding": "*",
"ext-encoding": "^1.0.0",
"ext-sockets": "*",
"symfony/filesystem": "^7.1"
},
Expand Down
11 changes: 5 additions & 6 deletions src/SendQueue.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

namespace cooldogedev\spectral;

use pmmp\encoding\ByteBuffer;
use pmmp\encoding\ByteBufferWriter;
use function array_shift;
use function count;
use function min;
Expand All @@ -14,11 +14,11 @@ final class SendQueue
{
private array $queue = [];
private int $mss = Protocol::MIN_PACKET_SIZE;
private ByteBuffer $pk;
private ByteBufferWriter $pk;

public function __construct()
{
$this->pk = new ByteBuffer();
$this->pk = new ByteBufferWriter();
$this->pk->reserve(Protocol::MAX_PACKET_SIZE);
}

Expand Down Expand Up @@ -57,14 +57,13 @@ public function pack(int $window): ?string
array_shift($this->queue);
$this->pk->writeByteArray($entry);
}
return $this->pk->toString();
return $this->pk->getData();
}

public function flush(): void
{
$this->pk->clear();
$this->pk->setReadOffset(0);
$this->pk->setWriteOffset(0);
$this->pk->setOffset(0);
}

public function clear(): void
Expand Down
40 changes: 21 additions & 19 deletions src/frame/Acknowledgement.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace cooldogedev\spectral\frame;

use pmmp\encoding\ByteBuffer;
use pmmp\encoding\ByteBufferReader;
use pmmp\encoding\ByteBufferWriter;
use pmmp\encoding\LE;
use function count;

final class Acknowledgement extends Frame
Expand All @@ -30,27 +32,27 @@ public function id(): int
return FrameIds::ACKNOWLEDGEMENT;
}

public function encode(ByteBuffer $buf): void
public function encode(ByteBufferWriter $buf): void
{
$buf->writeSignedLongLE($this->delay);
$buf->writeUnsignedIntLE($this->max);
$buf->writeUnsignedIntLE(count($this->ranges));
foreach ($this->ranges as $range) {
$buf->writeUnsignedIntLE($range[0]);
$buf->writeUnsignedIntLE($range[1]);
}
LE::writeSignedLong($buf, $this->delay);
LE::writeUnsignedInt($buf, $this->max);
LE::writeUnsignedInt($buf, count($this->ranges));
foreach ($this->ranges as $range) {
LE::writeUnsignedInt($buf, $range[0]);
LE::writeUnsignedInt($buf, $range[1]);
}
}

public function decode(ByteBuffer $buf): void
public function decode(ByteBufferReader $buf): void
{
$this->delay = $buf->readSignedLongLE();
$this->max = $buf->readUnsignedIntLE();
$length = $buf->readUnsignedIntLE();
for ($i = 0; $i < $length; $i++) {
$this->ranges[$i] = [
$buf->readUnsignedIntLE(),
$buf->readUnsignedIntLE(),
];
}
$this->delay = LE::readSignedLong($buf);
$this->max = LE::readUnsignedInt($buf);
$length = LE::readUnsignedInt($buf);
for ($i = 0; $i < $length; $i++) {
$this->ranges[$i] = [
LE::readUnsignedInt($buf),
LE::readUnsignedInt($buf)
];
}
}
}
19 changes: 12 additions & 7 deletions src/frame/ConnectionClose.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace cooldogedev\spectral\frame;

use pmmp\encoding\ByteBuffer;
use pmmp\encoding\Byte;
use pmmp\encoding\ByteBufferReader;
use pmmp\encoding\ByteBufferWriter;
use pmmp\encoding\LE;
use function strlen;

final class ConnectionClose extends Frame
Expand All @@ -30,16 +33,18 @@ public function id(): int
return FrameIds::CONNECTION_CLOSE;
}

public function encode(ByteBuffer $buf): void
public function encode(ByteBufferWriter $buf): void
{
$buf->writeUnsignedByte($this->code);
$buf->writeUnsignedIntLE(strlen($this->message));
// $buf->writeUnsignedByte($this->code);
// $buf->writeUnsignedIntLE(strlen($this->message));
Byte::writeUnsigned($buf, $this->code);
LE::writeUnsignedInt($buf, strlen($this->message));
$buf->writeByteArray($this->message);
}

public function decode(ByteBuffer $buf): void
public function decode(ByteBufferReader $buf): void
{
$this->code = $buf->readUnsignedByte();
$this->message = $buf->readByteArray($buf->readUnsignedIntLE());
$this->code = Byte::readUnsigned($buf);
$this->message = $buf->readByteArray(LE::readUnsignedInt($buf));
}
}
7 changes: 4 additions & 3 deletions src/frame/ConnectionRequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

namespace cooldogedev\spectral\frame;

use pmmp\encoding\ByteBuffer;
use pmmp\encoding\ByteBufferReader;
use pmmp\encoding\ByteBufferWriter;

final class ConnectionRequest extends Frame
{
Expand All @@ -18,7 +19,7 @@ public function id(): int
return FrameIds::CONNECTION_REQUEST;
}

public function encode(ByteBuffer $buf): void {}
public function encode(ByteBufferWriter $buf): void {}

public function decode(ByteBuffer $buf): void {}
public function decode(ByteBufferReader $buf): void {}
}
16 changes: 10 additions & 6 deletions src/frame/ConnectionResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@

namespace cooldogedev\spectral\frame;

use pmmp\encoding\Byte;
use pmmp\encoding\ByteBuffer;
use pmmp\encoding\ByteBufferReader;
use pmmp\encoding\ByteBufferWriter;
use pmmp\encoding\LE;

final class ConnectionResponse extends Frame
{
Expand All @@ -27,15 +31,15 @@ public function id(): int
return FrameIds::CONNECTION_RESPONSE;
}

public function encode(ByteBuffer $buf): void
public function encode(ByteBufferWriter $buf): void
{
$buf->writeSignedLongLE($this->connectionID);
$buf->writeUnsignedByte($this->response);
LE::writeSignedLong($buf, $this->connectionID);
Byte::writeUnsigned($buf, $this->response);
}

public function decode(ByteBuffer $buf): void
public function decode(ByteBufferReader $buf): void
{
$this->connectionID = $buf->readSignedLongLE();
$this->response = $buf->readUnsignedByte();
$this->connectionID = LE::readSignedLong($buf);
$this->response = Byte::readUnsigned($buf);
}
}
7 changes: 4 additions & 3 deletions src/frame/Frame.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@

namespace cooldogedev\spectral\frame;

use pmmp\encoding\ByteBuffer;
use pmmp\encoding\ByteBufferReader;
use pmmp\encoding\ByteBufferWriter;

abstract class Frame
{
abstract public function id(): int;

abstract public function encode(ByteBuffer $buf): void;
abstract public function encode(ByteBufferWriter $buf): void;

abstract public function decode(ByteBuffer $buf): void;
abstract public function decode(ByteBufferReader $buf): void;
}
12 changes: 7 additions & 5 deletions src/frame/MTURequest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace cooldogedev\spectral\frame;

use pmmp\encoding\ByteBuffer;
use pmmp\encoding\ByteBufferReader;
use pmmp\encoding\ByteBufferWriter;
use pmmp\encoding\LE;
use function str_repeat;

final class MTURequest extends Frame
Expand All @@ -23,15 +25,15 @@ public function id(): int
return FrameIds::MTU_REQUEST;
}

public function encode(ByteBuffer $buf): void
public function encode(ByteBufferWriter $buf): void
{
$buf->writeSignedLongLE($this->mtu);
LE::writeSignedLong($buf, $this->mtu);
$buf->writeByteArray(str_repeat("\x00", $this->mtu - 8));
}

public function decode(ByteBuffer $buf): void
public function decode(ByteBufferReader $buf): void
{
$this->mtu = $buf->readSignedLongLE();
$this->mtu = LE::readSignedLong($buf);
$buf->readByteArray($this->mtu - 8);
}
}
12 changes: 7 additions & 5 deletions src/frame/MTUResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace cooldogedev\spectral\frame;

use pmmp\encoding\ByteBuffer;
use pmmp\encoding\ByteBufferReader;
use pmmp\encoding\ByteBufferWriter;
use pmmp\encoding\LE;

final class MTUResponse extends Frame
{
Expand All @@ -22,13 +24,13 @@ public function id(): int
return FrameIds::MTU_RESPONSE;
}

public function encode(ByteBuffer $buf): void
public function encode(ByteBufferWriter $buf): void
{
$buf->writeSignedLongLE($this->mtu);
LE::writeSignedLong($buf, $this->mtu);
}

public function decode(ByteBuffer $buf): void
public function decode(ByteBufferReader $buf): void
{
$this->mtu = $buf->readSignedLongLE();
$this->mtu = LE::readSignedLong($buf);
}
}
45 changes: 23 additions & 22 deletions src/frame/Pack.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,47 +5,49 @@
namespace cooldogedev\spectral\frame;

use cooldogedev\spectral\Protocol;
use pmmp\encoding\ByteBuffer;
use pmmp\encoding\ByteBufferReader;
use pmmp\encoding\ByteBufferWriter;
use pmmp\encoding\LE;
use function strlen;

final class Pack
{
private static ?ByteBuffer $buf = null;
private static ?ByteBufferWriter $writeBuf = null;

public static function packSingle(Frame $fr): string
{
$buf = Pack::getBuffer();
$buf->writeUnsignedIntLE($fr->id());
$buf = Pack::getWriter();
LE::writeUnsignedInt($buf, $fr->id());
$fr->encode($buf);
return $buf->toString();
return $buf->getData();
}

public static function pack(int $connectionID, int $sequenceID, string $frames): string
{
$buf = Pack::getBuffer();
$buf = Pack::getWriter();
$buf->writeByteArray(Protocol::MAGIC);
$buf->writeSignedLongLE($connectionID);
$buf->writeUnsignedIntLE($sequenceID);
LE::writeSignedLong($buf, $connectionID);
LE::writeUnsignedInt($buf, $sequenceID);
$buf->writeByteArray($frames);
return $buf->toString();
return $buf->getData();
}

public static function unpack(string $payload): ?array
{
$buf = Pack::getBuffer();
if (strlen($payload) < Protocol::PACKET_HEADER_SIZE) {
return null;
}

$buf->writeByteArray($payload);
$buf = new ByteBufferReader($payload);
if ($buf->readByteArray(4) !== Protocol::MAGIC) {
return null;
}

$connectionID = $buf->readSignedLongLE();
$sequenceID = $buf->readUnsignedIntLE();
$connectionID = LE::readSignedLong($buf);
$sequenceID = LE::readUnsignedInt($buf);
$frames = [];
while ($buf->getUsedLength() > $buf->getReadOffset()) {
$fr = Pool::getFrame($buf->readUnsignedIntLE());
while ($buf->getUnreadLength() > 0) {
$fr = Pool::getFrame(LE::readUnsignedInt($buf));
if ($fr === null) {
break;
}
Expand All @@ -55,14 +57,13 @@ public static function unpack(string $payload): ?array
return [$connectionID, $sequenceID, $frames];
}

private static function getBuffer(): ByteBuffer
private static function getWriter(): ByteBufferWriter
{
if (Pack::$buf === null) {
Pack::$buf = new ByteBuffer();
if (Pack::$writeBuf === null) {
Pack::$writeBuf = new ByteBufferWriter();
}
Pack::$buf->setReadOffset(0);
Pack::$buf->setWriteOffset(0);
Pack::$buf->clear();
return Pack::$buf;
Pack::$writeBuf->setOffset(0);
Pack::$writeBuf->clear();
return Pack::$writeBuf;
}
}
12 changes: 7 additions & 5 deletions src/frame/StreamClose.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@

namespace cooldogedev\spectral\frame;

use pmmp\encoding\ByteBuffer;
use pmmp\encoding\ByteBufferReader;
use pmmp\encoding\ByteBufferWriter;
use pmmp\encoding\LE;

final class StreamClose extends Frame
{
Expand All @@ -22,13 +24,13 @@ public function id(): int
return FrameIds::STREAM_CLOSE;
}

public function encode(ByteBuffer $buf): void
public function encode(ByteBufferWriter $buf): void
{
$buf->writeSignedLongLE($this->streamID);
LE::writeSignedLong($buf, $this->streamID);
}

public function decode(ByteBuffer $buf): void
public function decode(ByteBufferReader $buf): void
{
$this->streamID = $buf->readSignedLongLE();
$this->streamID = LE::readSignedLong($buf);
}
}
Loading