-
-
Notifications
You must be signed in to change notification settings - Fork 48
/
Socket.php
562 lines (525 loc) · 19 KB
/
Socket.php
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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
<?php
namespace Socket\Raw;
/**
* Simple and lightweight OOP wrapper for the low-level sockets extension (ext-sockets)
*
* @author clue
* @link https://github.com/clue/php-socket-raw
*/
class Socket
{
/**
* reference to actual socket resource
*
* @var \Socket|resource
*/
private $resource;
/**
* instanciate socket wrapper for given socket resource
*
* should usually not be called manually, see Factory
*
* @param \Socket|resource $resource
* @see Factory as the preferred (and simplest) way to construct socket instances
*/
public function __construct($resource)
{
$this->resource = $resource;
}
/**
* get actual socket resource
*
* @return \Socket|resource returns the socket resource (a `Socket` object as of PHP 8)
*/
public function getResource()
{
return $this->resource;
}
/**
* accept an incomming connection on this listening socket
*
* @return \Socket\Raw\Socket new connected socket used for communication
* @throws Exception on error, if this is not a listening socket or there's no connection pending
* @throws \Error PHP 8 only: throws \Error when socket is invalid
* @see self::selectRead() to check if this listening socket can accept()
* @see Factory::createServer() to create a listening socket
* @see self::listen() has to be called first
* @uses socket_accept()
*/
public function accept()
{
$resource = @socket_accept($this->resource);
if ($resource === false) {
throw Exception::createFromGlobalSocketOperation();
}
return new Socket($resource);
}
/**
* binds a name/address/path to this socket
*
* has to be called before issuing connect() or listen()
*
* @param string $address either of IPv4:port, hostname:port, [IPv6]:port, unix-path
* @return self $this (chainable)
* @throws Exception on error
* @throws \Error PHP 8 only: throws \Error when socket or arguments are invalid
* @uses socket_bind()
*/
public function bind($address)
{
$ret = @socket_bind($this->resource, $this->unformatAddress($address, $port), $port);
if ($ret === false) {
throw Exception::createFromSocketResource($this->resource);
}
return $this;
}
/**
* close this socket
*
* ATTENTION: make sure to NOT re-use this socket instance after closing it!
* its socket resource remains closed and most further operations will fail!
*
* @return self $this (chainable)
* @throws \Error PHP 8 only: throws \Error when socket is invalid
* @see self::shutdown() should be called before closing socket
* @uses socket_close()
*/
public function close()
{
socket_close($this->resource);
return $this;
}
/**
* initiate a connection to given address
*
* @param string $address either of IPv4:port, hostname:port, [IPv6]:port, unix-path
* @return self $this (chainable)
* @throws Exception on error
* @throws \Error PHP 8 only: throws \Error when socket or arguments are invalid
* @uses socket_connect()
*/
public function connect($address)
{
$ret = @socket_connect($this->resource, $this->unformatAddress($address, $port), $port);
if ($ret === false) {
throw Exception::createFromSocketResource($this->resource);
}
return $this;
}
/**
* Initiates a new connection to given address, wait for up to $timeout seconds
*
* The given $timeout parameter is an upper bound, a maximum time to wait
* for the connection to be either accepted or rejected.
*
* The resulting socket resource will be set to non-blocking mode,
* regardless of its previous state and whether this method succedes or
* if it fails. Make sure to reset with `setBlocking(true)` if you want to
* continue using blocking calls.
*
* @param string $address either of IPv4:port, hostname:port, [IPv6]:port, unix-path
* @param float $timeout maximum time to wait (in seconds)
* @return self $this (chainable)
* @throws Exception on error
* @throws \Error PHP 8 only: throws \Error when socket or arguments are invalid
* @uses self::setBlocking() to enable non-blocking mode
* @uses self::connect() to initiate the connection
* @uses self::selectWrite() to wait for the connection to complete
* @uses self::assertAlive() to check connection state
*/
public function connectTimeout($address, $timeout)
{
$this->setBlocking(false);
try {
// socket is non-blocking, so connect should emit EINPROGRESS
$this->connect($address);
// socket is already connected immediately?
return $this;
} catch (Exception $e) {
// non-blocking connect() should be EINPROGRESS (or EWOULDBLOCK on Windows) => otherwise re-throw
if ($e->getCode() !== SOCKET_EINPROGRESS && $e->getCode() !== SOCKET_EWOULDBLOCK) {
throw $e;
}
// connection should be completed (or rejected) within timeout: socket becomes writable on success or error
// Windows requires special care because it uses exceptfds for socket errors: https://github.com/reactphp/event-loop/issues/206
$r = null;
$w = array($this->resource);
$e = DIRECTORY_SEPARATOR === '\\' ? $w : null;
$ret = @socket_select($r, $w, $e, $timeout === null ? null : (int) $timeout, (int) (($timeout - floor($timeout)) * 1000000));
if ($ret === false) {
throw Exception::createFromGlobalSocketOperation('Failed to select socket for writing');
} elseif ($ret === 0) {
throw new Exception('Timed out while waiting for connection', SOCKET_ETIMEDOUT);
}
// confirm connection success (or fail if connected has been rejected)
$this->assertAlive();
return $this;
}
}
/**
* get socket option
*
* @param int $level
* @param int $optname
* @return mixed
* @throws Exception on error
* @throws \Error PHP 8 only: throws \Error when socket or arguments are invalid
* @uses socket_get_option()
*/
public function getOption($level, $optname)
{
$value = @socket_get_option($this->resource, $level, $optname);
if ($value === false) {
throw Exception::createFromSocketResource($this->resource);
}
return $value;
}
/**
* get remote side's address/path
*
* @return string
* @throws Exception on error
* @throws \Error PHP 8 only: throws \Error when socket is invalid
* @uses socket_getpeername()
*/
public function getPeerName()
{
$ret = @socket_getpeername($this->resource, $address, $port);
if ($ret === false) {
throw Exception::createFromSocketResource($this->resource);
}
return $this->formatAddress($address, $port);
}
/**
* get local side's address/path
*
* @return string
* @throws Exception on error
* @throws \Error PHP 8 only: throws \Error when socket is invalid
* @uses socket_getsockname()
*/
public function getSockName()
{
$ret = @socket_getsockname($this->resource, $address, $port);
if ($ret === false) {
throw Exception::createFromSocketResource($this->resource);
}
return $this->formatAddress($address, $port);
}
/**
* start listen for incoming connections
*
* @param int $backlog maximum number of incoming connections to be queued
* @return self $this (chainable)
* @throws Exception on error
* @throws \Error PHP 8 only: throws \Error when socket or arguments are invalid
* @see self::bind() has to be called first to bind name to socket
* @uses socket_listen()
*/
public function listen($backlog = 0)
{
$ret = @socket_listen($this->resource, $backlog);
if ($ret === false) {
throw Exception::createFromSocketResource($this->resource);
}
return $this;
}
/**
* read up to $length bytes from connect()ed / accept()ed socket
*
* The $type parameter specifies if this should use either binary safe reading
* (PHP_BINARY_READ, the default) or stop at CR or LF characters (PHP_NORMAL_READ)
*
* @param int $length maximum length to read
* @param int $type either of PHP_BINARY_READ (the default) or PHP_NORMAL_READ
* @return string
* @throws Exception on error
* @throws \Error PHP 8 only: throws \Error when socket or arguments are invalid
* @see self::recv() if you need to pass flags
* @uses socket_read()
*/
public function read($length, $type = PHP_BINARY_READ)
{
$data = @socket_read($this->resource, $length, $type);
if ($data === false) {
throw Exception::createFromSocketResource($this->resource);
}
return $data;
}
/**
* receive up to $length bytes from connect()ed / accept()ed socket
*
* @param int $length maximum length to read
* @param int $flags
* @return string
* @throws Exception on error
* @throws \Error PHP 8 only: throws \Error when socket or arguments are invalid
* @see self::read() if you do not need to pass $flags
* @see self::recvFrom() if your socket is not connect()ed
* @uses socket_recv()
*/
public function recv($length, $flags)
{
$ret = @socket_recv($this->resource, $buffer, $length, $flags);
if ($ret === false) {
throw Exception::createFromSocketResource($this->resource);
}
return $buffer;
}
/**
* receive up to $length bytes from socket
*
* @param int $length maximum length to read
* @param int $flags
* @param string $remote reference will be filled with remote/peer address/path
* @return string
* @throws Exception on error
* @throws \Error PHP 8 only: throws \Error when socket or arguments are invalid
* @see self::recv() if your socket is connect()ed
* @uses socket_recvfrom()
*/
public function recvFrom($length, $flags, &$remote)
{
$ret = @socket_recvfrom($this->resource, $buffer, $length, $flags, $address, $port);
if ($ret === false) {
throw Exception::createFromSocketResource($this->resource);
}
$remote = $this->formatAddress($address, $port);
return $buffer;
}
/**
* check socket to see if a read/recv/revFrom will not block
*
* @param float|null $sec maximum time to wait (in seconds), 0 = immediate polling, null = no limit
* @return boolean true = socket ready (read will not block), false = timeout expired, socket is not ready
* @throws Exception on error
* @throws \Error PHP 8 only: throws \Error when socket or arguments are invalid
* @uses socket_select()
*/
public function selectRead($sec = 0)
{
$usec = $sec === null ? 0 : (int) (($sec - floor($sec)) * 1000000);
$r = array($this->resource);
$n = null;
$ret = @socket_select($r, $n, $n, $sec === null ? null : (int) $sec, $usec);
if ($ret === false) {
throw Exception::createFromGlobalSocketOperation('Failed to select socket for reading');
}
return !!$ret;
}
/**
* check socket to see if a write/send/sendTo will not block
*
* @param float|null $sec maximum time to wait (in seconds), 0 = immediate polling, null = no limit
* @return boolean true = socket ready (write will not block), false = timeout expired, socket is not ready
* @throws Exception on error
* @throws \Error PHP 8 only: throws \Error when socket or arguments are invalid
* @uses socket_select()
*/
public function selectWrite($sec = 0)
{
$usec = $sec === null ? 0 : (int) (($sec - floor($sec)) * 1000000);
$w = array($this->resource);
$n = null;
$ret = @socket_select($n, $w, $n, $sec === null ? null : (int) $sec, $usec);
if ($ret === false) {
throw Exception::createFromGlobalSocketOperation('Failed to select socket for writing');
}
return !!$ret;
}
/**
* send given $buffer to connect()ed / accept()ed socket
*
* @param string $buffer
* @param int $flags
* @return int number of bytes actually written (make sure to check against given buffer length!)
* @throws Exception on error
* @throws \Error PHP 8 only: throws \Error when socket or arguments are invalid
* @see self::write() if you do not need to pass $flags
* @see self::sendTo() if your socket is not connect()ed
* @uses socket_send()
*/
public function send($buffer, $flags)
{
$ret = @socket_send($this->resource, $buffer, strlen($buffer), $flags);
if ($ret === false) {
throw Exception::createFromSocketResource($this->resource);
}
return $ret;
}
/**
* send given $buffer to socket
*
* @param string $buffer
* @param int $flags
* @param string $remote remote/peer address/path
* @return int number of bytes actually written
* @throws Exception on error
* @throws \Error PHP 8 only: throws \Error when socket or arguments are invalid
* @see self::send() if your socket is connect()ed
* @uses socket_sendto()
*/
public function sendTo($buffer, $flags, $remote)
{
$ret = @socket_sendto($this->resource, $buffer, strlen($buffer), $flags, $this->unformatAddress($remote, $port), $port);
if ($ret === false) {
throw Exception::createFromSocketResource($this->resource);
}
return $ret;
}
/**
* enable/disable blocking/nonblocking mode (O_NONBLOCK flag)
*
* @param boolean $toggle
* @return self $this (chainable)
* @throws Exception on error
* @throws \Error PHP 8 only: throws \Error when socket or arguments are invalid
* @uses socket_set_block()
* @uses socket_set_nonblock()
*/
public function setBlocking($toggle = true)
{
$ret = $toggle ? @socket_set_block($this->resource) : @socket_set_nonblock($this->resource);
if ($ret === false) {
throw Exception::createFromSocketResource($this->resource);
}
return $this;
}
/**
* set socket option
*
* @param int $level
* @param int $optname
* @param mixed $optval
* @return self $this (chainable)
* @throws Exception on error
* @throws \Error PHP 8 only: throws \Error when socket or arguments are invalid
* @see self::getOption()
* @uses socket_set_option()
*/
public function setOption($level, $optname, $optval)
{
$ret = @socket_set_option($this->resource, $level, $optname, $optval);
if ($ret === false) {
throw Exception::createFromSocketResource($this->resource);
}
return $this;
}
/**
* shuts down socket for receiving, sending or both
*
* @param int $how 0 = shutdown reading, 1 = shutdown writing, 2 = shutdown reading and writing
* @return self $this (chainable)
* @throws Exception on error
* @throws \Error PHP 8 only: throws \Error when socket or arguments are invalid
* @see self::close()
* @uses socket_shutdown()
*/
public function shutdown($how = 2)
{
$ret = @socket_shutdown($this->resource, $how);
if ($ret === false) {
throw Exception::createFromSocketResource($this->resource);
}
return $this;
}
/**
* write $buffer to connect()ed / accept()ed socket
*
* @param string $buffer
* @return int number of bytes actually written
* @throws Exception on error
* @throws \Error PHP 8 only: throws \Error when socket or arguments are invalid
* @see self::send() if you need to pass flags
* @uses socket_write()
*/
public function write($buffer)
{
$ret = @socket_write($this->resource, $buffer);
if ($ret === false) {
throw Exception::createFromSocketResource($this->resource);
}
return $ret;
}
/**
* get socket type as passed to socket_create()
*
* @return int usually either SOCK_STREAM or SOCK_DGRAM
* @throws Exception on error
* @throws \Error PHP 8 only: throws \Error when socket is invalid
* @uses self::getOption()
*/
public function getType()
{
return $this->getOption(SOL_SOCKET, SO_TYPE);
}
/**
* assert that this socket is alive and its error code is 0
*
* This will fetch and reset the current socket error code from the
* socket and options and will throw an Exception along with error
* message and code if the code is not 0, i.e. if it does indicate
* an error situation.
*
* Calling this method should not be needed in most cases and is
* likely to not throw an Exception. Each socket operation like
* connect(), send(), etc. will throw a dedicated Exception in case
* of an error anyway.
*
* @return self $this (chainable)
* @throws Exception if error code is not 0
* @throws \Error PHP 8 only: throws \Error when socket is invalid
* @uses self::getOption() to retrieve and clear current error code
* @uses self::getErrorMessage() to translate error code to
*/
public function assertAlive()
{
$code = $this->getOption(SOL_SOCKET, SO_ERROR);
if ($code !== 0) {
throw Exception::createFromCode($code, 'Socket error');
}
return $this;
}
/**
* format given address/host/path and port
*
* @param string $address
* @param int $port
* @return string
*/
protected function formatAddress($address, $port)
{
if ($port !== 0) {
if (strpos($address, ':') !== false) {
$address = '[' . $address . ']';
}
$address .= ':' . $port;
}
return $address;
}
/**
* format given address by splitting it into returned address and port set by reference
*
* @param string $address
* @param int $port
* @return string address with port removed
*/
protected function unformatAddress($address, &$port)
{
// [::1]:2 => ::1 2
// test:2 => test 2
// ::1 => ::1
// test => test
$colon = strrpos($address, ':');
// there is a colon and this is the only colon or there's a closing IPv6 bracket right before it
if ($colon !== false && (strpos($address, ':') === $colon || strpos($address, ']') === ($colon - 1))) {
$port = (int)substr($address, $colon + 1);
$address = substr($address, 0, $colon);
// remove IPv6 square brackets
if (substr($address, 0, 1) === '[') {
$address = substr($address, 1, -1);
}
}
return $address;
}
}