1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is a part of Woketo package. |
5
|
|
|
* |
6
|
|
|
* (c) Nekland <[email protected]> |
7
|
|
|
* |
8
|
|
|
* For the full license, take a look to the LICENSE file |
9
|
|
|
* on the root directory of this project |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Nekland\Woketo\Server; |
13
|
|
|
|
14
|
|
|
use Nekland\Woketo\Exception\ConfigException; |
15
|
|
|
use Nekland\Woketo\Exception\RuntimeException; |
16
|
|
|
use Nekland\Woketo\Message\MessageHandlerInterface; |
17
|
|
|
use Nekland\Woketo\Rfc6455\FrameFactory; |
18
|
|
|
use Nekland\Woketo\Rfc6455\Handshake\ServerHandshake; |
19
|
|
|
use Nekland\Woketo\Rfc6455\MessageFactory; |
20
|
|
|
use Nekland\Woketo\Rfc6455\FrameHandler\CloseFrameHandler; |
21
|
|
|
use Nekland\Woketo\Rfc6455\FrameHandler\RsvCheckFrameHandler; |
22
|
|
|
use Nekland\Woketo\Rfc6455\FrameHandler\WrongOpcodeFrameHandler; |
23
|
|
|
use Nekland\Woketo\Rfc6455\FrameHandler\PingFrameHandler; |
24
|
|
|
use Nekland\Woketo\Rfc6455\MessageProcessor; |
25
|
|
|
use Nekland\Woketo\Utils\SimpleLogger; |
26
|
|
|
use Psr\Log\LoggerInterface; |
27
|
|
|
use Psr\Log\LogLevel; |
28
|
|
|
use React\EventLoop\LoopInterface; |
29
|
|
|
use React\Socket\ConnectionInterface; |
30
|
|
|
use React\Socket\ServerInterface; |
31
|
|
|
|
32
|
|
|
class WebSocketServer |
33
|
|
|
{ |
34
|
|
|
/** |
35
|
|
|
* @var int |
36
|
|
|
*/ |
37
|
|
|
private $port; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* @var string |
41
|
|
|
*/ |
42
|
|
|
private $host; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* @var ServerHandshake |
46
|
|
|
*/ |
47
|
|
|
private $handshake; |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* @var MessageHandlerInterface[] |
51
|
|
|
*/ |
52
|
|
|
private $messageHandlers; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* @var Connection[] |
56
|
|
|
*/ |
57
|
|
|
private $connections; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* @var LoopInterface |
61
|
|
|
*/ |
62
|
|
|
private $loop; |
63
|
|
|
|
64
|
|
|
/** |
65
|
|
|
* @var ServerInterface |
66
|
|
|
*/ |
67
|
|
|
private $server; |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @var MessageProcessor |
71
|
|
|
*/ |
72
|
|
|
private $messageProcessor; |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* @var array |
76
|
|
|
*/ |
77
|
|
|
private $config; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* @var LoggerInterface |
81
|
|
|
*/ |
82
|
|
|
private $logger; |
83
|
|
|
|
84
|
|
|
/** |
85
|
|
|
* @param int $port The number of the port to bind |
86
|
|
|
* @param string $host The host to listen on (by default 127.0.0.1) |
87
|
|
|
* @param array $config |
88
|
|
|
*/ |
89
|
9 |
|
public function __construct($port, $host = '127.0.0.1', $config = []) |
90
|
|
|
{ |
91
|
9 |
|
$this->setConfig($config); |
92
|
8 |
|
$this->host = $host; |
93
|
8 |
|
$this->port = $port; |
94
|
8 |
|
$this->handshake = new ServerHandshake(); |
95
|
8 |
|
$this->connections = []; |
96
|
8 |
|
$this->buildMessageProcessor(); |
97
|
|
|
|
98
|
|
|
// Some optimization |
99
|
7 |
|
\gc_enable(); // As the process never stops, the garbage collector will be usefull, you may need to call it manually sometimes for performance purpose |
100
|
7 |
|
\set_time_limit(0); // It's by default on most server for cli apps but better be sure of that fact |
101
|
7 |
|
} |
102
|
|
|
|
103
|
|
|
/** |
104
|
|
|
* @param MessageHandlerInterface|string $messageHandler An instance of a class as string |
105
|
|
|
* @param string $uri The URI you want to bind on |
106
|
|
|
*/ |
107
|
5 |
|
public function setMessageHandler($messageHandler, $uri = '*') |
108
|
|
|
{ |
109
|
5 |
|
if (!$messageHandler instanceof MessageHandlerInterface && !\is_string($messageHandler)) { |
110
|
|
|
throw new \InvalidArgumentException('The message handler must be an instance of MessageHandlerInterface or a string.'); |
111
|
|
|
} |
112
|
5 |
|
if (\is_string($messageHandler)) { |
113
|
|
|
try { |
114
|
|
|
$reflection = new \ReflectionClass($messageHandler); |
115
|
|
|
if(!$reflection->implementsInterface('Nekland\Woketo\Message\MessageHandlerInterface')) { |
116
|
|
|
throw new \InvalidArgumentException('The messageHandler must implement MessageHandlerInterface'); |
117
|
|
|
} |
118
|
|
|
} catch (\ReflectionException $e) { |
119
|
|
|
throw new \InvalidArgumentException('The messageHandler must be a string representing a class.'); |
120
|
|
|
} |
121
|
|
|
} |
122
|
5 |
|
$this->messageHandlers[$uri] = $messageHandler; |
123
|
5 |
|
} |
124
|
|
|
|
125
|
|
|
/** |
126
|
|
|
* Launch the WebSocket server and an infinite loop that act on event. |
127
|
|
|
* |
128
|
|
|
* @throws \Exception |
129
|
|
|
*/ |
130
|
5 |
|
public function start() |
131
|
|
|
{ |
132
|
5 |
|
if ($this->config['prod'] && \extension_loaded('xdebug')) { |
133
|
|
|
throw new \Exception('xdebug is enabled, it\'s a performance issue. Disable that extension or specify "prod" option to false.'); |
134
|
|
|
} |
135
|
|
|
|
136
|
5 |
|
$this->loop = $this->loop ?? \React\EventLoop\Factory::create(); |
137
|
5 |
|
$this->server = $this->server ?? new \React\Socket\TcpServer($this->host . ':' . $this->port, $this->loop); |
138
|
|
|
|
139
|
5 |
|
if ($this->config['ssl']) { |
140
|
|
|
$this->server = new \React\Socket\SecureServer($this->server, $this->loop, array_merge([ |
141
|
|
|
'local_cert' => $this->config['certFile'], |
142
|
|
|
'passphrase' => $this->config['passphrase'], |
143
|
|
|
], $this->config['sslContextOptions'])); |
144
|
|
|
$this->getLogger()->info('Enabled ssl'); |
145
|
|
|
} |
146
|
|
|
|
147
|
5 |
|
$this->server->on('connection', function (ConnectionInterface $socketStream) { |
148
|
5 |
|
$this->onNewConnection($socketStream); |
149
|
5 |
|
}); |
150
|
|
|
|
151
|
5 |
|
$this->getLogger()->info('Listening on ' . $this->host . ':' . $this->port); |
152
|
|
|
|
153
|
5 |
|
$this->loop->run(); |
154
|
5 |
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* @param ConnectionInterface $socketStream |
158
|
|
|
*/ |
159
|
|
|
private function onNewConnection(ConnectionInterface $socketStream) |
160
|
|
|
{ |
161
|
5 |
|
$connection = new Connection($socketStream, function ($uri, Connection $connection) { |
162
|
5 |
|
return $this->getMessageHandler($uri, $connection); |
163
|
5 |
|
}, $this->loop, $this->messageProcessor); |
164
|
|
|
|
165
|
5 |
|
$socketStream->on('end', function () use($connection) { |
166
|
1 |
|
$this->onDisconnect($connection); |
167
|
5 |
|
}); |
168
|
|
|
|
169
|
5 |
|
$connection->setLogger($this->getLogger()); |
170
|
5 |
|
$connection->getLogger()->info(sprintf('Ip "%s" establish connection', $connection->getIp())); |
171
|
5 |
|
$this->connections[] = $connection; |
172
|
5 |
|
} |
173
|
|
|
|
174
|
|
|
/** |
175
|
|
|
* |
176
|
|
|
* @param Connection $connection |
177
|
|
|
*/ |
178
|
1 |
|
private function onDisconnect(Connection $connection) |
179
|
|
|
{ |
180
|
1 |
|
$this->removeConnection($connection); |
181
|
1 |
|
$connection->getLogger()->info(sprintf('Ip "%s" left connection', $connection->getIp())); |
182
|
1 |
|
} |
183
|
|
|
|
184
|
|
|
/** |
185
|
|
|
* Remove a Connection instance by his object id |
186
|
|
|
* @param Connection $connection |
187
|
|
|
* @throws RuntimeException This method throw an exception if the $connection instance object isn't findable in websocket server's connections |
188
|
|
|
*/ |
189
|
1 |
|
private function removeConnection(Connection $connection) |
190
|
|
|
{ |
191
|
1 |
|
$connectionId = spl_object_hash($connection); |
192
|
1 |
|
foreach ($this->connections as $index => $connectionItem) { |
193
|
1 |
|
if ($connectionId === spl_object_hash($connectionItem)) { |
194
|
1 |
|
unset($this->connections[$index]); |
195
|
1 |
|
return; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
$this->logger->critical('No connection found in the server connection list, impossible to delete the given connection id. Something wrong happened'); |
200
|
|
|
throw new RuntimeException('No connection found in the server connection list, impossible to delete the given connection id. Something wrong happened'); |
201
|
|
|
} |
202
|
|
|
|
203
|
|
|
/** |
204
|
|
|
* @param string $uri |
205
|
|
|
* @param Connection $connection |
206
|
|
|
* @return MessageHandlerInterface|null |
207
|
|
|
*/ |
208
|
5 |
|
private function getMessageHandler(string $uri, Connection $connection) |
|
|
|
|
209
|
|
|
{ |
210
|
5 |
|
$handler = null; |
211
|
|
|
|
212
|
5 |
|
if (!empty($this->messageHandlers[$uri])) { |
213
|
2 |
|
$handler = $this->messageHandlers[$uri]; |
214
|
|
|
} |
215
|
|
|
|
216
|
5 |
|
if (null === $handler && !empty($this->messageHandlers['*'])) { |
217
|
2 |
|
$handler = $this->messageHandlers['*']; |
218
|
|
|
} |
219
|
|
|
|
220
|
5 |
|
if (null !== $handler) { |
221
|
4 |
|
if (\is_string($handler)) { |
222
|
|
|
$handler = new $handler; |
223
|
|
|
} |
224
|
|
|
|
225
|
4 |
|
return $handler; |
226
|
|
|
} |
227
|
|
|
|
228
|
1 |
|
$this->logger->warning('Connection on ' . $uri . ' but no handler found.'); |
229
|
1 |
|
return null; |
230
|
|
|
} |
231
|
|
|
|
232
|
|
|
/** |
233
|
|
|
* Build the message processor with configuration |
234
|
|
|
*/ |
235
|
8 |
|
private function buildMessageProcessor() |
236
|
|
|
{ |
237
|
8 |
|
$this->messageProcessor = new MessageProcessor( |
238
|
8 |
|
false, |
239
|
8 |
|
new FrameFactory($this->config['frame']), |
240
|
8 |
|
new MessageFactory($this->config['message']) |
241
|
|
|
); |
242
|
8 |
|
$this->messageProcessor->addHandler(new PingFrameHandler()); |
243
|
8 |
|
$this->messageProcessor->addHandler(new CloseFrameHandler()); |
244
|
8 |
|
$this->messageProcessor->addHandler(new WrongOpcodeFrameHandler()); |
245
|
8 |
|
$this->messageProcessor->addHandler(new RsvCheckFrameHandler()); |
246
|
|
|
|
247
|
8 |
|
foreach ($this->config['messageHandlers'] as $handler) { |
248
|
1 |
|
if (!$handler instanceof MessageHandlerInterface) { |
249
|
1 |
|
throw new RuntimeException(sprintf('%s is not an instance of MessageHandlerInterface but must be !', get_class($handler))); |
250
|
|
|
} |
251
|
|
|
} |
252
|
7 |
|
} |
253
|
|
|
|
254
|
|
|
/** |
255
|
|
|
* Sets the configuration |
256
|
|
|
* |
257
|
|
|
* @param array $config |
258
|
|
|
* @throws ConfigException |
259
|
|
|
*/ |
260
|
9 |
|
private function setConfig(array $config) |
261
|
|
|
{ |
262
|
9 |
|
$this->config = \array_merge([ |
263
|
9 |
|
'frame' => [], |
264
|
|
|
'message' => [], |
265
|
|
|
'messageHandlers' => [], |
266
|
|
|
'prod' => true, |
267
|
|
|
'ssl' => false, |
268
|
|
|
'certFile' => '', |
269
|
|
|
'passphrase' => '', |
270
|
|
|
'sslContextOptions' => [], |
271
|
9 |
|
], $config); |
272
|
|
|
|
273
|
9 |
|
if ($this->config['ssl'] && !is_file($this->config['certFile'])) { |
274
|
1 |
|
throw new ConfigException('With ssl configuration, you need to specify a certificate file.'); |
275
|
|
|
} |
276
|
8 |
|
} |
277
|
|
|
|
278
|
|
|
/** |
279
|
|
|
* @return SimpleLogger|LoggerInterface |
280
|
|
|
*/ |
281
|
5 |
|
public function getLogger() |
282
|
|
|
{ |
283
|
5 |
|
if (null === $this->logger) { |
284
|
3 |
|
return $this->logger = new SimpleLogger(!$this->config['prod']); |
285
|
|
|
} |
286
|
|
|
|
287
|
5 |
|
return $this->logger; |
288
|
|
|
} |
289
|
|
|
|
290
|
|
|
/** |
291
|
|
|
* Allows you to set a custom logger |
292
|
|
|
* |
293
|
|
|
* @param LoggerInterface $logger |
294
|
|
|
* @return WebSocketServer |
295
|
|
|
*/ |
296
|
2 |
|
public function setLogger(LoggerInterface $logger) |
297
|
|
|
{ |
298
|
2 |
|
$this->logger = $logger; |
299
|
|
|
|
300
|
2 |
|
return $this; |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
/** |
304
|
|
|
* Allows to specify a loop that will be used instead of the reactphp generated loop. |
305
|
|
|
* |
306
|
|
|
* @param LoopInterface $loop |
307
|
|
|
* @return WebSocketServer |
308
|
|
|
*/ |
309
|
5 |
|
public function setLoop(LoopInterface $loop) |
310
|
|
|
{ |
311
|
5 |
|
$this->loop = $loop; |
312
|
|
|
|
313
|
5 |
|
return $this; |
314
|
|
|
} |
315
|
|
|
|
316
|
|
|
/** |
317
|
|
|
* @param ServerInterface $server |
318
|
|
|
* @return WebSocketServer |
319
|
|
|
*/ |
320
|
5 |
|
public function setSocketServer(ServerInterface $server) |
321
|
|
|
{ |
322
|
5 |
|
$this->server = $server; |
323
|
|
|
|
324
|
5 |
|
return $this; |
325
|
|
|
} |
326
|
|
|
} |
327
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.