1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Async sockets |
4
|
|
|
* |
5
|
|
|
* @copyright Copyright (c) 2015-2016, 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\RequestExecutor\EventHandlerInterface; |
14
|
|
|
use AsyncSockets\Operation\OperationInterface; |
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
|
|
|
* Socket for this operation |
26
|
|
|
* |
27
|
|
|
* @var SocketInterface |
28
|
|
|
*/ |
29
|
|
|
private $socket; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* Key-value pairs with meta information |
33
|
|
|
* |
34
|
|
|
* @var array |
35
|
|
|
*/ |
36
|
|
|
private $metadata; |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Event handler object |
40
|
|
|
* |
41
|
|
|
* @var EventHandlerInterface |
42
|
|
|
*/ |
43
|
|
|
private $handlers; |
44
|
|
|
|
45
|
|
|
/** |
46
|
|
|
* Flag whether this socket request is running |
47
|
|
|
* |
48
|
|
|
* @var bool |
49
|
|
|
*/ |
50
|
|
|
private $isRunning; |
51
|
|
|
|
52
|
|
|
/** |
53
|
|
|
* Operation to perform on socket |
54
|
|
|
* |
55
|
|
|
* @var OperationInterface |
56
|
|
|
*/ |
57
|
|
|
private $operation; |
58
|
|
|
|
59
|
|
|
/** |
60
|
|
|
* Flag if this socket is postponed |
61
|
|
|
* |
62
|
|
|
* @var bool |
63
|
|
|
*/ |
64
|
|
|
private $isPostponed = false; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* RequestDescriptor constructor. |
68
|
|
|
* |
69
|
|
|
* @param SocketInterface $socket Socket object |
70
|
|
|
* @param OperationInterface $operation Operation to perform on socket |
71
|
|
|
* @param array $metadata Metadata |
72
|
|
|
* @param EventHandlerInterface $handlers Handlers for this socket |
73
|
|
|
*/ |
74
|
158 |
|
public function __construct( |
75
|
|
|
SocketInterface $socket, |
76
|
|
|
OperationInterface $operation, |
77
|
|
|
array $metadata, |
78
|
|
|
EventHandlerInterface $handlers = null |
79
|
|
|
) { |
80
|
158 |
|
$this->socket = $socket; |
81
|
158 |
|
$this->operation = $operation; |
82
|
158 |
|
$this->metadata = $metadata; |
83
|
158 |
|
$this->handlers = $handlers; |
84
|
158 |
|
$this->initialize(); |
85
|
158 |
|
} |
86
|
|
|
|
87
|
|
|
/** |
88
|
|
|
* Return Operation |
89
|
|
|
* |
90
|
|
|
* @return OperationInterface |
91
|
|
|
*/ |
92
|
99 |
|
public function getOperation() |
93
|
|
|
{ |
94
|
99 |
|
return $this->operation; |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
/** |
98
|
|
|
* Sets Operation |
99
|
|
|
* |
100
|
|
|
* @param OperationInterface $operation New operation |
101
|
|
|
* |
102
|
|
|
* @return void |
103
|
|
|
*/ |
104
|
49 |
|
public function setOperation(OperationInterface $operation) |
105
|
|
|
{ |
106
|
49 |
|
$this->operation = $operation; |
107
|
49 |
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Initialize data before request |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
158 |
|
public function initialize() |
115
|
|
|
{ |
116
|
158 |
|
$this->isRunning = false; |
117
|
158 |
|
} |
118
|
|
|
|
119
|
|
|
/** |
120
|
|
|
* Return flag whether request is running |
121
|
|
|
* |
122
|
|
|
* @return boolean |
123
|
|
|
*/ |
124
|
127 |
|
public function isRunning() |
125
|
|
|
{ |
126
|
127 |
|
return $this->isRunning; |
127
|
|
|
} |
128
|
|
|
|
129
|
|
|
/** |
130
|
|
|
* Sets running flag |
131
|
|
|
* |
132
|
|
|
* @param boolean $isRunning New value for IsRunning |
133
|
|
|
* |
134
|
|
|
* @return void |
135
|
|
|
*/ |
136
|
98 |
|
public function setRunning($isRunning) |
137
|
|
|
{ |
138
|
98 |
|
$this->isRunning = $isRunning; |
139
|
98 |
|
} |
140
|
|
|
|
141
|
|
|
/** |
142
|
|
|
* Return Socket |
143
|
|
|
* |
144
|
|
|
* @return SocketInterface |
145
|
|
|
*/ |
146
|
126 |
|
public function getSocket() |
147
|
|
|
{ |
148
|
126 |
|
return $this->socket; |
149
|
|
|
} |
150
|
|
|
|
151
|
|
|
/** {@inheritdoc} */ |
152
|
48 |
|
public function getStreamResource() |
153
|
|
|
{ |
154
|
48 |
|
return $this->socket->getStreamResource(); |
155
|
|
|
} |
156
|
|
|
|
157
|
|
|
/** |
158
|
|
|
* Return key-value array with metadata |
159
|
|
|
* |
160
|
|
|
* @return array |
161
|
|
|
*/ |
162
|
145 |
|
public function getMetadata() |
163
|
|
|
{ |
164
|
145 |
|
return $this->metadata; |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Set metadata for given socket |
169
|
|
|
* |
170
|
|
|
* @param string|array $key Either string or key-value array of metadata. If string, then value must be |
171
|
|
|
* passed in third argument, if array, then third argument will be ignored |
172
|
|
|
* @param mixed $value Value for key or null, if $key is array |
173
|
|
|
* |
174
|
|
|
* @return void |
175
|
|
|
*/ |
176
|
140 |
|
public function setMetadata($key, $value = null) |
177
|
|
|
{ |
178
|
140 |
|
if (!is_array($key)) { |
179
|
130 |
|
$this->metadata[$key] = $value; |
180
|
130 |
|
} else { |
181
|
20 |
|
$this->metadata = array_merge( |
182
|
20 |
|
$this->metadata, |
183
|
|
|
$key |
184
|
20 |
|
); |
185
|
|
|
} |
186
|
140 |
|
} |
187
|
|
|
|
188
|
|
|
/** {@inheritdoc} */ |
189
|
125 |
|
public function invokeEvent(Event $event) |
190
|
|
|
{ |
191
|
125 |
|
if ($this->handlers) { |
192
|
21 |
|
$this->handlers->invokeEvent($event); |
193
|
17 |
|
} |
194
|
121 |
|
} |
195
|
|
|
|
196
|
|
|
/** |
197
|
|
|
* Completes processing this socket in event loop, but keep this socket connection opened. Applicable |
198
|
|
|
* only to persistent sockets, all other socket types are ignored by this method. |
199
|
|
|
* |
200
|
|
|
* @return void |
201
|
|
|
*/ |
202
|
6 |
|
public function postpone() |
203
|
|
|
{ |
204
|
6 |
|
if (!($this->socket instanceof PersistentClientSocket)) { |
205
|
5 |
|
return; |
206
|
|
|
} |
207
|
|
|
|
208
|
1 |
|
$this->isPostponed = true; |
209
|
1 |
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Return true, if this socket shouldn't be processed by executor engine |
213
|
|
|
* |
214
|
|
|
* @return bool |
215
|
|
|
*/ |
216
|
112 |
|
public function isPostponed() |
217
|
|
|
{ |
218
|
112 |
|
return $this->isPostponed; |
219
|
|
|
} |
220
|
|
|
} |
221
|
|
|
|