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 | 115 | public function __construct(SocketInterface $socket, $maxOobPacketLength) |
|
68 | |||
69 | /** {@inheritdoc} */ |
||
70 | 53 | final public function read(FramePickerInterface $picker, Context $context, $isOutOfBand) |
|
99 | |||
100 | /** {@inheritdoc} */ |
||
101 | 44 | final public function write($data, Context $context, $isOutOfBand) |
|
115 | |||
116 | /** |
||
117 | * Verify, that we are in connected state |
||
118 | * |
||
119 | * @return void |
||
120 | * @throws ConnectionException |
||
121 | */ |
||
122 | 97 | private function setConnectedState() |
|
140 | |||
141 | /** |
||
142 | * Check whether given socket resource is connected |
||
143 | * |
||
144 | * @return bool |
||
145 | */ |
||
146 | abstract protected function isConnected(); |
||
147 | |||
148 | /** |
||
149 | * Verifies given data according to OOB rules |
||
150 | * |
||
151 | * @param bool $isOutOfBand Flag if data are out of band |
||
152 | * @param string $data Accepted data or null to skip check |
||
153 | * |
||
154 | * @return void |
||
155 | */ |
||
156 | 69 | private function verifyOobData($isOutOfBand, $data) |
|
174 | |||
175 | /** |
||
176 | * Read unhandled data if there is something from the previous operation |
||
177 | * |
||
178 | * @param FramePickerInterface $picker Frame picker to use |
||
179 | * @param Context $context Socket context |
||
180 | * @param bool $isOutOfBand Flag if it is out-of-band data |
||
181 | * |
||
182 | * @return bool Flag whether it is the end of the frame |
||
183 | */ |
||
184 | 43 | private function handleUnreadData(FramePickerInterface $picker, Context $context, $isOutOfBand) |
|
196 | |||
197 | /** |
||
198 | * Return remote socket ip address |
||
199 | * |
||
200 | * @return string |
||
201 | */ |
||
202 | abstract protected function getRemoteAddress(); |
||
203 | |||
204 | /** |
||
205 | * Read raw data from network into given picker |
||
206 | * |
||
207 | * @param FramePickerInterface $picker Frame picker |
||
208 | * @param bool $isOutOfBand Flag if these are out of band data |
||
209 | * |
||
210 | * @return string Data after end of frame |
||
211 | */ |
||
212 | abstract protected function readRawDataIntoPicker(FramePickerInterface $picker, $isOutOfBand); |
||
213 | |||
214 | /** |
||
215 | * Return true if frame can be collected in nearest future, false otherwise |
||
216 | * |
||
217 | * @return bool |
||
218 | */ |
||
219 | abstract protected function canReachFrame(); |
||
220 | |||
221 | /** |
||
222 | * Write data to socket |
||
223 | * |
||
224 | * @param string $data Data to write |
||
225 | * @param bool $isOutOfBand Flag if data are out of band |
||
226 | * |
||
227 | * @return int Number of written bytes |
||
228 | */ |
||
229 | abstract protected function writeRawData($data, $isOutOfBand); |
||
230 | } |
||
231 |