Skip to content

Commit fb53df6

Browse files
committed
catch exception
1 parent 9e1b4e7 commit fb53df6

File tree

7 files changed

+208
-32
lines changed

7 files changed

+208
-32
lines changed

Connection/AsyncTcpConnection.php

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,15 @@ protected function emitError($code, $msg)
8989
{
9090
if($this->onError)
9191
{
92-
call_user_func($this->onError, $this, $code, $msg);
92+
try
93+
{
94+
call_user_func($this->onError, $this, $code, $msg);
95+
}
96+
catch(\Exception $e)
97+
{
98+
echo $e;
99+
exit(250);
100+
}
93101
}
94102
}
95103

@@ -121,7 +129,15 @@ public function checkConnection($socket)
121129
// 如果有设置onConnect回调,则执行
122130
if($this->onConnect)
123131
{
124-
call_user_func($this->onConnect, $this);
132+
try
133+
{
134+
call_user_func($this->onConnect, $this);
135+
}
136+
catch(\Exception $e)
137+
{
138+
echo $e;
139+
exit(250);
140+
}
125141
}
126142
}
127143
else

Connection/TcpConnection.php

Lines changed: 71 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,15 @@ public function send($send_buffer, $raw = false)
256256
// 如果有设置失败回调,则执行
257257
if($this->onError)
258258
{
259-
call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'client closed');
259+
try
260+
{
261+
call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'client closed');
262+
}
263+
catch(\Exception $e)
264+
{
265+
echo $e;
266+
exit(250);
267+
}
260268
}
261269
// 销毁连接
262270
$this->destroy();
@@ -281,7 +289,15 @@ public function send($send_buffer, $raw = false)
281289
// 如果有设置失败回调,则执行
282290
if($this->onError)
283291
{
284-
call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
292+
try
293+
{
294+
call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
295+
}
296+
catch(\Exception $e)
297+
{
298+
echo $e;
299+
exit(250);
300+
}
285301
}
286302
return false;
287303
}
@@ -431,8 +447,16 @@ public function baseRead($socket)
431447
{
432448
continue ;
433449
}
434-
// 处理数据包
435-
call_user_func($this->onMessage, $this, $parser::decode($one_request_buffer, $this));
450+
try
451+
{
452+
// 处理数据包
453+
call_user_func($this->onMessage, $this, $parser::decode($one_request_buffer, $this));
454+
}
455+
catch(\Exception $e)
456+
{
457+
echo $e;
458+
exit(250);
459+
}
436460
}
437461
return;
438462
}
@@ -449,7 +473,15 @@ public function baseRead($socket)
449473
$this->_recvBuffer = '';
450474
return ;
451475
}
452-
call_user_func($this->onMessage, $this, $this->_recvBuffer);
476+
try
477+
{
478+
call_user_func($this->onMessage, $this, $this->_recvBuffer);
479+
}
480+
catch(\Exception $e)
481+
{
482+
echo $e;
483+
exit(250);
484+
}
453485
// 清空缓冲区
454486
$this->_recvBuffer = '';
455487
}
@@ -468,7 +500,15 @@ public function baseWrite()
468500
// 发送缓冲区的数据被发送完毕,尝试触发onBufferDrain回调
469501
if($this->onBufferDrain)
470502
{
471-
call_user_func($this->onBufferDrain, $this);
503+
try
504+
{
505+
call_user_func($this->onBufferDrain, $this);
506+
}
507+
catch(\Exception $e)
508+
{
509+
echo $e;
510+
exit(250);
511+
}
472512
}
473513
// 如果连接状态为关闭,则销毁连接
474514
if($this->_status === self::STATUS_CLOSING)
@@ -568,7 +608,15 @@ protected function checkBufferIsFull()
568608
{
569609
if($this->onBufferFull)
570610
{
571-
call_user_func($this->onBufferFull, $this);
611+
try
612+
{
613+
call_user_func($this->onBufferFull, $this);
614+
}
615+
catch(\Exception $e)
616+
{
617+
echo $e;
618+
exit(250);
619+
}
572620
}
573621
}
574622
}
@@ -594,14 +642,22 @@ public function destroy()
594642
unset($this->worker->connections[$this->_id]);
595643
}
596644
// 标记该连接已经关闭
597-
$this->_status = self::STATUS_CLOSED;
598-
// 触发onClose回调
599-
if($this->onClose)
600-
{
601-
call_user_func($this->onClose, $this);
602-
}
603-
// 清理回调,避免内存泄露
604-
$this->onMessage = $this->onClose = $this->onError = $this->onBufferFull = $this->onBufferDrain = null;
645+
$this->_status = self::STATUS_CLOSED;
646+
// 触发onClose回调
647+
if($this->onClose)
648+
{
649+
try
650+
{
651+
call_user_func($this->onClose, $this);
652+
}
653+
catch(\Exception $e)
654+
{
655+
echo $e;
656+
exit(250);
657+
}
658+
}
659+
// 清理回调,避免内存泄露
660+
$this->onMessage = $this->onClose = $this->onError = $this->onBufferFull = $this->onBufferDrain = null;
605661
}
606662

607663
/**

Events/Ev.php

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,13 +56,21 @@ public function add($fd, $flag, $func, $args=null)
5656
{
5757
$callback = function($event,$socket)use($fd,$func)
5858
{
59-
call_user_func($func,$fd);
59+
try
60+
{
61+
call_user_func($func,$fd);
62+
}
63+
catch(\Exception $e)
64+
{
65+
echo $e;
66+
exit(250);
67+
}
6068
};
6169

6270
switch($flag)
6371
{
6472
case self::EV_SIGNAL:
65-
$event = new \EvSignal($fd,$callback);
73+
$event = new \EvSignal($fd, $callback);
6674
$this->_eventSignal[$fd] = $event;
6775
return true;
6876
case self::EV_TIMER:
@@ -136,7 +144,15 @@ public function timerCallback($event)
136144
$this->_eventTimer[$timer_id]->stop();
137145
unset($this->_eventTimer[$timer_id]);
138146
}
139-
call_user_func_array($param[0],$param[1]);
147+
try
148+
{
149+
call_user_func_array($param[0],$param[1]);
150+
}
151+
catch(\Exception $e)
152+
{
153+
echo $e;
154+
exit(250);
155+
}
140156
}
141157

142158
/**

Events/Libevent.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -182,8 +182,16 @@ protected function timerCallback($_null, $_null, $timer_id)
182182
{
183183
event_add($this->_eventTimer[$timer_id][2], $this->_eventTimer[$timer_id][4]);
184184
}
185-
// 执行任务
186-
call_user_func_array($this->_eventTimer[$timer_id][0], $this->_eventTimer[$timer_id][1]);
185+
try
186+
{
187+
// 执行任务
188+
call_user_func_array($this->_eventTimer[$timer_id][0], $this->_eventTimer[$timer_id][1]);
189+
}
190+
catch(\Exception $e)
191+
{
192+
echo $e;
193+
exit(250);
194+
}
187195
if(isset($this->_eventTimer[$timer_id]) && $this->_eventTimer[$timer_id][3] === self::EV_TIMER_ONCE)
188196
{
189197
$this->del($timer_id, self::EV_TIMER_ONCE);

Protocols/Websocket.php

Lines changed: 36 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,15 @@ public static function input($buffer, ConnectionInterface $connection)
9090
// 如果有设置onWebSocketClose回调,尝试执行
9191
if(isset($connection->onWebSocketClose))
9292
{
93-
call_user_func($connection->onWebSocketClose, $connection);
93+
try
94+
{
95+
call_user_func($connection->onWebSocketClose, $connection);
96+
}
97+
catch(\Exception $e)
98+
{
99+
echo $e;
100+
exit(250);
101+
}
94102
}
95103
// 默认行为是关闭连接
96104
else
@@ -103,7 +111,15 @@ public static function input($buffer, ConnectionInterface $connection)
103111
// 如果有设置onWebSocketPing回调,尝试执行
104112
if(isset($connection->onWebSocketPing))
105113
{
106-
call_user_func($connection->onWebSocketPing, $connection);
114+
try
115+
{
116+
call_user_func($connection->onWebSocketPing, $connection);
117+
}
118+
catch(\Exception $e)
119+
{
120+
echo $e;
121+
exit(250);
122+
}
107123
}
108124
// 默认发送pong
109125
else
@@ -122,7 +138,15 @@ public static function input($buffer, ConnectionInterface $connection)
122138
// 如果有设置onWebSocketPong回调,尝试执行
123139
if(isset($connection->onWebSocketPong))
124140
{
125-
call_user_func($connection->onWebSocketPong, $connection);
141+
try
142+
{
143+
call_user_func($connection->onWebSocketPong, $connection);
144+
}
145+
catch(\Exception $e)
146+
{
147+
echo $e;
148+
exit(250);
149+
}
126150
}
127151
// 从接受缓冲区中消费掉该数据包
128152
if(!$data_len)
@@ -339,7 +363,15 @@ protected static function dealHandshake($buffer, $connection)
339363
if(isset($connection->onWebSocketConnect))
340364
{
341365
self::parseHttpHeader($buffer);
342-
call_user_func($connection->onWebSocketConnect, $connection, $buffer);
366+
try
367+
{
368+
call_user_func($connection->onWebSocketConnect, $connection, $buffer);
369+
}
370+
catch(\Exception $e)
371+
{
372+
echo $e;
373+
exit(250);
374+
}
343375
$_GET = $_COOKIE = $_SERVER = array();
344376
}
345377
return 0;

WebServer.php

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,15 @@ public function onWorkerStart()
103103
// 尝试执行开发者设定的onWorkerStart回调
104104
if($this->_onWorkerStart)
105105
{
106-
call_user_func($this->_onWorkerStart, $this);
106+
try
107+
{
108+
call_user_func($this->_onWorkerStart, $this);
109+
}
110+
catch(\Exception $e)
111+
{
112+
echo $e;
113+
exit(250);
114+
}
107115
}
108116
}
109117

Worker.php

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1142,7 +1142,15 @@ protected static function reload()
11421142
// 如果有设置Reload回调,则执行
11431143
if($worker->onWorkerReload)
11441144
{
1145-
call_user_func($worker->onWorkerReload, $worker);
1145+
try
1146+
{
1147+
call_user_func($worker->onWorkerReload, $worker);
1148+
}
1149+
catch(\Exception $e)
1150+
{
1151+
echo $e;
1152+
exit(250);
1153+
}
11461154
}
11471155
if($worker->reloadable)
11481156
{
@@ -1484,7 +1492,15 @@ public function run()
14841492
// 如果有设置进程启动回调,则执行
14851493
if($this->onWorkerStart)
14861494
{
1487-
call_user_func($this->onWorkerStart, $this);
1495+
try
1496+
{
1497+
call_user_func($this->onWorkerStart, $this);
1498+
}
1499+
catch(\Exception $e)
1500+
{
1501+
echo $e;
1502+
exit(250);
1503+
}
14881504
}
14891505

14901506
// 子进程主循环
@@ -1500,7 +1516,15 @@ public function stop()
15001516
// 如果有设置进程终止回调,则执行
15011517
if($this->onWorkerStop)
15021518
{
1503-
call_user_func($this->onWorkerStop, $this);
1519+
try
1520+
{
1521+
call_user_func($this->onWorkerStop, $this);
1522+
}
1523+
catch(\Exception $e)
1524+
{
1525+
echo $e;
1526+
exit(250);
1527+
}
15041528
}
15051529
// 删除相关监听事件,关闭_mainSocket
15061530
self::$globalEvent->del($this->_mainSocket, EventInterface::EV_READ);
@@ -1536,7 +1560,15 @@ public function acceptConnection($socket)
15361560
// 如果有设置连接回调,则执行
15371561
if($this->onConnect)
15381562
{
1539-
call_user_func($this->onConnect, $connection);
1563+
try
1564+
{
1565+
call_user_func($this->onConnect, $connection);
1566+
}
1567+
catch(\Exception $e)
1568+
{
1569+
echo $e;
1570+
exit(250);
1571+
}
15401572
}
15411573
}
15421574

@@ -1565,7 +1597,15 @@ public function acceptUdpConnection($socket)
15651597
$recv_buffer = $parser::decode($recv_buffer, $connection);
15661598
}
15671599
ConnectionInterface::$statistics['total_request']++;
1568-
call_user_func($this->onMessage, $connection, $recv_buffer);
1600+
try
1601+
{
1602+
call_user_func($this->onMessage, $connection, $recv_buffer);
1603+
}
1604+
catch(\Exception $e)
1605+
{
1606+
echo $e;
1607+
exit(250);
1608+
}
15691609
}
15701610
}
15711611
}

0 commit comments

Comments
 (0)