|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* (c) Alexander Zhukov <[email protected]> |
|
5
|
|
|
* |
|
6
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
7
|
|
|
* file that was distributed with this source code. |
|
8
|
|
|
*/ |
|
9
|
|
|
|
|
10
|
|
|
namespace Zbox\UnifiedPush\Utils; |
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Class SocketClient |
|
14
|
|
|
* @package Zbox\UnifiedPush\Utils |
|
15
|
|
|
*/ |
|
16
|
|
|
class SocketClient |
|
17
|
|
|
{ |
|
18
|
|
|
/** |
|
19
|
|
|
* @var int |
|
20
|
|
|
*/ |
|
21
|
|
|
private $addressType; |
|
22
|
|
|
|
|
23
|
|
|
/** |
|
24
|
|
|
* @var string |
|
25
|
|
|
*/ |
|
26
|
|
|
private $transport; |
|
27
|
|
|
|
|
28
|
|
|
/** |
|
29
|
|
|
* @var string |
|
30
|
|
|
*/ |
|
31
|
|
|
private $target; |
|
32
|
|
|
|
|
33
|
|
|
/** |
|
34
|
|
|
* @var int |
|
35
|
|
|
*/ |
|
36
|
|
|
private $targetPort; |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @var array |
|
40
|
|
|
*/ |
|
41
|
|
|
private $contextOptions = array(); |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* @var bool |
|
45
|
|
|
*/ |
|
46
|
|
|
private $blockingMode = true; |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @var int |
|
50
|
|
|
*/ |
|
51
|
|
|
private $socketTimeout; |
|
52
|
|
|
|
|
53
|
|
|
/** |
|
54
|
|
|
* @var array |
|
55
|
|
|
*/ |
|
56
|
|
|
private $connectionFlags = array(); |
|
57
|
|
|
|
|
58
|
|
|
/** |
|
59
|
|
|
* @var resource |
|
60
|
|
|
*/ |
|
61
|
|
|
private $streamResource; |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @param string $target |
|
65
|
|
|
* @param int $targetPort |
|
66
|
|
|
* @param int $addressType |
|
67
|
|
|
*/ |
|
68
|
|
|
public function __construct($target, $targetPort = null, $addressType = AF_INET) |
|
69
|
|
|
{ |
|
70
|
|
|
$this |
|
71
|
|
|
->setAddressType($addressType) |
|
72
|
|
|
->setTarget($target) |
|
73
|
|
|
->setTargetPort($targetPort) |
|
74
|
|
|
; |
|
75
|
|
|
return $this; |
|
|
|
|
|
|
76
|
|
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Returns address to the socket to connect to |
|
80
|
|
|
* |
|
81
|
|
|
* @return string |
|
82
|
|
|
*/ |
|
83
|
|
|
public function getAddress() |
|
84
|
|
|
{ |
|
85
|
|
|
$isUnix = $this->getAddressType() === AF_UNIX; |
|
86
|
|
|
|
|
87
|
|
|
$address = array(); |
|
88
|
|
|
$address['transport'] = $this->getTransport() . ( $isUnix ? ':///' : '://' ); |
|
89
|
|
|
$address['target'] = $this->getTarget(); |
|
90
|
|
|
$address['port'] = ( $isUnix ? '' : ':' ) . $this->getTargetPort(); |
|
91
|
|
|
|
|
92
|
|
|
return implode('', $address); |
|
93
|
|
|
} |
|
94
|
|
|
|
|
95
|
|
|
/** |
|
96
|
|
|
* Gets address type |
|
97
|
|
|
* |
|
98
|
|
|
* @return int |
|
99
|
|
|
*/ |
|
100
|
|
|
public function getAddressType() |
|
101
|
|
|
{ |
|
102
|
|
|
return $this->addressType; |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
|
|
/** |
|
106
|
|
|
* Sets address type |
|
107
|
|
|
* |
|
108
|
|
|
* @param int $addressType |
|
109
|
|
|
* @return $this |
|
110
|
|
|
*/ |
|
111
|
|
|
public function setAddressType($addressType) |
|
112
|
|
|
{ |
|
113
|
|
|
if (!in_array($addressType, |
|
114
|
|
|
array( |
|
115
|
|
|
AF_INET, |
|
116
|
|
|
AF_INET6, |
|
117
|
|
|
AF_UNIX |
|
118
|
|
|
) |
|
119
|
|
|
)) { |
|
120
|
|
|
throw new \DomainException('Unsupported address type'); |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
$this->addressType = $addressType; |
|
124
|
|
|
return $this; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Gets socket transport |
|
129
|
|
|
* |
|
130
|
|
|
* @return string |
|
131
|
|
|
*/ |
|
132
|
|
|
public function getTransport() |
|
133
|
|
|
{ |
|
134
|
|
|
return $this->transport; |
|
135
|
|
|
} |
|
136
|
|
|
|
|
137
|
|
|
/** |
|
138
|
|
|
* Defines socket transport |
|
139
|
|
|
* |
|
140
|
|
|
* @param string $transport |
|
141
|
|
|
* @return $this |
|
142
|
|
|
*/ |
|
143
|
|
|
public function setTransport($transport) |
|
144
|
|
|
{ |
|
145
|
|
|
if (!in_array($transport,stream_get_transports())) { |
|
146
|
|
|
throw new \DomainException(sprintf('Unsupported type of transport "%s"', $transport)); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$this->transport = $transport; |
|
150
|
|
|
return $this; |
|
151
|
|
|
} |
|
152
|
|
|
|
|
153
|
|
|
/** |
|
154
|
|
|
* Gets target |
|
155
|
|
|
* |
|
156
|
|
|
* @return string |
|
157
|
|
|
*/ |
|
158
|
|
|
public function getTarget() |
|
159
|
|
|
{ |
|
160
|
|
|
return $this->target; |
|
161
|
|
|
} |
|
162
|
|
|
|
|
163
|
|
|
/** |
|
164
|
|
|
* Sets target portion of the remote socket parameter |
|
165
|
|
|
* |
|
166
|
|
|
* @param string $target |
|
167
|
|
|
* @return $this |
|
168
|
|
|
*/ |
|
169
|
|
|
public function setTarget($target) |
|
170
|
|
|
{ |
|
171
|
|
|
$this->target = $target; |
|
172
|
|
|
return $this; |
|
173
|
|
|
} |
|
174
|
|
|
|
|
175
|
|
|
/** |
|
176
|
|
|
* Gets target port |
|
177
|
|
|
* |
|
178
|
|
|
* @return int |
|
179
|
|
|
*/ |
|
180
|
|
|
public function getTargetPort() |
|
181
|
|
|
{ |
|
182
|
|
|
return $this->targetPort; |
|
183
|
|
|
} |
|
184
|
|
|
|
|
185
|
|
|
/** |
|
186
|
|
|
* Sets target port |
|
187
|
|
|
* |
|
188
|
|
|
* @param int $targetPort |
|
189
|
|
|
* @return $this |
|
190
|
|
|
*/ |
|
191
|
|
|
public function setTargetPort($targetPort) |
|
192
|
|
|
{ |
|
193
|
|
|
if ( |
|
194
|
|
|
$this->getAddressType() != AF_UNIX |
|
195
|
|
|
&& !is_integer($targetPort) |
|
196
|
|
|
) { |
|
197
|
|
|
throw new \InvalidArgumentException('Target port parameter must be an integer'); |
|
198
|
|
|
} |
|
199
|
|
|
$this->targetPort = $targetPort; |
|
200
|
|
|
|
|
201
|
|
|
return $this; |
|
202
|
|
|
} |
|
203
|
|
|
|
|
204
|
|
|
/** |
|
205
|
|
|
* Sets stream context parameters |
|
206
|
|
|
* |
|
207
|
|
|
* @param array $contextOptions |
|
208
|
|
|
* @return $this |
|
209
|
|
|
*/ |
|
210
|
|
|
public function setContextOptions(array $contextOptions) |
|
211
|
|
|
{ |
|
212
|
|
|
$this->contextOptions = $contextOptions; |
|
213
|
|
|
return $this; |
|
214
|
|
|
} |
|
215
|
|
|
|
|
216
|
|
|
/** |
|
217
|
|
|
* Gets stream context parameters |
|
218
|
|
|
* |
|
219
|
|
|
* @return array |
|
220
|
|
|
*/ |
|
221
|
|
|
public function getContextOptions() |
|
222
|
|
|
{ |
|
223
|
|
|
return $this->contextOptions; |
|
224
|
|
|
} |
|
225
|
|
|
|
|
226
|
|
|
/** |
|
227
|
|
|
* Gets blocking mode |
|
228
|
|
|
* |
|
229
|
|
|
* @return boolean |
|
230
|
|
|
*/ |
|
231
|
|
|
public function isBlockingMode() |
|
232
|
|
|
{ |
|
233
|
|
|
return $this->blockingMode; |
|
234
|
|
|
} |
|
235
|
|
|
|
|
236
|
|
|
/** |
|
237
|
|
|
* Sets blocking/non-blocking mode on a stream |
|
238
|
|
|
* |
|
239
|
|
|
* @param boolean $blockingMode |
|
240
|
|
|
* @return $this |
|
241
|
|
|
*/ |
|
242
|
|
|
public function setBlockingMode($blockingMode) |
|
243
|
|
|
{ |
|
244
|
|
|
$this->blockingMode = $blockingMode; |
|
245
|
|
|
|
|
246
|
|
|
return $this; |
|
247
|
|
|
} |
|
248
|
|
|
|
|
249
|
|
|
/** |
|
250
|
|
|
* Gets a socket timeout |
|
251
|
|
|
* |
|
252
|
|
|
* @return int |
|
253
|
|
|
*/ |
|
254
|
|
|
public function getSocketTimeout() |
|
255
|
|
|
{ |
|
256
|
|
|
if (!$this->socketTimeout) { |
|
257
|
|
|
return ini_get("default_socket_timeout"); |
|
258
|
|
|
} |
|
259
|
|
|
|
|
260
|
|
|
return $this->socketTimeout; |
|
261
|
|
|
} |
|
262
|
|
|
|
|
263
|
|
|
/** |
|
264
|
|
|
* Sets a timeout for reading/writing data over the socket |
|
265
|
|
|
* |
|
266
|
|
|
* @param int $socketTimeout |
|
267
|
|
|
* @return $this |
|
268
|
|
|
*/ |
|
269
|
|
|
public function setSocketTimeout($socketTimeout) |
|
270
|
|
|
{ |
|
271
|
|
|
if (!is_integer($socketTimeout)) { |
|
272
|
|
|
throw new \InvalidArgumentException('Socket timeout parameter must be an integer'); |
|
273
|
|
|
} |
|
274
|
|
|
$this->socketTimeout = $socketTimeout; |
|
275
|
|
|
|
|
276
|
|
|
return $this; |
|
277
|
|
|
} |
|
278
|
|
|
|
|
279
|
|
|
/** |
|
280
|
|
|
* Gets combination of connection flags |
|
281
|
|
|
* @return array |
|
282
|
|
|
*/ |
|
283
|
|
|
public function getConnectionFlags() |
|
284
|
|
|
{ |
|
285
|
|
|
if (empty($this->connectionFlags)) { |
|
286
|
|
|
return STREAM_CLIENT_CONNECT; |
|
287
|
|
|
} |
|
288
|
|
|
|
|
289
|
|
|
return implode('|', $this->connectionFlags); |
|
290
|
|
|
} |
|
291
|
|
|
|
|
292
|
|
|
/** |
|
293
|
|
|
* Adds a connection flag |
|
294
|
|
|
* |
|
295
|
|
|
* @param int $connectionFlag |
|
296
|
|
|
* @return $this |
|
297
|
|
|
*/ |
|
298
|
|
|
public function addConnectionFlag($connectionFlag) |
|
299
|
|
|
{ |
|
300
|
|
|
if (!in_array($connectionFlag, array( |
|
301
|
|
|
STREAM_CLIENT_CONNECT, |
|
302
|
|
|
STREAM_CLIENT_PERSISTENT, |
|
303
|
|
|
STREAM_CLIENT_ASYNC_CONNECT |
|
304
|
|
|
) |
|
305
|
|
|
)) { |
|
306
|
|
|
throw new \InvalidArgumentException('Invalid connection flag argument'); |
|
307
|
|
|
} |
|
308
|
|
|
|
|
309
|
|
|
if (!in_array($connectionFlag, $this->connectionFlags)) { |
|
310
|
|
|
$this->connectionFlags[] = $connectionFlag; |
|
311
|
|
|
} |
|
312
|
|
|
|
|
313
|
|
|
return $this; |
|
314
|
|
|
} |
|
315
|
|
|
|
|
316
|
|
|
/** |
|
317
|
|
|
* Drops all connection flags |
|
318
|
|
|
* |
|
319
|
|
|
* @return $this |
|
320
|
|
|
*/ |
|
321
|
|
|
public function dropConnectionFlags() |
|
322
|
|
|
{ |
|
323
|
|
|
$this->connectionFlags = array(); |
|
324
|
|
|
return $this; |
|
325
|
|
|
} |
|
326
|
|
|
|
|
327
|
|
|
/** |
|
328
|
|
|
* Returns a stream resource |
|
329
|
|
|
* |
|
330
|
|
|
* @return resource |
|
331
|
|
|
*/ |
|
332
|
|
|
public function getStreamResource() |
|
333
|
|
|
{ |
|
334
|
|
|
return $this->streamResource; |
|
335
|
|
|
} |
|
336
|
|
|
|
|
337
|
|
|
/** |
|
338
|
|
|
* Sets a stream resource |
|
339
|
|
|
* |
|
340
|
|
|
* @param resource $streamResource |
|
341
|
|
|
* @return $this |
|
342
|
|
|
*/ |
|
343
|
|
|
public function setStreamResource($streamResource) |
|
344
|
|
|
{ |
|
345
|
|
|
if (!is_resource($streamResource)) { |
|
346
|
|
|
throw new \InvalidArgumentException('Stream resource parameter must be a resource'); |
|
347
|
|
|
} |
|
348
|
|
|
$this->streamResource = $streamResource; |
|
349
|
|
|
|
|
350
|
|
|
return $this; |
|
351
|
|
|
} |
|
352
|
|
|
|
|
353
|
|
|
/** |
|
354
|
|
|
* Checks if stream resource exists |
|
355
|
|
|
* |
|
356
|
|
|
* @return bool |
|
357
|
|
|
*/ |
|
358
|
|
|
public function isAlive() |
|
359
|
|
|
{ |
|
360
|
|
|
return (bool) $this->streamResource; |
|
361
|
|
|
} |
|
362
|
|
|
|
|
363
|
|
|
/** |
|
364
|
|
|
* Establishes a socket connection |
|
365
|
|
|
* |
|
366
|
|
|
* @return $this |
|
367
|
|
|
*/ |
|
368
|
|
|
public function connect() |
|
369
|
|
|
{ |
|
370
|
|
|
$streamContext = stream_context_create(array( |
|
371
|
|
|
$this->getTransport() => $this->getContextOptions() |
|
372
|
|
|
)); |
|
373
|
|
|
|
|
374
|
|
|
$streamResource = stream_socket_client( |
|
375
|
|
|
$this->getAddress(), |
|
376
|
|
|
$errorCode, |
|
377
|
|
|
$errorMessage, |
|
378
|
|
|
$this->getSocketTimeout(), |
|
379
|
|
|
$this->getConnectionFlags(), |
|
380
|
|
|
$streamContext |
|
381
|
|
|
); |
|
382
|
|
|
|
|
383
|
|
|
if (!$streamResource) { |
|
384
|
|
|
throw new \RuntimeException(sprintf( |
|
385
|
|
|
'Unable to connect on socket. Error [%d]: %s', |
|
386
|
|
|
$errorCode, |
|
387
|
|
|
$errorMessage |
|
388
|
|
|
)); |
|
389
|
|
|
} |
|
390
|
|
|
|
|
391
|
|
|
stream_set_blocking($streamResource, $this->isBlockingMode()); |
|
392
|
|
|
|
|
393
|
|
|
$this->setStreamResource($streamResource); |
|
394
|
|
|
|
|
395
|
|
|
return $this; |
|
396
|
|
|
} |
|
397
|
|
|
|
|
398
|
|
|
/** |
|
399
|
|
|
* Writes data |
|
400
|
|
|
* |
|
401
|
|
|
* @param string $data |
|
402
|
|
|
* @return $this |
|
403
|
|
|
*/ |
|
404
|
|
|
public function write($data) |
|
405
|
|
|
{ |
|
406
|
|
|
$streamResource = $this->getStreamResource(); |
|
407
|
|
|
if (!is_resource($streamResource)) { |
|
408
|
|
|
throw new \UnexpectedValueException('Stream resource parameter must be a resource'); |
|
409
|
|
|
} |
|
410
|
|
|
|
|
411
|
|
|
$dataLength = strlen($data); |
|
412
|
|
|
|
|
413
|
|
|
if ($dataLength !== fwrite($streamResource, $data, $dataLength)) { |
|
414
|
|
|
throw new \RuntimeException('Unable to write data to the stream'); |
|
415
|
|
|
} |
|
416
|
|
|
|
|
417
|
|
|
return $this; |
|
418
|
|
|
} |
|
419
|
|
|
|
|
420
|
|
|
/** |
|
421
|
|
|
* Reads data |
|
422
|
|
|
* |
|
423
|
|
|
* @param int $length |
|
424
|
|
|
* @return string |
|
425
|
|
|
*/ |
|
426
|
|
|
public function read($length) |
|
427
|
|
|
{ |
|
428
|
|
|
$streamResource = $this->getStreamResource(); |
|
429
|
|
|
if (!is_resource($streamResource)) { |
|
430
|
|
|
throw new \UnexpectedValueException('Stream resource parameter must be a resource'); |
|
431
|
|
|
} |
|
432
|
|
|
$streamsRead = array($streamResource); |
|
433
|
|
|
$streamsWrite = NULL; |
|
434
|
|
|
$streamsExcept = NULL; |
|
435
|
|
|
|
|
436
|
|
|
$hasDataToRead = stream_select( |
|
437
|
|
|
$streamsRead, |
|
438
|
|
|
$streamsWrite, |
|
439
|
|
|
$streamsExcept, |
|
440
|
|
|
0 |
|
441
|
|
|
); |
|
442
|
|
|
|
|
443
|
|
|
if ($hasDataToRead) { |
|
444
|
|
|
return fread($streamResource, $length); |
|
445
|
|
|
} |
|
446
|
|
|
return false; |
|
447
|
|
|
} |
|
448
|
|
|
|
|
449
|
|
|
/** |
|
450
|
|
|
* Shutdowns a connection |
|
451
|
|
|
* |
|
452
|
|
|
* @return $this |
|
453
|
|
|
*/ |
|
454
|
|
|
public function disconnect() |
|
455
|
|
|
{ |
|
456
|
|
|
$streamResource = $this->getStreamResource(); |
|
457
|
|
|
|
|
458
|
|
|
if (is_resource($streamResource)) { |
|
459
|
|
|
stream_socket_shutdown($streamResource, STREAM_SHUT_RDWR); |
|
460
|
|
|
$this->streamResource = null; |
|
461
|
|
|
} |
|
462
|
|
|
|
|
463
|
|
|
return $this; |
|
464
|
|
|
} |
|
465
|
|
|
} |
|
466
|
|
|
|