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 | 110 | public function __construct(SocketInterface $socket, $maxOobPacketLength) |
|
63 | { |
||
64 | 110 | parent::__construct($socket); |
|
65 | 110 | $this->maxOobPacketLength = $maxOobPacketLength; |
|
66 | 110 | } |
|
67 | |||
68 | /** |
||
69 | * Read raw data from network into given picker |
||
70 | * |
||
71 | * @param FramePickerInterface $picker Frame picker |
||
72 | * @param bool $isOutOfBand Flag if these are out of band data |
||
73 | * |
||
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 | 51 | final public function read(FramePickerInterface $picker, Context $context, $isOutOfBand) |
|
135 | |||
136 | /** {@inheritdoc} */ |
||
137 | 44 | final public function write($data, Context $context, $isOutOfBand) |
|
152 | |||
153 | /** |
||
154 | * Verifies given data according to OOB rules |
||
155 | * |
||
156 | * @param bool $isOutOfBand Flag if data are out of band |
||
157 | * @param string $data Accepted data or null to skip check |
||
158 | * |
||
159 | * @return void |
||
160 | */ |
||
161 | 67 | private function verifyOobData($isOutOfBand, $data) |
|
162 | { |
||
163 | 67 | if (!$isOutOfBand) { |
|
164 | 53 | return; |
|
165 | } |
||
166 | |||
167 | 14 | if ($this->maxOobPacketLength === 0) { |
|
168 | 8 | throw UnsupportedOperationException::oobDataUnsupported($this->socket); |
|
169 | } |
||
170 | |||
171 | 6 | if ($data !== null && strlen($data) > $this->maxOobPacketLength) { |
|
172 | 4 | throw UnsupportedOperationException::oobDataPackageSizeExceeded( |
|
173 | 4 | $this->socket, |
|
174 | 4 | $this->maxOobPacketLength, |
|
175 | 4 | strlen($data) |
|
176 | 4 | ); |
|
177 | } |
||
178 | 2 | } |
|
179 | |||
180 | /** |
||
181 | * Verify, that we are in connected state |
||
182 | * |
||
183 | * @return void |
||
184 | * @throws ConnectionException |
||
185 | */ |
||
186 | 95 | private function setConnectedState() |
|
204 | |||
205 | /** |
||
206 | * 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 | 2 | final protected function throwExceptionIfNotConnected($message) |
|
220 | |||
221 | /** |
||
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 | 41 | private function handleUnreadData(FramePickerInterface $picker, Context $context, $isOutOfBand) |
|
242 | } |
||
243 |