-
Notifications
You must be signed in to change notification settings - Fork 38
/
Copy pathswoole_server.php
225 lines (212 loc) · 7.74 KB
/
swoole_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
<?php
class Server
{
private $serv;
private $mysql;
public function __construct() {
$this->serv = new swoole_server("0.0.0.0", 9501);
$this->serv->set(array(
'worker_num' => 8,
'daemonize' => false,
'max_request' => 10000,
'open_mqtt_protocol' => true,
'dispatch_mode' => 2,
'debug_mode'=> 1
));
$this->serv->on('Start', array($this, 'onStart'));
$this->serv->on('Connect', array($this, 'onConnect'));
$this->serv->on('Receive', array($this, 'onReceive'));
$this->serv->on('Close', array($this, 'onClose'));
$this->serv->on('WorkerStart', array($this, 'onWorkerStart'));
$this->serv->start();
}
public function onStart( $serv ) {
$this->debug("Swoole Server Start");
}
public function onWorkerStart($serv,$worker_id){
// if ($worker_id == 1)
// {
// $serv->tick(1000, function () {
// echo 'test';
// });
// }
}
public function onConnect( $serv, $fd, $from_id ) {
//$serv->send( $fd, "Hello {$fd}!" );
// swoole_timer_tick(1000, function(){
// echo "timeout\n";
// });
}
public function onReceive( swoole_server $serv, $fd, $from_id, $data ) {
// echo "Get Message From Client {$fd}:{$data}\n";
$this->decode_mqtt($data,$serv,$fd);
}
public function onClose( $serv, $fd, $from_id ) {
$this->debug("Client {$fd} close connection");
$client_id = $this->redis_get("client_".$fd);
$this->redis_delete("client_".$fd);
$this->redis_delete("fd_".$client_id);
$this->debug("delete client redis data");
}
public function decode_mqtt($data,$serv,$fd)
{
$this->printstr($data);
$data_len_byte = 1;
$fix_header['data_len'] = $this->getmsglength($data,$data_len_byte);
$this->debug($fix_header['data_len'],"get msg length");
$byte = ord($data[0]);
$fix_header['type'] = ($byte & 0xF0) >> 4;
switch ($fix_header['type'])
{
case 1:
$this->debug("CONNECT");
$resp = chr(32) . chr(2) . chr(0) . chr(0);//转换为二进制返回应该使用chr
$client_info = $this->get_connect_info(substr($data, 2));
$client_id = $client_info['clientId'];
$serv->send($fd, $resp);
$this->debug("Send CONNACK");
break;
case 3:
$this->debug("PUBLISH");
$fix_header['dup'] = ($byte & 0x08) >> 3;
$fix_header['qos'] = ($byte & 0x06) >> 1;
$fix_header['retain'] = $byte & 0x01;
$offset = 2;
$topic = $this->decodeString(substr($data, $offset));
$offset += strlen($topic) + 2;
$msg = substr($data, $offset);
echo "client msg: $topic\n---------------------------------\n$msg\n---------------------------------\n";
$client_id = $this->redis_get("client_".$fd);
break;
case 8:
$this->debug("SUBSCRIBE");
//id有可能是两个字节的,这个需要更多的测试
// $msg_id = ord($data[2]);
$msg_id = ord($data[3]);
$fix_header['sign'] = ($byte & 0x02) >> 1;
$qos = ord($data[$fix_header['data_len']+1]);
if($fix_header['sign']==1)
{
echo "this is subscribe message!!!!\n";
$this->debug($msg_id,"msg id");
$this->debug($qos,"QOS");
//这里没有从协议中读取topic的长度,按照固定的写做6
$topic = substr($data,6,$fix_header['data_len']-1);
$this->debug($topic,"topic");
}
//订阅后返回
$resp = chr(0x90).chr(3).chr(0).chr($msg_id).chr(0);
$this->printstr($resp);
$serv->send($fd,$resp);
$this->debug("send SUBACK");
break;
case 10:
$this->debug("UNSUBSCRIBE");
break;
case 12:
$this->debug("PINGREQ");
$resp = chr(208) . chr(0);//转换为二进制返回应该使用chr
//保存最后ping的时间
$serv->send($fd, $resp);
$this->debug("Send PINGRESP");
break;
case 14:
$this->debug("DISCONNECT");
break;
}
}
public function decodeValue($data)
{
return 256 * ord($data[0]) + ord($data[1]);
}
public function decodeString($data)
{
$length = $this->decodeValue($data);
return substr($data, 2, $length);
}
public function get_connect_info($data)
{
$connect_info['protocol_name'] = $this->decodeString($data);
$offset = strlen($connect_info['protocol_name']) + 2;
$connect_info['version'] = ord(substr($data, $offset, 1));
$offset += 1;
$byte = ord($data[$offset]);
$connect_info['willRetain'] = ($byte & 0x20 == 0x20);
$connect_info['willQos'] = ($byte & 0x18 >> 3);
$connect_info['willFlag'] = ($byte & 0x04 == 0x04);
$connect_info['cleanStart'] = ($byte & 0x02 == 0x02);
$offset += 1;
$connect_info['keepalive'] = $this->decodeValue(substr($data, $offset, 2));
$offset += 2;
$connect_info['clientId'] = $this->decodeString(substr($data, $offset));
return $connect_info;
}
public function debug($str,$title = "Debug")
{
echo "-------------------------------\n";
echo '[' . time() . "] ".$title .':['. $str . "]\n";
echo "-------------------------------\n";
}
public function printstr($string){
$strlen = strlen($string);
for($j=0;$j<$strlen;$j++){
$num = ord($string{$j});
if($num > 31)
$chr = $string{$j}; else $chr = " ";
printf("%4d: %08b : 0x%02x : %s \n",$j,$num,$num,$chr);
}
}
/* getmsglength: */
public function getmsglength(&$msg, &$i){
$multiplier = 1;
$value = 0 ;
do{
$digit = ord($msg{$i});
$value += ($digit & 127) * $multiplier;
$multiplier *= 128;
$i++;
}while (($digit & 128) != 0);
return $value;
}
/* setmsglength: */
public function setmsglength($len){
$string = "";
do{
$digit = $len % 128;
$len = $len >> 7;
// if there are more digits to encode, set the top bit of this digit
if ( $len > 0 )
$digit = ($digit | 0x80);
$string .= chr($digit);
}while ( $len > 0 );
return $string;
}
/* strwritestring: writes a string to a buffer */
public function strwritestring($str, &$i){
$ret = " ";
$len = strlen($str);
$msb = $len >> 8;
$lsb = $len % 256;
$ret = chr($msb);
$ret .= chr($lsb);
$ret .= $str;
$i += ($len+2);
return $ret;
}
/* publish: publishes $content on a $topic */
function send_publish($topic, $content, $qos = 0, $retain = 0)
{
$buffer = "";
$buffer .= $topic;
$buffer .= $content;
$head = " ";
$cmd = 0x30;
$head{0} = chr($cmd);
$head .= $this->setmsglength(strlen($topic)+strlen($content)+2);
echo "+++++++++++++++++++++++++++\n";
$this->printstr($head.chr(0).chr(0x06).$buffer);
echo "+++++++++++++++++++++++++++\n";
return $head.chr(0).chr(0x06).$buffer;
}
}
$server = new Server();