1 | <?php |
||
18 | class DatagramClientIo extends AbstractClientIo |
||
19 | { |
||
20 | /** |
||
21 | * Destination address |
||
22 | * |
||
23 | * @var string |
||
24 | */ |
||
25 | protected $remoteAddress; |
||
26 | |||
27 | /** |
||
28 | * Constructor |
||
29 | * |
||
30 | * @param SocketInterface $socket Socket object |
||
31 | * @param string|null $remoteAddress Destination address in form scheme://host:port or null for local files io |
||
32 | */ |
||
33 | 50 | public function __construct(SocketInterface $socket, $remoteAddress) |
|
41 | |||
42 | /** {@inheritdoc} */ |
||
43 | 14 | protected function readRawDataIntoPicker(FramePickerInterface $picker, $isOutOfBand) |
|
44 | { |
||
45 | 14 | $size = self::SOCKET_BUFFER_SIZE; |
|
46 | 14 | $resource = $this->socket->getStreamResource(); |
|
47 | do { |
||
48 | 14 | $data = stream_socket_recvfrom($resource, $size, STREAM_PEEK); |
|
49 | 14 | if (strlen($data) < $size) { |
|
50 | 14 | break; |
|
51 | } |
||
52 | |||
53 | 4 | $size += $size; |
|
54 | 4 | } while (true); |
|
55 | |||
56 | 14 | $data = stream_socket_recvfrom($resource, $size, 0, $actualRemoteAddress); |
|
57 | |||
58 | 14 | return $picker->pickUpData($data, $actualRemoteAddress); |
|
59 | } |
||
60 | |||
61 | /** {@inheritdoc} */ |
||
62 | 8 | protected function writeRawData($data, $isOutOfBand) |
|
63 | { |
||
64 | 8 | $result = stream_socket_sendto($this->socket->getStreamResource(), $data, 0, $this->remoteAddress); |
|
65 | 8 | $this->throwNetworkSocketExceptionIf($result < 0, 'Failed to send data.'); |
|
66 | 4 | return $result; |
|
67 | } |
||
68 | |||
69 | /** {@inheritdoc} */ |
||
70 | protected function getRemoteAddress() |
||
74 | |||
75 | /** {@inheritdoc} */ |
||
76 | 31 | protected function isConnected() |
|
80 | |||
81 | /** {@inheritdoc} */ |
||
82 | 4 | protected function canReachFrame() |
|
87 | } |
||
88 |