-
Notifications
You must be signed in to change notification settings - Fork 1
/
Socket.php
284 lines (249 loc) · 6.2 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
<?php
namespace Aza\Components\Socket;
use Aza\Components\Socket\Exceptions\Exception;
// TODO: Cover with tests
// TODO: Add UDP support
/**
* Socket functional helper
*
* @link http://php.net/sockets
* @link http://php.net/stream
*
* @uses sockets
* @uses stream
*
* @project Anizoptera CMF
* @package system.socket
* @author Amal Samally <amal.samally at gmail.com>
* @license MIT
*/
abstract class Socket
{
const TYPE_TCP = 0;
const TYPE_SOCKET = 1;
/**
* Whether to use socket extension.
* Stream extension will be used otherwise.
*/
public static $useSockets = true;
/**
* Whether re-using of listener ports across multiple
* processes is available.
*/
public static $reusePort = false;
/**
* Creates Unix-socket server
*
* @throws Exception
*
* @param string $path Local socket path
* @param bool $group A group name or number
* @param bool $user A user name or number
* @param bool $nonBlock Set nonblocking mode to socket
* @param int $chmod File access mode
*
* @return ASocket
*/
public static function serverUnix($path, $group = false, $user = false,
$nonBlock = true, $chmod = 0770)
{
if ('sock' !== pathinfo($path, PATHINFO_EXTENSION)) {
throw new Exception(
"Unix-socket '{$path}' must has '.sock' extension."
);
}
if (file_exists($path)) {
unlink($path);
}
$socket = self::$useSockets
? SocketReal::server(AF_UNIX, SOCK_STREAM, 0, $path)
: SocketStream::server('unix://' . $path);
$nonBlock && $socket->setNonBlock();
if ($chmod && !chmod($path, $chmod)) {
unlink($path);
throw new Exception(sprintf(
"chmod() to '%o' failed on unix-socket '%s'",
$chmod, $path
));
}
if (false !== $group && !@chgrp($path, $group)) {
unlink($path);
throw new Exception(
"chgrp() to '{$group}' failed on unix-socket '{$path}'"
);
}
if (false !== $group && !@chown($path, $user)) {
unlink($path);
throw new Exception(
"chown() to '{$user}' failed on unix-socket '{$path}'"
);
}
return $socket;
}
/**
* Creates TCP-socket server
*
* @throws Exception
*
* @param string $addr Addresses to bind
* @param int $port Port to bind
* @param bool $reuse Whether to reuse address and port of connection
* @param bool $nonBlock Set nonblocking mode to socket
*
* @return ASocket
*/
public static function server($addr, $port = 0, $reuse = true, $nonBlock = true)
{
$socket = self::$useSockets
? SocketReal::server(AF_INET, SOCK_STREAM, SOL_TCP, $addr, $port, $reuse)
: SocketStream::server("tcp://{$addr}:{$port}");
$nonBlock && $socket->setNonBlock();
return $socket;
}
/**
* Opens socket connection
*
* @see stream_socket_client
*
* @throws Exception
*
* @param string $path Local socket path
* @param bool $nonBlock Whether to enable nonblocking mode
*
* @return ASocket
*/
public static function clientUnix($path, $nonBlock = true)
{
return self::$useSockets
? SocketReal::client(
AF_UNIX, SOCK_STREAM, 0, $path, null, $nonBlock
)
: SocketStream::client(
'unix://' . $path, $nonBlock
);
}
/**
* Opens socket connection
*
* @see stream_socket_client
*
* @throws Exception
*
* @param string $addr Address to connect
* @param int $port Port
* @param bool $nonBlock Whether to enable nonblocking mode
*
* @return ASocket
*/
public static function client($addr, $port = 0, $nonBlock = true)
{
return self::$useSockets
? SocketReal::client(
AF_INET, SOCK_STREAM, SOL_TCP, $addr, $port, $nonBlock
)
: SocketStream::client(
"{$addr}:{$port}", $nonBlock
);
}
/**
* Creates a pair of connected, indistinguishable sockets
*
* @see socket_create_pair
* @see stream_socket_pair
*
* @throws Exception
*
* @param bool $nonBlock
*
* @return ASocket[] Array with two socket instances.
* Default use is: read/write or master/worker.
*/
public static function pair($nonBlock = true)
{
$sockets = self::$useSockets
? SocketReal::pair()
: SocketStream::pair();
if ($nonBlock) {
$sockets[0]->setNonBlock();
$sockets[1]->setNonBlock();
}
return $sockets;
}
/**
* Runs the select() system call on the given arrays of
* sockets with a specified timeout.
*
* It works. But it's very inefficient!
*
* @see socket_select
* @see stream_select
*
* @param ASocket[] $read
* @param ASocket[] $write
* @param ASocket[] $except
* @param int $tv_sec
* @param int $tv_usec
*
* @return int
*/
public static function select(&$read, &$write = null, &$except = null,
$tv_sec = null, $tv_usec = 0)
{
// Preparations
$arrays = array($read, $write, $except);
$o_arrays = $arrays;
foreach ($arrays as &$arr) {
if ($arr) {
/** @var $s ASocket */
foreach ($arr as &$s) {
$s = $s->resource;
}
} else {
$arr = null;
}
}
unset($arr, $s);
$_arrays = $arrays;
// Socket select
$num = self::$useSockets
? SocketReal::select(
$arrays[0], $arrays[1], $arrays[2], $tv_sec, $tv_usec
)
: SocketStream::select(
$arrays[0], $arrays[1], $arrays[2], $tv_sec, $tv_usec
);
// Results processing
if ($num) {
foreach ($arrays as $k => &$selected) {
if ($selected) {
$_selected = array();
$haystack = $_arrays[$k];
$original = $o_arrays[$k];
foreach ($selected as $needle) {
$key = array_search($needle, $haystack, true);
$_selected[] = $original[$key];
}
$selected = $_selected;
}
}
list($read, $write, $except) = $arrays;
} else {
$read = $write = $except = array();
}
return $num;
}
}
//Socket::$useSockets = version_compare(PHP_VERSION, '5.3.1', '>=');
// Currently re-using of listener ports across multiple processes is available
// only in BSD flavour operating systems via SO_REUSEPORT socket option
Socket::$reusePort = false !== stripos(php_uname('s'), 'BSD');
defined('SO_REUSEPORT') || define('SO_REUSEPORT', 0x200);
/**
* Windows flag
*
* It only needed if Anizoptera CMF is not bootstrapped
*
* We can use PHP_OS, but practically,
* testing for DIRECTORY_SEPARATOR is more straightforward.
*/
defined('IS_WIN') || define('IS_WIN', DIRECTORY_SEPARATOR !== '/');