فهرست منبع

新的workerman

lizhen_gitee 1 سال پیش
والد
کامیت
9815cab9ae

+ 4 - 0
vendor/workerman/gateway-worker/.gitignore

@@ -0,0 +1,4 @@
+.buildpath
+.project
+.settings
+.idea

+ 1 - 2
vendor/workerman/gateway-worker/src/BusinessWorker.php

@@ -12,8 +12,7 @@
  * @license   http://www.opensource.org/licenses/mit-license.php MIT License
  */
 namespace GatewayWorker;
-require_once __DIR__.'/../../workerman/Worker.php';
-require_once __DIR__.'/Lib/Gateway.php';
+
 use Workerman\Connection\TcpConnection;
 
 use Workerman\Worker;

+ 15 - 3
vendor/workerman/gateway-worker/src/Gateway.php

@@ -12,7 +12,7 @@
  * @license   http://www.opensource.org/licenses/mit-license.php MIT License
  */
 namespace GatewayWorker;
-require_once __DIR__.'/Protocols/GatewayProtocol.php';
+
 use GatewayWorker\Lib\Context;
 
 use Workerman\Connection\TcpConnection;
@@ -575,7 +575,13 @@ class Gateway extends Worker
             // 向某客户端发送数据,Gateway::sendToClient($client_id, $message);
             case GatewayProtocol::CMD_SEND_TO_ONE:
                 if (isset($this->_clientConnections[$data['connection_id']])) {
-                    $this->_clientConnections[$data['connection_id']]->send($data['body']);
+                    $raw = (bool)($data['flag'] & GatewayProtocol::FLAG_NOT_CALL_ENCODE);
+                    $body = $data['body'];
+                    if (!$raw && $this->protocolAccelerate && $this->protocol) {
+                        $body = $this->preEncodeForClient($body);
+                        $raw = true;
+                    }
+                    $this->_clientConnections[$data['connection_id']]->send($body, $raw);
                 }
                 return;
             // 踢出用户,Gateway::closeClient($client_id, $message);
@@ -766,12 +772,18 @@ class Gateway extends Worker
                 return;
             // 发送数据给 uid Gateway::sendToUid($uid, $msg);
             case GatewayProtocol::CMD_SEND_TO_UID:
+                $raw = (bool)($data['flag'] & GatewayProtocol::FLAG_NOT_CALL_ENCODE);
+                $body = $data['body'];
+                if (!$raw && $this->protocolAccelerate && $this->protocol) {
+                    $body = $this->preEncodeForClient($body);
+                    $raw = true;
+                }
                 $uid_array = json_decode($data['ext_data'], true);
                 foreach ($uid_array as $uid) {
                     if (!empty($this->_uidConnections[$uid])) {
                         foreach ($this->_uidConnections[$uid] as $connection) {
                             /** @var TcpConnection $connection */
-                            $connection->send($data['body']);
+                            $connection->send($body, $raw);
                         }
                     }
                 }

+ 3 - 4
vendor/workerman/gateway-worker/src/Lib/DbConnection.php

@@ -1682,17 +1682,16 @@ class DbConnection
      * @param string $db_name
      * @param string $charset
      */
-    public function __construct($config)
+    public function __construct($host, $port, $user, $password, $db_name, $charset = 'utf8')
     {
-        /*$this->settings = array(
+        $this->settings = array(
             'host'     => $host,
             'port'     => $port,
             'user'     => $user,
             'password' => $password,
             'dbname'   => $db_name,
             'charset'  => $charset,
-        );*/
-        $this->settings = $config;
+        );
         $this->connect();
     }
 

+ 28 - 18
vendor/workerman/gateway-worker/src/Lib/Gateway.php

@@ -137,24 +137,26 @@ class Gateway
     /**
      * 向某个client_id对应的连接发消息
      *
-     * @param int    $client_id
+     * @param string    $client_id
      * @param string $message
+     * @param bool $raw
      * @return void
      */
-    public static function sendToClient($client_id, $message)
+    public static function sendToClient($client_id, $message, $raw = false)
     {
-        return static::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_SEND_TO_ONE, $message);
+        return static::sendCmdAndMessageToClient($client_id, GatewayProtocol::CMD_SEND_TO_ONE, $message, $raw);
     }
 
     /**
      * 向当前客户端连接发送消息
      *
      * @param string $message
+     * @param bool $raw
      * @return bool
      */
-    public static function sendToCurrentClient($message)
+    public static function sendToCurrentClient($message, $raw = false)
     {
-        return static::sendCmdAndMessageToClient(null, GatewayProtocol::CMD_SEND_TO_ONE, $message);
+        return static::sendCmdAndMessageToClient(null, GatewayProtocol::CMD_SEND_TO_ONE, $message, $raw);
     }
 
     /**
@@ -171,7 +173,7 @@ class Gateway
     /**
      * 判断client_id对应的连接是否在线
      *
-     * @param int $client_id
+     * @param string $client_id
      * @return int 0|1
      */
     public static function isOnline($client_id)
@@ -837,7 +839,7 @@ class Gateway
     /**
      * 踢掉某个客户端并直接立即销毁相关连接
      *
-     * @param int $client_id
+     * @param string $client_id
      * @return bool
      */
     public static function destoryClient($client_id)
@@ -873,7 +875,7 @@ class Gateway
     /**
      * 将 client_id 与 uid 绑定
      *
-     * @param int        $client_id
+     * @param string        $client_id
      * @param int|string $uid
      * @return void
      */
@@ -885,7 +887,7 @@ class Gateway
     /**
      * 将 client_id 与 uid 解除绑定
      *
-     * @param int        $client_id
+     * @param string        $client_id
      * @param int|string $uid
      * @return void
      */
@@ -897,7 +899,7 @@ class Gateway
     /**
      * 将 client_id 加入组
      *
-     * @param int        $client_id
+     * @param string        $client_id
      * @param int|string $group
      * @return void
      */
@@ -910,7 +912,7 @@ class Gateway
     /**
      * 将 client_id 离开组
      *
-     * @param int        $client_id
+     * @param string        $client_id
      * @param int|string $group
      *
      * @return void
@@ -944,14 +946,18 @@ class Gateway
      *
      * @param int|string|array $uid
      * @param string           $message
+     * @param bool $raw
      *
      * @return void
      */
-    public static function sendToUid($uid, $message)
+    public static function sendToUid($uid, $message, $raw = false)
     {
         $gateway_data         = GatewayProtocol::$empty;
         $gateway_data['cmd']  = GatewayProtocol::CMD_SEND_TO_UID;
         $gateway_data['body'] = $message;
+        if ($raw) {
+            $gateway_data['flag'] |= GatewayProtocol::FLAG_NOT_CALL_ENCODE;
+        }
 
         if (!is_array($uid)) {
             $uid = array($uid);
@@ -1025,7 +1031,7 @@ class Gateway
     /**
      * 更新 session,框架自动调用,开发者不要调用
      *
-     * @param int    $client_id
+     * @param string    $client_id
      * @param string $session_str
      * @return bool
      */
@@ -1037,7 +1043,7 @@ class Gateway
     /**
      * 设置 session,原session值会被覆盖
      *
-     * @param int   $client_id
+     * @param string   $client_id
      * @param array $session
      *
      * @return void
@@ -1054,7 +1060,7 @@ class Gateway
     /**
      * 更新 session,实际上是与老的session合并
      *
-     * @param int   $client_id
+     * @param string   $client_id
      * @param array $session
      *
      * @return void
@@ -1071,7 +1077,7 @@ class Gateway
     /**
      * 获取某个client_id的session
      *
-     * @param int   $client_id
+     * @param string   $client_id
      * @return mixed false表示出错、null表示用户不存在、array表示具体的session信息 
      */
     public static function getSession($client_id)
@@ -1095,13 +1101,14 @@ class Gateway
     /**
      * 向某个用户网关发送命令和消息
      *
-     * @param int    $client_id
+     * @param string    $client_id
      * @param int    $cmd
      * @param string $message
      * @param string $ext_data
+     * @param bool $raw
      * @return boolean
      */
-    protected static function sendCmdAndMessageToClient($client_id, $cmd, $message, $ext_data = '')
+    protected static function sendCmdAndMessageToClient($client_id, $cmd, $message, $ext_data = '', $raw = false)
     {
         // 如果是发给当前用户则直接获取上下文中的地址
         if ($client_id === Context::$client_id || $client_id === null) {
@@ -1122,6 +1129,9 @@ class Gateway
         if (!empty($ext_data)) {
             $gateway_data['ext_data'] = $ext_data;
         }
+        if ($raw) {
+            $gateway_data['flag'] |= GatewayProtocol::FLAG_NOT_CALL_ENCODE;
+        }
 
         return static::sendToGateway($address, $gateway_data);
     }

+ 1 - 1
vendor/workerman/gatewayclient/Gateway.php

@@ -36,7 +36,7 @@ class Gateway
      *
      * @var string|array
      */
-    public static $registerAddress = '127.0.0.1:2345';
+    public static $registerAddress = '127.0.0.1:1236';
 
     /**
      * 秘钥

+ 5 - 1
vendor/workerman/workerman/Events/Swoole.php

@@ -61,7 +61,11 @@ class Swoole implements EventInterface
                     $this->mapId = 0;
                 }
                 $mapId = $this->mapId++;
-                $timer_id = Timer::$method($fd * 1000,
+                $t = (int)($fd * 1000);
+                if ($t < 1) {
+                   $t = 1;   
+                }
+                $timer_id = Timer::$method($t,
                     function ($timer_id = null) use ($func, $args, $mapId) {
                         \call_user_func_array($func, $args);
                         // EV_TIMER_ONCE

+ 23 - 4
vendor/workerman/workerman/Protocols/Http/Request.php

@@ -392,7 +392,11 @@ class Request
     {
         $this->_data['headers'] = array();
         $raw_head = $this->rawHead();
-        $head_buffer = \substr($raw_head, \strpos($raw_head, "\r\n") + 2);
+        $end_line_position = \strpos($raw_head, "\r\n");
+        if ($end_line_position === false) {
+            return;
+        }
+        $head_buffer = \substr($raw_head, $end_line_position + 2);
         $cacheable = static::$_enableCache && !isset($head_buffer[2048]);
         if ($cacheable && isset(static::$_headerCache[$head_buffer])) {
             $this->_data['headers'] = static::$_headerCache[$head_buffer];
@@ -402,9 +406,16 @@ class Request
         foreach ($head_data as $content) {
             if (false !== \strpos($content, ':')) {
                 list($key, $value) = \explode(':', $content, 2);
-                $this->_data['headers'][\strtolower($key)] = \ltrim($value);
+                $key = \strtolower($key);
+                $value = \ltrim($value);
             } else {
-                $this->_data['headers'][\strtolower($content)] = '';
+                $key = \strtolower($content);
+                $value = '';
+            }
+            if (isset($this->_data['headers'][$key])) {
+                $this->_data['headers'][$key] = "{$this->_data['headers'][$key]},$value";
+            } else {
+                $this->_data['headers'][$key] = $value;
             }
         }
         if ($cacheable) {
@@ -504,7 +515,7 @@ class Request
                 switch ($header_key) {
                     case "content-disposition":
                         // Is file data.
-                        if (\preg_match('/name="(.*?)"; filename="(.*?)"$/i', $header_value, $match)) {
+                        if (\preg_match('/name="(.*?)"; filename="(.*?)"/i', $header_value, $match)) {
                             $error = 0;
                             $tmp_file = '';
                             $size = \strlen($boundary_value);
@@ -605,6 +616,14 @@ class Request
     }
 
     /**
+     * __toString.
+     */
+    public function __toString()
+    {
+        return $this->_buffer;
+    }
+
+    /**
      * __destruct.
      *
      * @return void

+ 1 - 1
vendor/workerman/workerman/README.md

@@ -77,7 +77,7 @@ $http_worker->onMessage = function ($connection, $request) {
     //$request->post();
     //$request->header();
     //$request->cookie();
-    //$requset->session();
+    //$request->session();
     //$request->uri();
     //$request->path();
     //$request->method();

+ 35 - 5
vendor/workerman/workerman/Worker.php

@@ -33,7 +33,7 @@ class Worker
      *
      * @var string
      */
-    const VERSION = '4.0.18';
+    const VERSION = '4.0.19';
 
     /**
      * Status starting.
@@ -909,9 +909,8 @@ class Worker
 
         // Get master process PID.
         $master_pid      = \is_file(static::$pidFile) ? \file_get_contents(static::$pidFile) : 0;
-        $master_is_alive = $master_pid && \posix_kill($master_pid, 0) && \posix_getpid() !== $master_pid;
         // Master is still alive?
-        if ($master_is_alive) {
+        if (static::checkMasterIsAlive($master_pid)) {
             if ($command === 'start') {
                 static::log("Workerman[$start_file] already running");
                 exit;
@@ -2214,13 +2213,14 @@ class Worker
         }
 
         // Turn reusePort on.
-        if (static::$_OS === \OS_TYPE_LINUX  // if linux
+        /*if (static::$_OS === \OS_TYPE_LINUX  // if linux
             && \version_compare(\PHP_VERSION,'7.0.0', 'ge') // if php >= 7.0.0
+            && \version_compare(php_uname('r'), '3.9', 'ge') // if kernel >=3.9
             && \strtolower(\php_uname('s')) !== 'darwin' // if not Mac OS
             && strpos($socket_name,'unix') !== 0) { // if not unix socket
 
             $this->reusePort = true;
-        }
+        }*/
     }
 
 
@@ -2560,4 +2560,34 @@ class Worker
         }
         return true;
     }
+
+    /**
+     * Check master process is alive
+     *
+     * @param $master_pid
+     * @return bool
+     */
+    protected static function checkMasterIsAlive($master_pid)
+    {
+        if (empty($master_pid)) {
+            return false;
+        }
+
+        $master_is_alive = $master_pid && \posix_kill($master_pid, 0) && \posix_getpid() !== $master_pid;
+        if (!$master_is_alive) {
+            return false;
+        }
+
+        $cmdline = "/proc/{$master_pid}/cmdline";
+        if (!is_readable($cmdline) || empty(static::$processTitle)) {
+            return true;
+        }
+
+        $content = file_get_contents($cmdline);
+        if (empty($content)) {
+            return true;
+        }
+
+        return stripos($content, static::$processTitle) !== false;
+    }
 }