Skip to content
Merged
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
43 changes: 43 additions & 0 deletions src/Providers/ReaderProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

// Copyright (C) 2025 Ian Torres
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.

namespace Throttr\SDK\Providers;

use Throttr\SDK\Requests\BaseRequest;

class ReaderProvider
{
/**
* Read integers
*
* @param string $data
* @param array $columns
* @param int $offset
* @return array
*/
public static function readIntegers(string $data, array $columns, int &$offset): array
{
$result = [];

foreach ($columns as $column => $size) {
$result[$column] = unpack(BaseRequest::pack($size), substr($data, $offset, $size->value))[1];
$offset += $size->value;
}

return $result;
}
}
62 changes: 16 additions & 46 deletions src/Responses/ChannelResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
use Throttr\SDK\Enum\KeyType;
use Throttr\SDK\Enum\TTLType;
use Throttr\SDK\Enum\ValueSize;
use Throttr\SDK\Providers\ReaderProvider;
use Throttr\SDK\Requests\BaseRequest;

/**
Expand Down Expand Up @@ -47,71 +48,40 @@ public function __construct(public string $data, public bool $status, public arr
*/
public static function fromBytes(string $data, ValueSize $size): ChannelResponse|null
{
$valueSize = $size->value;
$offset = 0;

// Less than 1 byte? not enough for status.
if (strlen($data) < 1) {
return null;
}

$status = ord($data[$offset]) === 1;
$offset++;

if ($status) {
// Less than 1 + N bytes? not enough for number of subscribers.
if (strlen($data) < 1 + 8) {
// Less than 8 bytes? not enough for status and number of subscribers.
if (strlen($data) < $offset + 8) {
return null;
}

$subscribers = unpack(BaseRequest::pack(ValueSize::UINT64), substr($data, $offset, ValueSize::UINT64->value))[1];
$offset += ValueSize::UINT64->value;

if ($subscribers === 0) {
return new ChannelResponse($data, true, []);
}

$subscribers_container = [];

for ($i = 0; $i < $subscribers; ++$i) {
// Less than offset + 16 bytes? not enough for connection id.
if (strlen($data) < $offset + 16) {
return null;
}
if (strlen($data) < $offset + (16 + ValueSize::UINT64->value * 3) * $subscribers) {
return null;
}

for ($i = 0; $i < $subscribers; ++$i) {
$id = substr($data, $offset, 16);
$offset += 16;

// Less than offset + 8 bytes? not enough for subscribed at.
if (strlen($data) < $offset + ValueSize::UINT64->value) {
return null;
}

$subscribed_at = unpack(BaseRequest::pack(ValueSize::UINT64), substr($data, $offset, ValueSize::UINT64->value))[1];
$offset += ValueSize::UINT64->value;

// Less than offset + 8 bytes? not enough for read bytes.
if (strlen($data) < $offset + ValueSize::UINT64->value) {
return null;
}

$read_bytes = unpack(BaseRequest::pack(ValueSize::UINT64), substr($data, $offset, ValueSize::UINT64->value))[1];
$offset += ValueSize::UINT64->value;

// Less than offset + 8 bytes? not enough for write bytes.
if (strlen($data) < $offset + ValueSize::UINT64->value) {
return null;
}

$write_bytes = unpack(BaseRequest::pack(ValueSize::UINT64), substr($data, $offset, ValueSize::UINT64->value))[1];
$offset += ValueSize::UINT64->value;
$values = ReaderProvider::readIntegers($data, [
"subscribed_at" => ValueSize::UINT64,
"read_bytes" => ValueSize::UINT64,
"write_bytes" => ValueSize::UINT64,
], $offset);

$subscribers_container[] = [
"id" => bin2hex($id),
"subscribed_at" => $subscribed_at,
"read_bytes" => $read_bytes,
"write_bytes" => $write_bytes,
];
$subscribers_container[] = array_merge(
["id" => bin2hex($id)],
$values
);
}

return new ChannelResponse($data, true, $subscribers_container);
Expand Down
52 changes: 11 additions & 41 deletions src/Responses/ChannelsResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@

namespace Throttr\SDK\Responses;

use Throttr\SDK\Enum\KeyType;
use Throttr\SDK\Enum\TTLType;
use Throttr\SDK\Enum\ValueSize;
use Throttr\SDK\Providers\ReaderProvider;
use Throttr\SDK\Requests\BaseRequest;

/**
Expand Down Expand Up @@ -79,7 +78,6 @@ public static function fromBytes(string $data, ValueSize $size): ChannelsRespons
return null;
}

$fragment = unpack(BaseRequest::pack(ValueSize::UINT64), substr($data, $offset, ValueSize::UINT64->value))[1];
$offset += ValueSize::UINT64->value;

// Less than offset + 8 bytes? not enough for fragment keys count.
Expand All @@ -92,46 +90,18 @@ public static function fromBytes(string $data, ValueSize $size): ChannelsRespons

$channels_in_fragment = [];

if (strlen($data) < $offset + (ValueSize::UINT8->value + ValueSize::UINT64->value * 3) * $number_of_channels) {
return null;
}

// Per key in fragment
for ($e = 0; $e < $number_of_channels; ++$e) {
// Less than offset + 1 byte? not enough for key size.
if (strlen($data) < $offset + ValueSize::UINT8->value) {
return null;
}

$channel_size = unpack(BaseRequest::pack(ValueSize::UINT8), substr($data, $offset, ValueSize::UINT8->value))[1];
$offset += ValueSize::UINT8->value;

// Less than offset + 8 bytes? not enough for read bytes.
if (strlen($data) < $offset + ValueSize::UINT64->value) {
return null;
}

$read_bytes = unpack(BaseRequest::pack(ValueSize::UINT64), substr($data, $offset, ValueSize::UINT64->value))[1];
$offset += ValueSize::UINT64->value;

// Less than offset + 8 bytes? not enough for write bytes.
if (strlen($data) < $offset + ValueSize::UINT64->value) {
return null;
}

$write_bytes = unpack(BaseRequest::pack(ValueSize::UINT64), substr($data, $offset, ValueSize::UINT64->value))[1];
$offset += ValueSize::UINT64->value;

// Less than offset + 8 bytes? not enough for subscriptions.
if (strlen($data) < $offset + ValueSize::UINT64->value) {
return null;
}

$subscriptions = unpack(BaseRequest::pack(ValueSize::UINT64), substr($data, $offset, ValueSize::UINT64->value))[1];
$offset += ValueSize::UINT64->value;

$channels_in_fragment[] = [
"size" => $channel_size,
"read_bytes" => $read_bytes,
"write_bytes" => $write_bytes,
"subscriptions" => $subscriptions,
];
$channels_in_fragment[] = ReaderProvider::readIntegers($data, [
"size" => ValueSize::UINT8,
"read_bytes" => ValueSize::UINT64,
"write_bytes" => ValueSize::UINT64,
"subscriptions" => ValueSize::UINT64,
], $offset);
}

$total = array_sum(array_column($channels_in_fragment, 'size'));
Expand Down
136 changes: 36 additions & 100 deletions src/Responses/ConnectionResponse.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
use Throttr\SDK\Enum\KeyType;
use Throttr\SDK\Enum\TTLType;
use Throttr\SDK\Enum\ValueSize;
use Throttr\SDK\Providers\ReaderProvider;
use Throttr\SDK\Requests\BaseRequest;

/**
Expand All @@ -42,24 +43,24 @@ public function __construct(public string $data, public bool $status, public arr
}

public static array $types = [
"INSERT",
"SET",
"QUERY",
"GET",
"UPDATE",
"PURGE",
"LIST",
"INFO",
"STAT",
"STATS",
"PUBLISH",
"SUBSCRIBE",
"UNSUBSCRIBE",
"CONNECTIONS",
"CONNECTION",
"CHANNELS",
"CHANNEL",
"WHOAMI"
"INSERT" => ValueSize::UINT64,
"SET" => ValueSize::UINT64,
"QUERY" => ValueSize::UINT64,
"GET" => ValueSize::UINT64,
"UPDATE" => ValueSize::UINT64,
"PURGE" => ValueSize::UINT64,
"LIST" => ValueSize::UINT64,
"INFO" => ValueSize::UINT64,
"STAT" => ValueSize::UINT64,
"STATS" => ValueSize::UINT64,
"PUBLISH" => ValueSize::UINT64,
"SUBSCRIBE" => ValueSize::UINT64,
"UNSUBSCRIBE" => ValueSize::UINT64,
"CONNECTIONS" => ValueSize::UINT64,
"CONNECTION" => ValueSize::UINT64,
"CHANNELS" => ValueSize::UINT64,
"CHANNEL" => ValueSize::UINT64,
"WHOAMI" => ValueSize::UINT64,
];

/**
Expand All @@ -73,11 +74,6 @@ public static function fromBytes(string $data, ValueSize $size): ConnectionRespo
{
$offset = 0;

// Less than 1 byte? not enough for status.
if (strlen($data) < 1) {
return null;
}

$status = ord($data[$offset]) === 1;
$offset++;

Expand Down Expand Up @@ -122,96 +118,36 @@ public static function fromBytes(string $data, ValueSize $size): ConnectionRespo
$ip = substr($data, $offset, 16);
$offset += 16;

// Less than offset + 2 bytes? not enough for port.
if (strlen($data) < $offset + ValueSize::UINT16->value) {
return null;
}

$port = unpack(BaseRequest::pack(ValueSize::UINT16), substr($data, $offset, ValueSize::UINT16->value))[1];
$offset += ValueSize::UINT16->value;

// Less than offset + 8 bytes? not enough for connected at.
if (strlen($data) < $offset + ValueSize::UINT64->value) {
return null;
}

$connected_at = unpack(BaseRequest::pack(ValueSize::UINT64), substr($data, $offset, ValueSize::UINT64->value))[1];
$offset += ValueSize::UINT64->value;

// Less than offset + 8 bytes? not enough for read bytes.
if (strlen($data) < $offset + ValueSize::UINT64->value) {
return null;
}

$ready_bytes = unpack(BaseRequest::pack(ValueSize::UINT64), substr($data, $offset, ValueSize::UINT64->value))[1];
$offset += ValueSize::UINT64->value;

// Less than offset + 8 bytes? not enough for write bytes.
if (strlen($data) < $offset + ValueSize::UINT64->value) {
// Less than offset + 2 + 8 * 7 bytes? not enough for attributes.
if (strlen($data) < $offset + ValueSize::UINT16->value + ValueSize::UINT64->value * 7) {
return null;
}

$write_bytes = unpack(BaseRequest::pack(ValueSize::UINT64), substr($data, $offset, ValueSize::UINT64->value))[1];
$offset += ValueSize::UINT64->value;

// Less than offset + 8 bytes? not enough for published bytes.
if (strlen($data) < $offset + ValueSize::UINT64->value) {
return null;
}

$published_bytes = unpack(BaseRequest::pack(ValueSize::UINT64), substr($data, $offset, ValueSize::UINT64->value))[1];
$offset += ValueSize::UINT64->value;

// Less than offset + 8 bytes? not enough for received bytes.
if (strlen($data) < $offset + ValueSize::UINT64->value) {
return null;
}

$received_bytes = unpack(BaseRequest::pack(ValueSize::UINT64), substr($data, $offset, ValueSize::UINT64->value))[1];
$offset += ValueSize::UINT64->value;

// Less than offset + 8 bytes? not enough for allocated bytes.
if (strlen($data) < $offset + ValueSize::UINT64->value) {
$attributes = ReaderProvider::readIntegers($data, [
"port" => ValueSize::UINT16,
"connected_at" => ValueSize::UINT64,
"read_bytes" => ValueSize::UINT64,
"write_bytes" => ValueSize::UINT64,
"published_bytes" => ValueSize::UINT64,
"received_bytes" => ValueSize::UINT64,
"allocated_bytes" => ValueSize::UINT64,
"consumed_bytes" => ValueSize::UINT64,
], $offset);

if (strlen($data) < $offset + ValueSize::UINT64->value * count(static::$types)) {
return null;
}

$allocated_bytes = unpack(BaseRequest::pack(ValueSize::UINT64), substr($data, $offset, ValueSize::UINT64->value))[1];
$offset += ValueSize::UINT64->value;

// Less than offset + 8 bytes? not enough for consumed bytes.
if (strlen($data) < $offset + ValueSize::UINT64->value) {
return null;
}

$consumed_bytes = unpack(BaseRequest::pack(ValueSize::UINT64), substr($data, $offset, ValueSize::UINT64->value))[1];
$offset += ValueSize::UINT64->value;

$requests = [];
foreach (static::$types as $request_type) {
// Less than offset + 8 bytes? not enough for requests metric.
if (strlen($data) < $offset + ValueSize::UINT64->value) {
return null;
}
$requests[$request_type] = unpack(BaseRequest::pack(ValueSize::UINT64), substr($data, $offset, ValueSize::UINT64->value))[1];
$offset += ValueSize::UINT64->value;
}
$requests = ReaderProvider::readIntegers($data, static::$types, $offset);

return new ConnectionResponse($data, true, [
return new ConnectionResponse($data, true, array_merge([
"id" => bin2hex($id),
"type" => $type,
"kind" => $kind,
"ip_version" => $ip_version,
"ip" => $ip,
"port" => $port,
"connected_at" => $connected_at,
"read_bytes" => $ready_bytes,
"write_bytes" => $write_bytes,
"published_bytes" => $published_bytes,
"received_bytes" => $received_bytes,
"allocated_bytes" => $allocated_bytes,
"consumed_bytes" => $consumed_bytes,
"requests" => $requests,
]);
], $attributes));
}

return new ConnectionResponse($data, false, []);
Expand Down
Loading