forked from PocketDock/PocketDockConsole
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConnectionManager.php
More file actions
333 lines (290 loc) · 8.63 KB
/
ConnectionManager.php
File metadata and controls
333 lines (290 loc) · 8.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
<?php
namespace Wrench;
use InvalidArgumentException;
use Wrench\Util\Configurable;
use Wrench\Exception\Exception as WrenchException;
use Wrench\Exception\CloseException;
use \Exception;
use \Countable;
class ConnectionManager extends Configurable implements Countable
{
const TIMEOUT_SELECT = 0;
const TIMEOUT_SELECT_MICROSEC = 200000;
/**
* @var Server
*/
protected $server;
/**
* Master socket
*
* @var ServerSocket
*/
protected $socket;
/**
* An array of client connections
*
* @var array<int => Connection>
*/
protected $connections = array();
/**
* An array of raw socket resources, corresponding to connections, roughly
*
* @var array<int => resource>
*/
protected $resources = array();
/**
* Constructor
*
* @param Server $server
* @param array $options
*/
public function __construct(Server $server, array $options = array())
{
$this->server = $server;
parent::__construct($options);
}
/**
* @see Countable::count()
*/
public function count()
{
return count($this->connections);
}
/**
* @see Wrench\Socket.Socket::configure()
* Options include:
* - timeout_select => int, seconds, default 0
* - timeout_select_microsec => int, microseconds (NB: not milli), default: 200000
*/
protected function configure(array $options)
{
$options = array_merge(array(
'socket_master_class' => 'Wrench\Socket\ServerSocket',
'socket_master_options' => array(),
'socket_client_class' => 'Wrench\Socket\ServerClientSocket',
'socket_client_options' => array(),
'connection_class' => 'Wrench\Connection',
'connection_options' => array(),
'timeout_select' => self::TIMEOUT_SELECT,
'timeout_select_microsec' => self::TIMEOUT_SELECT_MICROSEC
), $options);
parent::configure($options);
$this->configureMasterSocket();
}
/**
* Gets the application associated with the given path
*
* @param string $path
*/
public function getApplicationForPath($path)
{
$path = ltrim($path, '/');
return $this->server->getApplication($path);
}
/**
* Configures the main server socket
*
*/
protected function configureMasterSocket()
{
$class = $this->options['socket_master_class'];
$options = $this->options['socket_master_options'];
$this->socket = new $class($this->server->getUri(), $options);
}
/**
* Listens on the main socket
*
* @return void
*/
public function listen()
{
$this->socket->listen();
$this->resources[$this->socket->getResourceId()] = $this->socket->getResource();
}
/**
* Gets all resources
*
* @return array<int => resource)
*/
protected function getAllResources()
{
return array_merge($this->resources, array(
$this->socket->getResourceId() => $this->socket->getResource()
));
}
/**
* Returns the Connection associated with the specified socket resource
*
* @param resource $socket
* @return Connection
*/
protected function getConnectionForClientSocket($socket)
{
if (!isset($this->connections[$this->resourceId($socket)])) {
return false;
}
return $this->connections[$this->resourceId($socket)];
}
/**
* Select and process an array of resources
*/
public function selectAndProcess()
{
$read = $this->resources;
$unused_write = null;
$unsued_exception = null;
stream_select(
$read,
$unused_write,
$unused_exception,
$this->options['timeout_select'],
$this->options['timeout_select_microsec']
);
foreach ($read as $socket) {
if ($socket == $this->socket->getResource()) {
$this->processMasterSocket();
} else {
$this->processClientSocket($socket);
}
}
}
/**
* Process events on the master socket ($this->socket)
*
* @return void
*/
protected function processMasterSocket()
{
$new = null;
try {
$new = $this->socket->accept();
} catch (Exception $e) {
$this->server->log('Socket error: ' . $e, 'err');
return;
}
$connection = $this->createConnection($new);
$this->server->notify(Server::EVENT_SOCKET_CONNECT, array($new, $connection));
}
/**
* Creates a connection from a socket resource
*
* The create connection object is based on the options passed into the
* constructor ('connection_class', 'connection_options'). This connection
* instance and its associated socket resource are then stored in the
* manager.
*
* @param resource $resource A socket resource
* @return Connection
*/
protected function createConnection($resource)
{
if (!$resource || !is_resource($resource)) {
throw new InvalidArgumentException('Invalid connection resource');
}
$socket_class = $this->options['socket_client_class'];
$socket_options = $this->options['socket_client_options'];
$connection_class = $this->options['connection_class'];
$connection_options = $this->options['connection_options'];
$socket = new $socket_class($resource, $socket_options);
$connection = new $connection_class($this, $socket, $connection_options);
$id = $this->resourceId($resource);
$this->resources[$id] = $resource;
$this->connections[$id] = $connection;
return $connection;
}
/**
* Process events on a client socket
*
* @param resource $socket
*/
protected function processClientSocket($socket)
{
$connection = $this->getConnectionForClientSocket($socket);
if (!$connection) {
$this->log('No connection for client socket', 'warning');
return;
}
try {
$this->server->notify(Server::EVENT_CLIENT_DATA, array($socket, $connection));
$connection->process();
} catch (CloseException $e) {
$this->log('Client connection closed: ' . $e, 'notice');
$connection->close($e);
} catch (WrenchException $e) {
$this->log('Error on client socket: ' . $e, 'warning');
$connection->close($e);
} catch (\InvalidArgumentException $e) {
$this->log('Wrong input arguments: ' . $e, 'warning');
$connection->close($e);
}
}
/**
* This server makes an explicit assumption: PHP resource types may be cast
* to a integer. Furthermore, we assume this is bijective. Both seem to be
* true in most circumstances, but may not be guaranteed.
*
* This method (and $this->getResourceId()) exist to make this assumption
* explicit.
*
* This is needed on the connection manager as well as on resources
*
* @param resource $resource
*/
protected function resourceId($resource)
{
return (int)$resource;
}
/**
* Gets the connection manager's listening URI
*
* @return string
*/
public function getUri()
{
return $this->server->getUri();
}
/**
* Logs a message
*
* @param string $message
* @param string $priority
*/
public function log($message, $priority = 'info')
{
$this->server->log(sprintf(
'%s: %s',
__CLASS__,
$message
), $priority);
}
/**
* @return \Wrench\Server
*/
public function getServer()
{
return $this->server;
}
/**
* Removes a connection
*
* @param Connection $connection
*/
public function removeConnection(Connection $connection)
{
$socket = $connection->getSocket();
if ($socket->getResource()) {
$index = $socket->getResourceId();
} else {
$index = array_search($connection, $this->connections);
}
if (!$index) {
$this->log('Could not remove connection: not found', 'warning');
}
unset($this->connections[$index]);
unset($this->resources[$index]);
$this->server->notify(
Server::EVENT_SOCKET_DISCONNECT,
array($connection->getSocket(), $connection)
);
}
}