2022-09-23 18:55:46 +08:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
namespace Kiri\Rpc;
|
|
|
|
|
|
|
|
|
|
use Kiri\Core\Json;
|
|
|
|
|
use Kiri\Core\Number;
|
2023-05-20 23:05:38 +08:00
|
|
|
use Kiri\Di\Inject\Container;
|
2022-09-23 18:55:46 +08:00
|
|
|
|
|
|
|
|
abstract class AbstractRpcClient
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string $service = '';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public string $version = '';
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @var JsonRpcTransporterInterface
|
|
|
|
|
*/
|
2023-05-20 23:05:38 +08:00
|
|
|
#[Container(JsonRpcTransporterInterface::class)]
|
2022-09-23 18:55:46 +08:00
|
|
|
private JsonRpcTransporterInterface $transporter;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param JsonRpcTransporterInterface $transporter
|
|
|
|
|
*/
|
|
|
|
|
public function setTransporter(JsonRpcTransporterInterface $transporter): void
|
|
|
|
|
{
|
|
|
|
|
$this->transporter = $transporter;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @return string
|
|
|
|
|
*/
|
|
|
|
|
public function getService(): string
|
|
|
|
|
{
|
|
|
|
|
if (empty($this->service)) {
|
|
|
|
|
return get_called_class();
|
|
|
|
|
}
|
|
|
|
|
return $this->service;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param string $method
|
|
|
|
|
* @param ...$args
|
|
|
|
|
* @return string|bool
|
|
|
|
|
*/
|
|
|
|
|
protected function send(string $method, ...$args): string|bool
|
|
|
|
|
{
|
|
|
|
|
$result = $this->transporter->push(Json::encode([
|
|
|
|
|
'jsonrpc' => $this->version,
|
|
|
|
|
'service' => $this->getService(),
|
|
|
|
|
'method' => $method,
|
|
|
|
|
'params' => $args,
|
|
|
|
|
'id' => Number::create(time())
|
|
|
|
|
]), $this->getService());
|
|
|
|
|
if (is_string($result)) {
|
|
|
|
|
return json_decode($result, true);
|
|
|
|
|
} else {
|
|
|
|
|
return $result;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|