This commit is contained in:
2021-03-23 10:30:14 +08:00
parent c6e8871b9c
commit 14bae86b66
3 changed files with 41 additions and 32 deletions
+15 -14
View File
@@ -4,6 +4,7 @@
namespace Rpc; namespace Rpc;
use Exception;
use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Component;
use Snowflake\Abstracts\Config; use Snowflake\Abstracts\Config;
use Snowflake\Exception\ConfigException; use Snowflake\Exception\ConfigException;
@@ -32,37 +33,37 @@ class Client extends Component
} }
/** /**
* @param string $route * @param string $route
* @param array $param * @param array $param
* @return mixed|string|null * @return mixed
* @throws ConfigException * @throws Exception
*/ */
public function request(string $route, array $param) public function request(string $route, array $param): mixed
{ {
$service = $this->config; $service = $this->config;
if (empty($service)) { if (empty($service)) {
return null; return null;
} }
if (!($this->client instanceof CClient)) { if (!($this->client instanceof CClient)) {
$client = $this->getClient(); $this->client = $this->getClient();
} }
if (!$this->client->isConnected()) { if (!$this->client->isConnected()) {
if (!$client->connect($service['host'], $service['port'], $service['timeout'])) { if (!$this->client->connect($service['host'], $service['port'], $service['timeout'])) {
return $client->errCode . ':' . $client->errMsg; 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) { 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 * @return mixed
* @throws \Exception * @throws Exception
*/ */
public function getClient(): CClient public function getClient(): CClient
{ {
+21 -15
View File
@@ -4,8 +4,13 @@
namespace Rpc; namespace Rpc;
use Exception;
use ReflectionException;
use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Component;
use Snowflake\Abstracts\Config; use Snowflake\Abstracts\Config;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
@@ -19,14 +24,15 @@ class Producer extends Component
private array $producers = []; private array $producers = [];
/** /**
* @param $name * @param $name
* @return Producer|bool * @return Producer|bool
* @throws \ReflectionException * @throws ReflectionException
* @throws \Snowflake\Exception\ComponentException * @throws ComponentException
* @throws \Snowflake\Exception\ConfigException * @throws ConfigException
* @throws \Snowflake\Exception\NotFindClassException * @throws NotFindClassException
*/ * @throws Exception
*/
public function get($name): Client|bool public function get($name): Client|bool
{ {
if (empty($this->producers)) { if (empty($this->producers)) {
@@ -52,13 +58,13 @@ class Producer extends Component
/** /**
* @param $name * @param $name
* @return mixed * @return Client|bool
* @throws \ReflectionException * @throws ReflectionException
* @throws \Snowflake\Exception\ComponentException * @throws ComponentException
* @throws \Snowflake\Exception\ConfigException * @throws ConfigException
* @throws \Snowflake\Exception\NotFindClassException * @throws NotFindClassException
*/ */
public function __get($name): mixed public function __get($name): Client|bool
{ {
return $this->get($name); // TODO: Change the autogenerated stub return $this->get($name); // TODO: Change the autogenerated stub
} }
@@ -69,7 +75,7 @@ class Producer extends Component
* @param $rand * @param $rand
* @return string * @return string
*/ */
private function getName($name, $rand) private function getName($name, $rand): string
{ {
return 'rpc.client.' . $name . '.' . $rand['host']; return 'rpc.client.' . $name . '.' . $rand['host'];
} }
+5 -3
View File
@@ -9,6 +9,7 @@ use HttpServer\Service\Receive;
use HttpServer\Service\Websocket; use HttpServer\Service\Websocket;
use Snowflake\Abstracts\Component; use Snowflake\Abstracts\Component;
use Snowflake\Abstracts\Config; 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 public function instance(Packet|Websocket|Receive|null|Http $server): void
{ {
$services = Config::get('rpc.service', false, []); $services = Config::get('rpc.service', false, []);