Workerman\Connection\TcpConnection::destroy PHP Method

destroy() public method

Destroy connection.
public destroy ( ) : void
return void
    public function destroy()
    {
        // Avoid repeated calls.
        if ($this->_status === self::STATUS_CLOSED) {
            return;
        }
        // Remove event listener.
        Worker::$globalEvent->del($this->_socket, EventInterface::EV_READ);
        Worker::$globalEvent->del($this->_socket, EventInterface::EV_WRITE);
        // Close socket.
        @fclose($this->_socket);
        // Remove from worker->connections.
        if ($this->worker) {
            unset($this->worker->connections[$this->_id]);
        }
        $this->_status = self::STATUS_CLOSED;
        // Try to emit onClose callback.
        if ($this->onClose) {
            try {
                call_user_func($this->onClose, $this);
            } catch (\Exception $e) {
                Worker::log($e);
                exit(250);
            } catch (\Error $e) {
                Worker::log($e);
                exit(250);
            }
        }
        // Try to emit protocol::onClose
        if (method_exists($this->protocol, 'onClose')) {
            try {
                call_user_func(array($this->protocol, 'onClose'), $this);
            } catch (\Exception $e) {
                Worker::log($e);
                exit(250);
            } catch (\Error $e) {
                Worker::log($e);
                exit(250);
            }
        }
        if ($this->_status === self::STATUS_CLOSED) {
            // Cleaning up the callback to avoid memory leaks.
            $this->onMessage = $this->onClose = $this->onError = $this->onBufferFull = $this->onBufferDrain = null;
        }
    }

Usage Example

Exemplo n.º 1
0
 /**
  * This method pulls all the data out of a readable stream, and writes it to the supplied destination.
  *
  * @param TcpConnection $dest
  * @return void
  */
 public function pipe($dest)
 {
     $source = $this;
     $this->onMessage = function ($source, $data) use($dest) {
         $dest->send($data);
     };
     $this->onClose = function ($source) use($dest) {
         $dest->destroy();
     };
     $dest->onBufferFull = function ($dest) use($source) {
         $source->pauseRecv();
     };
     $dest->onBufferDrain = function ($dest) use($source) {
         $source->resumeRecv();
     };
 }