1 | <?php |
||
24 | class RequestDescriptor implements StreamResourceInterface, EventHandlerInterface |
||
25 | { |
||
26 | /** |
||
27 | * This descriptor is ready for reading |
||
28 | */ |
||
29 | const RDS_READ = 0x0001; |
||
30 | |||
31 | /** |
||
32 | * This descriptor is ready for writing |
||
33 | */ |
||
34 | const RDS_WRITE = 0x0002; |
||
35 | |||
36 | /** |
||
37 | * This descriptor is has OOB data |
||
38 | */ |
||
39 | const RDS_OOB = 0x0004; |
||
40 | |||
41 | /** |
||
42 | * Minimum transfer rate counter for receiving data |
||
43 | */ |
||
44 | const COUNTER_RECV_MIN_RATE = 'recv_speed_rate_counter'; |
||
45 | |||
46 | /** |
||
47 | * Minimum transfer rate counter for sending data |
||
48 | */ |
||
49 | const COUNTER_SEND_MIN_RATE = 'send_speed_rate_counter'; |
||
50 | |||
51 | /** |
||
52 | * Socket for this operation |
||
53 | * |
||
54 | * @var SocketInterface |
||
55 | */ |
||
56 | private $socket; |
||
57 | |||
58 | /** |
||
59 | * Key-value pairs with meta information |
||
60 | * |
||
61 | * @var array |
||
62 | */ |
||
63 | private $metadata = []; |
||
64 | |||
65 | /** |
||
66 | * Event handler object |
||
67 | * |
||
68 | * @var EventHandlerInterface |
||
69 | */ |
||
70 | private $handlers; |
||
71 | |||
72 | /** |
||
73 | * Flag whether this socket request is running |
||
74 | * |
||
75 | * @var bool |
||
76 | */ |
||
77 | private $isRunning; |
||
78 | |||
79 | /** |
||
80 | * Operation to perform on socket |
||
81 | * |
||
82 | * @var OperationInterface |
||
83 | */ |
||
84 | private $operation; |
||
85 | |||
86 | /** |
||
87 | * Flag if this socket is postponed |
||
88 | * |
||
89 | * @var bool |
||
90 | */ |
||
91 | private $isForgotten = false; |
||
92 | |||
93 | /** |
||
94 | * Set of state flags: RDS_* consts |
||
95 | * |
||
96 | * @var int |
||
97 | */ |
||
98 | private $state = 0; |
||
99 | |||
100 | /** |
||
101 | * Array of counters |
||
102 | * |
||
103 | * @var SpeedRateCounter[] |
||
104 | */ |
||
105 | private $counters; |
||
106 | |||
107 | /** |
||
108 | * RequestDescriptor constructor. |
||
109 | * |
||
110 | * @param SocketInterface $socket Socket object |
||
111 | * @param OperationInterface $operation Operation to perform on socket |
||
112 | * @param array $metadata Metadata |
||
113 | * @param EventHandlerInterface $handlers Handlers for this socket |
||
114 | */ |
||
115 | 189 | public function __construct( |
|
128 | |||
129 | /** |
||
130 | * Initialize data before request |
||
131 | * |
||
132 | * @return void |
||
133 | */ |
||
134 | 189 | public function initialize() |
|
138 | |||
139 | /** |
||
140 | * Return Operation |
||
141 | * |
||
142 | * @return OperationInterface |
||
143 | */ |
||
144 | 109 | public function getOperation() |
|
148 | |||
149 | /** |
||
150 | * Sets Operation |
||
151 | * |
||
152 | * @param OperationInterface $operation New operation |
||
153 | * |
||
154 | * @return void |
||
155 | */ |
||
156 | 51 | public function setOperation(OperationInterface $operation) |
|
160 | |||
161 | /** |
||
162 | * Return flag whether request is running |
||
163 | * |
||
164 | * @return boolean |
||
165 | */ |
||
166 | 139 | public function isRunning() |
|
170 | |||
171 | /** |
||
172 | * Sets running flag |
||
173 | * |
||
174 | * @param boolean $isRunning New value for IsRunning |
||
175 | * |
||
176 | * @return void |
||
177 | */ |
||
178 | 110 | public function setRunning($isRunning) |
|
182 | |||
183 | /** |
||
184 | * Return Socket |
||
185 | * |
||
186 | * @return SocketInterface |
||
187 | */ |
||
188 | 138 | public function getSocket() |
|
192 | |||
193 | /** {@inheritdoc} */ |
||
194 | 53 | public function getStreamResource() |
|
198 | |||
199 | /** |
||
200 | * Return key-value array with metadata |
||
201 | * |
||
202 | * @return array |
||
203 | */ |
||
204 | 169 | public function getMetadata() |
|
208 | |||
209 | /** |
||
210 | * Set metadata for given socket |
||
211 | * |
||
212 | * @param string|array $key Either string or key-value array of metadata. If string, then value must be |
||
213 | * passed in third argument, if array, then third argument will be ignored |
||
214 | * @param mixed $value Value for key or null, if $key is array |
||
215 | * |
||
216 | * @return void |
||
217 | */ |
||
218 | 189 | public function setMetadata($key, $value = null) |
|
236 | |||
237 | /** {@inheritdoc} */ |
||
238 | 135 | public function invokeEvent( |
|
248 | |||
249 | /** |
||
250 | * Completes processing this socket in event loop, but keep this socket connection opened. |
||
251 | * |
||
252 | * @return void |
||
253 | */ |
||
254 | 12 | public function forget() |
|
262 | |||
263 | /** |
||
264 | * Return true, if this socket shouldn't be processed by executor engine |
||
265 | * |
||
266 | * @return bool |
||
267 | */ |
||
268 | 120 | public function isForgotten() |
|
272 | |||
273 | /** |
||
274 | * Return true if descriptor has given state |
||
275 | * |
||
276 | * @param int $state State to check, set of RDS_* consts |
||
277 | * |
||
278 | * @return bool |
||
279 | */ |
||
280 | 112 | public function hasState($state) |
|
284 | |||
285 | /** |
||
286 | * Sets one state into an object |
||
287 | * |
||
288 | * @param int $state State to set, set of RDS_* consts |
||
289 | * |
||
290 | * @return void |
||
291 | */ |
||
292 | 130 | public function setState($state) |
|
296 | |||
297 | /** |
||
298 | * Clears given state in object |
||
299 | * |
||
300 | * @param int $state State to clear, set of RDS_* consts |
||
301 | * |
||
302 | * @return void |
||
303 | */ |
||
304 | 112 | public function clearState($state) |
|
308 | |||
309 | /** |
||
310 | * Registers counter in this descriptor |
||
311 | * |
||
312 | * @param string $name SpeedRateCounter name for retrieving |
||
313 | * @param SpeedRateCounter $counter A counter object |
||
314 | * |
||
315 | * @return void |
||
316 | */ |
||
317 | 80 | public function registerCounter($name, SpeedRateCounter $counter) |
|
323 | |||
324 | /** |
||
325 | * Return counter by given name |
||
326 | * |
||
327 | * @param string $name SpeedRateCounter name |
||
328 | * |
||
329 | * @return SpeedRateCounter|null |
||
330 | */ |
||
331 | 80 | public function getCounter($name) |
|
335 | |||
336 | /** |
||
337 | * Resets counter with given name |
||
338 | * |
||
339 | * @param string $name Counter name |
||
340 | * |
||
341 | * @return void |
||
342 | */ |
||
343 | 80 | public function resetCounter($name) |
|
364 | } |
||
365 |