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
+11 -10
View File
@@ -4,6 +4,7 @@
namespace Rpc;
use Exception;
use Snowflake\Abstracts\Component;
use Snowflake\Abstracts\Config;
use Snowflake\Exception\ConfigException;
@@ -35,34 +36,34 @@ class Client extends Component
/**
* @param string $route
* @param array $param
* @return mixed|string|null
* @throws ConfigException
* @return mixed
* @throws Exception
*/
public function request(string $route, array $param)
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
{
+17 -11
View File
@@ -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;
@@ -22,10 +27,11 @@ class Producer extends Component
/**
* @param $name
* @return Producer|bool
* @throws \ReflectionException
* @throws \Snowflake\Exception\ComponentException
* @throws \Snowflake\Exception\ConfigException
* @throws \Snowflake\Exception\NotFindClassException
* @throws ReflectionException
* @throws ComponentException
* @throws ConfigException
* @throws NotFindClassException
* @throws Exception
*/
public function get($name): Client|bool
{
@@ -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'];
}
+3 -1
View File
@@ -9,6 +9,7 @@ use HttpServer\Service\Receive;
use HttpServer\Service\Websocket;
use Snowflake\Abstracts\Component;
use Snowflake\Abstracts\Config;
use Snowflake\Exception\ConfigException;
/**
@@ -20,7 +21,8 @@ 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
{