1 | <?php |
||
7 | class SocketIO extends AbstractIO |
||
8 | { |
||
9 | /** @var string */ |
||
10 | protected $host; |
||
11 | |||
12 | /** @var int */ |
||
13 | protected $port; |
||
14 | |||
15 | /** @var int */ |
||
16 | protected $timeout; |
||
17 | |||
18 | /** @var resource */ |
||
19 | private $sock; |
||
20 | |||
21 | /** @var bool */ |
||
22 | private $keepalive; |
||
23 | |||
24 | /** |
||
25 | * @param string $host |
||
26 | * @param int $port |
||
27 | * @param int $timeout |
||
28 | * @param bool $keepalive |
||
29 | */ |
||
30 | 25 | public function __construct($host, $port, $timeout, $keepalive = false) |
|
31 | { |
||
32 | 25 | $this->host = $host; |
|
33 | 25 | $this->port = $port; |
|
34 | 25 | $this->timeout = $timeout; |
|
35 | 25 | $this->keepalive = $keepalive; |
|
36 | 25 | } |
|
37 | |||
38 | /** |
||
39 | * Sets up the socket connection |
||
40 | * |
||
41 | * @throws \Exception |
||
42 | */ |
||
43 | 25 | public function connect() |
|
44 | { |
||
45 | 25 | $this->sock = socket_create(AF_INET, SOCK_STREAM, SOL_TCP); |
|
46 | |||
47 | 25 | socket_set_option($this->sock, SOL_SOCKET, SO_RCVTIMEO, array('sec' => $this->timeout, 'usec' => 0)); |
|
48 | 25 | socket_set_option($this->sock, SOL_SOCKET, SO_SNDTIMEO, array('sec' => $this->timeout, 'usec' => 0)); |
|
49 | |||
50 | 25 | if (!socket_connect($this->sock, $this->host, $this->port)) { |
|
51 | $errno = socket_last_error($this->sock); |
||
52 | $errstr = socket_strerror($errno); |
||
53 | throw new AMQPIOException(sprintf( |
||
54 | 'Error Connecting to server (%s): %s', |
||
55 | $errno, |
||
56 | $errstr |
||
57 | ), $errno); |
||
58 | } |
||
59 | |||
60 | 25 | socket_set_block($this->sock); |
|
61 | 25 | socket_set_option($this->sock, SOL_TCP, TCP_NODELAY, 1); |
|
62 | |||
63 | 25 | if ($this->keepalive) { |
|
64 | $this->enable_keepalive(); |
||
65 | } |
||
66 | 25 | } |
|
67 | |||
68 | /** |
||
69 | * @return resource |
||
70 | */ |
||
71 | public function getSocket() |
||
75 | |||
76 | /** |
||
77 | * Reconnects the socket |
||
78 | */ |
||
79 | 10 | public function reconnect() |
|
84 | |||
85 | /** |
||
86 | * @param $n |
||
87 | * @return mixed|string |
||
88 | * @throws \PhpAmqpLib\Exception\AMQPIOException |
||
89 | * @throws \PhpAmqpLib\Exception\AMQPRuntimeException |
||
90 | */ |
||
91 | 25 | public function read($n) |
|
121 | |||
122 | /** |
||
123 | * @param $data |
||
124 | * @return mixed|void |
||
125 | * @throws \PhpAmqpLib\Exception\AMQPIOException |
||
126 | * @throws \PhpAmqpLib\Exception\AMQPRuntimeException |
||
127 | */ |
||
128 | 25 | public function write($data) |
|
161 | |||
162 | 25 | public function close() |
|
169 | |||
170 | /** |
||
171 | * @param $sec |
||
172 | * @param $usec |
||
173 | * @return int|mixed |
||
174 | */ |
||
175 | public function select($sec, $usec) |
||
183 | |||
184 | /** |
||
185 | * @throws \PhpAmqpLib\Exception\AMQPIOException |
||
186 | */ |
||
187 | protected function enable_keepalive() |
||
195 | } |
||
196 |