Parcourir la source

connection->id

walkor il y a 10 ans
Parent
commit
1b7130e6d0
2 fichiers modifiés avec 9 ajouts et 3 suppressions
  1. 8 2
      Connection/TcpConnection.php
  2. 1 1
      Worker.php

+ 8 - 2
Connection/TcpConnection.php

@@ -103,6 +103,12 @@ class TcpConnection extends ConnectionInterface
     public $id = 0;
     
     /**
+     * 连接的id,为$id的副本,用来清理connections中的连接
+     * @var int
+     */
+    protected $_id = 0;
+    
+    /**
      * 设置当前连接的最大发送缓冲区大小,默认大小为TcpConnection::$defaultMaxSendBufferSize
      * 当发送缓冲区满时,会尝试触发onBufferFull回调(如果有设置的话)
      * 如果没设置onBufferFull回调,由于发送缓冲区满,则后续发送的数据将被丢弃,
@@ -197,7 +203,7 @@ class TcpConnection extends ConnectionInterface
     {
         // 统计数据
         self::$statistics['connection_count']++;
-        $this->id = self::$_idRecorder++;
+        $this->id = $this->_id = self::$_idRecorder++;
         $this->_socket = $socket;
         stream_set_blocking($this->_socket, 0);
         Worker::$globalEvent->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
@@ -615,7 +621,7 @@ class TcpConnection extends ConnectionInterface
         // 从连接中删除
         if($this->worker)
         {
-            unset($this->worker->connections[(int)$this->_socket]);
+            unset($this->worker->connections[$this->_id]);
         }
         // 标记该连接已经关闭
        $this->_status = self::STATUS_CLOSED;

+ 1 - 1
Worker.php

@@ -1328,6 +1328,7 @@ class Worker
         
         // 初始化连接对象
         $connection = new TcpConnection($new_socket);
+        $this->connections[$connection->id] = $connection;
         $connection->worker = $this;
         $connection->protocol = $this->_protocol;
         $connection->onMessage = $this->onMessage;
@@ -1335,7 +1336,6 @@ class Worker
         $connection->onError = $this->onError;
         $connection->onBufferDrain = $this->onBufferDrain;
         $connection->onBufferFull = $this->onBufferFull;
-        $this->connections[(int)$new_socket] = $connection;
         
         // 如果有设置连接回调,则执行
         if($this->onConnect)