Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/change seq to nostatic variable #33

Merged
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
Loading