瀏覽代碼

add connection->destroy

walkor 10 年之前
父節點
當前提交
338ff2c490
共有 2 個文件被更改,包括 25 次插入13 次删除
  1. 3 3
      GatewayWorker/Gateway.php
  2. 22 10
      Workerman/Connection/TcpConnection.php

+ 3 - 3
GatewayWorker/Gateway.php

@@ -240,7 +240,7 @@ class Gateway extends Worker
                 $msg = "SendBufferToWorker fail. The connections between Gateway and BusinessWorker are not ready";
                 $this->log($msg);
             }
-            $connection->close();
+            $connection->destroy();
             return false;
         }
         return true;
@@ -407,7 +407,7 @@ class Gateway extends Worker
             case GatewayProtocol::CMD_KICK:
                 if(isset($this->_clientConnections[$data['client_id']]))
                 {
-                    $this->_clientConnections[$data['client_id']]->close();
+                    $this->_clientConnections[$data['client_id']]->destroy();
                 }
                 break;
                 // 广播, Gateway::sendToAll($message, $client_id_array)
@@ -559,7 +559,7 @@ class Gateway extends Worker
             // 上次发送的心跳还没有回复次数大于限定值就断开
             if($this->pingNotResponseLimit > 0 && $connection->pingNotResponseCount >= $this->pingNotResponseLimit)
             {
-                $connection->close();
+                $connection->destroy();
                 continue;
             }
             $connection->pingNotResponseCount++;

+ 22 - 10
Workerman/Connection/TcpConnection.php

@@ -506,7 +506,7 @@ class TcpConnection extends ConnectionInterface
      */
     public function close($data = null)
     {
-        if($this->_status == self::STATUS_CLOSING)
+        if($this->_status == self::STATUS_CLOSING || $this->_status == self::STATUS_CLOSED)
         {
             return false;
         }
@@ -558,9 +558,29 @@ class TcpConnection extends ConnectionInterface
      * 销毁连接
      * @void
      */
-    protected function destroy()
+    public function destroy()
     {
+        // 避免重复调用
+        if($this->_status == self::STATUS_CLOSED)
+        {
+            return false;
+        }
+        // 删除事件监听
+        Worker::$globalEvent->del($this->_socket, EventInterface::EV_READ);
+        Worker::$globalEvent->del($this->_socket, EventInterface::EV_WRITE);
+        // 关闭socket
+        @fclose($this->_socket);
+        
+        // 从连接中删除
+        if($this->worker)
+        {
+            unset($this->worker->connections[(int)$this->_socket]);
+        }
+        // 标记该连接已经关闭
+       $this->_status = self::STATUS_CLOSED;
+       // 连接计数减一
        self::$statistics['connection_count']--;
+       // 触发onClose回调
        if($this->onClose)
        {
            try
@@ -573,13 +593,5 @@ class TcpConnection extends ConnectionInterface
                echo $e;
            }
        }
-       if($this->worker)
-       {
-           unset($this->worker->connections[(int)$this->_socket]);
-       }
-       Worker::$globalEvent->del($this->_socket, EventInterface::EV_READ);
-       Worker::$globalEvent->del($this->_socket, EventInterface::EV_WRITE);
-       @fclose($this->_socket);
-       $this->_status = self::STATUS_CLOSED;
     }
 }