forked from wosiwo/KeywordFilteringService
/
Server.php
274 lines (249 loc) · 5.78 KB
/
Server.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
<?php
namespace Swoole;
use Swoole;
abstract class Server implements Server\Driver
{
public $setting = array();
/**
* @var Swoole\IFace\Protocol
*/
public $protocol;
public $host = '0.0.0.0';
public $port;
public $timeout;
public $buffer_size = 8192;
public $write_buffer_size = 2097152;
public $server_block = 0; //0 block,1 noblock
public $client_block = 0; //0 block,1 noblock
//最大连接数
public $max_connect=1000;
public $client_num = 0;
//客户端socket列表
public $client_sock;
public $server_sock;
/**
* 文件描述符
* @var array
*/
public $fds = array();
function __construct($host, $port, $timeout=30)
{
$this->host = $host;
$this->port = $port;
$this->timeout = $timeout;
}
function addListener($host, $port, $type)
{
if (!($this->protocol instanceof Swoole\Network\Server))
{
throw new \Exception("addListener must use swoole extension.");
}
}
/**
* 应用协议
* @param $protocol Swoole\Protocol\Base
* @return null
*/
function setProtocol($protocol)
{
if (!($protocol instanceof Swoole\IFace\Protocol))
{
throw new \Exception("The protocol is not instanceof \\Swoole\\IFace\\Protocol");
}
$this->protocol = $protocol;
$protocol->server = $this;
}
function connection_info($fd)
{
$peername = stream_socket_get_name($this->fds[$fd], true);
list($ip, $port) = explode(':', $peername);
return array('remote_port' => $port, 'remote_ip' => $ip);
}
/**
* 接受连接
* @return bool|int
*/
function accept()
{
$client_socket = stream_socket_accept($this->server_sock, 0);
//惊群
if ($client_socket === false)
{
return false;
}
$client_socket_id = (int)$client_socket;
stream_set_blocking($client_socket, $this->client_block);
$this->client_sock[$client_socket_id] = $client_socket;
$this->client_num++;
if($this->client_num > $this->max_connect)
{
Network\Stream::close($client_socket);
return false;
}
else
{
//设置写缓冲区
stream_set_write_buffer($client_socket, $this->write_buffer_size);
return $client_socket_id;
}
}
function spawn($setting)
{
$num = 0;
if (isset($setting['worker_num']))
{
$num = (int) $setting['worker_num'];
}
if ($num < 2)
{
return;
}
if (!extension_loaded('pcntl'))
{
die(__METHOD__." require pcntl extension!");
}
$pids = array();
for($i=0; $i<$num; $i++)
{
$pid = pcntl_fork();
if($pid > 0)
{
$pids[] = $pid;
}
else
{
break;
}
}
return $pids;
}
function startWorker()
{
}
abstract function run($setting);
/**
* 发送数据到客户端
* @param $client_id
* @param $data
* @return bool
*/
abstract function send($client_id, $data);
/**
* 关闭连接
* @param $client_id
* @return mixed
*/
abstract function close($client_id);
abstract function shutdown();
function daemonize()
{
if (!function_exists('pcntl_fork'))
{
throw new \Exception(__METHOD__.": require pcntl_fork.");
}
$pid = pcntl_fork();
if ($pid == -1) {
die("fork(1) failed!\n");
}
elseif ($pid > 0)
{
//让由用户启动的进程退出
exit(0);
}
//建立一个有别于终端的新session以脱离终端
posix_setsid();
$pid = pcntl_fork();
if ($pid == -1)
{
die("fork(2) failed!\n");
}
elseif ($pid > 0)
{
//父进程退出, 剩下子进程成为最终的独立进程
exit(0);
}
}
function onError($errno,$errstr)
{
exit("$errstr ($errno)");
}
/**
* 创建一个Stream Server Socket
* @param $uri
* @return unknown_type
*/
function create($uri, $block=0)
{
//UDP
if($uri{0}=='u') $socket = stream_socket_server($uri,$errno,$errstr,STREAM_SERVER_BIND);
//TCP
else $socket = stream_socket_server($uri,$errno,$errstr);
if(!$socket) $this->onError($errno,$errstr);
//设置socket为非堵塞或者阻塞
stream_set_blocking($socket, $block);
return $socket;
}
function create_socket($uri, $block=false)
{
$set = parse_url($uri);
if($uri{0}=='u') $sock = socket_create(AF_INET, SOCK_DGRAM , SOL_UDP);
else $sock = socket_create(AF_INET, SOCK_STREAM , SOL_TCP);
if($block) socket_set_block($sock);
else socket_set_nonblock($sock);
socket_bind($sock,$set['host'],$set['port']);
socket_listen($sock);
return $sock;
}
function sendData($sock, $data)
{
return Network\Stream::write($sock, $data);
}
function log($log)
{
echo $log,NL;
}
}
function sw_run($cmd)
{
if(PHP_OS=='WINNT') pclose(popen("start /B ".$cmd,"r"));
else exec($cmd." > /dev/null &");
}
function sw_gc_array($array)
{
$new = array();
foreach($array as $k=>$v)
{
$new[$k] = $v;
unset($array[$k]);
}
unset($array);
return $new;
}
interface TCP_Server_Driver
{
function run($num=1);
function send($client_id,$data);
function close($client_id);
function shutdown();
function setProtocol($protocol);
}
interface UDP_Server_Driver
{
function run($num=1);
function shutdown();
function setProtocol($protocol);
}
interface TCP_Server_Protocol
{
function onStart();
function onConnect($client_id);
function onReceive($client_id, $data);
function onClose($client_id);
function onShutdown($server);
}
interface UDP_Server_Protocol
{
function onStart();
function onData($peer,$data);
function onShutdown();
}