瀏覽代碼

catch exception

walkor 9 年之前
父節點
當前提交
fb53df64d2
共有 7 個文件被更改,包括 208 次插入32 次删除
  1. 18 2
      Connection/AsyncTcpConnection.php
  2. 71 15
      Connection/TcpConnection.php
  3. 19 3
      Events/Ev.php
  4. 10 2
      Events/Libevent.php
  5. 36 4
      Protocols/Websocket.php
  6. 9 1
      WebServer.php
  7. 45 5
      Worker.php

+ 18 - 2
Connection/AsyncTcpConnection.php

@@ -89,7 +89,15 @@ class AsyncTcpConnection extends TcpConnection
     {
         if($this->onError)
         {
-            call_user_func($this->onError, $this, $code, $msg);
+            try
+            {
+                call_user_func($this->onError, $this, $code, $msg);
+            }
+            catch(\Exception $e)
+            {
+                echo $e;
+                exit(250);
+            }
         }
     }
     
@@ -121,7 +129,15 @@ class AsyncTcpConnection extends TcpConnection
             // 如果有设置onConnect回调,则执行
             if($this->onConnect)
             {
-                call_user_func($this->onConnect, $this);
+                try
+                {
+                    call_user_func($this->onConnect, $this);
+                }
+                catch(\Exception $e)
+                {
+                    echo $e;
+                    exit(250);
+                }
             }
         }
         else

+ 71 - 15
Connection/TcpConnection.php

@@ -256,7 +256,15 @@ class TcpConnection extends ConnectionInterface
                     // 如果有设置失败回调,则执行
                     if($this->onError)
                     {
-                        call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'client closed');
+                        try
+                        {
+                            call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'client closed');
+                        }
+                        catch(\Exception $e)
+                        {
+                            echo $e;
+                            exit(250);
+                        }
                     }
                     // 销毁连接
                     $this->destroy();
@@ -281,7 +289,15 @@ class TcpConnection extends ConnectionInterface
                 // 如果有设置失败回调,则执行
                 if($this->onError)
                 {
-                    call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
+                    try
+                    {
+                        call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
+                    }
+                    catch(\Exception $e)
+                    {
+                        echo $e;
+                        exit(250);
+                    }
                 }
                 return false;
             }
@@ -431,8 +447,16 @@ class TcpConnection extends ConnectionInterface
                {
                    continue ;
                }
-               // 处理数据包
-               call_user_func($this->onMessage, $this, $parser::decode($one_request_buffer, $this));
+               try
+               {
+                   // 处理数据包
+                   call_user_func($this->onMessage, $this, $parser::decode($one_request_buffer, $this));
+               }
+                catch(\Exception $e)
+                {
+                    echo $e;
+                    exit(250);
+                }
            }
            return;
         }
@@ -449,7 +473,15 @@ class TcpConnection extends ConnectionInterface
             $this->_recvBuffer = '';
             return ;
         }
-        call_user_func($this->onMessage, $this, $this->_recvBuffer);
+        try
+        {
+            call_user_func($this->onMessage, $this, $this->_recvBuffer);
+        }
+        catch(\Exception $e)
+        {
+            echo $e;
+            exit(250);
+        }
         // 清空缓冲区
         $this->_recvBuffer = '';
     }
@@ -468,7 +500,15 @@ class TcpConnection extends ConnectionInterface
             // 发送缓冲区的数据被发送完毕,尝试触发onBufferDrain回调
             if($this->onBufferDrain)
             {
-                call_user_func($this->onBufferDrain, $this);
+                try
+                {
+                    call_user_func($this->onBufferDrain, $this);
+                }
+                catch(\Exception $e)
+                {
+                    echo $e;
+                    exit(250);
+                }
             }
             // 如果连接状态为关闭,则销毁连接
             if($this->_status === self::STATUS_CLOSING)
@@ -568,7 +608,15 @@ class TcpConnection extends ConnectionInterface
         {
             if($this->onBufferFull)
             {
-                call_user_func($this->onBufferFull, $this);
+                try
+                {
+                    call_user_func($this->onBufferFull, $this);
+                }
+                catch(\Exception $e)
+                {
+                    echo $e;
+                    exit(250);
+                }
             }
         }
     }
@@ -594,14 +642,22 @@ class TcpConnection extends ConnectionInterface
             unset($this->worker->connections[$this->_id]);
         }
         // 标记该连接已经关闭
-       $this->_status = self::STATUS_CLOSED;
-       // 触发onClose回调
-       if($this->onClose)
-       {
-           call_user_func($this->onClose, $this);
-       }
-       // 清理回调,避免内存泄露
-       $this->onMessage = $this->onClose = $this->onError = $this->onBufferFull = $this->onBufferDrain = null;
+        $this->_status = self::STATUS_CLOSED;
+        // 触发onClose回调
+        if($this->onClose)
+        {
+            try
+            {
+               call_user_func($this->onClose, $this);
+            }
+            catch(\Exception $e)
+            {
+                echo $e;
+                exit(250);
+            }
+        }
+        // 清理回调,避免内存泄露
+        $this->onMessage = $this->onClose = $this->onError = $this->onBufferFull = $this->onBufferDrain = null;
     }
     
     /**

+ 19 - 3
Events/Ev.php

@@ -56,13 +56,21 @@ class Ev implements EventInterface
     {
         $callback = function($event,$socket)use($fd,$func)
         {
-            call_user_func($func,$fd);
+            try
+            {
+                call_user_func($func,$fd);
+            }
+            catch(\Exception $e)
+            {
+                echo $e;
+                exit(250);
+            }
         };
 
         switch($flag)
         {
             case self::EV_SIGNAL:
-                $event = new \EvSignal($fd,$callback);
+                $event = new \EvSignal($fd, $callback);
                 $this->_eventSignal[$fd] = $event;
                 return true;
             case self::EV_TIMER:
@@ -136,7 +144,15 @@ class Ev implements EventInterface
             $this->_eventTimer[$timer_id]->stop();
             unset($this->_eventTimer[$timer_id]);
         }
-        call_user_func_array($param[0],$param[1]);
+        try
+        {
+            call_user_func_array($param[0],$param[1]);
+        }
+        catch(\Exception $e)
+        {
+            echo $e;
+            exit(250);
+        }
     }
 
     /**

+ 10 - 2
Events/Libevent.php

@@ -182,8 +182,16 @@ class Libevent implements EventInterface
         {
             event_add($this->_eventTimer[$timer_id][2], $this->_eventTimer[$timer_id][4]);
         }
-        // 执行任务
-        call_user_func_array($this->_eventTimer[$timer_id][0], $this->_eventTimer[$timer_id][1]);
+        try 
+        {
+            // 执行任务
+            call_user_func_array($this->_eventTimer[$timer_id][0], $this->_eventTimer[$timer_id][1]);
+        }
+        catch(\Exception $e)
+        {
+            echo $e;
+            exit(250);
+        }
         if(isset($this->_eventTimer[$timer_id]) && $this->_eventTimer[$timer_id][3] === self::EV_TIMER_ONCE)
         {
             $this->del($timer_id, self::EV_TIMER_ONCE);

+ 36 - 4
Protocols/Websocket.php

@@ -90,7 +90,15 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                     // 如果有设置onWebSocketClose回调,尝试执行
                     if(isset($connection->onWebSocketClose))
                     {
-                        call_user_func($connection->onWebSocketClose, $connection);
+                        try 
+                        {
+                            call_user_func($connection->onWebSocketClose, $connection);
+                        }
+                        catch(\Exception $e)
+                        {
+                            echo $e;
+                            exit(250);
+                        }
                     }
                     // 默认行为是关闭连接
                     else
@@ -103,7 +111,15 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                     // 如果有设置onWebSocketPing回调,尝试执行
                     if(isset($connection->onWebSocketPing))
                     {
-                        call_user_func($connection->onWebSocketPing, $connection);
+                        try 
+                        {
+                            call_user_func($connection->onWebSocketPing, $connection);
+                        }
+                        catch(\Exception $e)
+                        {
+                            echo $e;
+                            exit(250);
+                        }
                     }
                     // 默认发送pong
                     else 
@@ -122,7 +138,15 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                     // 如果有设置onWebSocketPong回调,尝试执行
                     if(isset($connection->onWebSocketPong))
                     {
-                        call_user_func($connection->onWebSocketPong, $connection);
+                        try
+                        {
+                            call_user_func($connection->onWebSocketPong, $connection);
+                        }
+                        catch(\Exception $e)
+                        {
+                            echo $e;
+                            exit(250);
+                        }
                     }
                     // 从接受缓冲区中消费掉该数据包
                     if(!$data_len)
@@ -339,7 +363,15 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
             if(isset($connection->onWebSocketConnect))
             {
                 self::parseHttpHeader($buffer);
-                call_user_func($connection->onWebSocketConnect, $connection, $buffer);
+                try
+                {
+                    call_user_func($connection->onWebSocketConnect, $connection, $buffer);
+                }
+                catch(\Exception $e)
+                {
+                    echo $e;
+                    exit(250);
+                }
                 $_GET = $_COOKIE = $_SERVER = array();
             }
             return 0;

+ 9 - 1
WebServer.php

@@ -103,7 +103,15 @@ class WebServer extends Worker
         // 尝试执行开发者设定的onWorkerStart回调
         if($this->_onWorkerStart)
         {
-            call_user_func($this->_onWorkerStart, $this);
+            try
+            {
+                call_user_func($this->_onWorkerStart, $this);
+            }
+            catch(\Exception $e)
+            {
+                echo $e;
+                exit(250);
+            }
         }
     }
     

+ 45 - 5
Worker.php

@@ -1142,7 +1142,15 @@ class Worker
             // 如果有设置Reload回调,则执行
             if($worker->onWorkerReload)
             {
-                call_user_func($worker->onWorkerReload, $worker);
+                try 
+                {
+                    call_user_func($worker->onWorkerReload, $worker);
+                }
+                catch(\Exception $e)
+                {
+                    echo $e;
+                    exit(250);
+                }
             }
             if($worker->reloadable)
             {
@@ -1484,7 +1492,15 @@ class Worker
         // 如果有设置进程启动回调,则执行
         if($this->onWorkerStart)
         {
-            call_user_func($this->onWorkerStart, $this);
+            try 
+            {
+                call_user_func($this->onWorkerStart, $this);
+            }
+            catch(\Exception $e)
+            {
+                echo $e;
+                exit(250);
+            }
         }
         
         // 子进程主循环
@@ -1500,7 +1516,15 @@ class Worker
         // 如果有设置进程终止回调,则执行
         if($this->onWorkerStop)
         {
-            call_user_func($this->onWorkerStop, $this);
+            try 
+            {
+                call_user_func($this->onWorkerStop, $this);
+            }
+            catch(\Exception $e)
+            {
+                echo $e;
+                exit(250);
+            }
         }
         // 删除相关监听事件,关闭_mainSocket
         self::$globalEvent->del($this->_mainSocket, EventInterface::EV_READ);
@@ -1536,7 +1560,15 @@ class Worker
         // 如果有设置连接回调,则执行
         if($this->onConnect)
         {
-            call_user_func($this->onConnect, $connection);
+            try
+            {
+                call_user_func($this->onConnect, $connection);
+            }
+            catch(\Exception $e)
+            {
+                echo $e;
+                exit(250);
+            }
         }
     }
 
@@ -1565,7 +1597,15 @@ class Worker
                 $recv_buffer = $parser::decode($recv_buffer, $connection);
             }
             ConnectionInterface::$statistics['total_request']++;
-            call_user_func($this->onMessage, $connection, $recv_buffer);
+            try
+            {
+                call_user_func($this->onMessage, $connection, $recv_buffer);
+            }
+            catch(\Exception $e)
+            {
+                echo $e;
+                exit(250);
+            }
         }
     }
 }