Sfoglia il codice sorgente

add worker->protocol && status add eventloop

walkor 9 anni fa
parent
commit
761993a621

+ 2 - 16
Connection/AsyncTcpConnection.php

@@ -89,13 +89,7 @@ class AsyncTcpConnection extends TcpConnection
     {
         if($this->onError)
         {
-            try{
-                call_user_func($this->onError, $this, $code, $msg);
-            }
-            catch(Exception $e)
-            {
-                echo $e;
-            }
+            call_user_func($this->onError, $this, $code, $msg);
         }
     }
     
@@ -127,15 +121,7 @@ class AsyncTcpConnection extends TcpConnection
             // 如果有设置onConnect回调,则执行
             if($this->onConnect)
             {
-                try 
-                {
-                    call_user_func($this->onConnect, $this);
-                }
-                catch(Exception $e)
-                {
-                    self::$statistics['throw_exception']++;
-                    echo $e;
-                }
+                call_user_func($this->onConnect, $this);
             }
         }
         else

+ 7 - 59
Connection/TcpConnection.php

@@ -256,14 +256,7 @@ class TcpConnection extends ConnectionInterface
                     // 如果有设置失败回调,则执行
                     if($this->onError)
                     {
-                        try
-                        {
-                            call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'client closed');
-                        }
-                        catch(Exception $e)
-                        {
-                            echo $e;
-                        }
+                        call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'client closed');
                     }
                     // 销毁连接
                     $this->destroy();
@@ -288,14 +281,7 @@ class TcpConnection extends ConnectionInterface
                 // 如果有设置失败回调,则执行
                 if($this->onError)
                 {
-                    try
-                    {
-                        call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
-                    }
-                    catch(Exception $e)
-                    {
-                        echo $e;
-                    }
+                    call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
                 }
                 return false;
             }
@@ -446,15 +432,7 @@ class TcpConnection extends ConnectionInterface
                    continue ;
                }
                // 处理数据包
-               try
-               {
-                   call_user_func($this->onMessage, $this, $parser::decode($one_request_buffer, $this));
-               }
-               catch(Exception $e)
-               {
-                   self::$statistics['throw_exception']++;
-                   echo $e;
-               }
+               call_user_func($this->onMessage, $this, $parser::decode($one_request_buffer, $this));
            }
            return;
         }
@@ -471,15 +449,7 @@ class TcpConnection extends ConnectionInterface
             $this->_recvBuffer = '';
             return ;
         }
-        try 
-        {
-           call_user_func($this->onMessage, $this, $this->_recvBuffer);
-        }
-        catch(Exception $e)
-        {
-           self::$statistics['throw_exception']++;
-           echo $e;
-        }
+        call_user_func($this->onMessage, $this, $this->_recvBuffer);
         // 清空缓冲区
         $this->_recvBuffer = '';
     }
@@ -498,14 +468,7 @@ class TcpConnection extends ConnectionInterface
             // 发送缓冲区的数据被发送完毕,尝试触发onBufferDrain回调
             if($this->onBufferDrain)
             {
-                try 
-                {
-                    call_user_func($this->onBufferDrain, $this);
-                }
-                catch(Exception $e)
-                {
-                    echo $e;
-                }
+                call_user_func($this->onBufferDrain, $this);
             }
             // 如果连接状态为关闭,则销毁连接
             if($this->_status === self::STATUS_CLOSING)
@@ -605,14 +568,7 @@ class TcpConnection extends ConnectionInterface
         {
             if($this->onBufferFull)
             {
-                try
-                {
-                    call_user_func($this->onBufferFull, $this);
-                }
-                catch(Exception $e)
-                {
-                    echo $e;
-                }
+                call_user_func($this->onBufferFull, $this);
             }
         }
     }
@@ -642,15 +598,7 @@ class TcpConnection extends ConnectionInterface
        // 触发onClose回调
        if($this->onClose)
        {
-           try
-           {
-               call_user_func($this->onClose, $this);
-           }
-           catch (Exception $e)
-           {
-               self::$statistics['throw_exception']++;
-               echo $e;
-           }
+           call_user_func($this->onClose, $this);
        }
        // 清理回调,避免内存泄露
        $this->onMessage = $this->onClose = $this->onError = $this->onBufferFull = $this->onBufferDrain = null;

+ 3 - 10
Events/Libevent.php

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

+ 2 - 9
Events/Select.php

@@ -198,15 +198,8 @@ class Select implements EventInterface
                     $this->_scheduler->insert($timer_id, -$next_run_time);
                 }
                 // 尝试执行任务
-                try
-                {
-                    call_user_func_array($task_data[0], $task_data[1]);
-                }
-                catch(\Exception $e)
-                {
-                    echo $e;
-                }
-                if($task_data[2] === self::EV_TIMER_ONCE)
+                call_user_func_array($task_data[0], $task_data[1]);
+                if(isset($this->_task[$timer_id]) && $task_data[2] === self::EV_TIMER_ONCE)
                 {
                     $this->del($timer_id, self::EV_TIMER_ONCE);
                 }

+ 1 - 8
Protocols/Websocket.php

@@ -339,14 +339,7 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
             if(isset($connection->onWebSocketConnect))
             {
                 self::parseHttpHeader($buffer);
-                try
-                {
-                    call_user_func($connection->onWebSocketConnect, $connection, $buffer);
-                }
-                catch(\Exception $e)
-                {
-                    echo $e;
-                }
+                call_user_func($connection->onWebSocketConnect, $connection, $buffer);
                 $_GET = $_COOKIE = $_SERVER = array();
             }
             return 0;

+ 21 - 32
Worker.php

@@ -34,7 +34,7 @@ class Worker
      * 版本号
      * @var string
      */
-    const VERSION = '3.2.6';
+    const VERSION = '3.2.7';
     
     /**
      * 状态 启动中
@@ -195,7 +195,7 @@ class Worker
      * 例如 new worker('http://0.0.0.0:8080');指定使用http协议
      * @var string
      */
-    protected $_protocol = '';
+    public $protocol = '';
     
     /**
      * 当前worker实例初始化目录位置,用于设置应用自动加载的根目录
@@ -446,6 +446,13 @@ class Worker
             {
                 $worker->user = self::getCurrentUser();
             }
+            else 
+            {
+                if(posix_getuid() !== 0 && $worker->user != self::getCurrentUser())
+                {
+                    self::log('Waring: You must have the root privileges to change uid and gid.', true);
+                }
+            }
             $user_name_length = strlen($worker->user);
             if(self::$_maxUserNameLength < $user_name_length)
             {
@@ -913,11 +920,7 @@ class Worker
         // set uid and gid
         if($uid != posix_getuid() || $gid != posix_getgid())
         {
-            if (posix_getuid() != 0)
-            {
-                self::log('Waring: You must have the root privileges to change uid and gid.', true);
-            }
-            elseif(!posix_setgid($gid) || !posix_initgroups($user_info['name'], $gid) || !posix_setuid($uid))
+            if(!posix_setgid($gid) || !posix_initgroups($user_info['name'], $gid) || !posix_setuid($uid))
             {
                 self::log( "Waring: change gid or uid fail.", true);
             }
@@ -1153,7 +1156,8 @@ class Worker
             file_put_contents(self::$_statisticsFile, "---------------------------------------GLOBAL STATUS--------------------------------------------\n");
             file_put_contents(self::$_statisticsFile, 'Workerman version:' . Worker::VERSION . "          PHP version:".PHP_VERSION."\n", FILE_APPEND);
             file_put_contents(self::$_statisticsFile, 'start time:'. date('Y-m-d H:i:s', self::$_globalStatistics['start_timestamp']).'   run ' . floor((time()-self::$_globalStatistics['start_timestamp'])/(24*60*60)). ' days ' . floor(((time()-self::$_globalStatistics['start_timestamp'])%(24*60*60))/(60*60)) . " hours   \n", FILE_APPEND);
-            file_put_contents(self::$_statisticsFile, 'load average: ' . implode(", ", $loadavg) . "\n", FILE_APPEND);
+            $load_str = 'load average: ' . implode(", ", $loadavg);
+            file_put_contents(self::$_statisticsFile, str_pad($load_str, 33) . 'event-loop:'.(extension_loaded('libevent') ? 'libevent' : 'select')."\n", FILE_APPEND);
             file_put_contents(self::$_statisticsFile,  count(self::$_pidMap) . ' workers       ' . count(self::getAllWorkerPids())." processes\n", FILE_APPEND);
             file_put_contents(self::$_statisticsFile, str_pad('worker_name', self::$_maxWorkerNameLength) . " exit_status     exit_count\n", FILE_APPEND);
             foreach(self::$_pidMap as $worker_id =>$worker_pid_array)
@@ -1323,11 +1327,11 @@ class Worker
         if(!isset(self::$_builtinTransports[$scheme]))
         {
             $scheme = ucfirst($scheme);
-            $this->_protocol = '\\Protocols\\'.$scheme;
-            if(!class_exists($this->_protocol))
+            $this->protocol = '\\Protocols\\'.$scheme;
+            if(!class_exists($this->protocol))
             {
-                $this->_protocol = "\\Workerman\\Protocols\\$scheme";
-                if(!class_exists($this->_protocol))
+                $this->protocol = "\\Workerman\\Protocols\\$scheme";
+                if(!class_exists($this->protocol))
                 {
                     throw new Exception("class \\Protocols\\$scheme not exist");
                 }
@@ -1487,7 +1491,7 @@ class Worker
         $connection = new TcpConnection($new_socket, $remote_address);
         $this->connections[$connection->id] = $connection;
         $connection->worker = $this;
-        $connection->protocol = $this->_protocol;
+        $connection->protocol = $this->protocol;
         $connection->onMessage = $this->onMessage;
         $connection->onClose = $this->onClose;
         $connection->onError = $this->onError;
@@ -1497,15 +1501,7 @@ class Worker
         // 如果有设置连接回调,则执行
         if($this->onConnect)
         {
-            try
-            {
-                call_user_func($this->onConnect, $connection);
-            }
-            catch(Exception $e)
-            {
-                ConnectionInterface::$statistics['throw_exception']++;
-                self::log($e);
-            }
+            call_user_func($this->onConnect, $connection);
         }
     }
 
@@ -1526,21 +1522,14 @@ class Worker
         $connection = new UdpConnection($socket, $remote_address);
         if($this->onMessage)
         {
-            if($this->_protocol)
+            if($this->protocol)
             {
                 /** @var \Workerman\Protocols\ProtocolInterface $parser */
-                $parser = $this->_protocol;
+                $parser = $this->protocol;
                 $recv_buffer = $parser::decode($recv_buffer, $connection);
             }
             ConnectionInterface::$statistics['total_request']++;
-            try
-            {
-               call_user_func($this->onMessage, $connection, $recv_buffer);
-            }
-            catch(Exception $e)
-            {
-                ConnectionInterface::$statistics['throw_exception']++;
-            }
+            call_user_func($this->onMessage, $connection, $recv_buffer);
         }
     }
 }