-
Notifications
You must be signed in to change notification settings - Fork 2
/
class-d-asyncsocket.php
57 lines (47 loc) · 1.27 KB
/
class-d-asyncsocket.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
<?php
// Wrapper to make it easier to use `::ready_for_write()` and `::ready_for_read()`
class D_AsyncSocket {
protected $socket;
protected $error = false;
function __construct( $connect_str, $timeout = 10 ) {
$flags = STREAM_CLIENT_CONNECT | STREAM_CLIENT_ASYNC_CONNECT;
$this->socket = @stream_socket_client( $connect_str, $errno, $errorMessage, $timeout, $flags );
if ( ! $this->socket || $errno ) {
$this->error = $errno ? $errno . ' - ' . $errorMessage : false;
}
}
public function error() {
if ( $this->error ) {
return $this->error;
}
if ( ! $this->socket ) {
return true;
}
}
public function ready_for_write() {
$read = array();
$write = array( $this->socket );
$except = array();
return (bool) stream_select( $read, $write, $except, 0 );
}
public function ready_for_read() {
$read = array( $this->socket );
$write = array();
$except = array();
return (bool) stream_select( $read, $write, $except, 0 );
}
public function write( $data, $length ) {
return fwrite( $this->socket, $data, $length );
}
public function read( $length ) {
return fread( $this->socket, $length );
}
public function eof() {
return feof( $this->socket );
}
function __destruct() {
if ( $this->socket ) {
fclose( $this->socket );
}
}
}