-
Notifications
You must be signed in to change notification settings - Fork 121
/
Copy pathDqServer.php
255 lines (228 loc) · 8.82 KB
/
DqServer.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
<?php
include_once 'DqLoader.php';
class DqServer{
private $fd = null;
private $clientsObjects=array(); //所有客户端对象
public function createClientsObject($newfd){
if(is_resource($newfd)) {
if (count($this->clientsObjects) >= DqConf::$max_connection) {
DqLog::writeLog('over max connection ' . DqConf::$max_connection . ',will reject', DqLog::LOG_TYPE_EXCEPTION);
socket_close($newfd);
return false;
}
list($ip, $port) = $this->getIpPortFromFd($newfd);
$clients = array(
'fd' => $newfd,
'ip' => $ip,
'port' => $port,
'create_time' => date('Y-m-d H:i:s'),
);
$key = $this->getClientKey($newfd);
$this->clientsObjects[$key] = $clients;
self::wirteLog("client_create {$ip}:{$port},clients=" . count($this->clientsObjects).',maxfd='.intval($newfd));
}
}
//根据fd获取在对象数组中的下标
private function getClientKey($newfd){
$num = intval($newfd);
return sprintf('fd:%s',$num);
}
//删除对象
public function delClientsObject($newfd){
if(is_resource($newfd)) {
socket_close($newfd);
}
$key = $this->getClientKey($newfd);
if(isset($this->clientsObjects[$key])) {
unset($this->clientsObjects[$key]);
DqLog::writeLog('now_clients,clients='.count($this->clientsObjects));
}
}
//序列化对象
private function clientToStr($newfd){
$key = $this->getClientKey($newfd);
$clientObject = $this->clientsObjects[$key];
unset($clientObject['fd']);
$strClients = json_encode($clientObject);
return $strClients;
}
//获取所有客户端连接的fd
public function getAllClientsFd(){
$fds = array();
$time = time();
foreach ($this->clientsObjects as $v){
$cfd = $v['fd'];
$key = $this->getClientKey($cfd);
$client = isset($this->clientsObjects[$key]) ? $this->clientsObjects[$key] : array();
//超过5分钟断开连接
if(!empty($client) && ($time-strtotime($client['create_time']))>300){
$this->delClientsObject($cfd);
$strMsg = sprintf('clients over max time,will disconnect,ip=%s:%s',$client['ip'],$client['port']);
DqLog::writeLog($strMsg,DqLog::LOG_TYPE_EXCEPTION);
continue;
}
array_push($fds,$cfd);
}
return $fds;
}
//从数组删除一个元素,并返回删除后数组
public function array_del($element,$arr){
$key = array_search($element,$arr);
if($key!==false){
unset($arr[$key]);
}
return $arr;
}
public function run(){
try {
$fd = $this->get_fd();
if($fd===false){
throw new DqException('socket error,will exitd');
}
list($ip,$port) = $this->getIpPortFromFd($fd);
self::wirteLog("listen on $ip:$port,warting...");
//线上不能使用echo输出,会导致EIO报错,程序异常退出
//echo "listen on $ip:$port,warting..."."\n";
DqMain::install_sig_usr1();
//SIGPIPE信号忽略
pcntl_signal(SIGPIPE, SIG_IGN, false);
while(true){
try{
pcntl_signal_dispatch();
if(DqMain::$stop){
DqRedis::incr_force(); //刷新统计数据
DqMain::sig_stop_check();
}
$read = array_merge(array($this->fd),$this->getAllClientsFd());
@socket_select($read, $write, $exce, DqConf::get_socket_select_timeout());
//刷新数据
if (count($read) == 0) {
DqRedis::incr_force(); //刷新统计数据
}
//检测是否有新的链接过来
$this->check_new_connection($read);
$read = $this->array_del($this->fd, $read);
//处理请求
$this->handle_request($read);
}catch (Exception $e){
self::wirteLog($e->getMessage(),DqLog::LOG_TYPE_EXCEPTION);
}
}
}catch (DqException $e){
self::wirteLog($e->getDqMessage());
}
}
public function handle_request($fdsRead){
foreach ($fdsRead as $cfd){
$arrMsg = DqComm::socket_read($cfd);
if($arrMsg===false){ //异常: 连接关闭,数据格式不对
$this->delClientsObject($cfd);
}else{
$this->handle_msg($cfd,$arrMsg);
}
}
}
//处理消息
public function handle_msg($cfd,$arr){
if(!is_resource($cfd)){
return false;
}
switch ($arr['cmd']) {
case 'add':
if(DqRedis::handle($arr)) {
$this->sendReply($cfd, 1, 'succ');
}else{
$this->sendReply($cfd, 0, 'fail');
}
break;
case 'del':
$id = $arr['id'];
$topic = $arr['topic'];
$tid = DqRedis::create_tid($topic,$id);
$ret = DqRedis::delMsg($topic,$tid,true);
if($ret){
$this->sendReply($cfd,1,'succ');
}else{
$this->sendReply($cfd,0,'fail');
}
break;
case 'get':
$id = $arr['id'];
$topic = $arr['topic'];
$tid = DqRedis::create_tid($topic,$id);
$result = DqRedis::getBody($tid);
if(!empty($result)) {
$this->sendReply($cfd, 1, 'succ',$result);
}else{
$this->sendReply($cfd, 0, 'fail');
}
break;
default:
break;
}
}
public function sendReply($fd,$code,$msg,$data=array()){
$reply = array('code'=>$code,'msg'=>$msg,'data'=>$data);
if(DqComm::socket_wirte($fd,$reply) ===false){ //如果写入异常
$this->delClientsObject($fd);
DqLog::writeLog('response error,msg='.json_encode($reply).' clients='.count($this->clientsObjects),DqLog::LOG_TYPE_EXCEPTION);
}
}
public function check_new_connection(&$read){
if(in_array($this->get_fd(),$read)){
$newfd = socket_accept($this->get_fd());
if(is_resource($newfd)){
$this->createClientsObject($newfd);
}else{
DqLog::writeLog('socket_accept failed');
$errorcode = socket_last_error();
$errormsg = socket_strerror($errorcode);
DqLog::writeLog('socket_accept failed,msg='.$errormsg,DqLog::LOG_TYPE_EXCEPTION);
}
}
}
public function getIpPortFromFd($fd){
try {
if ($this->fd !== $fd) {
socket_getpeername($fd, $ip, $port);
} else {
socket_getsockname($fd, $ip, $port);
}
return array($ip, $port);
}catch (Exception $e){
self::wirteLog($e->getMessage().$e->getLine());
}
}
//创建服务端socket
private function get_fd(){
if(!is_null($this->fd)){
return $this->fd;
}
try {
if (($sfd = socket_create(AF_INET, SOCK_STREAM, SOL_TCP)) === false) {
throw new DqException("socket_create() failed reason: " . socket_strerror(socket_last_error()));
}
if (socket_bind($sfd,DqConf::getLocalHost(), DqConf::getListenPort()) === false) {
socket_close($sfd);
throw new DqException("socket_bind() failed reason: " . socket_strerror(socket_last_error()).',');
}
if (socket_listen($sfd, DqConf::getListenQueueLen()) === false) {
socket_close($sfd);
throw new DqException('socket_listen failed reason:'.socket_strerror(socket_last_error()));
}
if (!socket_set_option($sfd, SOL_SOCKET, SO_REUSEADDR, 1)) {
socket_close($sfd);
throw new DqException('socket_set_option failed reason:'.socket_strerror(socket_last_error()));
}
$this->fd = $sfd;
return $sfd;
}catch (DqException $e){
self::wirteLog($e->getDqMessage(),DqLog::LOG_TYPE_EXCEPTION);
}
return false;
}
//日志接口
public static function wirteLog($strMsg,$flag=DqLog::LOG_TYPE_NORMAL){
DqLog::writeLog($strMsg,$flag);
}
}