Browse Source

performance optimization

walkor 6 years ago
parent
commit
ef6fe16cff

+ 7 - 7
Autoloader.php

@@ -44,21 +44,21 @@ class Autoloader
      */
     public static function loadByNamespace($name)
     {
-        $class_path = str_replace('\\', DIRECTORY_SEPARATOR, $name);
-        if (strpos($name, 'Workerman\\') === 0) {
-            $class_file = __DIR__ . substr($class_path, strlen('Workerman')) . '.php';
+        $class_path = \str_replace('\\', DIRECTORY_SEPARATOR, $name);
+        if (\strpos($name, 'Workerman\\') === 0) {
+            $class_file = __DIR__ . \substr($class_path, \strlen('Workerman')) . '.php';
         } else {
             if (self::$_autoloadRootPath) {
                 $class_file = self::$_autoloadRootPath . DIRECTORY_SEPARATOR . $class_path . '.php';
             }
-            if (empty($class_file) || !is_file($class_file)) {
+            if (empty($class_file) || !\is_file($class_file)) {
                 $class_file = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . "$class_path.php";
             }
         }
 
-        if (is_file($class_file)) {
+        if (\is_file($class_file)) {
             require_once($class_file);
-            if (class_exists($name, false)) {
+            if (\class_exists($name, false)) {
                 return true;
             }
         }
@@ -66,4 +66,4 @@ class Autoloader
     }
 }
 
-spl_autoload_register('\Workerman\Autoloader::loadByNamespace');
+\spl_autoload_register('\Workerman\Autoloader::loadByNamespace');

+ 24 - 24
Connection/AsyncTcpConnection.php

@@ -111,9 +111,9 @@ class AsyncTcpConnection extends TcpConnection
      */
     public function __construct($remote_address, $context_option = null)
     {
-        $address_info = parse_url($remote_address);
+        $address_info = \parse_url($remote_address);
         if (!$address_info) {
-            list($scheme, $this->_remoteAddress) = explode(':', $remote_address, 2);
+            list($scheme, $this->_remoteAddress) = \explode(':', $remote_address, 2);
             if (!$this->_remoteAddress) {
                 Worker::safeEcho(new \Exception('bad remote_address'));
             }
@@ -142,11 +142,11 @@ class AsyncTcpConnection extends TcpConnection
         }
         // Check application layer protocol class.
         if (!isset(self::$_builtinTransports[$scheme])) {
-            $scheme         = ucfirst($scheme);
+            $scheme         = \ucfirst($scheme);
             $this->protocol = '\\Protocols\\' . $scheme;
-            if (!class_exists($this->protocol)) {
+            if (!\class_exists($this->protocol)) {
                 $this->protocol = "\\Workerman\\Protocols\\$scheme";
-                if (!class_exists($this->protocol)) {
+                if (!\class_exists($this->protocol)) {
                     throw new Exception("class \\Protocols\\$scheme not exist");
                 }
             }
@@ -173,23 +173,23 @@ class AsyncTcpConnection extends TcpConnection
             return;
         }
         $this->_status           = self::STATUS_CONNECTING;
-        $this->_connectStartTime = microtime(true);
+        $this->_connectStartTime = \microtime(true);
         if ($this->transport !== 'unix') {
             // Open socket connection asynchronously.
             if ($this->_contextOption) {
-                $context = stream_context_create($this->_contextOption);
-                $this->_socket = stream_socket_client("tcp://{$this->_remoteHost}:{$this->_remotePort}",
+                $context = \stream_context_create($this->_contextOption);
+                $this->_socket = \stream_socket_client("tcp://{$this->_remoteHost}:{$this->_remotePort}",
                     $errno, $errstr, 0, STREAM_CLIENT_ASYNC_CONNECT, $context);
             } else {
-                $this->_socket = stream_socket_client("tcp://{$this->_remoteHost}:{$this->_remotePort}",
+                $this->_socket = \stream_socket_client("tcp://{$this->_remoteHost}:{$this->_remotePort}",
                     $errno, $errstr, 0, STREAM_CLIENT_ASYNC_CONNECT);
             }
         } else {
-            $this->_socket = stream_socket_client("{$this->transport}://{$this->_remoteAddress}", $errno, $errstr, 0,
+            $this->_socket = \stream_socket_client("{$this->transport}://{$this->_remoteAddress}", $errno, $errstr, 0,
                 STREAM_CLIENT_ASYNC_CONNECT);
         }
         // If failed attempt to emit onError callback.
-        if (!$this->_socket || !is_resource($this->_socket)) {
+        if (!$this->_socket || !\is_resource($this->_socket)) {
             $this->emitError(WORKERMAN_CONNECT_FAIL, $errstr);
             if ($this->_status === self::STATUS_CLOSING) {
                 $this->destroy();
@@ -269,7 +269,7 @@ class AsyncTcpConnection extends TcpConnection
         $this->_status = self::STATUS_CLOSING;
         if ($this->onError) {
             try {
-                call_user_func($this->onError, $this, $code, $msg);
+                \call_user_func($this->onError, $this, $code, $msg);
             } catch (\Exception $e) {
                 Worker::log($e);
                 exit(250);
@@ -301,18 +301,18 @@ class AsyncTcpConnection extends TcpConnection
         }
 
         // Check socket state.
-        if ($address = stream_socket_get_name($this->_socket, true)) {
+        if ($address = \stream_socket_get_name($this->_socket, true)) {
             // Nonblocking.
-            stream_set_blocking($this->_socket, 0);
+            \stream_set_blocking($this->_socket, 0);
             // Compatible with hhvm
-            if (function_exists('stream_set_read_buffer')) {
-                stream_set_read_buffer($this->_socket, 0);
+            if (\function_exists('stream_set_read_buffer')) {
+                \stream_set_read_buffer($this->_socket, 0);
             }
             // Try to open keepalive for tcp and disable Nagle algorithm.
-            if (function_exists('socket_import_stream') && $this->transport === 'tcp') {
-                $raw_socket = socket_import_stream($this->_socket);
-                socket_set_option($raw_socket, SOL_SOCKET, SO_KEEPALIVE, 1);
-                socket_set_option($raw_socket, SOL_TCP, TCP_NODELAY, 1);
+            if (\function_exists('socket_import_stream') && $this->transport === 'tcp') {
+                $raw_socket = \socket_import_stream($this->_socket);
+                \socket_set_option($raw_socket, SOL_SOCKET, SO_KEEPALIVE, 1);
+                \socket_set_option($raw_socket, SOL_TCP, TCP_NODELAY, 1);
             }
 
             // SSL handshake.
@@ -337,7 +337,7 @@ class AsyncTcpConnection extends TcpConnection
             // Try to emit onConnect callback.
             if ($this->onConnect) {
                 try {
-                    call_user_func($this->onConnect, $this);
+                    \call_user_func($this->onConnect, $this);
                 } catch (\Exception $e) {
                     Worker::log($e);
                     exit(250);
@@ -347,9 +347,9 @@ class AsyncTcpConnection extends TcpConnection
                 }
             }
             // Try to emit protocol::onConnect
-            if (method_exists($this->protocol, 'onConnect')) {
+            if (\method_exists($this->protocol, 'onConnect')) {
                 try {
-                    call_user_func(array($this->protocol, 'onConnect'), $this);
+                    \call_user_func(array($this->protocol, 'onConnect'), $this);
                 } catch (\Exception $e) {
                     Worker::log($e);
                     exit(250);
@@ -360,7 +360,7 @@ class AsyncTcpConnection extends TcpConnection
             }
         } else {
             // Connection failed.
-            $this->emitError(WORKERMAN_CONNECT_FAIL, 'connect ' . $this->_remoteAddress . ' fail after ' . round(microtime(true) - $this->_connectStartTime, 4) . ' seconds');
+            $this->emitError(WORKERMAN_CONNECT_FAIL, 'connect ' . $this->_remoteAddress . ' fail after ' . round(\microtime(true) - $this->_connectStartTime, 4) . ' seconds');
             if ($this->_status === self::STATUS_CLOSING) {
                 $this->destroy();
             }

+ 15 - 15
Connection/AsyncUdpConnection.php

@@ -59,20 +59,20 @@ class AsyncUdpConnection extends UdpConnection
     public function __construct($remote_address, $context_option = null)
     {
         // Get the application layer communication protocol and listening address.
-        list($scheme, $address) = explode(':', $remote_address, 2);
+        list($scheme, $address) = \explode(':', $remote_address, 2);
         // Check application layer protocol class.
         if ($scheme !== 'udp') {
-            $scheme         = ucfirst($scheme);
+            $scheme         = \ucfirst($scheme);
             $this->protocol = '\\Protocols\\' . $scheme;
-            if (!class_exists($this->protocol)) {
+            if (!\class_exists($this->protocol)) {
                 $this->protocol = "\\Workerman\\Protocols\\$scheme";
-                if (!class_exists($this->protocol)) {
+                if (!\class_exists($this->protocol)) {
                     throw new Exception("class \\Protocols\\$scheme not exist");
                 }
             }
         }
         
-        $this->_remoteAddress = substr($address, 2);
+        $this->_remoteAddress = \substr($address, 2);
         $this->_contextOption = $context_option;
     }
     
@@ -84,7 +84,7 @@ class AsyncUdpConnection extends UdpConnection
      */
     public function baseRead($socket)
     {
-        $recv_buffer = stream_socket_recvfrom($socket, Worker::MAX_UDP_PACKAGE_SIZE, 0, $remote_address);
+        $recv_buffer = \stream_socket_recvfrom($socket, Worker::MAX_UDP_PACKAGE_SIZE, 0, $remote_address);
         if (false === $recv_buffer || empty($remote_address)) {
             return false;
         }
@@ -96,7 +96,7 @@ class AsyncUdpConnection extends UdpConnection
             }
             ConnectionInterface::$statistics['total_request']++;
             try {
-                call_user_func($this->onMessage, $this, $recv_buffer);
+                \call_user_func($this->onMessage, $this, $recv_buffer);
             } catch (\Exception $e) {
                 Worker::log($e);
                 exit(250);
@@ -127,7 +127,7 @@ class AsyncUdpConnection extends UdpConnection
         if ($this->connected === false) {
             $this->connect();
         }
-        return strlen($send_buffer) === stream_socket_sendto($this->_socket, $send_buffer, 0);
+        return \strlen($send_buffer) === \stream_socket_sendto($this->_socket, $send_buffer, 0);
     }
     
     
@@ -145,12 +145,12 @@ class AsyncUdpConnection extends UdpConnection
             $this->send($data, $raw);
         }
         Worker::$globalEvent->del($this->_socket, EventInterface::EV_READ);
-        fclose($this->_socket);
+        \fclose($this->_socket);
         $this->connected = false;
         // Try to emit onClose callback.
         if ($this->onClose) {
             try {
-                call_user_func($this->onClose, $this);
+                \call_user_func($this->onClose, $this);
             } catch (\Exception $e) {
                 Worker::log($e);
                 exit(250);
@@ -174,11 +174,11 @@ class AsyncUdpConnection extends UdpConnection
             return;
         }
         if ($this->_contextOption) {
-            $context = stream_context_create($this->_contextOption);
-            $this->_socket = stream_socket_client("udp://{$this->_remoteAddress}", $errno, $errmsg,
+            $context = \stream_context_create($this->_contextOption);
+            $this->_socket = \stream_socket_client("udp://{$this->_remoteAddress}", $errno, $errmsg,
                 30, STREAM_CLIENT_CONNECT, $context);
         } else {
-            $this->_socket = stream_socket_client("udp://{$this->_remoteAddress}", $errno, $errmsg);
+            $this->_socket = \stream_socket_client("udp://{$this->_remoteAddress}", $errno, $errmsg);
         }
 
         if (!$this->_socket) {
@@ -186,7 +186,7 @@ class AsyncUdpConnection extends UdpConnection
             return;
         }
         
-        stream_set_blocking($this->_socket, false);
+        \stream_set_blocking($this->_socket, false);
         
         if ($this->onMessage) {
             Worker::$globalEvent->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
@@ -195,7 +195,7 @@ class AsyncUdpConnection extends UdpConnection
         // Try to emit onConnect callback.
         if ($this->onConnect) {
             try {
-                call_user_func($this->onConnect, $this);
+                \call_user_func($this->onConnect, $this);
             } catch (\Exception $e) {
                 Worker::log($e);
                 exit(250);

+ 62 - 62
Connection/TcpConnection.php

@@ -271,9 +271,9 @@ class TcpConnection extends ConnectionInterface
      */
     public function __call($name, $arguments) {
         // Try to emit custom function within protocol
-        if (method_exists($this->protocol, $name)) {
+        if (\method_exists($this->protocol, $name)) {
             try {
-                return call_user_func(array($this->protocol, $name), $this, $arguments);
+                return \call_user_func(array($this->protocol, $name), $this, $arguments);
             } catch (\Exception $e) {
                 Worker::log($e);
                 exit(250);
@@ -298,10 +298,10 @@ class TcpConnection extends ConnectionInterface
             self::$_idRecorder = 0;
         }
         $this->_socket = $socket;
-        stream_set_blocking($this->_socket, 0);
+        \stream_set_blocking($this->_socket, 0);
         // Compatible with hhvm
-        if (function_exists('stream_set_read_buffer')) {
-            stream_set_read_buffer($this->_socket, 0);
+        if (\function_exists('stream_set_read_buffer')) {
+            \stream_set_read_buffer($this->_socket, 0);
         }
         Worker::$globalEvent->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
         $this->maxSendBufferSize        = self::$defaultMaxSendBufferSize;
@@ -369,25 +369,25 @@ class TcpConnection extends ConnectionInterface
                 $this->checkBufferWillFull();
                 return null;
             }
-            set_error_handler(function(){});
-            $len = fwrite($this->_socket, $send_buffer);
-            restore_error_handler();
+            \set_error_handler(function(){});
+            $len = \fwrite($this->_socket, $send_buffer);
+            \restore_error_handler();
             // send successful.
-            if ($len === strlen($send_buffer)) {
+            if ($len === \strlen($send_buffer)) {
                 $this->bytesWritten += $len;
                 return true;
             }
             // Send only part of the data.
             if ($len > 0) {
-                $this->_sendBuffer = substr($send_buffer, $len);
+                $this->_sendBuffer = \substr($send_buffer, $len);
                 $this->bytesWritten += $len;
             } else {
                 // Connection closed?
-                if (!is_resource($this->_socket) || feof($this->_socket)) {
+                if (!\is_resource($this->_socket) || \feof($this->_socket)) {
                     self::$statistics['send_fail']++;
                     if ($this->onError) {
                         try {
-                            call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'client closed');
+                            \call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'client closed');
                         } catch (\Exception $e) {
                             Worker::log($e);
                             exit(250);
@@ -424,9 +424,9 @@ class TcpConnection extends ConnectionInterface
      */
     public function getRemoteIp()
     {
-        $pos = strrpos($this->_remoteAddress, ':');
+        $pos = \strrpos($this->_remoteAddress, ':');
         if ($pos) {
-            return substr($this->_remoteAddress, 0, $pos);
+            return \substr($this->_remoteAddress, 0, $pos);
         }
         return '';
     }
@@ -439,7 +439,7 @@ class TcpConnection extends ConnectionInterface
     public function getRemotePort()
     {
         if ($this->_remoteAddress) {
-            return (int)substr(strrchr($this->_remoteAddress, ':'), 1);
+            return (int)\substr(\strrchr($this->_remoteAddress, ':'), 1);
         }
         return 0;
     }
@@ -462,11 +462,11 @@ class TcpConnection extends ConnectionInterface
     public function getLocalIp()
     {
         $address = $this->getLocalAddress();
-        $pos = strrpos($address, ':');
+        $pos = \strrpos($address, ':');
         if (!$pos) {
             return '';
         }
-        return substr($address, 0, $pos);
+        return \substr($address, 0, $pos);
     }
 
     /**
@@ -477,11 +477,11 @@ class TcpConnection extends ConnectionInterface
     public function getLocalPort()
     {
         $address = $this->getLocalAddress();
-        $pos = strrpos($address, ':');
+        $pos = \strrpos($address, ':');
         if (!$pos) {
             return 0;
         }
-        return (int)substr(strrchr($address, ':'), 1);
+        return (int)\substr(\strrchr($address, ':'), 1);
     }
 
     /**
@@ -491,7 +491,7 @@ class TcpConnection extends ConnectionInterface
      */
     public function getLocalAddress()
     {
-        return (string)@stream_socket_get_name($this->_socket, false);
+        return (string)@\stream_socket_get_name($this->_socket, false);
     }
 
     /**
@@ -501,7 +501,7 @@ class TcpConnection extends ConnectionInterface
      */
     public function getSendBufferQueueSize()
     {
-        return strlen($this->_sendBuffer);
+        return \strlen($this->_sendBuffer);
     }
 
     /**
@@ -511,7 +511,7 @@ class TcpConnection extends ConnectionInterface
      */
     public function getRecvBufferQueueSize()
     {
-        return strlen($this->_recvBuffer);
+        return \strlen($this->_recvBuffer);
     }
 
     /**
@@ -524,7 +524,7 @@ class TcpConnection extends ConnectionInterface
         if ($this->transport === 'unix') {
             return false;
         }
-        return strpos($this->getRemoteIp(), ':') === false;
+        return \strpos($this->getRemoteIp(), ':') === false;
     }
 
     /**
@@ -537,7 +537,7 @@ class TcpConnection extends ConnectionInterface
         if ($this->transport === 'unix') {
             return false;
         }
-        return strpos($this->getRemoteIp(), ':') !== false;
+        return \strpos($this->getRemoteIp(), ':') !== false;
     }
 
     /**
@@ -588,18 +588,18 @@ class TcpConnection extends ConnectionInterface
             }
         }
 
-        set_error_handler(function(){});
-        $buffer = fread($socket, self::READ_BUFFER_SIZE);
-        restore_error_handler();
+        \set_error_handler(function(){});
+        $buffer = \fread($socket, self::READ_BUFFER_SIZE);
+        \restore_error_handler();
 
         // Check connection closed.
         if ($buffer === '' || $buffer === false) {
-            if ($check_eof && (feof($socket) || !is_resource($socket) || $buffer === false)) {
+            if ($check_eof && (\feof($socket) || !\is_resource($socket) || $buffer === false)) {
                 $this->destroy();
                 return;
             }
         } else {
-            $this->bytesRead += strlen($buffer);
+            $this->bytesRead += \strlen($buffer);
             $this->_recvBuffer .= $buffer;
         }
 
@@ -610,22 +610,22 @@ class TcpConnection extends ConnectionInterface
                 // The current packet length is known.
                 if ($this->_currentPackageLength) {
                     // Data is not enough for a package.
-                    if ($this->_currentPackageLength > strlen($this->_recvBuffer)) {
+                    if ($this->_currentPackageLength > \strlen($this->_recvBuffer)) {
                         break;
                     }
                 } else {
                     // Get current package length.
-                    set_error_handler(function($code, $msg, $file, $line){
+                    \set_error_handler(function($code, $msg, $file, $line){
                         Worker::safeEcho("$msg in file $file on line $line\n");
                     });
                     $this->_currentPackageLength = $parser::input($this->_recvBuffer, $this);
-                    restore_error_handler();
+                    \restore_error_handler();
                     // The packet length is unknown.
                     if ($this->_currentPackageLength === 0) {
                         break;
                     } elseif ($this->_currentPackageLength > 0 && $this->_currentPackageLength <= $this->maxPackageSize) {
                         // Data is not enough for a package.
-                        if ($this->_currentPackageLength > strlen($this->_recvBuffer)) {
+                        if ($this->_currentPackageLength > \strlen($this->_recvBuffer)) {
                             break;
                         }
                     } // Wrong package.
@@ -639,14 +639,14 @@ class TcpConnection extends ConnectionInterface
                 // The data is enough for a packet.
                 self::$statistics['total_request']++;
                 // The current packet length is equal to the length of the buffer.
-                if (strlen($this->_recvBuffer) === $this->_currentPackageLength) {
+                if (\strlen($this->_recvBuffer) === $this->_currentPackageLength) {
                     $one_request_buffer = $this->_recvBuffer;
                     $this->_recvBuffer  = '';
                 } else {
                     // Get a full package from the buffer.
-                    $one_request_buffer = substr($this->_recvBuffer, 0, $this->_currentPackageLength);
+                    $one_request_buffer = \substr($this->_recvBuffer, 0, $this->_currentPackageLength);
                     // Remove the current package from the receive buffer.
-                    $this->_recvBuffer = substr($this->_recvBuffer, $this->_currentPackageLength);
+                    $this->_recvBuffer = \substr($this->_recvBuffer, $this->_currentPackageLength);
                 }
                 // Reset the current packet length to 0.
                 $this->_currentPackageLength = 0;
@@ -655,7 +655,7 @@ class TcpConnection extends ConnectionInterface
                 }
                 try {
                     // Decode request buffer before Emitting onMessage callback.
-                    call_user_func($this->onMessage, $this, $parser::decode($one_request_buffer, $this));
+                    \call_user_func($this->onMessage, $this, $parser::decode($one_request_buffer, $this));
                 } catch (\Exception $e) {
                     Worker::log($e);
                     exit(250);
@@ -678,7 +678,7 @@ class TcpConnection extends ConnectionInterface
             return;
         }
         try {
-            call_user_func($this->onMessage, $this, $this->_recvBuffer);
+            \call_user_func($this->onMessage, $this, $this->_recvBuffer);
         } catch (\Exception $e) {
             Worker::log($e);
             exit(250);
@@ -697,21 +697,21 @@ class TcpConnection extends ConnectionInterface
      */
     public function baseWrite()
     {
-        set_error_handler(function(){});
+        \set_error_handler(function(){});
         if ($this->transport === 'ssl') {
-            $len = fwrite($this->_socket, $this->_sendBuffer, 8192);
+            $len = \fwrite($this->_socket, $this->_sendBuffer, 8192);
         } else {
-            $len = fwrite($this->_socket, $this->_sendBuffer);
+            $len = \fwrite($this->_socket, $this->_sendBuffer);
         }
-        restore_error_handler();
-        if ($len === strlen($this->_sendBuffer)) {
+        \restore_error_handler();
+        if ($len === \strlen($this->_sendBuffer)) {
             $this->bytesWritten += $len;
             Worker::$globalEvent->del($this->_socket, EventInterface::EV_WRITE);
             $this->_sendBuffer = '';
             // Try to emit onBufferDrain callback when the send buffer becomes empty.
             if ($this->onBufferDrain) {
                 try {
-                    call_user_func($this->onBufferDrain, $this);
+                    \call_user_func($this->onBufferDrain, $this);
                 } catch (\Exception $e) {
                     Worker::log($e);
                     exit(250);
@@ -727,7 +727,7 @@ class TcpConnection extends ConnectionInterface
         }
         if ($len > 0) {
             $this->bytesWritten += $len;
-            $this->_sendBuffer = substr($this->_sendBuffer, $len);
+            $this->_sendBuffer = \substr($this->_sendBuffer, $len);
         } else {
             self::$statistics['send_fail']++;
             $this->destroy();
@@ -741,7 +741,7 @@ class TcpConnection extends ConnectionInterface
      * @return bool
      */
     public function doSslHandshake($socket){
-        if (feof($socket)) {
+        if (\feof($socket)) {
             $this->destroy();
             return false;
         }
@@ -764,13 +764,13 @@ class TcpConnection extends ConnectionInterface
         }
         
         // Hidden error.
-        set_error_handler(function($errno, $errstr, $file){
+        \set_error_handler(function($errno, $errstr, $file){
             if (!Worker::$daemonize) {
                 Worker::safeEcho("SSL handshake error: $errstr \n");
             }
         });
-        $ret     = stream_socket_enable_crypto($socket, true, $type);
-        restore_error_handler();
+        $ret = \stream_socket_enable_crypto($socket, true, $type);
+        \restore_error_handler();
         // Negotiation has failed.
         if (false === $ret) {
             $this->destroy();
@@ -781,7 +781,7 @@ class TcpConnection extends ConnectionInterface
         }
         if (isset($this->onSslHandshake)) {
             try {
-                call_user_func($this->onSslHandshake, $this);
+                \call_user_func($this->onSslHandshake, $this);
             } catch (\Exception $e) {
                 Worker::log($e);
                 exit(250);
@@ -824,7 +824,7 @@ class TcpConnection extends ConnectionInterface
      */
     public function consumeRecvBuffer($length)
     {
-        $this->_recvBuffer = substr($this->_recvBuffer, $length);
+        $this->_recvBuffer = \substr($this->_recvBuffer, $length);
     }
 
     /**
@@ -872,10 +872,10 @@ class TcpConnection extends ConnectionInterface
      */
     protected function checkBufferWillFull()
     {
-        if ($this->maxSendBufferSize <= strlen($this->_sendBuffer)) {
+        if ($this->maxSendBufferSize <= \strlen($this->_sendBuffer)) {
             if ($this->onBufferFull) {
                 try {
-                    call_user_func($this->onBufferFull, $this);
+                    \call_user_func($this->onBufferFull, $this);
                 } catch (\Exception $e) {
                     Worker::log($e);
                     exit(250);
@@ -895,10 +895,10 @@ class TcpConnection extends ConnectionInterface
     protected function bufferIsFull()
     {
         // Buffer has been marked as full but still has data to send then the packet is discarded.
-        if ($this->maxSendBufferSize <= strlen($this->_sendBuffer)) {
+        if ($this->maxSendBufferSize <= \strlen($this->_sendBuffer)) {
             if ($this->onError) {
                 try {
-                    call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
+                    \call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
                 } catch (\Exception $e) {
                     Worker::log($e);
                     exit(250);
@@ -938,15 +938,15 @@ class TcpConnection extends ConnectionInterface
         Worker::$globalEvent->del($this->_socket, EventInterface::EV_WRITE);
 
         // Close socket.
-        set_error_handler(function(){});
-        fclose($this->_socket);
-        restore_error_handler();
+        \set_error_handler(function(){});
+        \fclose($this->_socket);
+        \restore_error_handler();
 
         $this->_status = self::STATUS_CLOSED;
         // Try to emit onClose callback.
         if ($this->onClose) {
             try {
-                call_user_func($this->onClose, $this);
+                \call_user_func($this->onClose, $this);
             } catch (\Exception $e) {
                 Worker::log($e);
                 exit(250);
@@ -956,9 +956,9 @@ class TcpConnection extends ConnectionInterface
             }
         }
         // Try to emit protocol::onClose
-        if ($this->protocol && method_exists($this->protocol, 'onClose')) {
+        if ($this->protocol && \method_exists($this->protocol, 'onClose')) {
             try {
-                call_user_func(array($this->protocol, 'onClose'), $this);
+                \call_user_func(array($this->protocol, 'onClose'), $this);
             } catch (\Exception $e) {
                 Worker::log($e);
                 exit(250);
@@ -994,7 +994,7 @@ class TcpConnection extends ConnectionInterface
             }
 
             if (0 === self::$statistics['connection_count'] % $mod) {
-                Worker::log('worker[' . posix_getpid() . '] remains ' . self::$statistics['connection_count'] . ' connection(s)');
+                Worker::log('worker[' . \posix_getpid() . '] remains ' . self::$statistics['connection_count'] . ' connection(s)');
             }
 
             if(0 === self::$statistics['connection_count']) {

+ 11 - 11
Connection/UdpConnection.php

@@ -68,7 +68,7 @@ class UdpConnection extends ConnectionInterface
                 return null;
             }
         }
-        return strlen($send_buffer) === stream_socket_sendto($this->_socket, $send_buffer, 0, $this->_remoteAddress);
+        return \strlen($send_buffer) === \stream_socket_sendto($this->_socket, $send_buffer, 0, $this->_remoteAddress);
     }
 
     /**
@@ -78,9 +78,9 @@ class UdpConnection extends ConnectionInterface
      */
     public function getRemoteIp()
     {
-        $pos = strrpos($this->_remoteAddress, ':');
+        $pos = \strrpos($this->_remoteAddress, ':');
         if ($pos) {
-            return trim(substr($this->_remoteAddress, 0, $pos), '[]');
+            return \trim(\substr($this->_remoteAddress, 0, $pos), '[]');
         }
         return '';
     }
@@ -93,7 +93,7 @@ class UdpConnection extends ConnectionInterface
     public function getRemotePort()
     {
         if ($this->_remoteAddress) {
-            return (int)substr(strrchr($this->_remoteAddress, ':'), 1);
+            return (int)\substr(\strrchr($this->_remoteAddress, ':'), 1);
         }
         return 0;
     }
@@ -116,11 +116,11 @@ class UdpConnection extends ConnectionInterface
     public function getLocalIp()
     {
         $address = $this->getLocalAddress();
-        $pos = strrpos($address, ':');
+        $pos = \strrpos($address, ':');
         if (!$pos) {
             return '';
         }
-        return substr($address, 0, $pos);
+        return \substr($address, 0, $pos);
     }
 
     /**
@@ -131,11 +131,11 @@ class UdpConnection extends ConnectionInterface
     public function getLocalPort()
     {
         $address = $this->getLocalAddress();
-        $pos = strrpos($address, ':');
+        $pos = \strrpos($address, ':');
         if (!$pos) {
             return 0;
         }
-        return (int)substr(strrchr($address, ':'), 1);
+        return (int)\substr(\strrchr($address, ':'), 1);
     }
 
     /**
@@ -145,7 +145,7 @@ class UdpConnection extends ConnectionInterface
      */
     public function getLocalAddress()
     {
-        return (string)@stream_socket_get_name($this->_socket, false);
+        return (string)@\stream_socket_get_name($this->_socket, false);
     }
 
     /**
@@ -158,7 +158,7 @@ class UdpConnection extends ConnectionInterface
         if ($this->transport === 'unix') {
             return false;
         }
-        return strpos($this->getRemoteIp(), ':') === false;
+        return \strpos($this->getRemoteIp(), ':') === false;
     }
 
     /**
@@ -171,7 +171,7 @@ class UdpConnection extends ConnectionInterface
         if ($this->transport === 'unix') {
             return false;
         }
-        return strpos($this->getRemoteIp(), ':') !== false;
+        return \strpos($this->getRemoteIp(), ':') !== false;
     }
 
     /**

+ 3 - 3
Events/Ev.php

@@ -56,7 +56,7 @@ class Ev implements EventInterface
     {
         $callback = function ($event, $socket) use ($fd, $func) {
             try {
-                call_user_func($func, $fd);
+                \call_user_func($func, $fd);
             } catch (\Exception $e) {
                 Worker::log($e);
                 exit(250);
@@ -137,7 +137,7 @@ class Ev implements EventInterface
             unset($this->_eventTimer[$timer_id]);
         }
         try {
-            call_user_func_array($param[0], $param[1]);
+            \call_user_func_array($param[0], $param[1]);
         } catch (\Exception $e) {
             Worker::log($e);
             exit(250);
@@ -189,6 +189,6 @@ class Ev implements EventInterface
      */
     public function getTimerCount()
     {
-        return count($this->_eventTimer);
+        return \count($this->_eventTimer);
     }
 }

+ 4 - 4
Events/Event.php

@@ -57,7 +57,7 @@ class Event implements EventInterface
      */
     public function __construct()
     {
-        if (class_exists('\\\\EventBase', false)) {
+        if (\class_exists('\\\\EventBase', false)) {
             $class_name = '\\\\EventBase';
         } else {
             $class_name = '\EventBase';
@@ -70,7 +70,7 @@ class Event implements EventInterface
      */
     public function add($fd, $flag, $func, $args=array())
     {
-        if (class_exists('\\\\Event', false)) {
+        if (\class_exists('\\\\Event', false)) {
             $class_name = '\\\\Event';
         } else {
             $class_name = '\Event';
@@ -164,7 +164,7 @@ class Event implements EventInterface
         }
 
         try {
-            call_user_func_array($param[0], $param[1]);
+            \call_user_func_array($param[0], $param[1]);
         } catch (\Exception $e) {
             Worker::log($e);
             exit(250);
@@ -214,6 +214,6 @@ class Event implements EventInterface
      */
     public function getTimerCount()
     {
-        return count($this->_eventTimer);
+        return \count($this->_eventTimer);
     }
 }

+ 20 - 20
Events/Libevent.php

@@ -67,30 +67,30 @@ class Libevent implements EventInterface
                 $fd_key                      = (int)$fd;
                 $real_flag                   = EV_SIGNAL | EV_PERSIST;
                 $this->_eventSignal[$fd_key] = event_new();
-                if (!event_set($this->_eventSignal[$fd_key], $fd, $real_flag, $func, null)) {
+                if (!\event_set($this->_eventSignal[$fd_key], $fd, $real_flag, $func, null)) {
                     return false;
                 }
-                if (!event_base_set($this->_eventSignal[$fd_key], $this->_eventBase)) {
+                if (!\event_base_set($this->_eventSignal[$fd_key], $this->_eventBase)) {
                     return false;
                 }
-                if (!event_add($this->_eventSignal[$fd_key])) {
+                if (!\event_add($this->_eventSignal[$fd_key])) {
                     return false;
                 }
                 return true;
             case self::EV_TIMER:
             case self::EV_TIMER_ONCE:
-                $event    = event_new();
+                $event    = \event_new();
                 $timer_id = (int)$event;
-                if (!event_set($event, 0, EV_TIMEOUT, array($this, 'timerCallback'), $timer_id)) {
+                if (!\event_set($event, 0, EV_TIMEOUT, array($this, 'timerCallback'), $timer_id)) {
                     return false;
                 }
 
-                if (!event_base_set($event, $this->_eventBase)) {
+                if (!\event_base_set($event, $this->_eventBase)) {
                     return false;
                 }
 
                 $time_interval = $fd * 1000000;
-                if (!event_add($event, $time_interval)) {
+                if (!\event_add($event, $time_interval)) {
                     return false;
                 }
                 $this->_eventTimer[$timer_id] = array($func, (array)$args, $event, $flag, $time_interval);
@@ -100,17 +100,17 @@ class Libevent implements EventInterface
                 $fd_key    = (int)$fd;
                 $real_flag = $flag === self::EV_READ ? EV_READ | EV_PERSIST : EV_WRITE | EV_PERSIST;
 
-                $event = event_new();
+                $event = \event_new();
 
-                if (!event_set($event, $fd, $real_flag, $func, null)) {
+                if (!\event_set($event, $fd, $real_flag, $func, null)) {
                     return false;
                 }
 
-                if (!event_base_set($event, $this->_eventBase)) {
+                if (!\event_base_set($event, $this->_eventBase)) {
                     return false;
                 }
 
-                if (!event_add($event)) {
+                if (!\event_add($event)) {
                     return false;
                 }
 
@@ -131,7 +131,7 @@ class Libevent implements EventInterface
             case self::EV_WRITE:
                 $fd_key = (int)$fd;
                 if (isset($this->_allEvents[$fd_key][$flag])) {
-                    event_del($this->_allEvents[$fd_key][$flag]);
+                    \event_del($this->_allEvents[$fd_key][$flag]);
                     unset($this->_allEvents[$fd_key][$flag]);
                 }
                 if (empty($this->_allEvents[$fd_key])) {
@@ -141,7 +141,7 @@ class Libevent implements EventInterface
             case  self::EV_SIGNAL:
                 $fd_key = (int)$fd;
                 if (isset($this->_eventSignal[$fd_key])) {
-                    event_del($this->_eventSignal[$fd_key]);
+                    \event_del($this->_eventSignal[$fd_key]);
                     unset($this->_eventSignal[$fd_key]);
                 }
                 break;
@@ -149,7 +149,7 @@ class Libevent implements EventInterface
             case self::EV_TIMER_ONCE:
                 // 这里 fd 为timerid 
                 if (isset($this->_eventTimer[$fd])) {
-                    event_del($this->_eventTimer[$fd][2]);
+                    \event_del($this->_eventTimer[$fd][2]);
                     unset($this->_eventTimer[$fd]);
                 }
                 break;
@@ -167,10 +167,10 @@ class Libevent implements EventInterface
     protected function timerCallback($_null1, $_null2, $timer_id)
     {
         if ($this->_eventTimer[$timer_id][3] === self::EV_TIMER) {
-            event_add($this->_eventTimer[$timer_id][2], $this->_eventTimer[$timer_id][4]);
+            \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]);
+            \call_user_func_array($this->_eventTimer[$timer_id][0], $this->_eventTimer[$timer_id][1]);
         } catch (\Exception $e) {
             Worker::log($e);
             exit(250);
@@ -189,7 +189,7 @@ class Libevent implements EventInterface
     public function clearAllTimer()
     {
         foreach ($this->_eventTimer as $task_data) {
-            event_del($task_data[2]);
+            \event_del($task_data[2]);
         }
         $this->_eventTimer = array();
     }
@@ -199,7 +199,7 @@ class Libevent implements EventInterface
      */
     public function loop()
     {
-        event_base_loop($this->_eventBase);
+        \event_base_loop($this->_eventBase);
     }
 
     /**
@@ -210,7 +210,7 @@ class Libevent implements EventInterface
     public function destroy()
     {
         foreach ($this->_eventSignal as $event) {
-            event_del($event);
+            \event_del($event);
         }
     }
 
@@ -221,7 +221,7 @@ class Libevent implements EventInterface
      */
     public function getTimerCount()
     {
-        return count($this->_eventTimer);
+        return \count($this->_eventTimer);
     }
 }
 

+ 3 - 3
Events/React/Base.php

@@ -74,7 +74,7 @@ class Base implements \React\EventLoop\LoopInterface
                 return $this->addSignal($fd, $func);
             case EventInterface::EV_TIMER:
                 $timer_obj = $this->addPeriodicTimer($fd, function() use ($func, $args) {
-                    call_user_func_array($func, $args);
+                    \call_user_func_array($func, $args);
                 });
                 $this->_timerIdMap[++$this->_timerIdIndex] = $timer_obj;
                 return $this->_timerIdIndex;
@@ -82,7 +82,7 @@ class Base implements \React\EventLoop\LoopInterface
                 $index = ++$this->_timerIdIndex;
                 $timer_obj = $this->addTimer($fd, function() use ($func, $args, $index) {
                     $this->del($index,EventInterface::EV_TIMER_ONCE);
-                    call_user_func_array($func, $args);
+                    \call_user_func_array($func, $args);
                 });
                 $this->_timerIdMap[$index] = $timer_obj;
                 return $this->_timerIdIndex;
@@ -153,7 +153,7 @@ class Base implements \React\EventLoop\LoopInterface
      */
     public function getTimerCount()
     {
-        return count($this->_timerIdMap);
+        return \count($this->_timerIdMap);
     }
 
     /**

+ 17 - 17
Events/Select.php

@@ -96,10 +96,10 @@ class Select implements EventInterface
     public function __construct()
     {
         // Create a pipeline and put into the collection of the read to read the descriptor to avoid empty polling.
-        $this->channel = stream_socket_pair(DIRECTORY_SEPARATOR === '/' ? STREAM_PF_UNIX : STREAM_PF_INET,
+        $this->channel = \stream_socket_pair(DIRECTORY_SEPARATOR === '/' ? STREAM_PF_UNIX : STREAM_PF_INET,
             STREAM_SOCK_STREAM, STREAM_IPPROTO_IP);
         if($this->channel) {
-            stream_set_blocking($this->channel[0], 0);
+            \stream_set_blocking($this->channel[0], 0);
             $this->_readFds[0] = $this->channel[0];
         }
         // Init SplPriorityQueue.
@@ -115,7 +115,7 @@ class Select implements EventInterface
         switch ($flag) {
             case self::EV_READ:
             case self::EV_WRITE:
-                $count = $flag === self::EV_READ ? count($this->_readFds) : count($this->_writeFds);
+                $count = $flag === self::EV_READ ? \count($this->_readFds) : \count($this->_writeFds);
                 if ($count >= 1024) {
                     echo "Warning: system call select exceeded the maximum number of connections 1024, please install event/libevent extension for more connections.\n";
                 } else if (DIRECTORY_SEPARATOR !== '/' && $count >= 256) {
@@ -141,15 +141,15 @@ class Select implements EventInterface
                 }
                 $fd_key                              = (int)$fd;
                 $this->_signalEvents[$fd_key][$flag] = array($func, $fd);
-                pcntl_signal($fd, array($this, 'signalHandler'));
+                \pcntl_signal($fd, array($this, 'signalHandler'));
                 break;
             case self::EV_TIMER:
             case self::EV_TIMER_ONCE:
                 $timer_id = $this->_timerId++;
-                $run_time = microtime(true) + $fd;
+                $run_time = \microtime(true) + $fd;
                 $this->_scheduler->insert($timer_id, -$run_time);
                 $this->_eventTimer[$timer_id] = array($func, (array)$args, $flag, $fd);
-                $select_timeout = ($run_time - microtime(true)) * 1000000;
+                $select_timeout = ($run_time - \microtime(true)) * 1000000;
                 if( $this->_selectTimeout > $select_timeout ){ 
                     $this->_selectTimeout = $select_timeout;   
                 }  
@@ -166,7 +166,7 @@ class Select implements EventInterface
      */
     public function signalHandler($signal)
     {
-        call_user_func_array($this->_signalEvents[$signal][self::EV_SIGNAL][0], array($signal));
+        \call_user_func_array($this->_signalEvents[$signal][self::EV_SIGNAL][0], array($signal));
     }
 
     /**
@@ -200,7 +200,7 @@ class Select implements EventInterface
                     return false;
                 }
                 unset($this->_signalEvents[$fd_key]);
-                pcntl_signal($fd, SIG_IGN);
+                \pcntl_signal($fd, SIG_IGN);
                 break;
             case self::EV_TIMER:
             case self::EV_TIMER_ONCE;
@@ -221,7 +221,7 @@ class Select implements EventInterface
             $scheduler_data       = $this->_scheduler->top();
             $timer_id             = $scheduler_data['data'];
             $next_run_time        = -$scheduler_data['priority'];
-            $time_now             = microtime(true);
+            $time_now             = \microtime(true);
             $this->_selectTimeout = ($next_run_time - $time_now) * 1000000;
             if ($this->_selectTimeout <= 0) {
                 $this->_scheduler->extract();
@@ -236,7 +236,7 @@ class Select implements EventInterface
                     $next_run_time = $time_now + $task_data[3];
                     $this->_scheduler->insert($timer_id, -$next_run_time);
                 }
-                call_user_func_array($task_data[0], $task_data[1]);
+                \call_user_func_array($task_data[0], $task_data[1]);
                 if (isset($this->_eventTimer[$timer_id]) && $task_data[2] === self::EV_TIMER_ONCE) {
                     $this->del($timer_id, self::EV_TIMER_ONCE);
                 }
@@ -265,7 +265,7 @@ class Select implements EventInterface
         while (1) {
             if(DIRECTORY_SEPARATOR === '/') {
                 // Calls signal handlers for pending signals
-                pcntl_signal_dispatch();
+                \pcntl_signal_dispatch();
             }
 
             $read  = $this->_readFds;
@@ -273,9 +273,9 @@ class Select implements EventInterface
             $except = $this->_exceptFds;
 
             // Waiting read/write/signal/timeout events.
-            set_error_handler(function(){});
+            \set_error_handler(function(){});
             $ret = stream_select($read, $write, $except, 0, $this->_selectTimeout);
-            restore_error_handler();
+            \restore_error_handler();
 
 
             if (!$this->_scheduler->isEmpty()) {
@@ -290,7 +290,7 @@ class Select implements EventInterface
                 foreach ($read as $fd) {
                     $fd_key = (int)$fd;
                     if (isset($this->_allEvents[$fd_key][self::EV_READ])) {
-                        call_user_func_array($this->_allEvents[$fd_key][self::EV_READ][0],
+                        \call_user_func_array($this->_allEvents[$fd_key][self::EV_READ][0],
                             array($this->_allEvents[$fd_key][self::EV_READ][1]));
                     }
                 }
@@ -300,7 +300,7 @@ class Select implements EventInterface
                 foreach ($write as $fd) {
                     $fd_key = (int)$fd;
                     if (isset($this->_allEvents[$fd_key][self::EV_WRITE])) {
-                        call_user_func_array($this->_allEvents[$fd_key][self::EV_WRITE][0],
+                        \call_user_func_array($this->_allEvents[$fd_key][self::EV_WRITE][0],
                             array($this->_allEvents[$fd_key][self::EV_WRITE][1]));
                     }
                 }
@@ -310,7 +310,7 @@ class Select implements EventInterface
                 foreach($except as $fd) {
                     $fd_key = (int) $fd;
                     if(isset($this->_allEvents[$fd_key][self::EV_EXCEPT])) {
-                        call_user_func_array($this->_allEvents[$fd_key][self::EV_EXCEPT][0],
+                        \call_user_func_array($this->_allEvents[$fd_key][self::EV_EXCEPT][0],
                             array($this->_allEvents[$fd_key][self::EV_EXCEPT][1]));
                     }
                 }
@@ -335,6 +335,6 @@ class Select implements EventInterface
      */
     public function getTimerCount()
     {
-        return count($this->_eventTimer);
+        return \count($this->_eventTimer);
     }
 }

+ 6 - 6
Events/Swoole.php

@@ -45,11 +45,11 @@ class Swoole implements EventInterface
         }
         switch ($flag) {
             case self::EV_SIGNAL:
-                $res = pcntl_signal($fd, $func, false);
+                $res = \pcntl_signal($fd, $func, false);
                 if (! $this->_hasSignal && $res) {
                     Timer::tick(static::$signalDispatchInterval,
                         function () {
-                            pcntl_signal_dispatch();
+                            \pcntl_signal_dispatch();
                         });
                     $this->_hasSignal = true;
                 }
@@ -63,7 +63,7 @@ class Swoole implements EventInterface
                 $mapId = $this->mapId++;
                 $timer_id = Timer::$method($fd * 1000,
                     function ($timer_id = null) use ($func, $args, $mapId) {
-                        call_user_func_array($func, $args);
+                        \call_user_func_array($func, $args);
                         // EV_TIMER_ONCE
                         if (! isset($timer_id)) {
                             // may be deleted in $func
@@ -126,11 +126,11 @@ class Swoole implements EventInterface
     {
         switch ($flag) {
             case self::EV_SIGNAL:
-                return pcntl_signal($fd, SIG_IGN, false);
+                return \pcntl_signal($fd, SIG_IGN, false);
             case self::EV_TIMER:
             case self::EV_TIMER_ONCE:
                 // already remove in EV_TIMER_ONCE callback.
-                if (! array_key_exists($fd, $this->_timer)) {
+                if (! \array_key_exists($fd, $this->_timer)) {
                     return true;
                 }
                 $res = Timer::clear($fd);
@@ -216,6 +216,6 @@ class Swoole implements EventInterface
      */
     public function getTimerCount()
     {
-        return count($this->_timer);
+        return \count($this->_timer);
     }
 }

+ 10 - 10
Lib/Timer.php

@@ -55,8 +55,8 @@ class Timer
         if ($event) {
             self::$_event = $event;
         } else {
-            if (function_exists('pcntl_signal')) {
-                pcntl_signal(SIGALRM, array('\Workerman\Lib\Timer', 'signalHandle'), false);
+            if (\function_exists('pcntl_signal')) {
+                \pcntl_signal(SIGALRM, array('\Workerman\Lib\Timer', 'signalHandle'), false);
             }
         }
     }
@@ -69,7 +69,7 @@ class Timer
     public static function signalHandle()
     {
         if (!self::$_event) {
-            pcntl_alarm(1);
+            \pcntl_alarm(1);
             self::tick();
         }
     }
@@ -95,16 +95,16 @@ class Timer
                 $persistent ? EventInterface::EV_TIMER : EventInterface::EV_TIMER_ONCE, $func, $args);
         }
 
-        if (!is_callable($func)) {
+        if (!\is_callable($func)) {
             Worker::safeEcho(new Exception("not callable"));
             return false;
         }
 
         if (empty(self::$_tasks)) {
-            pcntl_alarm(1);
+            \pcntl_alarm(1);
         }
 
-        $time_now = time();
+        $time_now = \time();
         $run_time = $time_now + $time_interval;
         if (!isset(self::$_tasks[$run_time])) {
             self::$_tasks[$run_time] = array();
@@ -122,11 +122,11 @@ class Timer
     public static function tick()
     {
         if (empty(self::$_tasks)) {
-            pcntl_alarm(0);
+            \pcntl_alarm(0);
             return;
         }
 
-        $time_now = time();
+        $time_now = \time();
         foreach (self::$_tasks as $run_time => $task_data) {
             if ($time_now >= $run_time) {
                 foreach ($task_data as $index => $one_task) {
@@ -135,7 +135,7 @@ class Timer
                     $persistent    = $one_task[2];
                     $time_interval = $one_task[3];
                     try {
-                        call_user_func_array($task_func, $task_args);
+                        \call_user_func_array($task_func, $task_args);
                     } catch (\Exception $e) {
                         Worker::safeEcho($e);
                     }
@@ -171,7 +171,7 @@ class Timer
     public static function delAll()
     {
         self::$_tasks = array();
-        pcntl_alarm(0);
+        \pcntl_alarm(0);
         if (self::$_event) {
             self::$_event->clearAllTimer();
         }

+ 5 - 5
Protocols/Frame.php

@@ -29,10 +29,10 @@ class Frame
      */
     public static function input($buffer, TcpConnection $connection)
     {
-        if (strlen($buffer) < 4) {
+        if (\strlen($buffer) < 4) {
             return 0;
         }
-        $unpack_data = unpack('Ntotal_length', $buffer);
+        $unpack_data = \unpack('Ntotal_length', $buffer);
         return $unpack_data['total_length'];
     }
 
@@ -44,7 +44,7 @@ class Frame
      */
     public static function decode($buffer)
     {
-        return substr($buffer, 4);
+        return \substr($buffer, 4);
     }
 
     /**
@@ -55,7 +55,7 @@ class Frame
      */
     public static function encode($buffer)
     {
-        $total_length = 4 + strlen($buffer);
-        return pack('N', $total_length) . $buffer;
+        $total_length = 4 + \strlen($buffer);
+        return \pack('N', $total_length) . $buffer;
     }
 }

+ 84 - 83
Protocols/Http.php

@@ -36,19 +36,19 @@ class Http
      */
     public static function input($recv_buffer, TcpConnection $connection)
     {
-        if (!strpos($recv_buffer, "\r\n\r\n")) {
+        if (!\strpos($recv_buffer, "\r\n\r\n")) {
             // Judge whether the package length exceeds the limit.
-            if (strlen($recv_buffer) >= $connection->maxPackageSize) {
+            if (\strlen($recv_buffer) >= $connection->maxPackageSize) {
                 $connection->close();
                 return 0;
             }
             return 0;
         }
 
-        list($header,) = explode("\r\n\r\n", $recv_buffer, 2);
-        $method = substr($header, 0, strpos($header, ' '));
+        list($header,) = \explode("\r\n\r\n", $recv_buffer, 2);
+        $method = \substr($header, 0, \strpos($header, ' '));
 
-        if(in_array($method, static::$methods)) {
+        if(\in_array($method, static::$methods)) {
             return static::getRequestSize($header, $method);
         }else{
             $connection->send("HTTP/1.1 400 Bad Request\r\n\r\n", true);
@@ -66,14 +66,14 @@ class Http
     protected static function getRequestSize($header, $method)
     {
         if($method === 'GET' || $method === 'OPTIONS' || $method === 'HEAD') {
-            return strlen($header) + 4;
+            return \strlen($header) + 4;
         }
         $match = array();
-        if (preg_match("/\r\nContent-Length: ?(\d+)/i", $header, $match)) {
+        if (\preg_match("/\r\nContent-Length: ?(\d+)/i", $header, $match)) {
             $content_length = isset($match[1]) ? $match[1] : 0;
-            return $content_length + strlen($header) + 4;
+            return $content_length + \strlen($header) + 4;
         }
-        return $method === 'DELETE' ? strlen($header) + 4 : 0;
+        return $method === 'DELETE' ? \strlen($header) + 4 : 0;
     }
 
     /**
@@ -109,14 +109,14 @@ class Http
             'CONTENT_TYPE'         => '',
             'REMOTE_ADDR'          => '',
             'REMOTE_PORT'          => '0',
-            'REQUEST_TIME'         => time()
+            'REQUEST_TIME'         => \time()
         );
 
         // Parse headers.
-        list($http_header, $http_body) = explode("\r\n\r\n", $recv_buffer, 2);
-        $header_data = explode("\r\n", $http_header);
+        list($http_header, $http_body) = \explode("\r\n\r\n", $recv_buffer, 2);
+        $header_data = \explode("\r\n", $http_header);
 
-        list($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], $_SERVER['SERVER_PROTOCOL']) = explode(' ',
+        list($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], $_SERVER['SERVER_PROTOCOL']) = \explode(' ',
             $header_data[0]);
 
         $http_post_boundary = '';
@@ -126,14 +126,14 @@ class Http
             if (empty($content)) {
                 continue;
             }
-            list($key, $value)       = explode(':', $content, 2);
-            $key                     = str_replace('-', '_', strtoupper($key));
-            $value                   = trim($value);
+            list($key, $value)       = \explode(':', $content, 2);
+            $key                     = \str_replace('-', '_', strtoupper($key));
+            $value                   = \trim($value);
             $_SERVER['HTTP_' . $key] = $value;
             switch ($key) {
                 // HTTP_HOST
                 case 'HOST':
-                    $tmp                    = explode(':', $value);
+                    $tmp                    = \explode(':', $value);
                     $_SERVER['SERVER_NAME'] = $tmp[0];
                     if (isset($tmp[1])) {
                         $_SERVER['SERVER_PORT'] = $tmp[1];
@@ -141,13 +141,13 @@ class Http
                     break;
                 // cookie
                 case 'COOKIE':
-                    parse_str(str_replace('; ', '&', $_SERVER['HTTP_COOKIE']), $_COOKIE);
+                    \parse_str(\str_replace('; ', '&', $_SERVER['HTTP_COOKIE']), $_COOKIE);
                     break;
                 // content-type
                 case 'CONTENT_TYPE':
-                    if (!preg_match('/boundary="?(\S+)"?/', $value, $match)) {
-                        if ($pos = strpos($value, ';')) {
-                            $_SERVER['CONTENT_TYPE'] = substr($value, 0, $pos);
+                    if (!\preg_match('/boundary="?(\S+)"?/', $value, $match)) {
+                        if ($pos = \strpos($value, ';')) {
+                            $_SERVER['CONTENT_TYPE'] = \substr($value, 0, $pos);
                         } else {
                             $_SERVER['CONTENT_TYPE'] = $value;
                         }
@@ -167,7 +167,7 @@ class Http
                     break;
             }
         }
-		if(isset($_SERVER['HTTP_ACCEPT_ENCODING']) && strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE){
+		if(isset($_SERVER['HTTP_ACCEPT_ENCODING']) && \strpos($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip') !== FALSE){
 			HttpCache::$gzip = true;
 		}
         // Parse $_POST.
@@ -178,10 +178,10 @@ class Http
                         self::parseUploadFiles($http_body, $http_post_boundary);
                         break;
                     case 'application/json':
-                        $_POST = json_decode($http_body, true);
+                        $_POST = \json_decode($http_body, true);
                         break;
                     case 'application/x-www-form-urlencoded':
-                        parse_str($http_body, $_POST);
+                        \parse_str($http_body, $_POST);
                         break;
                 }
             }
@@ -191,31 +191,31 @@ class Http
         if ($_SERVER['REQUEST_METHOD'] != 'GET' && $_SERVER['REQUEST_METHOD'] != "POST") {
             $data = array();
             if ($_SERVER['CONTENT_TYPE'] === "application/x-www-form-urlencoded") {
-                parse_str($http_body, $data);
+                \parse_str($http_body, $data);
             } elseif ($_SERVER['CONTENT_TYPE'] === "application/json") {
-                $data = json_decode($http_body, true);
+                $data = \json_decode($http_body, true);
             }
-            $_REQUEST = array_merge($_REQUEST, $data);
+            $_REQUEST = \array_merge($_REQUEST, $data);
         }
 
         // HTTP_RAW_REQUEST_DATA HTTP_RAW_POST_DATA
         $GLOBALS['HTTP_RAW_REQUEST_DATA'] = $GLOBALS['HTTP_RAW_POST_DATA'] = $http_body;
 
         // QUERY_STRING
-        $_SERVER['QUERY_STRING'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
+        $_SERVER['QUERY_STRING'] = \parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
         if ($_SERVER['QUERY_STRING']) {
             // $GET
-            parse_str($_SERVER['QUERY_STRING'], $_GET);
+            \parse_str($_SERVER['QUERY_STRING'], $_GET);
         } else {
             $_SERVER['QUERY_STRING'] = '';
         }
 
-        if (is_array($_POST)) {
+        if (\is_array($_POST)) {
             // REQUEST
-            $_REQUEST = array_merge($_GET, $_POST, $_REQUEST);
+            $_REQUEST = \array_merge($_GET, $_POST, $_REQUEST);
         } else {
             // REQUEST
-            $_REQUEST = array_merge($_GET, $_REQUEST);
+            $_REQUEST = \array_merge($_GET, $_REQUEST);
         }
 
         // REMOTE_ADDR REMOTE_PORT
@@ -249,7 +249,7 @@ class Http
 
         // other headers
         foreach (HttpCache::$header as $key => $item) {
-            if ('Set-Cookie' === $key && is_array($item)) {
+            if ('Set-Cookie' === $key && \is_array($item)) {
                 foreach ($item as $it) {
                     $header .= $it . "\r\n";
                 }
@@ -259,10 +259,10 @@ class Http
         }
 		if(HttpCache::$gzip && isset($connection->gzip) && $connection->gzip){
 			$header .= "Content-Encoding: gzip\r\n";
-			$content = gzencode($content,$connection->gzip);
+			$content = \gzencode($content,$connection->gzip);
 		}
         // header
-        $header .= "Server: workerman/" . Worker::VERSION . "\r\nContent-Length: " . strlen($content) . "\r\n\r\n";
+        $header .= "Server: workerman/" . Worker::VERSION . "\r\nContent-Length: " . \strlen($content) . "\r\n\r\n";
 
         // save session
         self::sessionWriteClose();
@@ -279,18 +279,18 @@ class Http
     public static function header($content, $replace = true, $http_response_code = 0)
     {
         if (PHP_SAPI != 'cli') {
-            return $http_response_code ? header($content, $replace, $http_response_code) : header($content, $replace);
+            return $http_response_code ? \header($content, $replace, $http_response_code) : \header($content, $replace);
         }
-        if (strpos($content, 'HTTP') === 0) {
+        if (\strpos($content, 'HTTP') === 0) {
             $key = 'Http-Code';
         } else {
-            $key = strstr($content, ":", true);
+            $key = \strstr($content, ":", true);
             if (empty($key)) {
                 return false;
             }
         }
 
-        if ('location' === strtolower($key) && !$http_response_code) {
+        if ('location' === \strtolower($key) && !$http_response_code) {
             return self::header($content, true, 302);
         }
 
@@ -319,7 +319,7 @@ class Http
     public static function headerRemove($name)
     {
         if (PHP_SAPI != 'cli') {
-            header_remove($name);
+            \header_remove($name);
             return;
         }
         unset(HttpCache::$header[$name]);
@@ -347,7 +347,7 @@ class Http
         $HTTPOnly = false
     ) {
         if (PHP_SAPI != 'cli') {
-            return setcookie($name, $value, $maxage, $path, $domain, $secure, $HTTPOnly);
+            return \setcookie($name, $value, $maxage, $path, $domain, $secure, $HTTPOnly);
         }
         return self::header(
             'Set-Cookie: ' . $name . '=' . rawurlencode($value)
@@ -365,8 +365,8 @@ class Http
      */
     public static function sessionCreateId()
     {
-        mt_srand();
-        return bin2hex(pack('d', microtime(true)) . pack('N',mt_rand(0, 2147483647)));
+        \mt_srand();
+        return bin2hex(\pack('d', \microtime(true)) . \pack('N',\mt_rand(0, 2147483647)));
     }
 
     /**
@@ -379,10 +379,10 @@ class Http
     public static function sessionId($id = null)
     {
         if (PHP_SAPI != 'cli') {
-            return $id ? session_id($id) : session_id();
+            return $id ? \session_id($id) : \session_id();
         }
         if (static::sessionStarted() && HttpCache::$instance->sessionFile) {
-            return str_replace('ses_', '', basename(HttpCache::$instance->sessionFile));
+            return \str_replace('ses_', '', \basename(HttpCache::$instance->sessionFile));
         }
         return '';
     }
@@ -397,7 +397,7 @@ class Http
     public static function sessionName($name = null)
     {
         if (PHP_SAPI != 'cli') {
-            return $name ? session_name($name) : session_name();
+            return $name ? \session_name($name) : \session_name();
         }
         $session_name = HttpCache::$sessionName;
         if ($name && ! static::sessionStarted()) {
@@ -416,9 +416,9 @@ class Http
     public static function sessionSavePath($path = null)
     {
         if (PHP_SAPI != 'cli') {
-            return $path ? session_save_path($path) : session_save_path();
+            return $path ? \session_save_path($path) : \session_save_path();
         }
-        if ($path && is_dir($path) && is_writable($path) && !static::sessionStarted()) {
+        if ($path && \is_dir($path) && \is_writable($path) && !static::sessionStarted()) {
             HttpCache::$sessionPath = $path;
         }
         return HttpCache::$sessionPath;
@@ -444,7 +444,7 @@ class Http
     public static function sessionStart()
     {
         if (PHP_SAPI != 'cli') {
-            return session_start();
+            return \session_start();
         }
 
         self::tryGcSessions();
@@ -455,21 +455,21 @@ class Http
         }
         HttpCache::$instance->sessionStarted = true;
         // Generate a SID.
-        if (!isset($_COOKIE[HttpCache::$sessionName]) || !is_file(HttpCache::$sessionPath . '/ses_' . $_COOKIE[HttpCache::$sessionName])) {
+        if (!isset($_COOKIE[HttpCache::$sessionName]) || !\is_file(HttpCache::$sessionPath . '/ses_' . $_COOKIE[HttpCache::$sessionName])) {
             // Create a unique session_id and the associated file name.
             while (true) {
                 $session_id = static::sessionCreateId();
-                if (!is_file($file_name = HttpCache::$sessionPath . '/ses_' . $session_id)) break;
+                if (!\is_file($file_name = HttpCache::$sessionPath . '/ses_' . $session_id)) break;
             }
             HttpCache::$instance->sessionFile = $file_name;
             return self::setcookie(
                 HttpCache::$sessionName
                 , $session_id
-                , ini_get('session.cookie_lifetime')
-                , ini_get('session.cookie_path')
-                , ini_get('session.cookie_domain')
-                , ini_get('session.cookie_secure')
-                , ini_get('session.cookie_httponly')
+                , \ini_get('session.cookie_lifetime')
+                , \ini_get('session.cookie_path')
+                , \ini_get('session.cookie_domain')
+                , \ini_get('session.cookie_secure')
+                , \ini_get('session.cookie_httponly')
             );
         }
         if (!HttpCache::$instance->sessionFile) {
@@ -477,9 +477,9 @@ class Http
         }
         // Read session from session file.
         if (HttpCache::$instance->sessionFile) {
-            $raw = file_get_contents(HttpCache::$instance->sessionFile);
+            $raw = \file_get_contents(HttpCache::$instance->sessionFile);
             if ($raw) {
-                $_SESSION = unserialize($raw);
+                $_SESSION = \unserialize($raw);
             }
         }
         return true;
@@ -493,12 +493,13 @@ class Http
     public static function sessionWriteClose()
     {
         if (PHP_SAPI != 'cli') {
-            return session_write_close();
+            \session_write_close();
+            return true;
         }
         if (!empty(HttpCache::$instance->sessionStarted) && !empty($_SESSION)) {
-            $session_str = serialize($_SESSION);
+            $session_str = \serialize($_SESSION);
             if ($session_str && HttpCache::$instance->sessionFile) {
-                return file_put_contents(HttpCache::$instance->sessionFile, $session_str);
+                return \file_put_contents(HttpCache::$instance->sessionFile, $session_str);
             }
         }
         return empty($_SESSION);
@@ -540,43 +541,43 @@ class Http
      */
     protected static function parseUploadFiles($http_body, $http_post_boundary)
     {
-        $http_body           = substr($http_body, 0, strlen($http_body) - (strlen($http_post_boundary) + 4));
-        $boundary_data_array = explode($http_post_boundary . "\r\n", $http_body);
+        $http_body           = \substr($http_body, 0, \strlen($http_body) - (\strlen($http_post_boundary) + 4));
+        $boundary_data_array = \explode($http_post_boundary . "\r\n", $http_body);
         if ($boundary_data_array[0] === '') {
             unset($boundary_data_array[0]);
         }
         $key = -1;
         foreach ($boundary_data_array as $boundary_data_buffer) {
-            list($boundary_header_buffer, $boundary_value) = explode("\r\n\r\n", $boundary_data_buffer, 2);
+            list($boundary_header_buffer, $boundary_value) = \explode("\r\n\r\n", $boundary_data_buffer, 2);
             // Remove \r\n from the end of buffer.
-            $boundary_value = substr($boundary_value, 0, -2);
+            $boundary_value = \substr($boundary_value, 0, -2);
             $key ++;
-            foreach (explode("\r\n", $boundary_header_buffer) as $item) {
-                list($header_key, $header_value) = explode(": ", $item);
-                $header_key = strtolower($header_key);
+            foreach (\explode("\r\n", $boundary_header_buffer) as $item) {
+                list($header_key, $header_value) = \explode(": ", $item);
+                $header_key = \strtolower($header_key);
                 switch ($header_key) {
                     case "content-disposition":
                         // Is file data.
-                        if (preg_match('/name="(.*?)"; filename="(.*?)"$/', $header_value, $match)) {
+                        if (\preg_match('/name="(.*?)"; filename="(.*?)"$/', $header_value, $match)) {
                             // Parse $_FILES.
                             $_FILES[$key] = array(
                                 'name' => $match[1],
                                 'file_name' => $match[2],
                                 'file_data' => $boundary_value,
-                                'file_size' => strlen($boundary_value),
+                                'file_size' => \strlen($boundary_value),
                             );
                             break;
                         } // Is post field.
                         else {
                             // Parse $_POST.
-                            if (preg_match('/name="(.*?)"$/', $header_value, $match)) {
+                            if (\preg_match('/name="(.*?)"$/', $header_value, $match)) {
                                 $_POST[$match[1]] = $boundary_value;
                             }
                         }
                         break;
                     case "content-type":
                         // add file_type
-                        $_FILES[$key]['file_type'] = trim($header_value);
+                        $_FILES[$key]['file_type'] = \trim($header_value);
                         break;
                 }
             }
@@ -592,14 +593,14 @@ class Http
     {
         if (HttpCache::$sessionGcProbability <= 0 ||
             HttpCache::$sessionGcDivisor     <= 0 ||
-            rand(1, HttpCache::$sessionGcDivisor) > HttpCache::$sessionGcProbability) {
+            \rand(1, HttpCache::$sessionGcDivisor) > HttpCache::$sessionGcProbability) {
             return;
         }
 
-        $time_now = time();
+        $time_now = \time();
         foreach(glob(HttpCache::$sessionPath.'/ses*') as $file) {
-            if(is_file($file) && $time_now - filemtime($file) > HttpCache::$sessionGcMaxLifeTime) {
-                unlink($file);
+            if(\is_file($file) && $time_now - \filemtime($file) > HttpCache::$sessionGcMaxLifeTime) {
+                \unlink($file);
             }
         }
     }
@@ -673,26 +674,26 @@ class HttpCache
     public static function init()
     {
         if (!self::$sessionName) {
-            self::$sessionName = ini_get('session.name');
+            self::$sessionName = \ini_get('session.name');
         }
 
         if (!self::$sessionPath) {
-            self::$sessionPath = @session_save_path();
+            self::$sessionPath = @\session_save_path();
         }
 
-        if (!self::$sessionPath || strpos(self::$sessionPath, 'tcp://') === 0) {
-            self::$sessionPath = sys_get_temp_dir();
+        if (!self::$sessionPath || \strpos(self::$sessionPath, 'tcp://') === 0) {
+            self::$sessionPath = \sys_get_temp_dir();
         }
 
-        if ($gc_probability = ini_get('session.gc_probability')) {
+        if ($gc_probability = \ini_get('session.gc_probability')) {
             self::$sessionGcProbability = $gc_probability;
         }
 
-        if ($gc_divisor = ini_get('session.gc_divisor')) {
+        if ($gc_divisor = \ini_get('session.gc_divisor')) {
             self::$sessionGcDivisor = $gc_divisor;
         }
 
-        if ($gc_max_life_time = ini_get('session.gc_maxlifetime')) {
+        if ($gc_max_life_time = \ini_get('session.gc_maxlifetime')) {
             self::$sessionGcMaxLifeTime = $gc_max_life_time;
         }
     }

+ 3 - 3
Protocols/Text.php

@@ -30,12 +30,12 @@ class Text
     public static function input($buffer, TcpConnection $connection)
     {
         // Judge whether the package length exceeds the limit.
-        if (strlen($buffer) >= $connection->maxPackageSize) {
+        if (\strlen($buffer) >= $connection->maxPackageSize) {
             $connection->close();
             return 0;
         }
         //  Find the position of  "\n".
-        $pos = strpos($buffer, "\n");
+        $pos = \strpos($buffer, "\n");
         // No "\n", packet length is unknown, continue to wait for the data so return 0.
         if ($pos === false) {
             return 0;
@@ -65,6 +65,6 @@ class Text
     public static function decode($buffer)
     {
         // Remove "\n"
-        return rtrim($buffer, "\r\n");
+        return \rtrim($buffer, "\r\n");
     }
 }

+ 54 - 54
Protocols/Websocket.php

@@ -46,7 +46,7 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
     public static function input($buffer, ConnectionInterface $connection)
     {
         // Receive length.
-        $recv_len = strlen($buffer);
+        $recv_len = \strlen($buffer);
         // We need more data.
         if ($recv_len < 6) {
             return 0;
@@ -65,8 +65,8 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                 return 0;
             }
         } else {
-            $firstbyte    = ord($buffer[0]);
-            $secondbyte   = ord($buffer[1]);
+            $firstbyte    = \ord($buffer[0]);
+            $secondbyte   = \ord($buffer[1]);
             $data_len     = $secondbyte & 127;
             $is_fin_frame = $firstbyte >> 7;
             $masked       = $secondbyte >> 7;
@@ -92,7 +92,7 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                     // Try to emit onWebSocketClose callback.
                     if (isset($connection->onWebSocketClose) || isset($connection->worker->onWebSocketClose)) {
                         try {
-                            call_user_func(isset($connection->onWebSocketClose)?$connection->onWebSocketClose:$connection->worker->onWebSocketClose, $connection);
+                            \call_user_func(isset($connection->onWebSocketClose)?$connection->onWebSocketClose:$connection->worker->onWebSocketClose, $connection);
                         } catch (\Exception $e) {
                             Worker::log($e);
                             exit(250);
@@ -125,7 +125,7 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                 if ($head_len > $recv_len) {
                     return 0;
                 }
-                $pack     = unpack('nn/ntotal_len', $buffer);
+                $pack     = \unpack('nn/ntotal_len', $buffer);
                 $data_len = $pack['total_len'];
             } else {
                 if ($data_len === 127) {
@@ -133,13 +133,13 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                     if ($head_len > $recv_len) {
                         return 0;
                     }
-                    $arr      = unpack('n/N2c', $buffer);
+                    $arr      = \unpack('n/N2c', $buffer);
                     $data_len = $arr['c1']*4294967296 + $arr['c2'];
                 }
             }
             $current_frame_length = $head_len + $data_len;
 
-            $total_package_size = strlen($connection->websocketDataBuffer) + $current_frame_length;
+            $total_package_size = \strlen($connection->websocketDataBuffer) + $current_frame_length;
             if ($total_package_size > $connection->maxPackageSize) {
                 Worker::safeEcho("error package. package_length=$total_package_size\n");
                 $connection->close();
@@ -149,13 +149,13 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
             if ($is_fin_frame) {
                 if ($opcode === 0x9) {
                     if ($recv_len >= $current_frame_length) {
-                        $ping_data = static::decode(substr($buffer, 0, $current_frame_length), $connection);
+                        $ping_data = static::decode(\substr($buffer, 0, $current_frame_length), $connection);
                         $connection->consumeRecvBuffer($current_frame_length);
                         $tmp_connection_type = isset($connection->websocketType) ? $connection->websocketType : static::BINARY_TYPE_BLOB;
                         $connection->websocketType = "\x8a";
                         if (isset($connection->onWebSocketPing) || isset($connection->worker->onWebSocketPing)) {
                             try {
-                                call_user_func(isset($connection->onWebSocketPing)?$connection->onWebSocketPing:$connection->worker->onWebSocketPing, $connection, $ping_data);
+                                \call_user_func(isset($connection->onWebSocketPing)?$connection->onWebSocketPing:$connection->worker->onWebSocketPing, $connection, $ping_data);
                             } catch (\Exception $e) {
                                 Worker::log($e);
                                 exit(250);
@@ -168,20 +168,20 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                         }
                         $connection->websocketType = $tmp_connection_type;
                         if ($recv_len > $current_frame_length) {
-                            return static::input(substr($buffer, $current_frame_length), $connection);
+                            return static::input(\substr($buffer, $current_frame_length), $connection);
                         }
                     }
                     return 0;
                 } else if ($opcode === 0xa) {
                     if ($recv_len >= $current_frame_length) {
-                        $pong_data = static::decode(substr($buffer, 0, $current_frame_length), $connection);
+                        $pong_data = static::decode(\substr($buffer, 0, $current_frame_length), $connection);
                         $connection->consumeRecvBuffer($current_frame_length);
                         $tmp_connection_type = isset($connection->websocketType) ? $connection->websocketType : static::BINARY_TYPE_BLOB;
                         $connection->websocketType = "\x8a";
                         // Try to emit onWebSocketPong callback.
                         if (isset($connection->onWebSocketPong) || isset($connection->worker->onWebSocketPong)) {
                             try {
-                                call_user_func(isset($connection->onWebSocketPong)?$connection->onWebSocketPong:$connection->worker->onWebSocketPong, $connection, $pong_data);
+                                \call_user_func(isset($connection->onWebSocketPong)?$connection->onWebSocketPong:$connection->worker->onWebSocketPong, $connection, $pong_data);
                             } catch (\Exception $e) {
                                 Worker::log($e);
                                 exit(250);
@@ -192,7 +192,7 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                         }
                         $connection->websocketType = $tmp_connection_type;
                         if ($recv_len > $current_frame_length) {
-                            return static::input(substr($buffer, $current_frame_length), $connection);
+                            return static::input(\substr($buffer, $current_frame_length), $connection);
                         }
                     }
                     return 0;
@@ -211,12 +211,12 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
             return 0;
         } // The length of the received data is greater than the length of a frame.
         elseif ($connection->websocketCurrentFrameLength < $recv_len) {
-            static::decode(substr($buffer, 0, $connection->websocketCurrentFrameLength), $connection);
+            static::decode(\substr($buffer, 0, $connection->websocketCurrentFrameLength), $connection);
             $connection->consumeRecvBuffer($connection->websocketCurrentFrameLength);
             $current_frame_length                    = $connection->websocketCurrentFrameLength;
             $connection->websocketCurrentFrameLength = 0;
             // Continue to read next frame.
-            return static::input(substr($buffer, $current_frame_length), $connection);
+            return static::input(\substr($buffer, $current_frame_length), $connection);
         } // The length of the received data is less than the length of a frame.
         else {
             return 0;
@@ -235,7 +235,7 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
         if (!is_scalar($buffer)) {
             throw new \Exception("You can't send(" . gettype($buffer) . ") to client, you need to convert it to a string. ");
         }
-        $len = strlen($buffer);
+        $len = \strlen($buffer);
         if (empty($connection->websocketType)) {
             $connection->websocketType = static::BINARY_TYPE_BLOB;
         }
@@ -243,12 +243,12 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
         $first_byte = $connection->websocketType;
 
         if ($len <= 125) {
-            $encode_buffer = $first_byte . chr($len) . $buffer;
+            $encode_buffer = $first_byte . \chr($len) . $buffer;
         } else {
             if ($len <= 65535) {
-                $encode_buffer = $first_byte . chr(126) . pack("n", $len) . $buffer;
+                $encode_buffer = $first_byte . \chr(126) . \pack("n", $len) . $buffer;
             } else {
-                $encode_buffer = $first_byte . chr(127) . pack("xxxxN", $len) . $buffer;
+                $encode_buffer = $first_byte . \chr(127) . \pack("xxxxN", $len) . $buffer;
             }
         }
 
@@ -258,10 +258,10 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                 $connection->tmpWebsocketData = '';
             }
             // If buffer has already full then discard the current package.
-            if (strlen($connection->tmpWebsocketData) > $connection->maxSendBufferSize) {
+            if (\strlen($connection->tmpWebsocketData) > $connection->maxSendBufferSize) {
                 if ($connection->onError) {
                     try {
-                        call_user_func($connection->onError, $connection, WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
+                        \call_user_func($connection->onError, $connection, WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
                     } catch (\Exception $e) {
                         Worker::log($e);
                         exit(250);
@@ -274,10 +274,10 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
             }
             $connection->tmpWebsocketData .= $encode_buffer;
             // Check buffer is full.
-            if ($connection->maxSendBufferSize <= strlen($connection->tmpWebsocketData)) {
+            if ($connection->maxSendBufferSize <= \strlen($connection->tmpWebsocketData)) {
                 if ($connection->onBufferFull) {
                     try {
-                        call_user_func($connection->onBufferFull, $connection);
+                        \call_user_func($connection->onBufferFull, $connection);
                     } catch (\Exception $e) {
                         Worker::log($e);
                         exit(250);
@@ -304,21 +304,21 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
      */
     public static function decode($buffer, ConnectionInterface $connection)
     {
-        $len = ord($buffer[1]) & 127;
+        $len = \ord($buffer[1]) & 127;
         if ($len === 126) {
-            $masks = substr($buffer, 4, 4);
-            $data  = substr($buffer, 8);
+            $masks = \substr($buffer, 4, 4);
+            $data  = \substr($buffer, 8);
         } else {
             if ($len === 127) {
-                $masks = substr($buffer, 10, 4);
-                $data  = substr($buffer, 14);
+                $masks = \substr($buffer, 10, 4);
+                $data  = \substr($buffer, 14);
             } else {
-                $masks = substr($buffer, 2, 4);
-                $data  = substr($buffer, 6);
+                $masks = \substr($buffer, 2, 4);
+                $data  = \substr($buffer, 6);
             }
         }
-        $dataLength = strlen($data);
-        $masks = str_repeat($masks, floor($dataLength / 4)) . substr($masks, 0, $dataLength % 4);
+        $dataLength = \strlen($data);
+        $masks = \str_repeat($masks, \floor($dataLength / 4)) . \substr($masks, 0, $dataLength % 4);
         $decoded = $data ^ $masks;
         if ($connection->websocketCurrentFrameLength) {
             $connection->websocketDataBuffer .= $decoded;
@@ -342,9 +342,9 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
     protected static function dealHandshake($buffer, $connection)
     {
         // HTTP protocol.
-        if (0 === strpos($buffer, 'GET')) {
+        if (0 === \strpos($buffer, 'GET')) {
             // Find \r\n\r\n.
-            $heder_end_pos = strpos($buffer, "\r\n\r\n");
+            $heder_end_pos = \strpos($buffer, "\r\n\r\n");
             if (!$heder_end_pos) {
                 return 0;
             }
@@ -352,7 +352,7 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
 
             // Get Sec-WebSocket-Key.
             $Sec_WebSocket_Key = '';
-            if (preg_match("/Sec-WebSocket-Key: *(.*?)\r\n/i", $buffer, $match)) {
+            if (\preg_match("/Sec-WebSocket-Key: *(.*?)\r\n/i", $buffer, $match)) {
                 $Sec_WebSocket_Key = $match[1];
             } else {
                 $connection->send("HTTP/1.1 200 Websocket\r\nServer: workerman/".Worker::VERSION."\r\n\r\n<div style=\"text-align:center\"><h1>Websocket</h1><hr>powerd by <a href=\"https://www.workerman.net\">workerman ".Worker::VERSION."</a></div>",
@@ -361,7 +361,7 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                 return 0;
             }
             // Calculation websocket key.
-            $new_key = base64_encode(sha1($Sec_WebSocket_Key . "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true));
+            $new_key = \base64_encode(sha1($Sec_WebSocket_Key . "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true));
             // Handshake response data.
             $handshake_message = "HTTP/1.1 101 Switching Protocols\r\n";
             $handshake_message .= "Upgrade: websocket\r\n";
@@ -389,7 +389,7 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
             if (isset($connection->onWebSocketConnect) || isset($connection->worker->onWebSocketConnect)) {
                 static::parseHttpHeader($buffer);
                 try {
-                    call_user_func(isset($connection->onWebSocketConnect)?$connection->onWebSocketConnect:$connection->worker->onWebSocketConnect, $connection, $buffer);
+                    \call_user_func(isset($connection->onWebSocketConnect)?$connection->onWebSocketConnect:$connection->worker->onWebSocketConnect, $connection, $buffer);
                 } catch (\Exception $e) {
                     Worker::log($e);
                     exit(250);
@@ -397,15 +397,15 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                     Worker::log($e);
                     exit(250);
                 }
-                if (!empty($_SESSION) && class_exists('\GatewayWorker\Lib\Context')) {
+                if (!empty($_SESSION) && \class_exists('\GatewayWorker\Lib\Context')) {
                     $connection->session = \GatewayWorker\Lib\Context::sessionEncode($_SESSION);
                 }
                 $_GET = $_SERVER = $_SESSION = $_COOKIE = array();
 
                 if (isset($connection->headers)) {
-                    if (is_array($connection->headers))  {
+                    if (\is_array($connection->headers))  {
                         foreach ($connection->headers as $header) {
-                            if (strpos($header, 'Server:') === 0) {
+                            if (\strpos($header, 'Server:') === 0) {
                                 $has_server_header = true;
                             }
                             $handshake_message .= "$header\r\n";
@@ -428,15 +428,15 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                 $connection->send($connection->tmpWebsocketData, true);
                 $connection->tmpWebsocketData = '';
             }
-            if (strlen($buffer) > $header_length) {
-                return static::input(substr($buffer, $header_length), $connection);
+            if (\strlen($buffer) > $header_length) {
+                return static::input(\substr($buffer, $header_length), $connection);
             }
             return 0;
         } // Is flash policy-file-request.
-        elseif (0 === strpos($buffer, '<polic')) {
+        elseif (0 === \strpos($buffer, '<polic')) {
             $policy_xml = '<?xml version="1.0"?><cross-domain-policy><site-control permitted-cross-domain-policies="all"/><allow-access-from domain="*" to-ports="*"/></cross-domain-policy>' . "\0";
             $connection->send($policy_xml, true);
-            $connection->consumeRecvBuffer(strlen($buffer));
+            $connection->consumeRecvBuffer(\strlen($buffer));
             return 0;
         }
         // Bad websocket handshake request.
@@ -455,14 +455,14 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
     protected static function parseHttpHeader($buffer)
     {
         // Parse headers.
-        list($http_header, ) = explode("\r\n\r\n", $buffer, 2);
-        $header_data = explode("\r\n", $http_header);
+        list($http_header, ) = \explode("\r\n\r\n", $buffer, 2);
+        $header_data = \explode("\r\n", $http_header);
 
         if ($_SERVER) {
             $_SERVER = array();
         }
 
-        list($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], $_SERVER['SERVER_PROTOCOL']) = explode(' ',
+        list($_SERVER['REQUEST_METHOD'], $_SERVER['REQUEST_URI'], $_SERVER['SERVER_PROTOCOL']) = \explode(' ',
             $header_data[0]);
 
         unset($header_data[0]);
@@ -471,14 +471,14 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
             if (empty($content)) {
                 continue;
             }
-            list($key, $value)       = explode(':', $content, 2);
-            $key                     = str_replace('-', '_', strtoupper($key));
-            $value                   = trim($value);
+            list($key, $value)       = \explode(':', $content, 2);
+            $key                     = \str_replace('-', '_', strtoupper($key));
+            $value                   = \trim($value);
             $_SERVER['HTTP_' . $key] = $value;
             switch ($key) {
                 // HTTP_HOST
                 case 'HOST':
-                    $tmp                    = explode(':', $value);
+                    $tmp                    = \explode(':', $value);
                     $_SERVER['SERVER_NAME'] = $tmp[0];
                     if (isset($tmp[1])) {
                         $_SERVER['SERVER_PORT'] = $tmp[1];
@@ -486,16 +486,16 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
                     break;
                 // cookie
                 case 'COOKIE':
-                    parse_str(str_replace('; ', '&', $_SERVER['HTTP_COOKIE']), $_COOKIE);
+                    \parse_str(\str_replace('; ', '&', $_SERVER['HTTP_COOKIE']), $_COOKIE);
                     break;
             }
         }
 
         // QUERY_STRING
-        $_SERVER['QUERY_STRING'] = parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
+        $_SERVER['QUERY_STRING'] = \parse_url($_SERVER['REQUEST_URI'], PHP_URL_QUERY);
         if ($_SERVER['QUERY_STRING']) {
             // $GET
-            parse_str($_SERVER['QUERY_STRING'], $_GET);
+            \parse_str($_SERVER['QUERY_STRING'], $_GET);
         } else {
             $_SERVER['QUERY_STRING'] = '';
         }

+ 46 - 46
Protocols/Ws.php

@@ -53,7 +53,7 @@ class Ws
         if ($connection->handshakeStep === 1) {
             return self::dealHandshake($buffer, $connection);
         }
-        $recv_len = strlen($buffer);
+        $recv_len = \strlen($buffer);
         if ($recv_len < 2) {
             return 0;
         }
@@ -66,8 +66,8 @@ class Ws
             }
         } else {
 
-            $firstbyte    = ord($buffer[0]);
-            $secondbyte   = ord($buffer[1]);
+            $firstbyte    = \ord($buffer[0]);
+            $secondbyte   = \ord($buffer[1]);
             $data_len     = $secondbyte & 127;
             $is_fin_frame = $firstbyte >> 7;
             $masked       = $secondbyte >> 7;
@@ -94,7 +94,7 @@ class Ws
                     // Try to emit onWebSocketClose callback.
                     if (isset($connection->onWebSocketClose)) {
                         try {
-                            call_user_func($connection->onWebSocketClose, $connection);
+                            \call_user_func($connection->onWebSocketClose, $connection);
                         } catch (\Exception $e) {
                             Worker::log($e);
                             exit(250);
@@ -121,22 +121,22 @@ class Ws
             }
             // Calculate packet length.
             if ($data_len === 126) {
-                if (strlen($buffer) < 4) {
+                if (\strlen($buffer) < 4) {
                     return 0;
                 }
-                $pack = unpack('nn/ntotal_len', $buffer);
+                $pack = \unpack('nn/ntotal_len', $buffer);
                 $current_frame_length = $pack['total_len'] + 4;
             } else if ($data_len === 127) {
-                if (strlen($buffer) < 10) {
+                if (\strlen($buffer) < 10) {
                     return 0;
                 }
-                $arr = unpack('n/N2c', $buffer);
+                $arr = \unpack('n/N2c', $buffer);
                 $current_frame_length = $arr['c1']*4294967296 + $arr['c2'] + 10;
             } else {
                 $current_frame_length = $data_len + 2;
             }
 
-            $total_package_size = strlen($connection->websocketDataBuffer) + $current_frame_length;
+            $total_package_size = \strlen($connection->websocketDataBuffer) + $current_frame_length;
             if ($total_package_size > $connection->maxPackageSize) {
                 Worker::safeEcho("error package. package_length=$total_package_size\n");
                 $connection->close();
@@ -146,13 +146,13 @@ class Ws
             if ($is_fin_frame) {
                 if ($opcode === 0x9) {
                     if ($recv_len >= $current_frame_length) {
-                        $ping_data = static::decode(substr($buffer, 0, $current_frame_length), $connection);
+                        $ping_data = static::decode(\substr($buffer, 0, $current_frame_length), $connection);
                         $connection->consumeRecvBuffer($current_frame_length);
                         $tmp_connection_type = isset($connection->websocketType) ? $connection->websocketType : static::BINARY_TYPE_BLOB;
                         $connection->websocketType = "\x8a";
                         if (isset($connection->onWebSocketPing)) {
                             try {
-                                call_user_func($connection->onWebSocketPing, $connection, $ping_data);
+                                \call_user_func($connection->onWebSocketPing, $connection, $ping_data);
                             } catch (\Exception $e) {
                                 Worker::log($e);
                                 exit(250);
@@ -165,21 +165,21 @@ class Ws
                         }
                         $connection->websocketType = $tmp_connection_type;
                         if ($recv_len > $current_frame_length) {
-                            return static::input(substr($buffer, $current_frame_length), $connection);
+                            return static::input(\substr($buffer, $current_frame_length), $connection);
                         }
                     }
                     return 0;
 
                 } else if ($opcode === 0xa) {
                     if ($recv_len >= $current_frame_length) {
-                        $pong_data = static::decode(substr($buffer, 0, $current_frame_length), $connection);
+                        $pong_data = static::decode(\substr($buffer, 0, $current_frame_length), $connection);
                         $connection->consumeRecvBuffer($current_frame_length);
                         $tmp_connection_type = isset($connection->websocketType) ? $connection->websocketType : static::BINARY_TYPE_BLOB;
                         $connection->websocketType = "\x8a";
                         // Try to emit onWebSocketPong callback.
                         if (isset($connection->onWebSocketPong)) {
                             try {
-                                call_user_func($connection->onWebSocketPong, $connection, $pong_data);
+                                \call_user_func($connection->onWebSocketPong, $connection, $pong_data);
                             } catch (\Exception $e) {
                                 Worker::log($e);
                                 exit(250);
@@ -190,7 +190,7 @@ class Ws
                         }
                         $connection->websocketType = $tmp_connection_type;
                         if ($recv_len > $current_frame_length) {
-                            return static::input(substr($buffer, $current_frame_length), $connection);
+                            return static::input(\substr($buffer, $current_frame_length), $connection);
                         }
                     }
                     return 0;
@@ -208,12 +208,12 @@ class Ws
             return 0;
         } // The length of the received data is greater than the length of a frame.
         elseif ($connection->websocketCurrentFrameLength < $recv_len) {
-            self::decode(substr($buffer, 0, $connection->websocketCurrentFrameLength), $connection);
+            self::decode(\substr($buffer, 0, $connection->websocketCurrentFrameLength), $connection);
             $connection->consumeRecvBuffer($connection->websocketCurrentFrameLength);
             $current_frame_length                    = $connection->websocketCurrentFrameLength;
             $connection->websocketCurrentFrameLength = 0;
             // Continue to read next frame.
-            return self::input(substr($buffer, $current_frame_length), $connection);
+            return self::input(\substr($buffer, $current_frame_length), $connection);
         } // The length of the received data is less than the length of a frame.
         else {
             return 0;
@@ -240,28 +240,28 @@ class Ws
         $mask_key = "\x00\x00\x00\x00";
 
         $pack = '';
-        $length = $length_flag = strlen($payload);
+        $length = $length_flag = \strlen($payload);
         if (65535 < $length) {
-            $pack   = pack('NN', ($length & 0xFFFFFFFF00000000) >> 32, $length & 0x00000000FFFFFFFF);
+            $pack   = \pack('NN', ($length & 0xFFFFFFFF00000000) >> 32, $length & 0x00000000FFFFFFFF);
             $length_flag = 127;
         } else if (125 < $length) {
-            $pack   = pack('n*', $length);
+            $pack   = \pack('n*', $length);
             $length_flag = 126;
         }
 
         $head = ($mask << 7) | $length_flag;
-        $head = $connection->websocketType . chr($head) . $pack;
+        $head = $connection->websocketType . \chr($head) . $pack;
 
         $frame = $head . $mask_key;
         // append payload to frame:
-        $mask_key = str_repeat($mask_key, floor($length / 4)) . substr($mask_key, 0, $length % 4);
+        $mask_key = \str_repeat($mask_key, \floor($length / 4)) . \substr($mask_key, 0, $length % 4);
         $frame .= $payload ^ $mask_key;
         if ($connection->handshakeStep === 1) {
             // If buffer has already full then discard the current package.
-            if (strlen($connection->tmpWebsocketData) > $connection->maxSendBufferSize) {
+            if (\strlen($connection->tmpWebsocketData) > $connection->maxSendBufferSize) {
                 if ($connection->onError) {
                     try {
-                        call_user_func($connection->onError, $connection, WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
+                        \call_user_func($connection->onError, $connection, WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
                     } catch (\Exception $e) {
                         Worker::log($e);
                         exit(250);
@@ -274,10 +274,10 @@ class Ws
             }
             $connection->tmpWebsocketData = $connection->tmpWebsocketData . $frame;
             // Check buffer is full.
-            if ($connection->maxSendBufferSize <= strlen($connection->tmpWebsocketData)) {
+            if ($connection->maxSendBufferSize <= \strlen($connection->tmpWebsocketData)) {
                 if ($connection->onBufferFull) {
                     try {
-                        call_user_func($connection->onBufferFull, $connection);
+                        \call_user_func($connection->onBufferFull, $connection);
                     } catch (\Exception $e) {
                         Worker::log($e);
                         exit(250);
@@ -301,14 +301,14 @@ class Ws
      */
     public static function decode($bytes, $connection)
     {
-        $data_length = ord($bytes[1]);
+        $data_length = \ord($bytes[1]);
 
         if ($data_length === 126) {
-            $decoded_data = substr($bytes, 4);
+            $decoded_data = \substr($bytes, 4);
         } else if ($data_length === 127) {
-            $decoded_data = substr($bytes, 10);
+            $decoded_data = \substr($bytes, 10);
         } else {
-            $decoded_data = substr($bytes, 2);
+            $decoded_data = \substr($bytes, 2);
         }
         if ($connection->websocketCurrentFrameLength) {
             $connection->websocketDataBuffer .= $decoded_data;
@@ -364,22 +364,22 @@ class Ws
         $port = $connection->getRemotePort();
         $host = $port === 80 ? $connection->getRemoteHost() : $connection->getRemoteHost() . ':' . $port;
         // Handshake header.
-        $connection->websocketSecKey = base64_encode(md5(mt_rand(), true));
+        $connection->websocketSecKey = \base64_encode(md5(\mt_rand(), true));
         $user_header = isset($connection->headers) ? $connection->headers :
             (isset($connection->wsHttpHeader) ? $connection->wsHttpHeader : null);
         $user_header_str = '';
         if (!empty($user_header)) {
-            if (is_array($user_header)){
+            if (\is_array($user_header)){
                 foreach($user_header as $k=>$v){
                     $user_header_str .= "$k: $v\r\n";
                 }
             } else {
                 $user_header_str .= $user_header;
             }
-            $user_header_str = "\r\n".trim($user_header_str);
+            $user_header_str = "\r\n".\trim($user_header_str);
         }
         $header = 'GET ' . $connection->getRemoteURI() . " HTTP/1.1\r\n".
-        (!preg_match("/\nHost:/i", $user_header_str) ? "Host: $host\r\n" : '').
+        (!\preg_match("/\nHost:/i", $user_header_str) ? "Host: $host\r\n" : '').
         "Connection: Upgrade\r\n".
         "Upgrade: websocket\r\n".
         (isset($connection->websocketOrigin) ? "Origin: ".$connection->websocketOrigin."\r\n":'').
@@ -402,17 +402,17 @@ class Ws
      */
     public static function dealHandshake($buffer, $connection)
     {
-        $pos = strpos($buffer, "\r\n\r\n");
+        $pos = \strpos($buffer, "\r\n\r\n");
         if ($pos) {
             //checking Sec-WebSocket-Accept
-            if (preg_match("/Sec-WebSocket-Accept: *(.*?)\r\n/i", $buffer, $match)) {
-                if ($match[1] !== base64_encode(sha1($connection->websocketSecKey . "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true))) {
-                    Worker::safeEcho("Sec-WebSocket-Accept not match. Header:\n" . substr($buffer, 0, $pos) . "\n");
+            if (\preg_match("/Sec-WebSocket-Accept: *(.*?)\r\n/i", $buffer, $match)) {
+                if ($match[1] !== \base64_encode(sha1($connection->websocketSecKey . "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true))) {
+                    Worker::safeEcho("Sec-WebSocket-Accept not match. Header:\n" . \substr($buffer, 0, $pos) . "\n");
                     $connection->close();
                     return 0;
                 }
             } else {
-                Worker::safeEcho("Sec-WebSocket-Accept not found. Header:\n" . substr($buffer, 0, $pos) . "\n");
+                Worker::safeEcho("Sec-WebSocket-Accept not found. Header:\n" . \substr($buffer, 0, $pos) . "\n");
                 $connection->close();
                 return 0;
             }
@@ -420,8 +420,8 @@ class Ws
             // handshake complete
 
             // Get WebSocket subprotocol (if specified by server)
-            if (preg_match("/Sec-WebSocket-Protocol: *(.*?)\r\n/i", $buffer, $match)) {
-                $connection->WSServerProtocol = trim($match[1]);
+            if (\preg_match("/Sec-WebSocket-Protocol: *(.*?)\r\n/i", $buffer, $match)) {
+                $connection->WSServerProtocol = \trim($match[1]);
             }
 
             $connection->handshakeStep = 2;
@@ -429,7 +429,7 @@ class Ws
             // Try to emit onWebSocketConnect callback.
             if (isset($connection->onWebSocketConnect)) {
                 try {
-                    call_user_func($connection->onWebSocketConnect, $connection, substr($buffer, 0, $handshake_response_length));
+                    \call_user_func($connection->onWebSocketConnect, $connection, \substr($buffer, 0, $handshake_response_length));
                 } catch (\Exception $e) {
                     Worker::log($e);
                     exit(250);
@@ -441,7 +441,7 @@ class Ws
             // Headbeat.
             if (!empty($connection->websocketPingInterval)) {
                 $connection->websocketPingTimer = Timer::add($connection->websocketPingInterval, function() use ($connection){
-                    if (false === $connection->send(pack('H*', '898000000000'), true)) {
+                    if (false === $connection->send(\pack('H*', '898000000000'), true)) {
                         Timer::del($connection->websocketPingTimer);
                         $connection->websocketPingTimer = null;
                     }
@@ -453,8 +453,8 @@ class Ws
                 $connection->send($connection->tmpWebsocketData, true);
                 $connection->tmpWebsocketData = '';
             }
-            if (strlen($buffer) > $handshake_response_length) {
-                return self::input(substr($buffer, $handshake_response_length), $connection);
+            if (\strlen($buffer) > $handshake_response_length) {
+                return self::input(\substr($buffer, $handshake_response_length), $connection);
             }
         }
         return 0;
@@ -465,7 +465,7 @@ class Ws
     }
 
     public static function WSGetServerProtocol($connection) {
-	return (property_exists($connection, 'WSServerProtocol')?$connection->WSServerProtocol:null);
+	return (\property_exists($connection, 'WSServerProtocol')?$connection->WSServerProtocol:null);
     }
 
 }

+ 35 - 35
WebServer.php

@@ -52,9 +52,9 @@ class WebServer extends Worker
      */
     public function addRoot($domain, $config)
     {
-	if (is_string($config)) {
+        if (is_string($config)) {
             $config = array('root' => $config);
-	}
+        }
         $this->serverRoot[$domain] = $config;
     }
 
@@ -66,7 +66,7 @@ class WebServer extends Worker
      */
     public function __construct($socket_name, $context_option = array())
     {
-        list(, $address) = explode(':', $socket_name, 2);
+        list(, $address) = \explode(':', $socket_name, 2);
         parent::__construct('http:' . $address, $context_option);
         $this->name = 'WebServer';
     }
@@ -102,7 +102,7 @@ class WebServer extends Worker
         // Try to emit onWorkerStart callback.
         if ($this->_onWorkerStart) {
             try {
-                call_user_func($this->_onWorkerStart, $this);
+                \call_user_func($this->_onWorkerStart, $this);
             } catch (\Exception $e) {
                 self::log($e);
                 exit(250);
@@ -121,20 +121,20 @@ class WebServer extends Worker
     public function initMimeTypeMap()
     {
         $mime_file = Http::getMimeTypesFile();
-        if (!is_file($mime_file)) {
+        if (!\is_file($mime_file)) {
             $this->log("$mime_file mime.type file not fond");
             return;
         }
-        $items = file($mime_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
-        if (!is_array($items)) {
+        $items = \file($mime_file, FILE_IGNORE_NEW_LINES | FILE_SKIP_EMPTY_LINES);
+        if (!\is_array($items)) {
             $this->log("get $mime_file mime.type content fail");
             return;
         }
         foreach ($items as $content) {
-            if (preg_match("/\s*(\S+)\s+(\S.+)/", $content, $match)) {
+            if (\preg_match("/\s*(\S+)\s+(\S.+)/", $content, $match)) {
                 $mime_type                      = $match[1];
                 $workerman_file_extension_var   = $match[2];
-                $workerman_file_extension_array = explode(' ', substr($workerman_file_extension_var, 0, -1));
+                $workerman_file_extension_array = \explode(' ', \substr($workerman_file_extension_var, 0, -1));
                 foreach ($workerman_file_extension_array as $workerman_file_extension) {
                     self::$mimeTypeMap[$workerman_file_extension] = $mime_type;
                 }
@@ -151,10 +151,10 @@ class WebServer extends Worker
     public function onMessage($connection)
     {
         // REQUEST_URI.
-        $workerman_url_info = parse_url('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
+        $workerman_url_info = \parse_url('http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI']);
         if (!$workerman_url_info) {
             Http::header('HTTP/1.1 400 Bad Request');
-            if (strtolower($_SERVER['HTTP_CONNECTION']) === "keep-alive") {
+            if (\strtolower($_SERVER['HTTP_CONNECTION']) === "keep-alive") {
                 $connection->send('<h1>400 Bad Request</h1>');
             } else {
                 $connection->close('<h1>400 Bad Request</h1>');
@@ -164,10 +164,10 @@ class WebServer extends Worker
 
         $workerman_path = isset($workerman_url_info['path']) ? $workerman_url_info['path'] : '/';
 
-        $workerman_path_info      = pathinfo($workerman_path);
+        $workerman_path_info      = \pathinfo($workerman_path);
         $workerman_file_extension = isset($workerman_path_info['extension']) ? $workerman_path_info['extension'] : '';
         if ($workerman_file_extension === '') {
-            $workerman_path           = ($len = strlen($workerman_path)) && $workerman_path[$len - 1] === '/' ? $workerman_path . 'index.php' : $workerman_path . '/index.php';
+            $workerman_path           = ($len = \strlen($workerman_path)) && $workerman_path[$len - 1] === '/' ? $workerman_path . 'index.php' : $workerman_path . '/index.php';
             $workerman_file_extension = 'php';
         }
 
@@ -177,22 +177,22 @@ class WebServer extends Worker
 		if(isset($workerman_siteConfig['additionHeader'])){
 			Http::header($workerman_siteConfig['additionHeader']);
 		}
-        if ($workerman_file_extension === 'php' && !is_file($workerman_file)) {
+        if ($workerman_file_extension === 'php' && !\is_file($workerman_file)) {
             $workerman_file = "$workerman_root_dir/index.php";
-            if (!is_file($workerman_file)) {
+            if (!\is_file($workerman_file)) {
                 $workerman_file           = "$workerman_root_dir/index.html";
                 $workerman_file_extension = 'html';
             }
         }
 
         // File exsits.
-        if (is_file($workerman_file)) {
+        if (\is_file($workerman_file)) {
             // Security check.
-            if ((!($workerman_request_realpath = realpath($workerman_file)) || !($workerman_root_dir_realpath = realpath($workerman_root_dir))) || 0 !== strpos($workerman_request_realpath,
+            if ((!($workerman_request_realpath = \realpath($workerman_file)) || !($workerman_root_dir_realpath = \realpath($workerman_root_dir))) || 0 !== \strpos($workerman_request_realpath,
                     $workerman_root_dir_realpath)
             ) {
                 Http::header('HTTP/1.1 400 Bad Request');
-                if (strtolower($_SERVER['HTTP_CONNECTION']) === "keep-alive") {
+                if (\strtolower($_SERVER['HTTP_CONNECTION']) === "keep-alive") {
                     $connection->send('<h1>400 Bad Request</h1>');
                 } else {
                     $connection->close('<h1>400 Bad Request</h1>');
@@ -200,14 +200,14 @@ class WebServer extends Worker
                 return;
             }
 
-            $workerman_file = realpath($workerman_file);
+            $workerman_file = \realpath($workerman_file);
 
             // Request php file.
             if ($workerman_file_extension === 'php') {
                 $workerman_cwd = getcwd();
-                chdir($workerman_root_dir);
-                ini_set('display_errors', 'off');
-                ob_start();
+                \chdir($workerman_root_dir);
+                \ini_set('display_errors', 'off');
+                \ob_start();
                 // Try to include php file.
                 try {
                     // $_SERVER.
@@ -220,14 +220,14 @@ class WebServer extends Worker
                         Worker::safeEcho($e);
                     }
                 }
-                $content = ob_get_clean();
-                ini_set('display_errors', 'on');
-                if (strtolower($_SERVER['HTTP_CONNECTION']) === "keep-alive") {
+                $content = \ob_get_clean();
+                \ini_set('display_errors', 'on');
+                if (\strtolower($_SERVER['HTTP_CONNECTION']) === "keep-alive") {
                     $connection->send($content);
                 } else {
                     $connection->close($content);
                 }
-                chdir($workerman_cwd);
+                \chdir($workerman_cwd);
                 return;
             }
 
@@ -237,11 +237,11 @@ class WebServer extends Worker
             // 404
             Http::header("HTTP/1.1 404 Not Found");
 			if(isset($workerman_siteConfig['custom404']) && file_exists($workerman_siteConfig['custom404'])){
-				$html404 = file_get_contents($workerman_siteConfig['custom404']);
+				$html404 = \file_get_contents($workerman_siteConfig['custom404']);
 			}else{
 				$html404 = '<html><head><title>404 File not found</title></head><body><center><h3>404 Not Found</h3></center></body></html>';
 			}
-            if (strtolower($_SERVER['HTTP_CONNECTION']) === "keep-alive") {
+            if (\strtolower($_SERVER['HTTP_CONNECTION']) === "keep-alive") {
                 $connection->send($html404);
             } else {
                 $connection->close($html404);
@@ -254,14 +254,14 @@ class WebServer extends Worker
     {
         // Check 304.
         $info = stat($file_path);
-        $modified_time = $info ? date('D, d M Y H:i:s', $info['mtime']) . ' ' . date_default_timezone_get() : '';
+        $modified_time = $info ? \date('D, d M Y H:i:s', $info['mtime']) . ' ' . \date_default_timezone_get() : '';
         if (!empty($_SERVER['HTTP_IF_MODIFIED_SINCE']) && $info) {
             // Http 304.
             if ($modified_time === $_SERVER['HTTP_IF_MODIFIED_SINCE']) {
                 // 304
                 Http::header('HTTP/1.1 304 Not Modified');
                 // Send nothing but http headers..
-                if (strtolower($_SERVER['HTTP_CONNECTION']) === "keep-alive") {
+                if (\strtolower($_SERVER['HTTP_CONNECTION']) === "keep-alive") {
                     $connection->send('');
                 } else {
                     $connection->close('');
@@ -274,8 +274,8 @@ class WebServer extends Worker
         if ($modified_time) {
             $modified_time = "Last-Modified: $modified_time\r\n";
         }
-        $file_size = filesize($file_path);
-        $file_info = pathinfo($file_path);
+        $file_size = \filesize($file_path);
+        $file_info = \pathinfo($file_path);
         $extension = isset($file_info['extension']) ? $file_info['extension'] : '';
         $file_name = isset($file_info['filename']) ? $file_info['filename'] : '';
         $header = "HTTP/1.1 200 OK\r\n";
@@ -290,19 +290,19 @@ class WebServer extends Worker
         $header .= "Content-Length: $file_size\r\n\r\n";
         $trunk_limit_size = 1024*1024;
         if ($file_size < $trunk_limit_size) {
-            return $connection->send($header.file_get_contents($file_path), true);
+            return $connection->send($header.\file_get_contents($file_path), true);
         }
         $connection->send($header, true);
 
         // Read file content from disk piece by piece and send to client.
-        $connection->fileHandler = fopen($file_path, 'r');
+        $connection->fileHandler = \fopen($file_path, 'r');
         $do_write = function()use($connection)
         {
             // Send buffer not full.
             while(empty($connection->bufferFull))
             {
                 // Read from disk.
-                $buffer = fread($connection->fileHandler, 8192);
+                $buffer = \fread($connection->fileHandler, 8192);
                 // Read eof.
                 if($buffer === '' || $buffer === false)
                 {

File diff suppressed because it is too large
+ 215 - 215
Worker.php


Some files were not shown because too many files changed in this diff