Workerman\Connection\TcpConnection::send PHP Method

send() public method

Sends data on the connection.
public send ( string $send_buffer, boolean $raw = false ) : void | boolean | null
$send_buffer string
$raw boolean
return void | boolean | null
    public function send($send_buffer, $raw = false)
    {
        // Try to call protocol::encode($send_buffer) before sending.
        if (false === $raw && $this->protocol) {
            $parser = $this->protocol;
            $send_buffer = $parser::encode($send_buffer, $this);
            if ($send_buffer === '') {
                return null;
            }
        }
        if ($this->_status === self::STATUS_INITIAL || $this->_status === self::STATUS_CONNECTING) {
            if ($this->_sendBuffer) {
                if ($this->bufferIsFull()) {
                    self::$statistics['send_fail']++;
                    return false;
                }
            }
            $this->_sendBuffer .= $send_buffer;
            $this->checkBufferWillFull();
            return null;
        } elseif ($this->_status === self::STATUS_CLOSING || $this->_status === self::STATUS_CLOSED) {
            return false;
        }
        // Attempt to send data directly.
        if ($this->_sendBuffer === '') {
            $len = @fwrite($this->_socket, $send_buffer);
            // send successful.
            if ($len === strlen($send_buffer)) {
                return true;
            }
            // Send only part of the data.
            if ($len > 0) {
                $this->_sendBuffer = substr($send_buffer, $len);
            } else {
                // Connection closed?
                if (!is_resource($this->_socket) || feof($this->_socket)) {
                    self::$statistics['send_fail']++;
                    if ($this->onError) {
                        try {
                            call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'client closed');
                        } catch (\Exception $e) {
                            Worker::log($e);
                            exit(250);
                        } catch (\Error $e) {
                            Worker::log($e);
                            exit(250);
                        }
                    }
                    $this->destroy();
                    return false;
                }
                $this->_sendBuffer = $send_buffer;
            }
            Worker::$globalEvent->add($this->_socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
            // Check if the send buffer will be full.
            $this->checkBufferWillFull();
            return null;
        } else {
            if ($this->bufferIsFull()) {
                self::$statistics['send_fail']++;
                return false;
            }
            $this->_sendBuffer .= $send_buffer;
            // Check if the send buffer is full.
            $this->checkBufferWillFull();
        }
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Check the integrity of the package.
  *
  * @param string        $recv_buffer
  * @param TcpConnection $connection
  * @return int
  */
 public static function input($recv_buffer, TcpConnection $connection)
 {
     if (!strpos($recv_buffer, "\r\n\r\n")) {
         // Judge whether the package length exceeds the limit.
         if (strlen($recv_buffer) >= TcpConnection::$maxPackageSize) {
             $connection->close();
             return 0;
         }
         return 0;
     }
     list($header, ) = explode("\r\n\r\n", $recv_buffer, 2);
     if (0 === strpos($recv_buffer, "POST")) {
         // find Content-Length
         $match = array();
         if (preg_match("/\r\nContent-Length: ?(\\d+)/i", $header, $match)) {
             $content_length = $match[1];
             return $content_length + strlen($header) + 4;
         } else {
             return 0;
         }
     } elseif (0 === strpos($recv_buffer, "GET")) {
         return strlen($header) + 4;
     } else {
         $connection->send("HTTP/1.1 400 Bad Request\r\n\r\n", true);
         return 0;
     }
 }
All Usage Examples Of Workerman\Connection\TcpConnection::send