-
Notifications
You must be signed in to change notification settings - Fork 0
/
Socket.php
247 lines (196 loc) · 6.1 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
<?php
/**
* Socket 处理类 基于websocket version 13
*
* */
class Socket
{
const READ_BUFFER_SIZE = 65535;
protected $mainSocket;
protected $context;
protected $socketName;
protected $sendBuffer;
protected $eventBase;
protected $event;
protected static $connectPools = [];
public $reusePort = false;
// 构造方法
public function __construct($socketName, $context = [])
{
$this->socketName = $socketName;
$this->context = stream_context_create($context);
$this->event = new Events();
}
public function start()
{
if ($this->reusePort) {
stream_context_set_option($this->context, 'socket', 'so_reuseport', 1);
}
$local_socket = $this->socketName;
$errorNo = 0;
$errorMsg = '';
$this->mainSocket = stream_socket_server($local_socket, $errorNo, $errorMsg, STREAM_SERVER_BIND | STREAM_SERVER_LISTEN, $this->context);
if (function_exists('socket_import_stream')) {
$socket = socket_import_stream($this->mainSocket);
@socket_set_option($socket, SOL_SOCKET, SO_KEEPALIVE, 1);
@socket_set_option($socket, SOL_TCP, TCP_NODELAY, 1);
}
stream_set_blocking($this->mainSocket, 0);
if (function_exists('stream_set_read_buffer')) {
stream_set_read_buffer($this->mainSocket, 0);
}
$flags = \Event::READ;
$this->event->add($this->mainSocket, $flags, [$this, 'accept']);
$this->event->loop();
}
public function baseRead($socket)
{
$this->connect($socket);
$buffer = fread($socket, self::READ_BUFFER_SIZE);
if ($buffer === '' || $buffer === false) {
$this->disconnect($socket);
return;
}
if (false === self::$connectPools[(int)$socket]['handshake']) {
self::$connectPools[(int)$socket]['handshake'] = $this->toHandshake($socket, $buffer);
} else {
$buffer = $this->decode($buffer);
$this->send($socket, $buffer);
}
}
public function accept($_socket)
{
$socket = @stream_socket_accept($_socket, 0, $remote_address);
if (!$socket) {
return;
}
stream_set_blocking($socket, 0);
if (function_exists('stream_set_read_buffer')) {
stream_set_read_buffer($socket, 0);
}
$flags = \Event::READ;
$this->event->add($socket, $flags, [$this, 'baseRead']);
}
public function baseError($buffer, $error, $id)
{
}
//打包函数 返回帧处理
protected function frame($buffer)
{
$len = strlen($buffer);
if ($len <= 125) {
return "\x81" . chr($len) . $buffer;
} else if ($len <= 65535) {
return "\x81" . chr(126) . pack("n", $len) . $buffer;
} else {
return "\x81" . char(127) . pack("xxxxN", $len) . $buffer;
}
}
//解码 解析数据帧
protected function decode($buffer)
{
$masks = $data = $decoded = null;
$len = ord($buffer[1]) & 127;
if ($len === 126) {
$masks = substr($buffer, 4, 4);
$data = substr($buffer, 8);
} else if ($len === 127) {
$masks = substr($buffer, 10, 4);
$data = substr($buffer, 14);
} else {
$masks = substr($buffer, 2, 4);
$data = substr($buffer, 6);
}
for ($index = 0; $index < strlen($data); $index++) {
$decoded .= $data[$index] ^ $masks[$index % 4];
}
return $decoded;
}
protected function toHandshake($socket, $buffer)
{
list($resource, $host, $origin, $key) = $this->getHeaders($buffer);
$upgrade = "HTTP/1.1 101 Switching Protocol\r\n" .
"Upgrade: websocket\r\n" .
"Connection: Upgrade\r\n" .
"Sec-WebSocket-Accept: " . $this->calcKey($key) . "\r\n\r\n"; //必须以两个回车结尾
$this->send($socket, $upgrade,false);
return true;
}
public function send($socket, $buffer,$frame = true)
{
if($frame){
$buffer = $this->frame($buffer);
}
$this->sendBuffer = $buffer;
$this->event->add($socket, \Event::WRITE, [$this, 'baseWrite']);
}
public function baseWrite($socket)
{
$len = @fwrite($socket, $this->sendBuffer, 8192);
if ($len === strlen($this->sendBuffer)) {
$this->event->del($socket, \Event::WRITE);
}
}
protected function getHeaders($req)
{
$r = $h = $o = $key = null;
if (preg_match("/GET (.*) HTTP/", $req, $match)) {
$r = $match[1];
}
if (preg_match("/Host: (.*)\r\n/", $req, $match)) {
$h = $match[1];
}
if (preg_match("/Origin: (.*)\r\n/", $req, $match)) {
$o = $match[1];
}
if (preg_match("/Sec-WebSocket-Key: (.*)\r\n/", $req, $match)) {
$key = $match[1];
}
return [$r, $h, $o, $key];
}
protected function calcKey($key)
{
//基于websocket version 13
$accept = base64_encode(sha1($key . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', true));
return $accept;
}
// 当链接
protected function connect($socket)
{
$fd_key = (int)$socket;
if (!isset(self::$connectPools[$fd_key])) {
$connect['handshake'] = false;
$connect['fd'] = $socket;
self::$connectPools[$fd_key] = $connect;
}
}
// 关闭链接
protected function disconnect($socket)
{
$fd_key = (int)$socket;
if (isset(self::$connectPools[$fd_key])) {
unset(self::$connectPools[$fd_key]);
$this->event->del($socket, \Event::READ);
$this->event->del($socket, \Event::WRITE);
@fclose($socket);
}
}
public function test()
{
}
public function test3()
{
}
public function test4()
{
}
public function test5()
{
}
public function test6()
{
}
public function test7()
{
}
}