|
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\Core\AbstractConnection; |
|
15
|
|
|
use Nekland\Woketo\Exception\NoHandlerException; |
|
16
|
|
|
use Nekland\Woketo\Exception\RuntimeException; |
|
17
|
|
|
use Nekland\Woketo\Exception\WebsocketException; |
|
18
|
|
|
use Nekland\Woketo\Http\Request; |
|
19
|
|
|
use Nekland\Woketo\Http\Response; |
|
20
|
|
|
use Nekland\Woketo\Rfc6455\Frame; |
|
21
|
|
|
use Nekland\Woketo\Rfc6455\MessageProcessor; |
|
22
|
|
|
use Nekland\Woketo\Rfc6455\Handshake\ServerHandshake; |
|
23
|
|
|
use React\EventLoop\LoopInterface; |
|
24
|
|
|
use React\Socket\ConnectionInterface; |
|
25
|
|
|
|
|
26
|
|
|
class Connection extends AbstractConnection |
|
27
|
|
|
{ |
|
28
|
6 |
|
public function __construct( |
|
29
|
|
|
ConnectionInterface $socketStream, |
|
30
|
|
|
\Closure $messageHandler, |
|
31
|
|
|
LoopInterface $loop, |
|
32
|
|
|
MessageProcessor $messageProcessor, |
|
33
|
|
|
ServerHandshake $handshake = null |
|
34
|
|
|
) { |
|
35
|
6 |
|
parent::__construct($messageProcessor, $loop, $handshake ?: new ServerHandshake); |
|
36
|
6 |
|
$this->stream = $socketStream; |
|
37
|
6 |
|
$this->initListeners(); |
|
38
|
6 |
|
$this->handler = $messageHandler; |
|
39
|
6 |
|
} |
|
40
|
|
|
|
|
41
|
|
|
private function initListeners() |
|
42
|
|
|
{ |
|
43
|
6 |
|
$this->stream->on('data', function ($data) { |
|
44
|
6 |
|
$this->processData($data); |
|
45
|
6 |
|
}); |
|
46
|
6 |
|
$this->stream->on('error', function ($data) { |
|
47
|
|
|
$this->error($data); |
|
48
|
6 |
|
}); |
|
49
|
6 |
|
} |
|
50
|
|
|
|
|
51
|
6 |
|
private function processData($data) |
|
52
|
|
|
{ |
|
53
|
|
|
try { |
|
54
|
6 |
|
if (!$this->handshakeDone) { |
|
55
|
6 |
|
$this->processHandshake($data); |
|
56
|
|
|
} else { |
|
57
|
2 |
|
$this->processMessage($data); |
|
58
|
|
|
} |
|
59
|
|
|
|
|
60
|
5 |
|
return; |
|
61
|
1 |
|
} catch (WebsocketException $e) { |
|
62
|
|
|
$this->messageProcessor->close($this->stream); |
|
63
|
|
|
$this->logger->notice('Connection to ' . $this->getIp() . ' closed with error : ' . $e->getMessage()); |
|
64
|
|
|
$this->getHandler()->onError($e, $this); |
|
65
|
1 |
|
} catch (NoHandlerException $e) { |
|
66
|
1 |
|
$this->getLogger()->info(sprintf('No handler found for uri %s. Connection closed.', $this->uri)); |
|
67
|
1 |
|
$this->close(); |
|
68
|
|
|
} |
|
69
|
1 |
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* This method build a message and buffer data in case of incomplete data. |
|
73
|
|
|
* |
|
74
|
|
|
* @param string $data |
|
75
|
|
|
*/ |
|
76
|
2 |
|
protected function processMessage(string $data) |
|
77
|
|
|
{ |
|
78
|
|
|
// It may be a timeout going (we were waiting for data), let's clear it. |
|
79
|
2 |
|
if ($this->timeout !== null) { |
|
80
|
|
|
$this->loop->cancelTimer($this->timeout); |
|
81
|
|
|
$this->timeout = null; |
|
82
|
|
|
} |
|
83
|
|
|
|
|
84
|
2 |
|
foreach ($this->messageProcessor->onData($data, $this->stream, $this->currentMessage) as $message) { |
|
85
|
2 |
|
$this->currentMessage = $message; |
|
86
|
2 |
|
if ($this->currentMessage->isComplete()) { |
|
87
|
|
|
// Sending the message through the woketo API. |
|
88
|
2 |
|
switch($this->currentMessage->getOpcode()) { |
|
89
|
2 |
|
case Frame::OP_TEXT: |
|
90
|
1 |
|
$this->getHandler()->onMessage($this->currentMessage->getContent(), $this); |
|
91
|
1 |
|
break; |
|
92
|
1 |
|
case Frame::OP_BINARY: |
|
93
|
1 |
|
$this->getHandler()->onBinary($this->currentMessage->getContent(), $this); |
|
94
|
1 |
|
break; |
|
95
|
|
|
} |
|
96
|
2 |
|
$this->currentMessage = null; |
|
97
|
|
|
|
|
98
|
|
|
} else { |
|
99
|
|
|
// We wait for more data so we start a timeout. |
|
100
|
2 |
|
$this->timeout = $this->loop->addTimer(Connection::DEFAULT_TIMEOUT, function () { |
|
|
|
|
|
|
101
|
|
|
$this->logger->notice('Connection to ' . $this->getIp() . ' timed out.'); |
|
102
|
|
|
$this->messageProcessor->timeout($this->stream); |
|
103
|
2 |
|
}); |
|
104
|
|
|
} |
|
105
|
|
|
} |
|
106
|
2 |
|
} |
|
107
|
|
|
|
|
108
|
|
|
public function disconnect() |
|
109
|
|
|
{ |
|
110
|
|
|
$this->getHandler()->onDisconnect($this); |
|
111
|
|
|
} |
|
112
|
|
|
|
|
113
|
|
|
public function write($frame, int $opCode = Frame::OP_TEXT) |
|
114
|
|
|
{ |
|
115
|
|
|
try { |
|
116
|
|
|
$this->messageProcessor->write($frame, $this->stream, $opCode); |
|
117
|
|
|
} catch (WebsocketException $e) { |
|
118
|
|
|
throw new RuntimeException($e); |
|
119
|
|
|
} |
|
120
|
|
|
} |
|
121
|
|
|
|
|
122
|
|
|
/** |
|
123
|
|
|
* @param mixed $data |
|
124
|
|
|
*/ |
|
125
|
|
|
protected function error($data) |
|
126
|
|
|
{ |
|
127
|
|
|
$message = "A connectivity error occurred: " . $data; |
|
128
|
|
|
$this->logger->error($message); |
|
129
|
|
|
$this->getHandler()->onError(new WebsocketException($message), $this); |
|
130
|
|
|
} |
|
131
|
|
|
|
|
132
|
|
|
/** |
|
133
|
|
|
* If it's a new client, we need to make some special actions named the handshake. |
|
134
|
|
|
* |
|
135
|
|
|
* @param string $data |
|
136
|
|
|
*/ |
|
137
|
6 |
|
protected function processHandshake(string $data) |
|
138
|
|
|
{ |
|
139
|
6 |
|
if ($this->handshakeDone) { |
|
140
|
|
|
return; |
|
141
|
|
|
} |
|
142
|
|
|
|
|
143
|
6 |
|
$request = Request::create($data); |
|
144
|
6 |
|
$this->handshake->verify($request); |
|
145
|
6 |
|
$this->uri = $request->getUri(); |
|
146
|
6 |
|
$response = Response::createSwitchProtocolResponse(); |
|
147
|
6 |
|
$this->handshake->sign($response, $this->handshake->extractKeyFromRequest($request)); |
|
|
|
|
|
|
148
|
6 |
|
$response->send($this->stream); |
|
149
|
|
|
|
|
150
|
6 |
|
$this->handshakeDone = true; |
|
151
|
6 |
|
$this->getHandler()->onConnection($this); |
|
152
|
5 |
|
} |
|
153
|
|
|
|
|
154
|
|
|
/** |
|
155
|
|
|
* @return string |
|
156
|
|
|
*/ |
|
157
|
4 |
|
public function getIp() |
|
158
|
|
|
{ |
|
159
|
4 |
|
return $this->stream->getRemoteAddress(); |
|
160
|
|
|
} |
|
161
|
|
|
} |
|
162
|
|
|
|
This check looks for accesses to local static members using the fully qualified name instead of
self::.While this is perfectly valid, the fully qualified name of
Certificate::TRIPLEDES_CBCcould just as well be replaced byself::TRIPLEDES_CBC. Referencing local members withself::assured the access will still work when the class is renamed, makes it perfectly clear that the member is in fact local and will usually be shorter.