Skip to content

Commit ca7987a

Browse files
committed
Optimized TcpServer::onReceive
1 parent c8a652a commit ca7987a

File tree

3 files changed

+65
-10
lines changed

3 files changed

+65
-10
lines changed

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@
1919
},
2020
"require": {
2121
"php": ">=7.3",
22-
"multiplex/socket": "^0.1"
22+
"multiplex/socket": "^0.1",
23+
"simple-swoole/simps": "^1.0"
2324
},
2425
"require-dev": {
2526
"friendsofphp/php-cs-fixer": "^2.14",

src/Protocol.php

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,16 @@ class Protocol
2828
*/
2929
public $params;
3030

31+
/**
32+
* @var mixed
33+
*/
34+
public $result;
35+
36+
/**
37+
* @var array
38+
*/
39+
public $error;
40+
3141
public function __construct(string $class, string $method, array $params)
3242
{
3343
$this->class = $class;
@@ -49,4 +59,30 @@ public function getParams(): array
4959
{
5060
return $this->params;
5161
}
62+
63+
public function getResult()
64+
{
65+
return $this->result;
66+
}
67+
68+
public function setResult($result)
69+
{
70+
$this->result = $result;
71+
return $this;
72+
}
73+
74+
public function setError(int $code, string $message)
75+
{
76+
$this->error = [
77+
'code' => $code,
78+
'message' => $message,
79+
];
80+
81+
return $this;
82+
}
83+
84+
public function getError(): array
85+
{
86+
return $this->error;
87+
}
5288
}

src/TcpServer.php

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
namespace Simps\RpcMultiplex;
1313

1414
use Multiplex\Packer;
15+
use Multiplex\Packet;
1516
use Multiplex\Serializer\StringSerializer;
1617
use Simps\Application;
1718
use Simps\Listener;
@@ -21,11 +22,19 @@
2122

2223
class TcpServer
2324
{
25+
/**
26+
* @var Server
27+
*/
2428
protected $_server;
2529

30+
/**
31+
* @var array
32+
*/
2633
protected $_config;
2734

28-
/** @var \Simps\Route */
35+
/**
36+
* @var Route
37+
*/
2938
protected $_route;
3039

3140
/**
@@ -75,13 +84,13 @@ public function __construct()
7584

7685
public function onStart(Server $server)
7786
{
78-
Application::echoSuccess("Swoole WebSocket Server running:ws://{$this->_config['ip']}:{$this->_config['port']}");
87+
Application::echoSuccess("Swoole Multiplex RPC Server running:tcp://{$this->_config['ip']}:{$this->_config['port']}");
7988
Listener::getInstance()->listen('start', $server);
8089
}
8190

8291
public function onManagerStart(Server $server)
8392
{
84-
Application::echoSuccess("Swoole WebSocket Server running:ws://{$this->_config['ip']}:{$this->_config['port']}");
93+
Application::echoSuccess("Swoole Multiplex RPC Server running:tcp://{$this->_config['ip']}:{$this->_config['port']}");
8594
Listener::getInstance()->listen('managerStart', $server);
8695
}
8796

@@ -93,14 +102,23 @@ public function onWorkerStart(Server $server, int $workerId)
93102

94103
public function onReceive(Server $server, int $fd, int $fromId, string $data)
95104
{
96-
Coroutine::create(function () use ($server, $fd, $fromId, $data) {
105+
Coroutine::create(function () use ($server, $fd, $data) {
97106
$packet = $this->packer->unpack($data);
98-
99107
$id = $packet->getId();
100-
$body = $packet->getBody();
101-
102-
/** @var Protocol $protocol */
103-
$protocol = unserialize($body);
108+
try {
109+
/** @var Protocol $protocol */
110+
$protocol = unserialize($packet->getBody());
111+
112+
$class = $protocol->getClass();
113+
$method = $protocol->getMethod();
114+
$params = $protocol->getParams();
115+
$protocol->setResult((new $class())->{$method}(...$params));
116+
$result = serialize($protocol);
117+
} catch (\Throwable $exception) {
118+
$result = serialize($protocol->setError($exception->getCode(), $exception->getMessage()));
119+
} finally {
120+
$server->send($fd, $this->packer->pack(new Packet($id, $this->serializer->serialize($result))));
121+
}
104122
});
105123
}
106124
}

0 commit comments

Comments
 (0)