1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Async sockets |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2015-2017, Efimov Evgenij <[email protected]> |
6
|
|
|
* |
7
|
|
|
* This source file is subject to the MIT license that is bundled |
8
|
|
|
* with this source code in the file LICENSE. |
9
|
|
|
*/ |
10
|
|
|
namespace AsyncSockets\RequestExecutor\Metadata; |
11
|
|
|
|
12
|
|
|
use AsyncSockets\Event\Event; |
13
|
|
|
use AsyncSockets\Operation\OperationInterface; |
14
|
|
|
use AsyncSockets\RequestExecutor\EventHandlerInterface; |
15
|
|
|
use AsyncSockets\Socket\PersistentClientSocket; |
16
|
|
|
use AsyncSockets\Socket\SocketInterface; |
17
|
|
|
use AsyncSockets\Socket\StreamResourceInterface; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class RequestDescriptor |
21
|
|
|
*/ |
22
|
|
|
class RequestDescriptor implements StreamResourceInterface, EventHandlerInterface |
23
|
|
|
{ |
24
|
|
|
/** |
25
|
|
|
* This descriptor is ready for reading |
26
|
|
|
*/ |
27
|
|
|
const RDS_READ = 0x0001; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* This descriptor is ready for writing |
31
|
|
|
*/ |
32
|
|
|
const RDS_WRITE = 0x0002; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* This descriptor is has OOB data |
36
|
|
|
*/ |
37
|
|
|
const RDS_OOB = 0x0004; |
38
|
|
|
|
39
|
|
|
/** |
40
|
|
|
* Minimum transfer rate counter |
41
|
|
|
*/ |
42
|
|
|
const COUNTER_TRANSFER_MIN_RATE = 'speed_rate_counter'; |
43
|
|
|
|
44
|
|
|
/** |
45
|
|
|
* Socket for this operation |
46
|
|
|
* |
47
|
|
|
* @var SocketInterface |
48
|
|
|
*/ |
49
|
|
|
private $socket; |
50
|
|
|
|
51
|
|
|
/** |
52
|
|
|
* Key-value pairs with meta information |
53
|
|
|
* |
54
|
|
|
* @var array |
55
|
|
|
*/ |
56
|
|
|
private $metadata; |
57
|
|
|
|
58
|
|
|
/** |
59
|
|
|
* Event handler object |
60
|
|
|
* |
61
|
|
|
* @var EventHandlerInterface |
62
|
|
|
*/ |
63
|
|
|
private $handlers; |
64
|
|
|
|
65
|
|
|
/** |
66
|
|
|
* Flag whether this socket request is running |
67
|
|
|
* |
68
|
|
|
* @var bool |
69
|
|
|
*/ |
70
|
|
|
private $isRunning; |
71
|
|
|
|
72
|
|
|
/** |
73
|
|
|
* Operation to perform on socket |
74
|
|
|
* |
75
|
|
|
* @var OperationInterface |
76
|
|
|
*/ |
77
|
|
|
private $operation; |
78
|
|
|
|
79
|
|
|
/** |
80
|
|
|
* Flag if this socket is postponed |
81
|
|
|
* |
82
|
|
|
* @var bool |
83
|
|
|
*/ |
84
|
|
|
private $isPostponed = false; |
85
|
|
|
|
86
|
|
|
/** |
87
|
|
|
* Set of state flags: RDS_* consts |
88
|
|
|
* |
89
|
|
|
* @var int |
90
|
|
|
*/ |
91
|
|
|
private $state = 0; |
92
|
|
|
|
93
|
|
|
/** |
94
|
|
|
* Array of counters |
95
|
|
|
* |
96
|
|
|
* @var SpeedRateCounter[] |
97
|
|
|
*/ |
98
|
|
|
private $counters; |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* RequestDescriptor constructor. |
102
|
|
|
* |
103
|
|
|
* @param SocketInterface $socket Socket object |
104
|
|
|
* @param OperationInterface $operation Operation to perform on socket |
105
|
|
|
* @param array $metadata Metadata |
106
|
|
|
* @param EventHandlerInterface $handlers Handlers for this socket |
107
|
|
|
*/ |
108
|
106 |
|
public function __construct( |
109
|
|
|
SocketInterface $socket, |
110
|
|
|
OperationInterface $operation, |
111
|
|
|
array $metadata, |
112
|
|
|
EventHandlerInterface $handlers = null |
113
|
|
|
) { |
114
|
106 |
|
$this->socket = $socket; |
115
|
106 |
|
$this->operation = $operation; |
116
|
106 |
|
$this->metadata = $metadata; |
117
|
106 |
|
$this->handlers = $handlers; |
118
|
106 |
|
$this->counters = []; |
119
|
106 |
|
$this->initialize(); |
120
|
106 |
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Initialize data before request |
124
|
|
|
* |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
106 |
|
public function initialize() |
128
|
|
|
{ |
129
|
106 |
|
$this->isRunning = false; |
130
|
106 |
|
} |
131
|
|
|
|
132
|
|
|
/** |
133
|
|
|
* Return Operation |
134
|
|
|
* |
135
|
|
|
* @return OperationInterface |
136
|
|
|
*/ |
137
|
55 |
|
public function getOperation() |
138
|
|
|
{ |
139
|
55 |
|
return $this->operation; |
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Sets Operation |
144
|
|
|
* |
145
|
|
|
* @param OperationInterface $operation New operation |
146
|
|
|
* |
147
|
|
|
* @return void |
148
|
|
|
*/ |
149
|
26 |
|
public function setOperation(OperationInterface $operation) |
150
|
|
|
{ |
151
|
26 |
|
$this->operation = $operation; |
152
|
26 |
|
} |
153
|
|
|
|
154
|
|
|
/** |
155
|
|
|
* Return flag whether request is running |
156
|
|
|
* |
157
|
|
|
* @return boolean |
158
|
|
|
*/ |
159
|
70 |
|
public function isRunning() |
160
|
|
|
{ |
161
|
70 |
|
return $this->isRunning; |
162
|
|
|
} |
163
|
|
|
|
164
|
|
|
/** |
165
|
|
|
* Sets running flag |
166
|
|
|
* |
167
|
|
|
* @param boolean $isRunning New value for IsRunning |
168
|
|
|
* |
169
|
|
|
* @return void |
170
|
|
|
*/ |
171
|
55 |
|
public function setRunning($isRunning) |
172
|
|
|
{ |
173
|
55 |
|
$this->isRunning = $isRunning; |
174
|
55 |
|
} |
175
|
|
|
|
176
|
|
|
/** |
177
|
|
|
* Return Socket |
178
|
|
|
* |
179
|
|
|
* @return SocketInterface |
180
|
|
|
*/ |
181
|
68 |
|
public function getSocket() |
182
|
|
|
{ |
183
|
68 |
|
return $this->socket; |
184
|
|
|
} |
185
|
|
|
|
186
|
|
|
/** {@inheritdoc} */ |
187
|
53 |
|
public function getStreamResource() |
188
|
|
|
{ |
189
|
53 |
|
return $this->socket->getStreamResource(); |
190
|
|
|
} |
191
|
|
|
|
192
|
|
|
/** |
193
|
|
|
* Return key-value array with metadata |
194
|
|
|
* |
195
|
|
|
* @return array |
196
|
|
|
*/ |
197
|
92 |
|
public function getMetadata() |
198
|
|
|
{ |
199
|
92 |
|
return $this->metadata; |
200
|
|
|
} |
201
|
|
|
|
202
|
|
|
/** |
203
|
|
|
* Set metadata for given socket |
204
|
|
|
* |
205
|
|
|
* @param string|array $key Either string or key-value array of metadata. If string, then value must be |
206
|
|
|
* passed in third argument, if array, then third argument will be ignored |
207
|
|
|
* @param mixed $value Value for key or null, if $key is array |
208
|
|
|
* |
209
|
|
|
* @return void |
210
|
|
|
*/ |
211
|
89 |
|
public function setMetadata($key, $value = null) |
212
|
|
|
{ |
213
|
89 |
|
if (!is_array($key)) { |
214
|
74 |
|
$this->metadata[$key] = $value; |
215
|
|
|
} else { |
216
|
23 |
|
$this->metadata = array_merge( |
217
|
23 |
|
$this->metadata, |
218
|
|
|
$key |
219
|
|
|
); |
220
|
|
|
} |
221
|
89 |
|
} |
222
|
|
|
|
223
|
|
|
/** {@inheritdoc} */ |
224
|
68 |
|
public function invokeEvent(Event $event) |
225
|
|
|
{ |
226
|
68 |
|
if ($this->handlers) { |
227
|
11 |
|
$this->handlers->invokeEvent($event); |
228
|
|
|
} |
229
|
66 |
|
} |
230
|
|
|
|
231
|
|
|
/** |
232
|
|
|
* Completes processing this socket in event loop, but keep this socket connection opened. Applicable |
233
|
|
|
* only to persistent sockets, all other socket types are ignored by this method. |
234
|
|
|
* |
235
|
|
|
* @return void |
236
|
|
|
*/ |
237
|
6 |
|
public function postpone() |
238
|
|
|
{ |
239
|
6 |
|
if (!($this->socket instanceof PersistentClientSocket)) { |
240
|
5 |
|
return; |
241
|
|
|
} |
242
|
|
|
|
243
|
1 |
|
$this->isPostponed = true; |
244
|
1 |
|
} |
245
|
|
|
|
246
|
|
|
/** |
247
|
|
|
* Return true, if this socket shouldn't be processed by executor engine |
248
|
|
|
* |
249
|
|
|
* @return bool |
250
|
|
|
*/ |
251
|
69 |
|
public function isPostponed() |
252
|
|
|
{ |
253
|
69 |
|
return $this->isPostponed; |
254
|
|
|
} |
255
|
|
|
|
256
|
|
|
/** |
257
|
|
|
* Return true if descriptor has given state |
258
|
|
|
* |
259
|
|
|
* @param int $state State to check, set of RDS_* consts |
260
|
|
|
* |
261
|
|
|
* @return bool |
262
|
|
|
*/ |
263
|
70 |
|
public function hasState($state) |
264
|
|
|
{ |
265
|
70 |
|
return (bool) ($this->state & $state); |
266
|
|
|
} |
267
|
|
|
|
268
|
|
|
/** |
269
|
|
|
* Sets one state into an object |
270
|
|
|
* |
271
|
|
|
* @param int $state State to set, set of RDS_* consts |
272
|
|
|
* |
273
|
|
|
* @return void |
274
|
|
|
*/ |
275
|
76 |
|
public function setState($state) |
276
|
|
|
{ |
277
|
76 |
|
$this->state |= $state; |
278
|
76 |
|
} |
279
|
|
|
|
280
|
|
|
/** |
281
|
|
|
* Clears given state in object |
282
|
|
|
* |
283
|
|
|
* @param int $state State to clear, set of RDS_* consts |
284
|
|
|
* |
285
|
|
|
* @return void |
286
|
|
|
*/ |
287
|
70 |
|
public function clearState($state) |
288
|
|
|
{ |
289
|
70 |
|
$this->state &= ~$state; |
290
|
70 |
|
} |
291
|
|
|
|
292
|
|
|
/** |
293
|
|
|
* Registers counter in this descriptor |
294
|
|
|
* |
295
|
|
|
* @param string $name SpeedRateCounter name for retrieving |
296
|
|
|
* @param SpeedRateCounter $counter A counter object |
297
|
|
|
* |
298
|
|
|
* @return void |
299
|
|
|
*/ |
300
|
30 |
|
public function registerCounter($name, SpeedRateCounter $counter) |
301
|
|
|
{ |
302
|
30 |
|
if (!isset($this->counters[$name])) { |
303
|
30 |
|
$this->counters[$name] = $counter; |
304
|
|
|
} |
305
|
30 |
|
} |
306
|
|
|
|
307
|
|
|
/** |
308
|
|
|
* Return counter by given name |
309
|
|
|
* |
310
|
|
|
* @param string $name SpeedRateCounter name |
311
|
|
|
* |
312
|
|
|
* @return SpeedRateCounter|null |
313
|
|
|
*/ |
314
|
30 |
|
public function getCounter($name) |
315
|
|
|
{ |
316
|
30 |
|
return isset($this->counters[$name]) ? $this->counters[$name] : null; |
317
|
|
|
} |
318
|
|
|
} |
319
|
|
|
|