DatagramClientIo::getRemoteAddress()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 0
crap 1
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