|
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 array |
|
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
|
8 |
|
public function __construct($port, $host = '127.0.0.1', $config = []) |
|
90
|
|
|
{ |
|
91
|
8 |
|
$this->setConfig($config); |
|
92
|
7 |
|
$this->host = $host; |
|
93
|
7 |
|
$this->port = $port; |
|
94
|
7 |
|
$this->handshake = new ServerHandshake(); |
|
95
|
7 |
|
$this->connections = []; |
|
96
|
7 |
|
$this->buildMessageProcessor(); |
|
97
|
|
|
|
|
98
|
|
|
// Some optimization |
|
99
|
6 |
|
\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
|
6 |
|
\set_time_limit(0); // It's by default on most server for cli apps but better be sure of that fact |
|
101
|
6 |
|
} |
|
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
|
4 |
|
public function setMessageHandler($messageHandler, $uri = '*') |
|
108
|
|
|
{ |
|
109
|
4 |
|
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
|
4 |
|
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
|
4 |
|
$this->messageHandlers[$uri] = $messageHandler; |
|
123
|
4 |
|
} |
|
124
|
|
|
|
|
125
|
|
|
/** |
|
126
|
|
|
* Launch the WebSocket server and an infinite loop that act on event. |
|
127
|
|
|
* |
|
128
|
|
|
* @throws \Exception |
|
129
|
|
|
*/ |
|
130
|
4 |
|
public function start() |
|
131
|
|
|
{ |
|
132
|
4 |
|
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
|
4 |
|
$this->loop = $this->loop ?? \React\EventLoop\Factory::create(); |
|
137
|
4 |
|
$this->server = $this->server ?? new \React\Socket\TcpServer($this->host . ':' . $this->port, $this->loop); |
|
138
|
|
|
|
|
139
|
4 |
|
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
|
4 |
|
$this->server->on('connection', function ($socketStream) { |
|
148
|
4 |
|
$this->onNewConnection($socketStream); |
|
149
|
4 |
|
}); |
|
150
|
|
|
|
|
151
|
4 |
|
$this->getLogger()->info('Listening on ' . $this->host . ':' . $this->port); |
|
152
|
|
|
|
|
153
|
4 |
|
$this->loop->run(); |
|
154
|
4 |
|
} |
|
155
|
|
|
|
|
156
|
|
|
/** |
|
157
|
|
|
* @param ConnectionInterface $socketStream |
|
158
|
|
|
*/ |
|
159
|
|
|
private function onNewConnection(ConnectionInterface $socketStream) |
|
160
|
|
|
{ |
|
161
|
4 |
|
$connection = new Connection($socketStream, function ($uri, Connection $connection) { |
|
162
|
4 |
|
return $this->getMessageHandler($uri, $connection); |
|
163
|
4 |
|
}, $this->loop, $this->messageProcessor); |
|
164
|
|
|
|
|
165
|
4 |
|
$connection->setLogger($this->getLogger()); |
|
166
|
4 |
|
$this->connections[] = $connection; |
|
167
|
4 |
|
} |
|
168
|
|
|
|
|
169
|
|
|
/** |
|
170
|
|
|
* @param string $uri |
|
171
|
|
|
* @param Connection $connection |
|
172
|
|
|
* @return MessageHandlerInterface|null |
|
173
|
|
|
*/ |
|
174
|
4 |
|
private function getMessageHandler(string $uri, Connection $connection) |
|
|
|
|
|
|
175
|
|
|
{ |
|
176
|
4 |
|
$handler = null; |
|
177
|
|
|
|
|
178
|
4 |
|
if (!empty($this->messageHandlers[$uri])) { |
|
179
|
2 |
|
$handler = $this->messageHandlers[$uri]; |
|
180
|
|
|
} |
|
181
|
|
|
|
|
182
|
4 |
|
if (null === $handler && !empty($this->messageHandlers['*'])) { |
|
183
|
1 |
|
$handler = $this->messageHandlers['*']; |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
4 |
|
if (null !== $handler) { |
|
187
|
3 |
|
if (\is_string($handler)) { |
|
188
|
|
|
$handler = new $handler; |
|
189
|
|
|
} |
|
190
|
|
|
|
|
191
|
3 |
|
return $handler; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
1 |
|
$this->logger->warning('Connection on ' . $uri . ' but no handler found.'); |
|
195
|
1 |
|
return null; |
|
196
|
|
|
} |
|
197
|
|
|
|
|
198
|
|
|
/** |
|
199
|
|
|
* Build the message processor with configuration |
|
200
|
|
|
*/ |
|
201
|
7 |
|
private function buildMessageProcessor() |
|
202
|
|
|
{ |
|
203
|
7 |
|
$this->messageProcessor = new MessageProcessor( |
|
204
|
7 |
|
false, |
|
205
|
7 |
|
new FrameFactory($this->config['frame']), |
|
206
|
7 |
|
new MessageFactory($this->config['message']) |
|
207
|
|
|
); |
|
208
|
7 |
|
$this->messageProcessor->addHandler(new PingFrameHandler()); |
|
209
|
7 |
|
$this->messageProcessor->addHandler(new CloseFrameHandler()); |
|
210
|
7 |
|
$this->messageProcessor->addHandler(new WrongOpcodeFrameHandler()); |
|
211
|
7 |
|
$this->messageProcessor->addHandler(new RsvCheckFrameHandler()); |
|
212
|
|
|
|
|
213
|
7 |
|
foreach ($this->config['messageHandlers'] as $handler) { |
|
214
|
1 |
|
if (!$handler instanceof MessageHandlerInterface) { |
|
215
|
1 |
|
throw new RuntimeException(sprintf('%s is not an instance of MessageHandlerInterface but must be !', get_class($handler))); |
|
216
|
|
|
} |
|
217
|
|
|
} |
|
218
|
6 |
|
} |
|
219
|
|
|
|
|
220
|
|
|
/** |
|
221
|
|
|
* Sets the configuration |
|
222
|
|
|
* |
|
223
|
|
|
* @param array $config |
|
224
|
|
|
* @throws ConfigException |
|
225
|
|
|
*/ |
|
226
|
8 |
|
private function setConfig(array $config) |
|
227
|
|
|
{ |
|
228
|
8 |
|
$this->config = \array_merge([ |
|
229
|
8 |
|
'frame' => [], |
|
230
|
|
|
'message' => [], |
|
231
|
|
|
'messageHandlers' => [], |
|
232
|
|
|
'prod' => true, |
|
233
|
|
|
'ssl' => false, |
|
234
|
|
|
'certFile' => '', |
|
235
|
|
|
'passphrase' => '', |
|
236
|
|
|
'sslContextOptions' => [], |
|
237
|
8 |
|
], $config); |
|
238
|
|
|
|
|
239
|
8 |
|
if ($this->config['ssl'] && !is_file($this->config['certFile'])) { |
|
240
|
1 |
|
throw new ConfigException('With ssl configuration, you need to specify a certificate file.'); |
|
241
|
|
|
} |
|
242
|
7 |
|
} |
|
243
|
|
|
|
|
244
|
|
|
/** |
|
245
|
|
|
* @return SimpleLogger|LoggerInterface |
|
246
|
|
|
*/ |
|
247
|
4 |
|
public function getLogger() |
|
248
|
|
|
{ |
|
249
|
4 |
|
if (null === $this->logger) { |
|
250
|
3 |
|
return $this->logger = new SimpleLogger(!$this->config['prod']); |
|
251
|
|
|
} |
|
252
|
|
|
|
|
253
|
4 |
|
return $this->logger; |
|
254
|
|
|
} |
|
255
|
|
|
|
|
256
|
|
|
/** |
|
257
|
|
|
* Allows you to set a custom logger |
|
258
|
|
|
* |
|
259
|
|
|
* @param LoggerInterface $logger |
|
260
|
|
|
* @return WebSocketServer |
|
261
|
|
|
*/ |
|
262
|
1 |
|
public function setLogger(LoggerInterface $logger) |
|
263
|
|
|
{ |
|
264
|
1 |
|
$this->logger = $logger; |
|
265
|
|
|
|
|
266
|
1 |
|
return $this; |
|
267
|
|
|
} |
|
268
|
|
|
|
|
269
|
|
|
/** |
|
270
|
|
|
* Allows to specify a loop that will be used instead of the reactphp generated loop. |
|
271
|
|
|
* |
|
272
|
|
|
* @param LoopInterface $loop |
|
273
|
|
|
* @return WebSocketServer |
|
274
|
|
|
*/ |
|
275
|
4 |
|
public function setLoop(LoopInterface $loop) |
|
276
|
|
|
{ |
|
277
|
4 |
|
$this->loop = $loop; |
|
278
|
|
|
|
|
279
|
4 |
|
return $this; |
|
280
|
|
|
} |
|
281
|
|
|
|
|
282
|
|
|
/** |
|
283
|
|
|
* @param ServerInterface $server |
|
284
|
|
|
* @return WebSocketServer |
|
285
|
|
|
*/ |
|
286
|
4 |
|
public function setSocketServer(ServerInterface $server) |
|
287
|
|
|
{ |
|
288
|
4 |
|
$this->server = $server; |
|
289
|
|
|
|
|
290
|
4 |
|
return $this; |
|
291
|
|
|
} |
|
292
|
|
|
} |
|
293
|
|
|
|
This check looks from parameters that have been defined for a function or method, but which are not used in the method body.