forked from bladeofsteel/CThread
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CSocket.php
executable file
·165 lines (155 loc) · 3.96 KB
/
CSocket.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
<?php
/**
* Class wrapper for socket functional
*
* @see http://php.net/sockets
* @see http://php.net/stream
*
* @project Anizoptera CMF
* @package system.socket
* @version $Id: CSocket.php 2787 2011-11-16 13:24:24Z samally $
*/
abstract class CSocket
{
/**
* Whether to use socket extension.
* Stream extension will be used otherwise.
*/
public static $useSocket = true;
/**
* Creates a pair of connected, indistinguishable sockets
*
* @see socket_create_pair
* @see stream_socket_pair
*
* @throws AzaSystemException
*
* @return resource[] Array with two socket resources. Default use is: read, write.
*/
public static function pair()
{
if (self::$useSocket) {
// On Windows we need to use AF_INET
$domain = IS_WIN ? AF_INET : AF_UNIX;
if (!socket_create_pair($domain, SOCK_STREAM, 0, $sockets)) {
$errno = socket_last_error();
$error = socket_strerror($errno);
throw new AzaException("Can't create socket pair. Reason ($errno): $error", 1);
}
} else {
// On Windows we need to use PF_INET
$domain = IS_WIN ? STREAM_PF_INET : STREAM_PF_UNIX;
if (!$sockets = stream_socket_pair($domain, STREAM_SOCK_STREAM, 0)) {
throw new AzaException('Can\'t create stream socket pair.', 1);
}
}
return $sockets;
}
/**
* Reads a maximum of length bytes from a socket
*
* @see socket_read
* @see fread
*
* @param resource $socket <p>
* A valid socket resource
* </p>
* @param int $length <p>
* The maximum number of bytes read is specified by the
* length parameter.
* </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>
*/
public static function read($socket, $length)
{
if (self::$useSocket) {
return socket_read($socket, $length, PHP_BINARY_READ);
} else {
return fread($socket, $length);
}
}
/**
* Reads a line from a socket
*
* @see socket_read
* @see fgets
*
* @param resource $socket <p>
* A valid socket resource
* </p>
* @param int $length <p>
* The maximum number of bytes read is specified by the
* length parameter. Otherwise you can use \r or \n.
* </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>
*/
public static function readLine($socket, $length)
{
if (self::$useSocket) {
return socket_read($socket, $length, PHP_NORMAL_READ);
} else {
return fgets($socket, $length);
}
}
/**
* Writes data to a socket (binary-safe)
*
* @see socket_write
* @see fwrite
*
* @param resource $socket A valid socket resource
* @param string $buffer The buffer to be written.
* @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>
*/
public static function write($socket, $buffer, $length = null)
{
if (self::$useSocket) {
if ($length === null) {
return socket_write($socket, $buffer);
}
return socket_write($socket, $buffer, $length);
} else {
if ($length === null) {
return fwrite($socket, $buffer);
}
return fwrite($socket, $buffer, $length);
}
}
/**
* Closes a socket resource
*
* @see socket_close
* @see fclose
*
* @param resource $socket A valid socket resource
*/
public static function close($socket)
{
if (self::$useSocket) {
socket_close($socket);
} else {
fclose($socket);
}
}
}