Completed
Push — master ( 82da5d...012a80 )
by Charlotte
10s
created

Connection::getIp()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 0
cts 2
cp 0
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 2
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\RuntimeException;
15
use Nekland\Woketo\Exception\WebsocketException;
16
use Nekland\Woketo\Http\Request;
17
use Nekland\Woketo\Http\Response;
18
use Nekland\Woketo\Message\MessageHandlerInterface;
19
use Nekland\Woketo\Rfc6455\Frame;
20
use Nekland\Woketo\Rfc6455\Message;
21
use Nekland\Woketo\Rfc6455\MessageProcessor;
22
use Nekland\Woketo\Rfc6455\ServerHandshake;
23
use Psr\Log\LoggerAwareTrait;
24
use React\EventLoop\LoopInterface;
25
use React\EventLoop\Timer\TimerInterface;
26
use React\Socket\ConnectionInterface;
27
28
class Connection
29
{
30
    use LoggerAwareTrait;
31
32
    /**
33
     * 5 seconds
34
     */
35
    const DEFAULT_TIMEOUT = 5;
36
37
    /**
38
     * @var ConnectionInterface
39
     */
40
    private $socketStream;
41
42
    /**
43
     * @var MessageHandlerInterface
44
     */
45
    private $handler;
46
47
    /**
48
     * @var bool
49
     */
50
    private $handshakeDone;
51
52
    /**
53
     * @var ServerHandshake
54
     */
55
    private $handshake;
56
57
    /**
58
     * @var Message
59
     */
60
    private $currentMessage;
61
62
    /**
63
     * @var MessageProcessor
64
     */
65
    private $messageProcessor;
66
67
    /**
68
     * @var LoopInterface
69
     */
70
    private $loop;
71
72
    /**
73
     * @var TimerInterface
74
     */
75
    private $timeout;
76
    
77 2
    public function __construct(
78
        ConnectionInterface $socketStream,
79
        MessageHandlerInterface $messageHandler,
80
        LoopInterface $loop,
81
        MessageProcessor $messageProcessor,
82
        ServerHandshake $handshake = null
83
    ) {
84 2
        $this->socketStream = $socketStream;
85 2
        $this->initListeners();
86 2
        $this->handler = $messageHandler;
87 2
        $this->handshake = $handshake ?: new ServerHandshake;
88 2
        $this->loop = $loop;
89 2
        $this->messageProcessor = $messageProcessor;
90 2
    }
91
92 2
    private function initListeners()
93
    {
94
        $this->socketStream->on('data', function ($data) {
95 2
            $this->processData($data);
96 2
        });
97
        $this->socketStream->on('error', function ($data) {
98
            $this->error($data);
99 2
        });
100 2
    }
101
102 2
    private function processData($data)
103
    {
104
        try {
105 2
            if (!$this->handshakeDone) {
106 2
                $this->processHandcheck($data);
107
            } else {
108 2
                $this->processMessage($data);
109
            }
110
111 2
            return;
112
        } catch (WebsocketException $e) {
113
            $this->messageProcessor->close($this->socketStream);
114
            $this->logger->notice('Connection to ' . $this->getIp() . ' closed with error : ' . $e->getMessage());
115
            $this->handler->onError($e, $this);
116
        }
117
    }
118
119
    /**
120
     * This method build a message and buffer data in case of incomplete data.
121
     *
122
     * @param string $data
123
     */
124 2
    protected function processMessage($data)
125
    {
126
        // It may be a timeout going (we were waiting for data), let's clear it.
127 2
        if ($this->timeout !== null) {
128
            $this->timeout->cancel();
129
            $this->timeout = null;
130
        }
131
132 2
        foreach ($this->messageProcessor->onData($data, $this->socketStream, $this->currentMessage) as $message) {
133 2
            $this->currentMessage = $message;
134 2
            if ($this->currentMessage->isComplete()) {
135
                // Sending the message through the woketo API.
136 2
                switch($this->currentMessage->getOpcode()) {
137 2
                    case Frame::OP_TEXT:
138 1
                        $this->handler->onMessage($this->currentMessage->getContent(), $this);
139 1
                        break;
140 1
                    case Frame::OP_BINARY:
141 1
                        $this->handler->onBinary($this->currentMessage->getContent(), $this);
142 1
                        break;
143
                }
144 2
                $this->currentMessage = null;
145
146
            } else {
147
                // We wait for more data so we start a timeout.
148 2
                $this->timeout = $this->loop->addTimer(Connection::DEFAULT_TIMEOUT, function () {
149
                    $this->logger->notice('Connection to ' . $this->getIp() . ' timed out.');
150
                    $this->messageProcessor->timeout($this->socketStream);
151 2
                });
152
            }
153
        }
154 2
    }
155
156
    /**
157
     * @param string|Frame $frame
158
     * @param int          $opCode An int representing binary or text data (const of Frame class)
159
     * @throws \Nekland\Woketo\Exception\RuntimeException
160
     */
161
    public function write($frame, int $opCode = Frame::OP_TEXT)
162
    {
163
        try {
164
            $this->messageProcessor->write($frame, $this->socketStream, $opCode);
165
        } catch (WebsocketException $e) {
166
            throw new RuntimeException($e);
167
        }
168
    }
169
170
    /**
171
     * @param mixed $data
172
     */
173
    protected function error($data)
174
    {
175
        $message = "A connectivity error occurred: " . $data;
176
        $this->logger->error($message);
177
        $this->handler->onError(new WebsocketException($message), $this);
178
    }
179
180
    /**
181
     * If it's a new client, we need to make some special actions named the handshake.
182
     *
183
     * @param string $data
184
     */
185 2
    protected function processHandcheck($data)
186
    {
187 2
        if ($this->handshakeDone) {
188
            return;
189
        }
190
191 2
        $request = Request::create($data);
192 2
        $this->handshake->verify($request);
193 2
        $response = Response::createSwitchProtocolResponse();
194 2
        $this->handshake->sign($request, $response);
0 ignored issues
show
Documentation introduced by
$request is of type object<Nekland\Woketo\Http\Request>, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
195 2
        $response->send($this->socketStream);
196
        
197 2
        $this->handshakeDone = true;
198 2
        $this->handler->onConnection($this);
199 2
    }
200
201
    /**
202
     * @return string
203
     */
204
    public function getIp()
205
    {
206
        return $this->socketStream->getRemoteAddress();
207
    }
208
209
    /**
210
     * @return \Psr\Log\LoggerInterface
211
     */
212
    public function getLogger()
213
    {
214
        return $this->logger;
215
    }
216
}
217