Skip to content

Commit c8a652a

Browse files
committed
Added TcpServer
1 parent 637e5eb commit c8a652a

File tree

3 files changed

+158
-32
lines changed

3 files changed

+158
-32
lines changed

src/ConfigProvider.php

Lines changed: 0 additions & 32 deletions
This file was deleted.

src/Protocol.php

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Simps\RpcMultiplex;
13+
14+
class Protocol
15+
{
16+
/**
17+
* @var string
18+
*/
19+
public $class;
20+
21+
/**
22+
* @var string
23+
*/
24+
public $method;
25+
26+
/**
27+
* @var array
28+
*/
29+
public $params;
30+
31+
public function __construct(string $class, string $method, array $params)
32+
{
33+
$this->class = $class;
34+
$this->method = $method;
35+
$this->params = $params;
36+
}
37+
38+
public function getClass(): string
39+
{
40+
return $this->class;
41+
}
42+
43+
public function getMethod(): string
44+
{
45+
return $this->method;
46+
}
47+
48+
public function getParams(): array
49+
{
50+
return $this->params;
51+
}
52+
}

src/TcpServer.php

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
/**
5+
* This file is part of Hyperf.
6+
*
7+
* @link https://www.hyperf.io
8+
* @document https://hyperf.wiki
9+
* @contact [email protected]
10+
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
11+
*/
12+
namespace Simps\RpcMultiplex;
13+
14+
use Multiplex\Packer;
15+
use Multiplex\Serializer\StringSerializer;
16+
use Simps\Application;
17+
use Simps\Listener;
18+
use Simps\Route;
19+
use Swoole\Coroutine;
20+
use Swoole\Server;
21+
22+
class TcpServer
23+
{
24+
protected $_server;
25+
26+
protected $_config;
27+
28+
/** @var \Simps\Route */
29+
protected $_route;
30+
31+
/**
32+
* @var Packer
33+
*/
34+
protected $packer;
35+
36+
/**
37+
* @var StringSerializer
38+
*/
39+
protected $serializer;
40+
41+
public function __construct()
42+
{
43+
$this->packer = new Packer();
44+
$this->serializer = new StringSerializer();
45+
46+
$config = config('servers');
47+
$wsConfig = $config['rpc'];
48+
$this->_config = $wsConfig;
49+
$this->_server = new Server($wsConfig['ip'], $wsConfig['port'], $config['mode']);
50+
$this->_server->set($wsConfig['settings']);
51+
52+
if ($config['mode'] == SWOOLE_BASE) {
53+
$this->_server->on('managerStart', [$this, 'onManagerStart']);
54+
} else {
55+
$this->_server->on('start', [$this, 'onStart']);
56+
}
57+
58+
$this->_server->on('workerStart', [$this, 'onWorkerStart']);
59+
$this->_server->on('receive', [$this, 'onReceive']);
60+
61+
foreach ($wsConfig['callbacks'] as $eventKey => $callbackItem) {
62+
[$class, $func] = $callbackItem;
63+
$this->_server->on($eventKey, [$class, $func]);
64+
}
65+
66+
if (isset($this->_config['process']) && ! empty($this->_config['process'])) {
67+
foreach ($this->_config['process'] as $processItem) {
68+
[$class, $func] = $processItem;
69+
$this->_server->addProcess($class::$func($this->_server));
70+
}
71+
}
72+
73+
$this->_server->start();
74+
}
75+
76+
public function onStart(Server $server)
77+
{
78+
Application::echoSuccess("Swoole WebSocket Server running:ws://{$this->_config['ip']}:{$this->_config['port']}");
79+
Listener::getInstance()->listen('start', $server);
80+
}
81+
82+
public function onManagerStart(Server $server)
83+
{
84+
Application::echoSuccess("Swoole WebSocket Server running:ws://{$this->_config['ip']}:{$this->_config['port']}");
85+
Listener::getInstance()->listen('managerStart', $server);
86+
}
87+
88+
public function onWorkerStart(Server $server, int $workerId)
89+
{
90+
$this->_route = Route::getInstance();
91+
Listener::getInstance()->listen('workerStart', $server, $workerId);
92+
}
93+
94+
public function onReceive(Server $server, int $fd, int $fromId, string $data)
95+
{
96+
Coroutine::create(function () use ($server, $fd, $fromId, $data) {
97+
$packet = $this->packer->unpack($data);
98+
99+
$id = $packet->getId();
100+
$body = $packet->getBody();
101+
102+
/** @var Protocol $protocol */
103+
$protocol = unserialize($body);
104+
});
105+
}
106+
}

0 commit comments

Comments
 (0)