-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinit.php
More file actions
102 lines (93 loc) · 2.68 KB
/
init.php
File metadata and controls
102 lines (93 loc) · 2.68 KB
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
<?php
const HOST = '127.0.0.1';
const PORT = 10932;
const BACKLOG = 100;
const CRLF = "\r\n";
const MAXLEN = 1 * 1024 * 5; //5KB
function write($fd, $data)
{
while (!empty($data)) {
$written = socket_write($fd, $data, strlen($data));
if ($written === false) {
$errno = socket_last_error($fd);
if ($errno == 4 || $errno == 35) continue;
getErrmsg($fd);
return false;
}
$data = substr($data, $written);
}
return strlen($data);
}
/** 客户端在阻塞模式下等待服务端的响应
* 客户端在阻塞模式下读取服务端的响应是不会有问题的,
* 但是如果在非阻塞模式下读取,如果没有while(empty($reply))
* 就会出现问题,数据会发现串起来的现象。 */
function read($fd)
{
$reply = $buf = '';
while (empty($reply)) {
if (($buf = socket_read($fd, MAXLEN)) === false) {
$errno = socket_last_error($fd);
if ($errno == 4 || $errno == 35) continue;
getErrmsg($fd);
unset($buf, $reply);
free($fd);
return false;
}
// 连接已关闭,这就是read函数有问题的地方。
if ($buf === '' || $buf === -1) {
echo 'buf:' . $buf . PHP_EOL;
return false;
}
$reply .= $buf;
}
return $reply;
}
/** 服务端读取客户端的命令 */
function readNormal($fd)
{
$reply = $buf = '';
while (substr($buf, -1, 1) !== "\n") {
if (($buf = socket_read($fd, 1024, PHP_NORMAL_READ)) === false) {
$errno = socket_last_error($fd);
if ($errno == 4 || $errno == 35) continue;
getErrmsg($fd);
unset($buf, $reply);
free($fd);
return false;
}
// 连接已关闭,这就是read函数有问题的地方。
if ($buf === '' || $buf === -1) {
echo 'buf:' . $buf . PHP_EOL;
return false;
}
$reply .= $buf;
}
return $reply;
}
function getErrmsg($fd = null)
{
$errno = socket_last_error($fd);
$msg = socket_strerror($errno);
printf("errno:%s, errmsg:%s \n", $errno, $msg);
}
function freeSockets(array $clients)
{
return array_walk($clients, function ($c) {
printf("关闭连接:%s \n", $c);
socket_close($c);
});
}
function release($cfd, &$clients)
{
if ($clients && ($index = array_search($cfd, $clients)) !== false) {
unset($clients[$index]);
printf("关闭连接:%s \n", $cfd);
is_resource($cfd) && socket_close($cfd);
}
}
function free($fd)
{
printf("关闭连接:%s \n", $fd);
is_resource($fd) && socket_close($fd);
}