-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathSphpSocket.php
More file actions
265 lines (234 loc) · 6.72 KB
/
SphpSocket.php
File metadata and controls
265 lines (234 loc) · 6.72 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
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
<?php
/**
* Created by PhpStorm.
* User: tioncico
* Date: 18-5-1
* Time: 下午7:56
*/
class SphpSocket
{
private static $_instance;
public $connect_list = array();//客户端列表
public $connect_callback, $receive_callback, $close_callback;//回调函数
public $server;//socket服务
public $is_run=true;//是否运行
public $config = array(//各种配置
'debug'=>true,
'host' => '0.0.0.0',
'port' => '9501',
'domain' => AF_INET,
'type' => SOCK_STREAM,
'protocol' => SOL_TCP,
'accept' => 511,
'option_level' => SOL_SOCKET,
'optname' => SO_REUSEADDR,
'optval' => 1,
'read_length'=>1024,
'read_type'=>PHP_NORMAL_READ
);
public $error_log=array();
public static function getInstance($host='0.0.0.0', $port='9501')
{
if (!(self::$_instance instanceof self)) {
self::$_instance = new static($host, $port);
}
return self::$_instance;
}
public function __construct($host, $port)
{
$this->config['host'] = $host;
$this->config['port'] = $port;
}
/**
* 绑定事件
* @param $type connect|receive|close
* @param callable $function
*/
public function on($type, callable $function)
{
switch (strtolower($type)) {
case 'connect':
$this->connect_callback = $function;
break;
case 'receive':
$this->receive_callback = $function;
break;
case 'close':
$this->close_callback = $function;
break;
}
return $this;
}
public function onConnect($connection){
if (is_callable($this->connect_callback)) {
call_user_func($this->connect_callback,$connection);
}
}
public function onReceive($connection,$data){
if (is_callable($this->receive_callback)) {
call_user_func($this->receive_callback,$connection,$data);
}
}
public function onClose($connection){
if (is_callable($this->close_callback)) {
call_user_func($this->close_callback,$connection);
}
}
/**
*
*/
public function start()
{
$this->createSocket();
echo '创建socket成功!'.PHP_EOL;
$this->bindSocket();
echo '绑定端口成功!'.PHP_EOL;
$this->listenSocket();
echo '监听端口成功!'.PHP_EOL;
$this->setOptionSocket();
$this->acceptSocket();
return $this;
}
/**
* 创建socket
* @return $this
* @throws Exception
*/
protected function createSocket()
{
$this->server = socket_create($this->config['domain'], $this->config['type'], $this->config['protocol']);
if ($this->server === false) {
throw new Exception('创建socket失败!');
}
return $this;
}
/**
* 绑定端口
* @return $this
* @throws Exception
*/
protected function bindSocket()
{
$this->server === false && $this->createSocket();
$result = socket_bind($this->server, $this->config['host'], $this->config['port']);
if ($result === false) {
throw new Exception('绑定端口失败!');
}
return $this;
}
/**
* 监听端口
* @param null $accept
* @return $this
* @throws Exception
*/
protected function listenSocket($accept = null)
{
$this->server === false && $this->createSocket();
$accept || $accept = $this->config['accept'];
$result = socket_listen($this->server, $accept);
if ($result === false) {
throw new Exception('监听端口失败!');
}
return $this;
}
/**
* 配置socket
* @return $this
* @throws Exception
*/
protected function setOptionSocket()
{
$this->server === false && $this->createSocket();
$result = socket_set_option($this->server, $this->config['option_level'], $this->config['optname'], $this->config['optval']);
if ($result === false) {
throw new Exception('配置socket失败!');
}
return $this;
}
/**
* 接收socket连接
*/
protected function acceptSocket(){
$this->server === false && $this->createSocket();
while(true&&$this->is_run===true){
$connection = socket_accept($this->server);
if($connection===false){
}else{
$this->addConnectionList($connection);
$this->onConnect($connection);
$this->forkProcess($connection);
}
}
}
/**
* 写入客户端信息
* @param $connection
* @return $this
*/
protected function addConnectionList($connection){
// $fd =
$this->connect_list[(string)$connection]['fd']=$connection;
return $this;
}
/**
* 写入客户端进程id
* @param $connection
* @param $pid
* @return $this
*/
protected function addConnectionListProcess($connection,$pid){
$this->connect_list[(string)$connection]['pid']=$pid;
return $this;
}
/**
* 派生进程处理
* @param $connection
*/
protected function forkProcess($connection){
echo "准备fork,当前进程id".getmypid()."\n";
$pid = pcntl_fork();
if($pid>0){
$this->addConnectionListProcess($connection,$pid);
$this->readSocket($connection);
}else{
}
}
/**
* 读取socket信息
* @param $connection
*/
protected function readSocket($connection){
while(true&&isset($this->connect_list[(string)$connection])&&$this->is_run){
$data = @socket_read($connection,$this->config['read_length'],$this->config['read_type']);
if($data===false){
$this->close($connection);
}else{
$this->onReceive($connection,$data);
}
}
}
/**
* 发送消息给客户端
* @param $connection
* @param $msg
* @return int
*/
public function send($connection,$msg){
$result = socket_write($connection, $msg,strlen($msg));
return $result;
}
/**
* 主动关闭客户端
* @param $connection
*/
public function close($connection){
$this->onClose($connection);
var_dump($this->connect_list[(string)$connection]['pid']);die;
//先关掉子进程
posix_kill($this->connect_list[(string)$connection]['pid'],SIGTERM);
$result = socket_close($connection);
unset($this->connect_list[(string)$connection]);
return $result;
}
}