Skip to content

Commit 80b1fce

Browse files
committed
Optimize code
1 parent decde49 commit 80b1fce

File tree

2 files changed

+21
-17
lines changed

2 files changed

+21
-17
lines changed

src/core/RemoteObject.php

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111

1212
namespace Swoole;
1313

14-
use Swoole\Coroutine\Http\Client as HttpClient;
1514
use Swoole\RemoteObject\Client;
1615
use Swoole\RemoteObject\Exception;
1716

@@ -23,7 +22,7 @@ class RemoteObject implements \ArrayAccess, \Stringable, \Iterator, \Countable
2322

2423
private string $clientId;
2524

26-
private ?HttpClient $client = null;
25+
private ?Client $client = null;
2726

2827
public function __construct($coroutineId, $clientId)
2928
{
@@ -89,7 +88,7 @@ public function __unserialize(array $data): void
8988
$this->objectId = $data['objectId'];
9089
$this->coroutineId = $data['coroutineId'];
9190
$this->clientId = $data['clientId'];
92-
$this->client = Client::getClient($this->clientId);
91+
$this->client = Client::getInstance($this->clientId);
9392
}
9493

9594
public function __serialize(): array
@@ -122,7 +121,7 @@ public function __invoke(...$args)
122121
public static function call(Client $client, string $fn, array $args)
123122
{
124123
$object = new self(Coroutine::getCid(), $client->getId());
125-
$object->client = Client::getClient($client->getId());
124+
$object->client = $client;
126125
$rs = $object->execute('/call_function', [
127126
'function' => $fn,
128127
'args' => serialize($args),
@@ -141,7 +140,7 @@ public function getObjectId(): int
141140
public static function create(Client $client, string $class, array $args): RemoteObject
142141
{
143142
$object = new self(Coroutine::getCid(), $client->getId());
144-
$object->client = Client::getClient($client->getId());
143+
$object->client = $client;
145144
$rs = $object->execute('/new', [
146145
'class' => $class,
147146
'args' => serialize($args),
@@ -236,15 +235,6 @@ private function execute(string $path, array $params = []): array
236235
if (!$this->client) {
237236
throw new Exception('This remote object is not bound to a client, and cannot initiate remote calls');
238237
}
239-
$rs = $this->client->post($path, $params);
240-
if (!$rs) {
241-
throw new Exception($this->client->errMsg);
242-
}
243-
$result = unserialize($this->client->body);
244-
if ($result['code'] != 0) {
245-
$ex = $result['exception'];
246-
throw new Exception('Server Error: ' . $ex['message'], $ex['code']);
247-
}
248-
return $result;
238+
return $this->client->execute($path, $params);
249239
}
250240
}

src/core/RemoteObject/Client.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,22 +55,36 @@ public function call(string $fn, mixed ...$args): mixed
5555
/**
5656
* @throws Exception
5757
*/
58-
public static function getClient(string $clientId): ?HttpClient
58+
public static function getInstance(string $clientId): ?static
5959
{
6060
if (empty($clientId)) {
6161
throw new Exception('RemoteObject is not bound to a client');
6262
}
6363
if (!isset(self::$clients[$clientId])) {
6464
return null;
6565
}
66-
return self::$clients[$clientId]->client;
66+
return self::$clients[$clientId];
6767
}
6868

6969
public function getId(): string
7070
{
7171
return $this->id;
7272
}
7373

74+
public function execute(string $path, array $array)
75+
{
76+
$rs = $this->client->post($path, $array);
77+
if (!$rs) {
78+
throw new Exception($this->client->errMsg);
79+
}
80+
$result = unserialize($this->client->body);
81+
if ($result['code'] != 0) {
82+
$ex = $result['exception'];
83+
throw new Exception('Server Error: ' . $ex['message'], $ex['code']);
84+
}
85+
return $result;
86+
}
87+
7488
private function genUuid(): string
7589
{
7690
$data = random_bytes(16);

0 commit comments

Comments
 (0)