DatagramClientIo   A
last analyzed

Complexity

Total Complexity 10

Size/Duplication

Total Lines 73
Duplicated Lines 0 %

Coupling/Cohesion

Components 1
Dependencies 3

Test Coverage

Coverage 100%

Importance

Changes 0
Metric Value
wmc 10
lcom 1
cbo 3
dl 0
loc 73
ccs 28
cts 28
cp 1
rs 10
c 0
b 0
f 0

6 Methods

Rating   Name   Duplication   Size   Complexity  
A readRawDataIntoPicker() 0 17 3
A writeRawData() 0 9 2
A getRemoteAddress() 0 4 1
A isConnected() 0 4 1
A canReachFrame() 0 5 1
A __construct() 0 8 2
1
<?php
2
/**
3
 * Async sockets
4
 *
5
 * @copyright Copyright (c) 2015-2017, Efimov Evgenij <[email protected]>
6
 *
7
 * This source file is subject to the MIT license that is bundled
8
 * with this source code in the file LICENSE.
9
 */
10
namespace AsyncSockets\Socket\Io;
11
12
use AsyncSockets\Exception\SendDataException;
13
use AsyncSockets\Frame\FramePickerInterface;
14
use AsyncSockets\Socket\SocketInterface;
15
16
/**
17
 * Class DatagramClientIo
18
 */
19
class DatagramClientIo extends AbstractClientIo
20
{
21
    /**
22
     * Destination address
23
     *
24
     * @var string
25
     */
26
    protected $remoteAddress;
27
28
    /**
29
     * Constructor
30
     *
31
     * @param SocketInterface $socket Socket object
32
     * @param string|null     $remoteAddress Destination address in form scheme://host:port or null for local files io
33
     */
34 56
    public function __construct(SocketInterface $socket, $remoteAddress)
35
    {
36 56
        parent::__construct($socket, 0);
37 56
        if ($remoteAddress) {
38 52
            $components = parse_url($remoteAddress);
39 52
            $this->remoteAddress = $components['host'] . ':' . $components['port'];
40 52
        }
41 56
    }
42
43
    /** {@inheritdoc} */
44 16
    protected function readRawDataIntoPicker(FramePickerInterface $picker, $isOutOfBand)
45
    {
46 16
        $size     = self::SOCKET_BUFFER_SIZE;
47 16
        $resource = $this->socket->getStreamResource();
48
        do {
49 16
            $data = stream_socket_recvfrom($resource, $size, STREAM_PEEK);
50 16
            if (strlen($data) < $size) {
51 16
                break;
52
            }
53
54 4
            $size += $size;
55 4
        } while (true);
56
57 16
        $data = stream_socket_recvfrom($resource, $size, 0, $actualRemoteAddress);
58
59 16
        return $picker->pickUpData($data, $actualRemoteAddress);
60
    }
61
62
    /** {@inheritdoc} */
63 10
    protected function writeRawData($data, $isOutOfBand)
64
    {
65 10
        $result = stream_socket_sendto($this->socket->getStreamResource(), $data, 0, $this->remoteAddress);
66 10
        if ($result < 0) {
67 4
            throw SendDataException::failedToSendData($this->socket);
68
        }
69
70 6
        return $result;
71
    }
72
73
    /** {@inheritdoc} */
74 2
    protected function getRemoteAddress()
75
    {
76 2
        return $this->remoteAddress;
77
    }
78
79
    /** {@inheritdoc} */
80 36
    public function isConnected()
81
    {
82 36
        return true;
83
    }
84
85
    /** {@inheritdoc} */
86 4
    protected function canReachFrame()
87
    {
88
        // in datagram we have no second chance to receive the frame
89 4
        return false;
90
    }
91
}
92