فهرست منبع

remove Connection->_event

walkor 10 سال پیش
والد
کامیت
b420abdc41
3فایلهای تغییر یافته به همراه33 افزوده شده و 41 حذف شده
  1. 6 7
      Workerman/Connection/AsyncTcpConnection.php
  2. 6 13
      Workerman/Connection/TcpConnection.php
  3. 21 21
      Workerman/Worker.php

+ 6 - 7
Workerman/Connection/AsyncTcpConnection.php

@@ -30,7 +30,7 @@ class AsyncTcpConnection extends TcpConnection
      * @param resource $socket
      * @param EventInterface $event
      */
-    public function __construct($remote_address, EventInterface $event)
+    public function __construct($remote_address)
     {
         list($scheme, $address) = explode(':', $remote_address, 2);
         if($scheme != 'tcp')
@@ -46,7 +46,6 @@ class AsyncTcpConnection extends TcpConnection
                 }
             }
         }
-        $this->_event = $event;
         $this->_socket = stream_socket_client("tcp:$address", $errno, $errstr, 0, STREAM_CLIENT_ASYNC_CONNECT);
         if(!$this->_socket)
         {
@@ -54,7 +53,7 @@ class AsyncTcpConnection extends TcpConnection
             return;
         }
         
-        $this->_event->add($this->_socket, EventInterface::EV_WRITE, array($this, 'checkConnection'));
+        Worker::$globalEvent->add($this->_socket, EventInterface::EV_WRITE, array($this, 'checkConnection'));
     }
     
     protected function emitError($code, $msg)
@@ -73,15 +72,15 @@ class AsyncTcpConnection extends TcpConnection
     
     public function checkConnection($socket)
     {
-        $this->_event->del($this->_socket, EventInterface::EV_WRITE);
+        Worker::$globalEvent->del($this->_socket, EventInterface::EV_WRITE);
         // php bug ?
         if(!feof($this->_socket) && !feof($this->_socket))
         {
             stream_set_blocking($this->_socket, 0);
-            $this->_event->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
+            Worker::$globalEvent->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
             if($this->_sendBuffer)
             {
-                $this->_event->add($this->_socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
+                Worker::$globalEvent->add($this->_socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
             }
             $this->_status = self::STATUS_ESTABLISH;
             if($this->onConnect)
@@ -153,7 +152,7 @@ class AsyncTcpConnection extends TcpConnection
                 $this->_sendBuffer = $send_buffer;
             }
             
-            $this->_event->add($this->_socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
+            Worker::$globalEvent->add($this->_socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
             return null;
         }
         else

+ 6 - 13
Workerman/Connection/TcpConnection.php

@@ -68,12 +68,6 @@ class TcpConnection extends ConnectionInterface
     public $protocol = '';
     
     /**
-     * eventloop
-     * @var EventInterface
-     */
-    protected $_event = null;
-    
-    /**
      * max send buffer size (Bytes)
      * @var int
      */
@@ -138,12 +132,11 @@ class TcpConnection extends ConnectionInterface
      * @param resource $socket
      * @param EventInterface $event
      */
-    public function __construct($socket, EventInterface $event)
+    public function __construct($socket)
     {
         $this->_socket = $socket;
         stream_set_blocking($this->_socket, 0);
-        $this->_event = $event;
-        $this->_event->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
+        Worker::$globalEvent->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
     }
     
     /**
@@ -191,7 +184,7 @@ class TcpConnection extends ConnectionInterface
                 $this->_sendBuffer = $send_buffer;
             }
             
-            $this->_event->add($this->_socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
+            Worker::$globalEvent->add($this->_socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
             return null;
         }
         else
@@ -349,7 +342,7 @@ class TcpConnection extends ConnectionInterface
         $len = @fwrite($this->_socket, $this->_sendBuffer);
         if($len === strlen($this->_sendBuffer))
         {
-            $this->_event->del($this->_socket, EventInterface::EV_WRITE);
+            Worker::$globalEvent->del($this->_socket, EventInterface::EV_WRITE);
             $this->_sendBuffer = '';
             if($this->_status == self::STATUS_CLOSING)
             {
@@ -425,8 +418,8 @@ class TcpConnection extends ConnectionInterface
                echo $e;
            }
        }
-       $this->_event->del($this->_socket, EventInterface::EV_READ);
-       $this->_event->del($this->_socket, EventInterface::EV_WRITE);
+       Worker::$globalEvent->del($this->_socket, EventInterface::EV_READ);
+       Worker::$globalEvent->del($this->_socket, EventInterface::EV_WRITE);
        @fclose($this->_socket);
        $this->_status = self::STATUS_CLOSED;
     }

+ 21 - 21
Workerman/Worker.php

@@ -163,16 +163,16 @@ class Worker
     public static $logFile = '';
     
     /**
-     * master process pid
-     * @var int
+     * event loop
+     * @var Select/Libevent
      */
-    protected static $_masterPid = 0;
+    public static $globalEvent = null;
     
     /**
-     * event loop
-     * @var Select/Libevent
+     * master process pid
+     * @var int
      */
-    protected static $_globalEvent = null;
+    protected static $_masterPid = 0;
     
     /**
      * stream socket of the worker
@@ -498,11 +498,11 @@ class Worker
         // uninstall  status signal handler
         pcntl_signal(SIGUSR2, SIG_IGN, false);
         // reinstall stop signal handler
-        self::$_globalEvent->add(SIGINT, EventInterface::EV_SIGNAL, array('\Workerman\Worker', 'signalHandler'));
+        self::$globalEvent->add(SIGINT, EventInterface::EV_SIGNAL, array('\Workerman\Worker', 'signalHandler'));
         //  uninstall  reload signal handler
-        self::$_globalEvent->add(SIGUSR1, EventInterface::EV_SIGNAL,array('\Workerman\Worker', 'signalHandler'));
+        self::$globalEvent->add(SIGUSR1, EventInterface::EV_SIGNAL,array('\Workerman\Worker', 'signalHandler'));
         // uninstall  status signal handler
-        self::$_globalEvent->add(SIGUSR2, EventInterface::EV_SIGNAL, array('\Workerman\Worker', 'signalHandler'));
+        self::$globalEvent->add(SIGUSR2, EventInterface::EV_SIGNAL, array('\Workerman\Worker', 'signalHandler'));
     }
     
     /**
@@ -1023,15 +1023,15 @@ class Worker
         
         stream_set_blocking($this->_mainSocket, 0);
         
-        if(self::$_globalEvent)
+        if(self::$globalEvent)
         {
             if($this->transport !== 'udp')
             {
-                self::$_globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptConnection'));
+                self::$globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptConnection'));
             }
             else
             {
-                self::$_globalEvent->add($this->_mainSocket,  EventInterface::EV_READ, array($this, 'acceptUdpConnection'));
+                self::$globalEvent->add($this->_mainSocket,  EventInterface::EV_READ, array($this, 'acceptUdpConnection'));
             }
         }
     }
@@ -1065,37 +1065,37 @@ class Worker
      */
     public function run()
     {
-        if(!self::$_globalEvent)
+        if(!self::$globalEvent)
         {
             if(extension_loaded('libevent'))
             {
-                self::$_globalEvent = new Libevent();
+                self::$globalEvent = new Libevent();
             }
             else
             {
-                self::$_globalEvent = new Select();
+                self::$globalEvent = new Select();
             }
             if($this->_socketName)
             {
                 if($this->transport !== 'udp')
                 {
-                    self::$_globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptConnection'));
+                    self::$globalEvent->add($this->_mainSocket, EventInterface::EV_READ, array($this, 'acceptConnection'));
                 }
                 else
                 {
-                    self::$_globalEvent->add($this->_mainSocket,  EventInterface::EV_READ, array($this, 'acceptUdpConnection'));
+                    self::$globalEvent->add($this->_mainSocket,  EventInterface::EV_READ, array($this, 'acceptUdpConnection'));
                 }
             }
         }
         self::reinstallSignal();
         
-        Timer::init(self::$_globalEvent);
+        Timer::init(self::$globalEvent);
         
         if($this->onWorkerStart)
         {
             call_user_func($this->onWorkerStart, $this);
         }
-        self::$_globalEvent->loop();
+        self::$globalEvent->loop();
     }
     
     /**
@@ -1108,7 +1108,7 @@ class Worker
         {
             call_user_func($this->onWorkerStop, $this);
         }
-        self::$_globalEvent->del($this->_mainSocket, EventInterface::EV_READ);
+        self::$globalEvent->del($this->_mainSocket, EventInterface::EV_READ);
         @fclose($this->_mainSocket);
     }
 
@@ -1132,7 +1132,7 @@ class Worker
             //unblock connection
             stream_set_blocking ($socket, false);
         }
-        $connection = new TcpConnection($new_socket, self::$_globalEvent);
+        $connection = new TcpConnection($new_socket);
         $connection->protocol = $this->_protocol;
         $connection->onMessage = $this->onMessage;
         $connection->onClose = $this->onClose;