Skip to content

Commit

Permalink
feat[php]: php support
Browse files Browse the repository at this point in the history
  • Loading branch information
jaysunxiao committed Jul 11, 2024
1 parent 4497a02 commit 5e6e8eb
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
26 changes: 24 additions & 2 deletions protocol/src/main/resources/php/ByteBuffer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,19 @@

use Exception;


class ByteBuffer
{
private string $buffer;
private int $writeOffset = 0;
private int $readOffset = 0;
private bool $bigEndian = true;

public function __construct()
{
$this->buffer = str_pad("", 128);
// 大端序还是小端序
$this->bigEndian = pack("L", 1) === "\x00\x00\x00\x01";
}

public function adjustPadding(int $predictionLength, int $beforewriteIndex): void
Expand Down Expand Up @@ -117,6 +121,24 @@ public function readBytes(int $count): string
return $bytes;
}

public function writeBytesBigEndian(string $bytes): void
{
if ($this->bigEndian) {
$this->writeBytes($bytes);
} else {
$this->writeBytes(strrev($bytes));
}
}

public function readBytesBigEndian(int $count): string
{
if ($this->bigEndian) {
return $this->readBytes($count);
} else {
return strrev($this->readBytes($count));
}
}

public function writeByte(int $value): void
{
$this->writeBytes(pack('c', $value));
Expand Down Expand Up @@ -149,12 +171,12 @@ public function readBool(): bool

public function writeShort(int $value): void
{
$this->writeBytes(pack('s', $value));
$this->writeBytesBigEndian(pack('s', $value));
}

public function readShort(): int
{
return unpack('s', $this->readBytes(2))[1];
return unpack('s', $this->readBytesBigEndian(2))[1];
}

public function writeRawInt(int $value): void
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ public function read(ByteBuffer $buffer): mixed
if ($length == 0) {
return $packet;
}
$beforeReadIndex = $buffer->readOffset();
$beforeReadIndex = $buffer->getReadOffset();
${protocol_read_deserialization}
if ($length > 0) {
$buffer->setReadOffset($beforeReadIndex + $length);
Expand Down

0 comments on commit 5e6e8eb

Please sign in to comment.