RecvTrait.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * This file is part of Hyperf.
  5. *
  6. * @link https://www.hyperf.io
  7. * @document https://hyperf.wiki
  8. * @contact group@hyperf.io
  9. * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
  10. */
  11. namespace Hyperf\JsonRpc;
  12. use Hyperf\Engine\Contract\SocketInterface;
  13. use Hyperf\JsonRpc\Pool\RpcConnection;
  14. use Hyperf\Rpc\Exception\RecvException;
  15. trait RecvTrait
  16. {
  17. /**
  18. * @param RpcConnection|SocketInterface $client
  19. */
  20. public function recvAndCheck(mixed $client, float $timeout)
  21. {
  22. $data = $client->recvPacket($timeout);
  23. if ($data === '') {
  24. // RpcConnection: When the next time the connection is taken out of the connection pool, it will reconnect to the target service.
  25. // Client: It will reconnect to the target service in the next request.
  26. $client->close();
  27. throw new RecvException('Connection is closed. ' . $client->errMsg, $client->errCode);
  28. }
  29. if ($data === false) {
  30. $client->close();
  31. throw new RecvException('Error receiving data, errno=' . $client->errCode . ' errmsg=' . $client->errMsg, $client->errCode);
  32. }
  33. return $data;
  34. }
  35. }