From 14bae86b666d939c3822f1ae873368e12757c973 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Tue, 23 Mar 2021 10:30:14 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Rpc/Client.php | 29 +++++++++++++++-------------- Rpc/Producer.php | 36 +++++++++++++++++++++--------------- Rpc/Service.php | 8 +++++--- 3 files changed, 41 insertions(+), 32 deletions(-) diff --git a/Rpc/Client.php b/Rpc/Client.php index 6ab825da..eaecb7cf 100644 --- a/Rpc/Client.php +++ b/Rpc/Client.php @@ -4,6 +4,7 @@ namespace Rpc; +use Exception; use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Config; use Snowflake\Exception\ConfigException; @@ -32,37 +33,37 @@ class Client extends Component } - /** - * @param string $route - * @param array $param - * @return mixed|string|null - * @throws ConfigException - */ - public function request(string $route, array $param) + /** + * @param string $route + * @param array $param + * @return mixed + * @throws Exception + */ + public function request(string $route, array $param): mixed { $service = $this->config; if (empty($service)) { return null; } if (!($this->client instanceof CClient)) { - $client = $this->getClient(); + $this->client = $this->getClient(); } if (!$this->client->isConnected()) { - if (!$client->connect($service['host'], $service['port'], $service['timeout'])) { - return $client->errCode . ':' . $client->errMsg; + if (!$this->client->connect($service['host'], $service['port'], $service['timeout'])) { + return $this->client->errCode . ':' . $this->client->errMsg; } } - $isSend = $client->send(serialize(['route' => $route, 'body' => $param])); + $isSend = $this->client->send(serialize(['route' => $route, 'body' => $param])); if ($isSend === false) { - return $client->errCode . ':' . $client->errMsg; + return $this->client->errCode . ':' . $this->client->errMsg; } - return unserialize($client->recv()); + return unserialize($this->client->recv()); } /** * @return mixed - * @throws \Exception + * @throws Exception */ public function getClient(): CClient { diff --git a/Rpc/Producer.php b/Rpc/Producer.php index 1c1c6a84..53dd70b1 100644 --- a/Rpc/Producer.php +++ b/Rpc/Producer.php @@ -4,8 +4,13 @@ namespace Rpc; +use Exception; +use ReflectionException; use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Config; +use Snowflake\Exception\ComponentException; +use Snowflake\Exception\ConfigException; +use Snowflake\Exception\NotFindClassException; use Snowflake\Snowflake; @@ -19,14 +24,15 @@ class Producer extends Component private array $producers = []; - /** - * @param $name - * @return Producer|bool - * @throws \ReflectionException - * @throws \Snowflake\Exception\ComponentException - * @throws \Snowflake\Exception\ConfigException - * @throws \Snowflake\Exception\NotFindClassException - */ + /** + * @param $name + * @return Producer|bool + * @throws ReflectionException + * @throws ComponentException + * @throws ConfigException + * @throws NotFindClassException + * @throws Exception + */ public function get($name): Client|bool { if (empty($this->producers)) { @@ -52,13 +58,13 @@ class Producer extends Component /** * @param $name - * @return mixed - * @throws \ReflectionException - * @throws \Snowflake\Exception\ComponentException - * @throws \Snowflake\Exception\ConfigException - * @throws \Snowflake\Exception\NotFindClassException + * @return Client|bool + * @throws ReflectionException + * @throws ComponentException + * @throws ConfigException + * @throws NotFindClassException */ - public function __get($name): mixed + public function __get($name): Client|bool { return $this->get($name); // TODO: Change the autogenerated stub } @@ -69,7 +75,7 @@ class Producer extends Component * @param $rand * @return string */ - private function getName($name, $rand) + private function getName($name, $rand): string { return 'rpc.client.' . $name . '.' . $rand['host']; } diff --git a/Rpc/Service.php b/Rpc/Service.php index 7c20c115..25fa3952 100644 --- a/Rpc/Service.php +++ b/Rpc/Service.php @@ -9,6 +9,7 @@ use HttpServer\Service\Receive; use HttpServer\Service\Websocket; use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Config; +use Snowflake\Exception\ConfigException; /** @@ -19,9 +20,10 @@ class Service extends Component { - /** - * @throws \Snowflake\Exception\ConfigException - */ + /** + * @param Packet|Websocket|Receive|Http|null $server + * @throws ConfigException + */ public function instance(Packet|Websocket|Receive|null|Http $server): void { $services = Config::get('rpc.service', false, []);