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
|
|
|
public function __construct( |
29
|
|
|
ConnectionInterface $socketStream, |
30
|
|
|
\Closure $messageHandler, |
31
|
|
|
LoopInterface $loop, |
32
|
|
|
MessageProcessor $messageProcessor, |
33
|
|
|
ServerHandshake $handshake = null |
34
|
|
|
) { |
35
|
|
|
parent::__construct($messageProcessor, $loop, $handshake ?: new ServerHandshake); |
36
|
|
|
$this->stream = $socketStream; |
|
|
|
|
37
|
|
|
$this->initListeners(); |
38
|
|
|
$this->handler = $messageHandler; |
39
|
|
|
} |
40
|
|
|
|
41
|
|
|
private function initListeners() |
42
|
|
|
{ |
43
|
|
|
$this->stream->on('data', function ($data) { |
44
|
|
|
$this->processData($data); |
45
|
|
|
}); |
46
|
|
|
$this->stream->on('error', function ($data) { |
47
|
|
|
$this->error($data); |
48
|
|
|
}); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
private function processData($data) |
52
|
|
|
{ |
53
|
|
|
try { |
54
|
|
|
if (!$this->handshakeDone) { |
55
|
|
|
$this->processHandshake($data); |
56
|
|
|
} else { |
57
|
|
|
$this->processMessage($data); |
58
|
|
|
} |
59
|
|
|
|
60
|
|
|
return; |
61
|
|
|
} 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
|
|
|
} catch (NoHandlerException $e) { |
66
|
|
|
$this->getLogger()->info(sprintf('No handler found for uri %s. Connection closed.', $this->uri)); |
67
|
|
|
$this->close(); |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
/** |
72
|
|
|
* This method build a message and buffer data in case of incomplete data. |
73
|
|
|
* |
74
|
|
|
* @param string $data |
75
|
|
|
*/ |
76
|
|
|
protected function processMessage(string $data) |
77
|
|
|
{ |
78
|
|
|
// It may be a timeout going (we were waiting for data), let's clear it. |
79
|
|
|
if ($this->timeout !== null) { |
80
|
|
|
$this->timeout->cancel(); |
81
|
|
|
$this->timeout = null; |
82
|
|
|
} |
83
|
|
|
|
84
|
|
|
foreach ($this->messageProcessor->onData($data, $this->stream, $this->currentMessage) as $message) { |
85
|
|
|
$this->currentMessage = $message; |
86
|
|
|
if ($this->currentMessage->isComplete()) { |
87
|
|
|
// Sending the message through the woketo API. |
88
|
|
|
switch($this->currentMessage->getOpcode()) { |
89
|
|
|
case Frame::OP_TEXT: |
90
|
|
|
$this->getHandler()->onMessage($this->currentMessage->getContent(), $this); |
91
|
|
|
break; |
92
|
|
|
case Frame::OP_BINARY: |
93
|
|
|
$this->getHandler()->onBinary($this->currentMessage->getContent(), $this); |
94
|
|
|
break; |
95
|
|
|
} |
96
|
|
|
$this->currentMessage = null; |
97
|
|
|
|
98
|
|
|
} else { |
99
|
|
|
// We wait for more data so we start a timeout. |
100
|
|
|
$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
|
|
|
}); |
104
|
|
|
} |
105
|
|
|
} |
106
|
|
|
} |
107
|
|
|
|
108
|
|
|
public function write($frame, int $opCode = Frame::OP_TEXT) |
109
|
|
|
{ |
110
|
|
|
try { |
111
|
|
|
$this->messageProcessor->write($frame, $this->stream, $opCode); |
112
|
|
|
} catch (WebsocketException $e) { |
113
|
|
|
throw new RuntimeException($e); |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|
117
|
|
|
/** |
118
|
|
|
* @param mixed $data |
119
|
|
|
*/ |
120
|
|
|
protected function error($data) |
121
|
|
|
{ |
122
|
|
|
$message = "A connectivity error occurred: " . $data; |
123
|
|
|
$this->logger->error($message); |
124
|
|
|
$this->getHandler()->onError(new WebsocketException($message), $this); |
125
|
|
|
} |
126
|
|
|
|
127
|
|
|
/** |
128
|
|
|
* If it's a new client, we need to make some special actions named the handshake. |
129
|
|
|
* |
130
|
|
|
* @param string $data |
131
|
|
|
*/ |
132
|
|
|
protected function processHandshake(string $data) |
133
|
|
|
{ |
134
|
|
|
if ($this->handshakeDone) { |
135
|
|
|
return; |
136
|
|
|
} |
137
|
|
|
|
138
|
|
|
$request = Request::create($data); |
139
|
|
|
$this->handshake->verify($request); |
140
|
|
|
$this->uri = $request->getUri(); |
141
|
|
|
$response = Response::createSwitchProtocolResponse(); |
142
|
|
|
$this->handshake->sign($response, $this->handshake->extractKeyFromRequest($request)); |
|
|
|
|
143
|
|
|
$response->send($this->stream); |
144
|
|
|
|
145
|
|
|
$this->handshakeDone = true; |
146
|
|
|
$this->getHandler()->onConnection($this); |
147
|
|
|
} |
148
|
|
|
|
149
|
|
|
/** |
150
|
|
|
* @return string |
151
|
|
|
*/ |
152
|
|
|
public function getIp() |
153
|
|
|
{ |
154
|
|
|
return $this->stream->getRemoteAddress(); |
155
|
|
|
} |
156
|
|
|
} |
157
|
|
|
|
Our type inference engine has found a suspicous assignment of a value to a property. This check raises an issue when a value that can be of a given class or a super-class is assigned to a property that is type hinted more strictly.
Either this assignment is in error or an instanceof check should be added for that assignment.