1 | <?php |
||
24 | abstract class AbstractClientIo extends AbstractIo |
||
25 | { |
||
26 | /** |
||
27 | * Disconnected state |
||
28 | */ |
||
29 | const STATE_DISCONNECTED = 0; |
||
30 | |||
31 | /** |
||
32 | * Connected state |
||
33 | */ |
||
34 | const STATE_CONNECTED = 1; |
||
35 | |||
36 | /** |
||
37 | * Socket state |
||
38 | * |
||
39 | * @var int |
||
40 | */ |
||
41 | private $state = self::STATE_DISCONNECTED; |
||
42 | |||
43 | /** |
||
44 | * Amount of attempts to write data before treat request as failed |
||
45 | * |
||
46 | * @var int |
||
47 | */ |
||
48 | private $writeAttempts = self::IO_ATTEMPTS; |
||
49 | |||
50 | /** |
||
51 | * Maximum amount of OOB data length (0 - not supported) |
||
52 | * |
||
53 | * @var int |
||
54 | */ |
||
55 | private $maxOobPacketLength; |
||
56 | |||
57 | /** |
||
58 | * AbstractClientIo constructor. |
||
59 | * |
||
60 | * @param SocketInterface $socket Socket object |
||
61 | * @param int $maxOobPacketLength Maximum amount of OOB data length (0 - not supported) |
||
62 | */ |
||
63 | 123 | public function __construct(SocketInterface $socket, $maxOobPacketLength) |
|
68 | |||
69 | /** {@inheritdoc} */ |
||
70 | 54 | final public function read(FramePickerInterface $picker, Context $context, $isOutOfBand) |
|
99 | |||
100 | /** {@inheritdoc} */ |
||
101 | 49 | final public function write($data, Context $context, $isOutOfBand) |
|
118 | |||
119 | /** |
||
120 | * Verify, that we are in connected state |
||
121 | * |
||
122 | * @return void |
||
123 | * @throws ConnectionException |
||
124 | */ |
||
125 | 103 | private function setConnectedState() |
|
143 | |||
144 | /** |
||
145 | * Verifies given data according to OOB rules |
||
146 | * |
||
147 | * @param bool $isOutOfBand Flag if data are out of band |
||
148 | * @param string $data Accepted data or null to skip check |
||
149 | * |
||
150 | * @return void |
||
151 | */ |
||
152 | 75 | private function verifyOobData($isOutOfBand, $data) |
|
170 | |||
171 | /** |
||
172 | * Read unhandled data if there is something from the previous operation |
||
173 | * |
||
174 | * @param FramePickerInterface $picker Frame picker to use |
||
175 | * @param Context $context Socket context |
||
176 | * @param bool $isOutOfBand Flag if it is out-of-band data |
||
177 | * |
||
178 | * @return bool Flag whether it is the end of the frame |
||
179 | */ |
||
180 | 44 | private function handleUnreadData(FramePickerInterface $picker, Context $context, $isOutOfBand) |
|
192 | |||
193 | /** |
||
194 | * Return remote socket ip address |
||
195 | * |
||
196 | * @return string |
||
197 | */ |
||
198 | abstract protected function getRemoteAddress(); |
||
199 | |||
200 | /** |
||
201 | * Read raw data from network into given picker |
||
202 | * |
||
203 | * @param FramePickerInterface $picker Frame picker |
||
204 | * @param bool $isOutOfBand Flag if these are out of band data |
||
205 | * |
||
206 | * @return string Data after end of frame |
||
207 | */ |
||
208 | abstract protected function readRawDataIntoPicker(FramePickerInterface $picker, $isOutOfBand); |
||
209 | |||
210 | /** |
||
211 | * Return true if frame can be collected in nearest future, false otherwise |
||
212 | * |
||
213 | * @return bool |
||
214 | */ |
||
215 | abstract protected function canReachFrame(); |
||
216 | |||
217 | /** |
||
218 | * Write data to socket |
||
219 | * |
||
220 | * @param string $data Data to write |
||
221 | * @param bool $isOutOfBand Flag if data are out of band |
||
222 | * |
||
223 | * @return int Number of written bytes |
||
224 | */ |
||
225 | abstract protected function writeRawData($data, $isOutOfBand); |
||
226 | } |
||
227 |