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 $isPostponed = 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 | 180 | public function __construct( |
|
128 | |||
129 | /** |
||
130 | * Initialize data before request |
||
131 | * |
||
132 | * @return void |
||
133 | */ |
||
134 | 180 | 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 | 137 | public function isRunning() |
|
170 | |||
171 | /** |
||
172 | * Sets running flag |
||
173 | * |
||
174 | * @param boolean $isRunning New value for IsRunning |
||
175 | * |
||
176 | * @return void |
||
177 | */ |
||
178 | 108 | public function setRunning($isRunning) |
|
182 | |||
183 | /** |
||
184 | * Return Socket |
||
185 | * |
||
186 | * @return SocketInterface |
||
187 | */ |
||
188 | 136 | 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 | 166 | 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 | 180 | public function setMetadata($key, $value = null) |
|
236 | |||
237 | /** {@inheritdoc} */ |
||
238 | 135 | public function invokeEvent(Event $event) |
|
244 | |||
245 | /** |
||
246 | * Completes processing this socket in event loop, but keep this socket connection opened. Applicable |
||
247 | * only to persistent sockets, all other socket types are ignored by this method. |
||
248 | * |
||
249 | * @return void |
||
250 | */ |
||
251 | 6 | public function postpone() |
|
259 | |||
260 | /** |
||
261 | * Return true, if this socket shouldn't be processed by executor engine |
||
262 | * |
||
263 | * @return bool |
||
264 | */ |
||
265 | 117 | public function isPostponed() |
|
269 | |||
270 | /** |
||
271 | * Return true if descriptor has given state |
||
272 | * |
||
273 | * @param int $state State to check, set of RDS_* consts |
||
274 | * |
||
275 | * @return bool |
||
276 | */ |
||
277 | 111 | public function hasState($state) |
|
281 | |||
282 | /** |
||
283 | * Sets one state into an object |
||
284 | * |
||
285 | * @param int $state State to set, set of RDS_* consts |
||
286 | * |
||
287 | * @return void |
||
288 | */ |
||
289 | 129 | public function setState($state) |
|
293 | |||
294 | /** |
||
295 | * Clears given state in object |
||
296 | * |
||
297 | * @param int $state State to clear, set of RDS_* consts |
||
298 | * |
||
299 | * @return void |
||
300 | */ |
||
301 | 111 | public function clearState($state) |
|
305 | |||
306 | /** |
||
307 | * Registers counter in this descriptor |
||
308 | * |
||
309 | * @param string $name SpeedRateCounter name for retrieving |
||
310 | * @param SpeedRateCounter $counter A counter object |
||
311 | * |
||
312 | * @return void |
||
313 | */ |
||
314 | 80 | public function registerCounter($name, SpeedRateCounter $counter) |
|
320 | |||
321 | /** |
||
322 | * Return counter by given name |
||
323 | * |
||
324 | * @param string $name SpeedRateCounter name |
||
325 | * |
||
326 | * @return SpeedRateCounter|null |
||
327 | */ |
||
328 | 80 | public function getCounter($name) |
|
332 | |||
333 | /** |
||
334 | * Resets counter with given name |
||
335 | * |
||
336 | * @param string $name Counter name |
||
337 | * |
||
338 | * @return void |
||
339 | */ |
||
340 | 80 | public function resetCounter($name) |
|
361 | } |
||
362 |