1 | <?php |
||
20 | class StreamedClientIo extends AbstractClientIo |
||
21 | { |
||
22 | /** |
||
23 | * Read attempts count |
||
24 | */ |
||
25 | const READ_ATTEMPTS = 2; |
||
26 | |||
27 | /** |
||
28 | * Amount of read attempts |
||
29 | * |
||
30 | * @var int |
||
31 | */ |
||
32 | private $readAttempts = self::READ_ATTEMPTS; |
||
33 | |||
34 | /** |
||
35 | * Remote socket address |
||
36 | * |
||
37 | * @var string |
||
38 | */ |
||
39 | private $remoteAddress; |
||
40 | |||
41 | /** {@inheritdoc} */ |
||
42 | 26 | protected function readRawDataIntoPicker(FramePickerInterface $picker, $isOutOfBand) |
|
46 | |||
47 | /** {@inheritdoc} */ |
||
48 | 5 | protected function writeRawData($data, $isOutOfBand) |
|
49 | { |
||
50 | 5 | $resource = $this->socket->getStreamResource(); |
|
51 | 5 | $test = stream_socket_sendto($resource, ''); |
|
52 | 5 | if ($test !== 0) { |
|
53 | 1 | throw new SendDataException( |
|
54 | 1 | $this->socket, |
|
55 | 1 | trim('Failed to send data. ' . $this->getLastPhpErrorMessage()) |
|
56 | ); |
||
57 | } |
||
58 | |||
59 | 4 | $written = $isOutOfBand ? |
|
60 | 1 | $this->writeOobData($resource, $data) : |
|
61 | 4 | fwrite($resource, $data, strlen($data)); |
|
62 | |||
63 | 4 | if ($written === false) { |
|
64 | 1 | throw new SendDataException( |
|
65 | 1 | $this->socket, |
|
66 | 1 | trim('Failed to send data. ' . $this->getLastPhpErrorMessage()) |
|
67 | ); |
||
68 | } |
||
69 | |||
70 | 3 | if ($written === 0 && !empty($data) && !$this->isConnected()) { |
|
71 | 1 | throw new DisconnectException($this->socket, 'Remote connection has been lost.'); |
|
72 | } |
||
73 | |||
74 | 2 | return $written; |
|
75 | } |
||
76 | |||
77 | /** {@inheritdoc} */ |
||
78 | 37 | protected function isConnected() |
|
82 | |||
83 | /** {@inheritdoc} */ |
||
84 | 25 | protected function getRemoteAddress() |
|
95 | |||
96 | /** {@inheritdoc} */ |
||
97 | 2 | protected function canReachFrame() |
|
101 | |||
102 | /** |
||
103 | * Read OOB data from socket |
||
104 | * |
||
105 | * @param FramePickerInterface $picker |
||
106 | * |
||
107 | * @return string |
||
108 | */ |
||
109 | 1 | private function readOobData(FramePickerInterface $picker) |
|
119 | |||
120 | /** |
||
121 | * Read regular data |
||
122 | * |
||
123 | * @param FramePickerInterface $picker Picker to read data into |
||
124 | * |
||
125 | * @return string |
||
126 | */ |
||
127 | 25 | private function readRegularData(FramePickerInterface $picker) |
|
156 | |||
157 | /** |
||
158 | * Return first byte from socket buffer |
||
159 | * |
||
160 | * @return string |
||
161 | */ |
||
162 | 25 | private function getDataInSocket() |
|
166 | |||
167 | /** |
||
168 | * Checks whether data read from stream buffer can be filled later |
||
169 | * |
||
170 | * @param string $data Read data |
||
171 | * |
||
172 | * @return bool |
||
173 | */ |
||
174 | 24 | private function isReadDataActuallyEmpty($data) |
|
184 | |||
185 | /** |
||
186 | * Calculate attempts value |
||
187 | * |
||
188 | * @param array $context Read context |
||
189 | * @param int $currentAttempts Current attempts counter |
||
190 | * |
||
191 | * @return int |
||
192 | */ |
||
193 | 24 | private function resolveReadAttempts(array $context, $currentAttempts) |
|
201 | |||
202 | /** |
||
203 | * Write out-of-band data |
||
204 | * |
||
205 | * @param resource $socket Socket resource |
||
206 | * @param string $data Data to write |
||
207 | * |
||
208 | * @return int Amount of written bytes |
||
209 | */ |
||
210 | 1 | private function writeOobData($socket, $data) |
|
232 | |||
233 | /** |
||
234 | * Return remote address if we connected or false otherwise |
||
235 | * |
||
236 | * @return string|null |
||
237 | */ |
||
238 | 37 | private function resolveRemoteAddress() |
|
244 | } |
||
245 |