2015-04-04 21:46:31 +08:00
|
|
|
<?php
|
2016-09-20 21:27:41 +08:00
|
|
|
/**
|
|
|
|
* This file is part of workerman.
|
|
|
|
*
|
|
|
|
* Licensed under The MIT License
|
|
|
|
* For full copyright and license information, please see the MIT-LICENSE.txt
|
|
|
|
* Redistributions of files must retain the above copyright notice.
|
|
|
|
*
|
|
|
|
* @author walkor<walkor@workerman.net>
|
|
|
|
* @copyright walkor<walkor@workerman.net>
|
|
|
|
* @link http://www.workerman.net/
|
|
|
|
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
|
|
|
*/
|
2015-04-04 21:46:31 +08:00
|
|
|
namespace Workerman\Connection;
|
|
|
|
|
|
|
|
use Workerman\Events\EventInterface;
|
|
|
|
use Workerman\Worker;
|
2016-09-20 21:27:41 +08:00
|
|
|
use Exception;
|
2015-04-04 21:46:31 +08:00
|
|
|
|
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* TcpConnection.
|
2015-04-04 21:46:31 +08:00
|
|
|
*/
|
|
|
|
class TcpConnection extends ConnectionInterface
|
|
|
|
{
|
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Read buffer size.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const READ_BUFFER_SIZE = 65535;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Status initial.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var int
|
|
|
|
*/
|
2016-09-20 21:27:41 +08:00
|
|
|
const STATUS_INITIAL = 0;
|
2015-04-04 21:46:31 +08:00
|
|
|
|
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Status connecting.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const STATUS_CONNECTING = 1;
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Status connection established.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const STATUS_ESTABLISH = 2;
|
|
|
|
|
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Status closing.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const STATUS_CLOSING = 4;
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Status closed.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
const STATUS_CLOSED = 8;
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Emitted when data is received.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var callback
|
|
|
|
*/
|
|
|
|
public $onMessage = null;
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Emitted when the other end of the socket sends a FIN packet.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var callback
|
|
|
|
*/
|
|
|
|
public $onClose = null;
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Emitted when an error occurs with connection.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var callback
|
|
|
|
*/
|
|
|
|
public $onError = null;
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Emitted when the send buffer becomes full.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var callback
|
|
|
|
*/
|
|
|
|
public $onBufferFull = null;
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Emitted when the send buffer becomes empty.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var callback
|
|
|
|
*/
|
|
|
|
public $onBufferDrain = null;
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Application layer protocol.
|
|
|
|
* The format is like this Workerman\\Protocols\\Http.
|
|
|
|
*
|
|
|
|
* @var \Workerman\Protocols\ProtocolInterface
|
2015-04-04 21:46:31 +08:00
|
|
|
*/
|
2016-09-20 21:27:41 +08:00
|
|
|
public $protocol = null;
|
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Which worker belong to.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var Worker
|
|
|
|
*/
|
|
|
|
public $worker = null;
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Connection->id.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $id = 0;
|
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* A copy of $worker->id which used to clean up the connection in worker->connections
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var int
|
|
|
|
*/
|
2016-09-20 21:27:41 +08:00
|
|
|
protected $_id = 0;
|
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Sets the maximum send buffer size for the current connection.
|
|
|
|
* OnBufferFull callback will be emited When the send buffer is full.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public $maxSendBufferSize = 1048576;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Default send buffer size.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public static $defaultMaxSendBufferSize = 1048576;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Maximum acceptable packet size.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
public static $maxPackageSize = 10485760;
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Id recorder.
|
|
|
|
*
|
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected static $_idRecorder = 1;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Socket
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var resource
|
|
|
|
*/
|
|
|
|
protected $_socket = null;
|
|
|
|
|
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Send buffer.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_sendBuffer = '';
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Receive buffer.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_recvBuffer = '';
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Current package length.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $_currentPackageLength = 0;
|
|
|
|
|
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Connection status.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var int
|
|
|
|
*/
|
|
|
|
protected $_status = self::STATUS_ESTABLISH;
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Remote address.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $_remoteAddress = '';
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Is paused.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @var bool
|
|
|
|
*/
|
|
|
|
protected $_isPaused = false;
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Construct.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @param resource $socket
|
2016-09-20 21:27:41 +08:00
|
|
|
* @param string $remote_address
|
2015-04-04 21:46:31 +08:00
|
|
|
*/
|
2016-09-20 21:27:41 +08:00
|
|
|
public function __construct($socket, $remote_address = '')
|
2015-04-04 21:46:31 +08:00
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
self::$statistics['connection_count']++;
|
|
|
|
$this->id = $this->_id = self::$_idRecorder++;
|
2015-04-04 21:46:31 +08:00
|
|
|
$this->_socket = $socket;
|
|
|
|
stream_set_blocking($this->_socket, 0);
|
2016-09-20 21:27:41 +08:00
|
|
|
// Compatible with hhvm
|
|
|
|
if (function_exists('stream_set_read_buffer')) {
|
|
|
|
stream_set_read_buffer($this->_socket, 0);
|
|
|
|
}
|
2015-04-04 21:46:31 +08:00
|
|
|
Worker::$globalEvent->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
|
2016-09-20 21:27:41 +08:00
|
|
|
$this->maxSendBufferSize = self::$defaultMaxSendBufferSize;
|
|
|
|
$this->_remoteAddress = $remote_address;
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Sends data on the connection.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @param string $send_buffer
|
2016-09-20 21:27:41 +08:00
|
|
|
* @param bool $raw
|
|
|
|
* @return void|bool|null
|
2015-04-04 21:46:31 +08:00
|
|
|
*/
|
|
|
|
public function send($send_buffer, $raw = false)
|
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
// 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) {
|
2015-04-04 21:46:31 +08:00
|
|
|
$this->_sendBuffer .= $send_buffer;
|
|
|
|
return null;
|
2016-09-20 21:27:41 +08:00
|
|
|
} elseif ($this->_status === self::STATUS_CLOSING || $this->_status === self::STATUS_CLOSED) {
|
2015-04-04 21:46:31 +08:00
|
|
|
return false;
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
// Attempt to send data directly.
|
|
|
|
if ($this->_sendBuffer === '') {
|
2015-04-04 21:46:31 +08:00
|
|
|
$len = @fwrite($this->_socket, $send_buffer);
|
2016-09-20 21:27:41 +08:00
|
|
|
// send successful.
|
|
|
|
if ($len === strlen($send_buffer)) {
|
2015-04-04 21:46:31 +08:00
|
|
|
return true;
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
// Send only part of the data.
|
|
|
|
if ($len > 0) {
|
2015-04-04 21:46:31 +08:00
|
|
|
$this->_sendBuffer = substr($send_buffer, $len);
|
2016-09-20 21:27:41 +08:00
|
|
|
} else {
|
|
|
|
// Connection closed?
|
|
|
|
if (!is_resource($this->_socket) || feof($this->_socket)) {
|
2015-04-04 21:46:31 +08:00
|
|
|
self::$statistics['send_fail']++;
|
2016-09-20 21:27:41 +08:00
|
|
|
if ($this->onError) {
|
|
|
|
try {
|
2015-04-04 21:46:31 +08:00
|
|
|
call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'client closed');
|
2016-09-20 21:27:41 +08:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
Worker::log($e);
|
|
|
|
exit(250);
|
|
|
|
} catch (\Error $e) {
|
|
|
|
Worker::log($e);
|
|
|
|
exit(250);
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
$this->destroy();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->_sendBuffer = $send_buffer;
|
|
|
|
}
|
|
|
|
Worker::$globalEvent->add($this->_socket, EventInterface::EV_WRITE, array($this, 'baseWrite'));
|
2016-09-20 21:27:41 +08:00
|
|
|
// Check if the send buffer is full.
|
2015-04-04 21:46:31 +08:00
|
|
|
$this->checkBufferIsFull();
|
|
|
|
return null;
|
2016-09-20 21:27:41 +08:00
|
|
|
} else {
|
|
|
|
// Buffer has been marked as full but still has data to send the packet is discarded.
|
|
|
|
if ($this->maxSendBufferSize <= strlen($this->_sendBuffer)) {
|
2015-04-04 21:46:31 +08:00
|
|
|
self::$statistics['send_fail']++;
|
2016-09-20 21:27:41 +08:00
|
|
|
if ($this->onError) {
|
|
|
|
try {
|
2015-04-04 21:46:31 +08:00
|
|
|
call_user_func($this->onError, $this, WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
|
2016-09-20 21:27:41 +08:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
Worker::log($e);
|
|
|
|
exit(250);
|
|
|
|
} catch (\Error $e) {
|
|
|
|
Worker::log($e);
|
|
|
|
exit(250);
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
$this->_sendBuffer .= $send_buffer;
|
2016-09-20 21:27:41 +08:00
|
|
|
// Check if the send buffer is full.
|
2015-04-04 21:46:31 +08:00
|
|
|
$this->checkBufferIsFull();
|
|
|
|
}
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Get remote IP.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function getRemoteIp()
|
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
$pos = strrpos($this->_remoteAddress, ':');
|
|
|
|
if ($pos) {
|
|
|
|
return trim(substr($this->_remoteAddress, 0, $pos), '[]');
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
return '';
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Get remote port.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @return int
|
|
|
|
*/
|
|
|
|
public function getRemotePort()
|
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
if ($this->_remoteAddress) {
|
|
|
|
return (int)substr(strrchr($this->_remoteAddress, ':'), 1);
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
return 0;
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Pauses the reading of data. That is onMessage will not be emitted. Useful to throttle back an upload.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function pauseRecv()
|
|
|
|
{
|
|
|
|
Worker::$globalEvent->del($this->_socket, EventInterface::EV_READ);
|
|
|
|
$this->_isPaused = true;
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Resumes reading after a call to pauseRecv.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function resumeRecv()
|
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
if ($this->_isPaused === true) {
|
2015-04-04 21:46:31 +08:00
|
|
|
Worker::$globalEvent->add($this->_socket, EventInterface::EV_READ, array($this, 'baseRead'));
|
|
|
|
$this->_isPaused = false;
|
2016-09-20 21:27:41 +08:00
|
|
|
$this->baseRead($this->_socket, false);
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Base read handler.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @param resource $socket
|
2016-09-20 21:27:41 +08:00
|
|
|
* @param bool $check_eof
|
2015-04-04 21:46:31 +08:00
|
|
|
* @return void
|
|
|
|
*/
|
2016-09-20 21:27:41 +08:00
|
|
|
public function baseRead($socket, $check_eof = true)
|
2015-04-04 21:46:31 +08:00
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
$buffer = fread($socket, self::READ_BUFFER_SIZE);
|
|
|
|
|
|
|
|
// Check connection closed.
|
|
|
|
if ($buffer === '' || $buffer === false) {
|
|
|
|
if ($check_eof && (feof($socket) || !is_resource($socket) || $buffer === false)) {
|
|
|
|
$this->destroy();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
$this->_recvBuffer .= $buffer;
|
|
|
|
}
|
|
|
|
|
|
|
|
// If the application layer protocol has been set up.
|
|
|
|
if ($this->protocol) {
|
|
|
|
$parser = $this->protocol;
|
|
|
|
while ($this->_recvBuffer !== '' && !$this->_isPaused) {
|
|
|
|
// The current packet length is known.
|
|
|
|
if ($this->_currentPackageLength) {
|
|
|
|
// Data is not enough for a package.
|
|
|
|
if ($this->_currentPackageLength > strlen($this->_recvBuffer)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Get current package length.
|
|
|
|
$this->_currentPackageLength = $parser::input($this->_recvBuffer, $this);
|
|
|
|
// The packet length is unknown.
|
|
|
|
if ($this->_currentPackageLength === 0) {
|
|
|
|
break;
|
|
|
|
} elseif ($this->_currentPackageLength > 0 && $this->_currentPackageLength <= self::$maxPackageSize) {
|
|
|
|
// Data is not enough for a package.
|
|
|
|
if ($this->_currentPackageLength > strlen($this->_recvBuffer)) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} // Wrong package.
|
|
|
|
else {
|
|
|
|
echo 'error package. package_length=' . var_export($this->_currentPackageLength, true);
|
|
|
|
$this->destroy();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// The data is enough for a packet.
|
|
|
|
self::$statistics['total_request']++;
|
|
|
|
// The current packet length is equal to the length of the buffer.
|
|
|
|
if (strlen($this->_recvBuffer) === $this->_currentPackageLength) {
|
|
|
|
$one_request_buffer = $this->_recvBuffer;
|
|
|
|
$this->_recvBuffer = '';
|
|
|
|
} else {
|
|
|
|
// Get a full package from the buffer.
|
|
|
|
$one_request_buffer = substr($this->_recvBuffer, 0, $this->_currentPackageLength);
|
|
|
|
// Remove the current package from the receive buffer.
|
|
|
|
$this->_recvBuffer = substr($this->_recvBuffer, $this->_currentPackageLength);
|
|
|
|
}
|
|
|
|
// Reset the current packet length to 0.
|
|
|
|
$this->_currentPackageLength = 0;
|
|
|
|
if (!$this->onMessage) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
// Decode request buffer before Emitting onMessage callback.
|
|
|
|
call_user_func($this->onMessage, $this, $parser::decode($one_request_buffer, $this));
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
Worker::log($e);
|
|
|
|
exit(250);
|
|
|
|
} catch (\Error $e) {
|
|
|
|
Worker::log($e);
|
|
|
|
exit(250);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ($this->_recvBuffer === '' || $this->_isPaused) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Applications protocol is not set.
|
|
|
|
self::$statistics['total_request']++;
|
|
|
|
if (!$this->onMessage) {
|
|
|
|
$this->_recvBuffer = '';
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
try {
|
|
|
|
call_user_func($this->onMessage, $this, $this->_recvBuffer);
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
Worker::log($e);
|
|
|
|
exit(250);
|
|
|
|
} catch (\Error $e) {
|
|
|
|
Worker::log($e);
|
|
|
|
exit(250);
|
|
|
|
}
|
|
|
|
// Clean receive buffer.
|
|
|
|
$this->_recvBuffer = '';
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Base write handler.
|
|
|
|
*
|
|
|
|
* @return void|bool
|
2015-04-04 21:46:31 +08:00
|
|
|
*/
|
|
|
|
public function baseWrite()
|
|
|
|
{
|
|
|
|
$len = @fwrite($this->_socket, $this->_sendBuffer);
|
2016-09-20 21:27:41 +08:00
|
|
|
if ($len === strlen($this->_sendBuffer)) {
|
2015-04-04 21:46:31 +08:00
|
|
|
Worker::$globalEvent->del($this->_socket, EventInterface::EV_WRITE);
|
|
|
|
$this->_sendBuffer = '';
|
2016-09-20 21:27:41 +08:00
|
|
|
// Try to emit onBufferDrain callback when the send buffer becomes empty.
|
|
|
|
if ($this->onBufferDrain) {
|
|
|
|
try {
|
2015-04-04 21:46:31 +08:00
|
|
|
call_user_func($this->onBufferDrain, $this);
|
2016-09-20 21:27:41 +08:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
Worker::log($e);
|
|
|
|
exit(250);
|
|
|
|
} catch (\Error $e) {
|
|
|
|
Worker::log($e);
|
|
|
|
exit(250);
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
if ($this->_status === self::STATUS_CLOSING) {
|
2015-04-04 21:46:31 +08:00
|
|
|
$this->destroy();
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
if ($len > 0) {
|
|
|
|
$this->_sendBuffer = substr($this->_sendBuffer, $len);
|
|
|
|
} else {
|
|
|
|
self::$statistics['send_fail']++;
|
|
|
|
$this->destroy();
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* 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();
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Remove $length of data from receive buffer.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @param int $length
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function consumeRecvBuffer($length)
|
|
|
|
{
|
|
|
|
$this->_recvBuffer = substr($this->_recvBuffer, $length);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Close connection.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @param mixed $data
|
2016-09-20 21:27:41 +08:00
|
|
|
* @param bool $raw
|
|
|
|
* @return void
|
2015-04-04 21:46:31 +08:00
|
|
|
*/
|
2016-09-20 21:27:41 +08:00
|
|
|
public function close($data = null, $raw = false)
|
2015-04-04 21:46:31 +08:00
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
if ($this->_status === self::STATUS_CLOSING || $this->_status === self::STATUS_CLOSED) {
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
if ($data !== null) {
|
|
|
|
$this->send($data, $raw);
|
|
|
|
}
|
2015-04-04 21:46:31 +08:00
|
|
|
$this->_status = self::STATUS_CLOSING;
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
if ($this->_sendBuffer === '') {
|
|
|
|
$this->destroy();
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Get the real socket.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @return resource
|
|
|
|
*/
|
|
|
|
public function getSocket()
|
|
|
|
{
|
|
|
|
return $this->_socket;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Check whether the send buffer is full.
|
|
|
|
*
|
2015-04-04 21:46:31 +08:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
protected function checkBufferIsFull()
|
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
if ($this->maxSendBufferSize <= strlen($this->_sendBuffer)) {
|
|
|
|
if ($this->onBufferFull) {
|
|
|
|
try {
|
2015-04-04 21:46:31 +08:00
|
|
|
call_user_func($this->onBufferFull, $this);
|
2016-09-20 21:27:41 +08:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
Worker::log($e);
|
|
|
|
exit(250);
|
|
|
|
} catch (\Error $e) {
|
|
|
|
Worker::log($e);
|
|
|
|
exit(250);
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-09-20 21:27:41 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Destroy connection.
|
|
|
|
*
|
|
|
|
* @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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-04 21:46:31 +08:00
|
|
|
/**
|
2016-09-20 21:27:41 +08:00
|
|
|
* Destruct.
|
|
|
|
*
|
|
|
|
* @return void
|
2015-04-04 21:46:31 +08:00
|
|
|
*/
|
2016-09-20 21:27:41 +08:00
|
|
|
public function __destruct()
|
2015-04-04 21:46:31 +08:00
|
|
|
{
|
2016-09-20 21:27:41 +08:00
|
|
|
self::$statistics['connection_count']--;
|
2015-04-04 21:46:31 +08:00
|
|
|
}
|
|
|
|
}
|