diff --git a/.github/workflows/ci-build.yml b/.github/workflows/ci-build.yml index f86f143..c6652e2 100644 --- a/.github/workflows/ci-build.yml +++ b/.github/workflows/ci-build.yml @@ -53,4 +53,4 @@ jobs: token: ${{ secrets.CODECOV_TOKEN }} file: ./coverage.xml flags: php - fail_ci_if_error: false + fail_ci_if_error: false \ No newline at end of file diff --git a/examples/swoole.php b/examples/swoole.php new file mode 100644 index 0000000..c8b479c --- /dev/null +++ b/examples/swoole.php @@ -0,0 +1,23 @@ + SWOOLE_HOOK_ALL]); +Co\Run(function () { + $barrier = Barrier::make(); + for ($i = 0; $i < 3; $i++) { + go(function () use ($barrier) { + $rpc = new Goridge\RPC\RPC( + Goridge\Relay::create('tcp://127.0.0.1:6001') + ); + echo $rpc->call('App.Hi', 'Antony'); + }); + } + Barrier::wait($barrier); +}); \ No newline at end of file diff --git a/src/RPC/RPC.php b/src/RPC/RPC.php index 1e07e9f..f2d4b49 100644 --- a/src/RPC/RPC.php +++ b/src/RPC/RPC.php @@ -30,11 +30,6 @@ class RPC implements RPCInterface */ private ?string $service = null; - /** - * @var positive-int - */ - private static int $seq = 1; - /** * @param RelayInterface $relay * @param CodecInterface|null $codec @@ -76,7 +71,9 @@ public function withCodec(CodecInterface $codec): RPCInterface */ public function call(string $method, $payload, $options = null) { - $this->relay->send($this->packFrame($method, $payload)); + $seq = $this->relay->getNextSeq(); + + $this->relay->send($this->packFrame($method, $payload, $seq)); // wait for the frame confirmation $frame = $this->relay->waitFrame(); @@ -85,12 +82,10 @@ public function call(string $method, $payload, $options = null) throw new RPCException('Invalid RPC frame, options missing'); } - if ($frame->options[0] !== self::$seq) { + if ($frame->options[0] !== $seq) { throw new RPCException('Invalid RPC frame, sequence mismatch'); } - self::$seq++; - return $this->decodeResponse($frame, $options); } @@ -163,13 +158,13 @@ private function decodeResponse(Frame $frame, $options = null) * @param mixed $payload * @return Frame */ - private function packFrame(string $method, $payload): Frame + private function packFrame(string $method, $payload, int $seq): Frame { if ($this->service !== null) { $method = $this->service . '.' . \ucfirst($method); } $body = $method . $this->codec->encode($payload); - return new Frame($body, [self::$seq, \strlen($method)], $this->codec->getIndex()); + return new Frame($body, [$seq, \strlen($method)], $this->codec->getIndex()); } } diff --git a/src/Relay.php b/src/Relay.php index ec4794d..a967452 100644 --- a/src/Relay.php +++ b/src/Relay.php @@ -13,6 +13,11 @@ abstract class Relay implements RelayInterface public const PIPES = 'pipes'; protected const CONNECTION_EXP = '/(?P[^:\/]+):\/\/(?P[^:]+)(:(?P[^:]+))?/'; + /** + * @var int + */ + private int $seq = 1; + /** * Create relay using string address. * @@ -93,4 +98,12 @@ private static function openOut(string $output) return $resource; } + + /** + * @return int + */ + public function getNextSeq(): int + { + return $this->seq++; + } } diff --git a/src/RelayInterface.php b/src/RelayInterface.php index 209983c..27e7f96 100644 --- a/src/RelayInterface.php +++ b/src/RelayInterface.php @@ -21,4 +21,9 @@ public function waitFrame(): Frame; * @param Frame $frame */ public function send(Frame $frame): void; + + /** + * @return int + */ + public function getNextSeq(): int; }