-
Notifications
You must be signed in to change notification settings - Fork 1
/
ASocket.php
295 lines (263 loc) · 6.52 KB
/
ASocket.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
<?php
namespace Aza\Components\Socket;
use Aza\Components\Socket\Exceptions\Exception;
/**
* Socket abstraction
*
* @project Anizoptera CMF
* @package system.socket
* @author Amal Samally <amal.samally at gmail.com>
* @license MIT
*/
abstract class ASocket
{
/**
* Unique socket IDs counter
*
* @var int
*/
private static $counter = 0;
/**
* Unique (for current process) socket id
*
* @var int
*/
public $id;
/**
* Socket resource
*
* @var resource
*/
public $resource;
/**
* Socket constructor
*
* @param resource $resource
*
* @throws Exception
*/
public function __construct($resource)
{
if (!is_resource($resource)) {
throw new Exception("Invalid socket resource");
}
$this->resource = $resource;
$this->id = ++self::$counter;
}
/**
* Cleanup
*/
public function __destruct()
{
$this->close();
}
/**
* Reads a maximum of length bytes from a socket
*
* @throws Exception
*
* @param int $length <p>
* The maximum number of bytes read is specified by the
* length parameter.
* </p>
* @param bool $quiet <p>
* Not to throw exception on error.
* </p>
*
* @return string|bool <p>
* Returns the data as a string on success, or FALSE on error
* (including if the remote host has closed the connection).
* Returns a zero length string ("") when there is no more data to read.
* </p>
*/
abstract public function read($length = 4096, $quiet = true);
/**
* Reads a line from a socket
*
* @throws Exception
*
* @param int $length <p>
* The maximum number of bytes read is specified by the
* length parameter. Otherwise you can use \r or \n.
* </p>
* @param bool $quiet <p>
* Not to throw exception on error.
* </p>
*
* @return string|bool <p>
* Returns the data as a string on success, or FALSE on error
* (including if the remote host has closed the connection).
* Returns a zero length string ("") when there is no more data to read.
* </p>
*/
abstract public function readLine($length = 4096, $quiet = true);
/**
* Writes data to a socket (binary-safe)
*
* @throws Exception
*
* @param string $buffer <p>
* The buffer to be written.
* </p>
* @param int $length [optional] <p>
* The optional parameter length can specify an alternate
* length of bytes written to the socket. If this length
* is greater then the buffer length, it is silently
* truncated to the length of the buffer.
* </p>
*
* @return int|bool <p>
* The number of bytes successfully written to the socket or FALSE
* for failure. It is perfectly valid for socket_write to return zero
* which means no bytes have been written.
* </p>
*/
abstract public function write($buffer, $length = null);
/**
* Accepts a connection on a socket
*
* @throws Exception
*
* @param bool $nonBlock Whether to enable nonblocking mode
* @param bool $throw Whether to throw exeption on error
*
* @return self|bool Returns new socket instance or FALSE on failure
*/
abstract public function accept($nonBlock = true, $throw = false);
/**
* Closes a socket resource
*/
abstract public function close();
/**
* Sets nonblocking mode for socket
*
* @throws Exception
*
* @return bool
*/
abstract public function setNonBlock();
/**
* Sets blocking mode for socket
*
* @throws Exception
*
* @return bool
*/
abstract public function setBlock();
/**
* Sets receive timeout period on a socket
*
* @throws Exception
*
* @param int $sec
* @param int $usec
*
* @return bool
*/
abstract public function setRecieveTimeout($sec, $usec = 0);
/**
* Sets send timeout period on a socket
*
* @throws Exception
*
* @param int $sec
* @param int $usec
*
* @return bool
*/
abstract public function setSendTimeout($sec, $usec = 0);
/**
* Sets size of read buffer on the socket
*
* @throws Exception
*
* @param int $buffer Size in bytes
*
* @return bool
*/
abstract public function setReadBuffer($buffer = 0);
/**
* Sets size of write buffer on the socket
*
* @throws Exception
*
* @param int $buffer Size in bytes
*
* @return bool
*/
abstract public function setWriteBuffer($buffer = 0);
/**
* Queries the remote side of the given socket which may either result in host/port
* or in a Unix filesystem path, dependent on its type.
*
* @see socket_getpeername
* @see stream_socket_get_name
*
* @param string $addr <p>
* If the given socket is of type AF_INET or
* AF_INET6, socket_getpeername
* will return the peers (remote) IP address in
* appropriate notation (e.g. 127.0.0.1 or
* fe80::1) in the address
* parameter and, if the optional port parameter is
* present, also the associated port.
* </p>
* <p>
* If the given socket is of type AF_UNIX,
* socket_getpeername will return the Unix filesystem
* path (e.g. /var/run/daemon.sock) in the
* address parameter.
* </p>
*
* @param int $port [optional] <p>
* If given, this will hold the port associated to
* address.
* </p>
*
* @return bool Returns true on success or false on failure. socket_getpeername may also return
* false if the socket type is not any of AF_INET,
* AF_INET6, or AF_UNIX, in which
* case the last socket error code is not updated.
*/
abstract public function getPeer(&$addr, &$port = null);
/**
* Queries the local side of the given socket which may either result in host/port
* or in a Unix filesystem path, dependent on its type
*
* @see socket_getsockname
* @see stream_socket_get_name
*
* @param string $addr <p>
* If the given socket is of type AF_INET
* or AF_INET6, socket_getsockname
* will return the local IP address in appropriate notation (e.g.
* 127.0.0.1 or fe80::1) in the
* address parameter and, if the optional
* port parameter is present, also the associated port.
* </p>
* <p>
* If the given socket is of type AF_UNIX,
* socket_getsockname will return the Unix filesystem
* path (e.g. /var/run/daemon.sock) in the
* address parameter.
* </p>
*
* @param int $port [optional] <p>
* If provided, this will hold the associated port.
* </p>
*
* @return bool Returns true on success or false on failure. socket_getsockname may also return
* false if the socket type is not any of AF_INET,
* AF_INET6, or AF_UNIX, in which
* case the last socket error code is not updated.
*/
abstract public function getLocal(&$addr, &$port = null);
/**
* Debugging method. Returns maximum information about socket
*
* @throws Exception
*
* @return array
*/
abstract public function getInfo();
}