1 | <?php |
||
29 | class Connection extends AbstractConnection |
||
30 | { |
||
31 | /** |
||
32 | * @var string|null |
||
33 | */ |
||
34 | private $handshakeKey; |
||
35 | |||
36 | /** |
||
37 | * @var string |
||
38 | */ |
||
39 | private $buffer; |
||
40 | |||
41 | /** |
||
42 | * @var Url |
||
43 | */ |
||
44 | private $url; |
||
45 | |||
46 | public function __construct(Url $url, PromiseInterface $clientPromise, MessageProcessor $messageProcessor, MessageHandlerInterface $handler, LoopInterface $loop) |
||
47 | { |
||
48 | parent::__construct($messageProcessor, $loop, new ClientHandshake()); |
||
49 | |||
50 | $this->url = $url; |
||
51 | $this->uri = $this->url->getUri(); |
||
52 | $this->buffer = ''; |
||
53 | $this->handler = $handler; |
||
54 | |||
55 | $clientPromise->then(function (Stream $stream) { |
||
56 | $this->stream = $stream; |
||
|
|||
57 | $this->onConnection($stream); |
||
58 | }, function (\Exception $error){ |
||
59 | $this->onError($error); |
||
60 | }); |
||
61 | } |
||
62 | |||
63 | private function onConnection(Stream $stream) |
||
72 | |||
73 | /** |
||
74 | * {@inheritdoc} |
||
75 | */ |
||
76 | protected function processHandshake(string $data) |
||
109 | |||
110 | /** |
||
111 | * {@inheritdoc} |
||
112 | */ |
||
113 | protected function processMessage(string $data) |
||
114 | { |
||
115 | // It may be a timeout going (we were waiting for data), let's clear it. |
||
116 | if ($this->timeout !== null) { |
||
117 | $this->timeout->cancel(); |
||
118 | $this->timeout = null; |
||
119 | } |
||
120 | |||
121 | foreach ($this->messageProcessor->onData($data, $this->stream, $this->currentMessage) as $message) { |
||
122 | $this->currentMessage = $message; |
||
123 | |||
124 | if ($this->currentMessage->isComplete()) { |
||
125 | // Sending the message through the woketo API. |
||
126 | switch($this->currentMessage->getOpcode()) { |
||
127 | case Frame::OP_TEXT: |
||
128 | $this->getHandler()->onMessage($this->currentMessage->getContent(), $this); |
||
129 | break; |
||
130 | case Frame::OP_BINARY: |
||
131 | $this->getHandler()->onBinary($this->currentMessage->getContent(), $this); |
||
132 | break; |
||
133 | } |
||
134 | $this->currentMessage = null; |
||
135 | |||
136 | } else { |
||
137 | // We wait for more data so we start a timeout. |
||
138 | $this->timeout = $this->loop->addTimer(Connection::DEFAULT_TIMEOUT, function () { |
||
139 | $this->getLogger()->notice('Connection to ' . $this->getIp() . ' timed out.'); |
||
140 | $this->messageProcessor->timeout($this->stream); |
||
141 | }); |
||
142 | } |
||
143 | } |
||
144 | } |
||
145 | |||
146 | /** |
||
147 | * @param string|Frame $frame |
||
148 | * @param int $opCode An int representing binary or text data (const of Frame class) |
||
149 | * @throws \Nekland\Woketo\Exception\RuntimeException |
||
150 | */ |
||
151 | public function write($frame, int $opCode = Frame::OP_TEXT) |
||
152 | { |
||
153 | try { |
||
154 | $this->messageProcessor->write($frame, $this->stream, $opCode); |
||
155 | } catch (WebsocketException $e) { |
||
156 | throw new RuntimeException($e); |
||
157 | } |
||
158 | } |
||
159 | |||
160 | /** |
||
161 | * @param \Exception|string $error |
||
162 | */ |
||
163 | private function onError($error) |
||
164 | { |
||
165 | $error = $error instanceof \Exception ? $error->getMessage() : $error; |
||
166 | |||
167 | $this->getLogger()->error(sprintf('An error occured: %s', $error)); |
||
168 | } |
||
169 | |||
170 | /** |
||
171 | * {@inheritdoc} |
||
172 | */ |
||
173 | public function getIp() |
||
177 | } |
||
178 |
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.