Skip to content

Commit

Permalink
Change seq to no-static variable (#33)
Browse files Browse the repository at this point in the history
  • Loading branch information
shellphy committed Jun 27, 2024
1 parent 1562254 commit 8852269
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 35 deletions.
32 changes: 6 additions & 26 deletions src/RPC/RPC.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,8 @@ class RPC implements RPCInterface

/**
* @var positive-int
* @deprecated since v3.2.1.
*/
private static int $seq = 1;

/**
* @deprecated since v3.2.1. Need for backward compatibility.
*/
private bool $hasSequence = false;
private int $seq = 1;

/**
* @param RelayInterface $relay
Expand All @@ -49,8 +43,6 @@ public function __construct(RelayInterface $relay, CodecInterface $codec = null)
{
$this->relay = $relay;
$this->codec = $codec ?? new JsonCodec();
/** @psalm-suppress DeprecatedProperty */
$this->hasSequence = \method_exists($this->relay, 'getNextSequence');
}

/**
Expand Down Expand Up @@ -84,10 +76,7 @@ public function withCodec(CodecInterface $codec): RPCInterface
*/
public function call(string $method, $payload, $options = null)
{
/** @psalm-suppress DeprecatedMethod */
$seq = $this->getNextSequence();

$this->relay->send($this->packFrame($method, $payload, $seq));
$this->relay->send($this->packFrame($method, $payload));

// wait for the frame confirmation
$frame = $this->relay->waitFrame();
Expand All @@ -96,11 +85,11 @@ public function call(string $method, $payload, $options = null)
throw new RPCException('Invalid RPC frame, options missing');
}

if ($frame->options[0] !== $seq) {
if ($frame->options[0] !== $this->seq) {
throw new RPCException('Invalid RPC frame, sequence mismatch');
}

self::$seq++;
$this->seq++;

return $this->decodeResponse($frame, $options);
}
Expand Down Expand Up @@ -174,22 +163,13 @@ private function decodeResponse(Frame $frame, $options = null)
* @param mixed $payload
* @return Frame
*/
private function packFrame(string $method, $payload, int $seq): Frame
private function packFrame(string $method, $payload): Frame
{
if ($this->service !== null) {
$method = $this->service . '.' . \ucfirst($method);
}

$body = $method . $this->codec->encode($payload);
return new Frame($body, [$seq, \strlen($method)], $this->codec->getIndex());
}

/**
* @deprecated since v3.2.1.
*/
private function getNextSequence(): int
{
/** @psalm-suppress DeprecatedProperty */
return $this->hasSequence ? $this->relay->getNextSequence() : self::$seq;
return new Frame($body, [$this->seq, \strlen($method)], $this->codec->getIndex());
}
}
7 changes: 0 additions & 7 deletions src/Relay.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ abstract class Relay implements RelayInterface
public const PIPES = 'pipes';
protected const CONNECTION_EXP = '/(?P<protocol>[^:\/]+):\/\/(?P<arg1>[^:]+)(:(?P<arg2>[^:]+))?/';

private int $sequence = 1;

/**
* Create relay using string address.
*
Expand Down Expand Up @@ -95,9 +93,4 @@ private static function openOut(string $output)

return $resource;
}

public function getNextSequence(): int
{
return $this->sequence++;
}
}
5 changes: 4 additions & 1 deletion src/RelayInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,17 @@

/**
* Blocking, duplex relay.
* @method getNextSequence(): int
*/
interface RelayInterface
{
/**
* @return Frame
* @throws RelayException
*/
public function waitFrame(): Frame;

/**
* @param Frame $frame
*/
public function send(Frame $frame): void;
}
2 changes: 1 addition & 1 deletion tests/Goridge/RPCTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public function testLongRawBody(): void
{
$conn = $this->makeRPC();
$payload = random_bytes(65000 * 1000);

$resp = $conn->withCodec(new RawCodec())->call(
'Service.EchoBinary',
$payload
Expand Down

0 comments on commit 8852269

Please sign in to comment.