1 | <?php |
||
23 | abstract class AbstractClientIo extends AbstractIo |
||
24 | { |
||
25 | /** |
||
26 | * Disconnected state |
||
27 | */ |
||
28 | const STATE_DISCONNECTED = 0; |
||
29 | |||
30 | /** |
||
31 | * Connected state |
||
32 | */ |
||
33 | const STATE_CONNECTED = 1; |
||
34 | |||
35 | /** |
||
36 | * Socket state |
||
37 | * |
||
38 | * @var int |
||
39 | */ |
||
40 | private $state = self::STATE_DISCONNECTED; |
||
41 | |||
42 | /** |
||
43 | * Amount of attempts to write data before treat request as failed |
||
44 | * |
||
45 | * @var int |
||
46 | */ |
||
47 | private $writeAttempts = self::IO_ATTEMPTS; |
||
48 | |||
49 | /** |
||
50 | * Maximum amount of OOB data length (0 - not supported) |
||
51 | * |
||
52 | * @var int |
||
53 | */ |
||
54 | private $maxOobPacketLength; |
||
55 | |||
56 | /** |
||
57 | * AbstractClientIo constructor. |
||
58 | * |
||
59 | * @param SocketInterface $socket Socket object |
||
60 | * @param int $maxOobPacketLength Maximum amount of OOB data length (0 - not supported) |
||
61 | */ |
||
62 | public function __construct(SocketInterface $socket, $maxOobPacketLength) |
||
67 | |||
68 | /** |
||
69 | 103 | * Read raw data from network into given picker |
|
70 | * |
||
71 | 103 | * @param FramePickerInterface $picker Frame picker |
|
72 | 103 | * @param bool $isOutOfBand Flag if these are out of band data |
|
73 | 103 | * |
|
74 | * @return string Data after end of frame |
||
75 | */ |
||
76 | abstract protected function readRawDataIntoPicker(FramePickerInterface $picker, $isOutOfBand); |
||
77 | |||
78 | /** |
||
79 | * Write data to socket |
||
80 | * |
||
81 | * @param string $data Data to write |
||
82 | * @param bool $isOutOfBand Flag if data are out of band |
||
83 | * |
||
84 | * @return int Number of written bytes |
||
85 | */ |
||
86 | abstract protected function writeRawData($data, $isOutOfBand); |
||
87 | |||
88 | /** |
||
89 | * Check whether given socket resource is connected |
||
90 | * |
||
91 | * @return bool |
||
92 | */ |
||
93 | abstract protected function isConnected(); |
||
94 | |||
95 | /** |
||
96 | * Return true if frame can be collected in nearest future, false otherwise |
||
97 | * |
||
98 | * @return bool |
||
99 | */ |
||
100 | abstract protected function canReachFrame(); |
||
101 | |||
102 | /** |
||
103 | * Return remote socket ip address |
||
104 | * |
||
105 | * @return string |
||
106 | */ |
||
107 | abstract protected function getRemoteAddress(); |
||
108 | |||
109 | /** {@inheritdoc} */ |
||
110 | final public function read(FramePickerInterface $picker, Context $context, $isOutOfBand) |
||
135 | |||
136 | /** {@inheritdoc} */ |
||
137 | final public function write($data, Context $context, $isOutOfBand) |
||
152 | 4 | ||
153 | /** |
||
154 | 1 | * Verifies given data according to OOB rules |
|
155 | * |
||
156 | 14 | * @param bool $isOutOfBand Flag if data are out of band |
|
157 | 7 | * @param string $data Accepted data or null to skip check |
|
158 | * |
||
159 | 7 | * @return void |
|
160 | 7 | */ |
|
161 | private function verifyOobData($isOutOfBand, $data) |
||
179 | 20 | ||
180 | 20 | /** |
|
181 | * Verify, that we are in connected state |
||
182 | * |
||
183 | 68 | * @return void |
|
184 | 68 | * @throws ConnectionException |
|
185 | 2 | */ |
|
186 | private function setConnectedState() |
||
204 | |||
205 | 2 | /** |
|
206 | 1 | * Checks that we are in connected state |
|
207 | * |
||
208 | * @param string $message Message to pass in exception |
||
209 | * |
||
210 | * @return void |
||
211 | * @throws NetworkSocketException |
||
212 | */ |
||
213 | final protected function throwExceptionIfNotConnected($message) |
||
220 | |||
221 | 40 | /** |
|
222 | * Read unhandled data if there is something from the previous operation |
||
223 | * |
||
224 | * @param FramePickerInterface $picker Frame picker to use |
||
225 | * @param Context $context Socket context |
||
226 | * @param bool $isOutOfBand Flag if it is out-of-band data |
||
227 | * |
||
228 | * @return bool Flag whether it is the end of the frame |
||
229 | */ |
||
230 | private function handleUnreadData(FramePickerInterface $picker, Context $context, $isOutOfBand) |
||
242 | } |
||
243 |