fix:修复BUG/升级1.1.6版本
This commit is contained in:
@@ -13,10 +13,11 @@
|
||||
*/
|
||||
namespace Workerman\Connection;
|
||||
|
||||
use StdClass;
|
||||
use Workerman\Events\EventInterface;
|
||||
use Workerman\Lib\Timer;
|
||||
use Workerman\Worker;
|
||||
use \Exception;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* AsyncTcpConnection.
|
||||
@@ -164,6 +165,7 @@ class AsyncTcpConnection extends TcpConnection
|
||||
$this->maxSendBufferSize = self::$defaultMaxSendBufferSize;
|
||||
$this->maxPackageSize = self::$defaultMaxPackageSize;
|
||||
$this->_contextOption = $context_option;
|
||||
$this->context = new StdClass;
|
||||
static::$connections[$this->_id] = $this;
|
||||
}
|
||||
|
||||
|
||||
@@ -157,6 +157,13 @@ class TcpConnection extends ConnectionInterface
|
||||
*/
|
||||
public $maxSendBufferSize = 1048576;
|
||||
|
||||
/**
|
||||
* Context.
|
||||
*
|
||||
* @var object|null
|
||||
*/
|
||||
public $context = null;
|
||||
|
||||
/**
|
||||
* Default send buffer size.
|
||||
*
|
||||
@@ -170,7 +177,7 @@ class TcpConnection extends ConnectionInterface
|
||||
* @var int
|
||||
*/
|
||||
public $maxPackageSize = 1048576;
|
||||
|
||||
|
||||
/**
|
||||
* Default maximum acceptable packet size.
|
||||
*
|
||||
@@ -285,6 +292,7 @@ class TcpConnection extends ConnectionInterface
|
||||
$this->maxPackageSize = self::$defaultMaxPackageSize;
|
||||
$this->_remoteAddress = $remote_address;
|
||||
static::$connections[$this->id] = $this;
|
||||
$this->context = new \stdClass;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -720,23 +728,23 @@ class TcpConnection extends ConnectionInterface
|
||||
return false;
|
||||
}
|
||||
$async = $this instanceof AsyncTcpConnection;
|
||||
|
||||
|
||||
/**
|
||||
* We disabled ssl3 because https://blog.qualys.com/ssllabs/2014/10/15/ssl-3-is-dead-killed-by-the-poodle-attack.
|
||||
* You can enable ssl3 by the codes below.
|
||||
*/
|
||||
* We disabled ssl3 because https://blog.qualys.com/ssllabs/2014/10/15/ssl-3-is-dead-killed-by-the-poodle-attack.
|
||||
* You can enable ssl3 by the codes below.
|
||||
*/
|
||||
/*if($async){
|
||||
$type = STREAM_CRYPTO_METHOD_SSLv2_CLIENT | STREAM_CRYPTO_METHOD_SSLv23_CLIENT | STREAM_CRYPTO_METHOD_SSLv3_CLIENT;
|
||||
}else{
|
||||
$type = STREAM_CRYPTO_METHOD_SSLv2_SERVER | STREAM_CRYPTO_METHOD_SSLv23_SERVER | STREAM_CRYPTO_METHOD_SSLv3_SERVER;
|
||||
}*/
|
||||
|
||||
|
||||
if($async){
|
||||
$type = \STREAM_CRYPTO_METHOD_SSLv2_CLIENT | \STREAM_CRYPTO_METHOD_SSLv23_CLIENT;
|
||||
}else{
|
||||
$type = \STREAM_CRYPTO_METHOD_SSLv2_SERVER | \STREAM_CRYPTO_METHOD_SSLv23_SERVER;
|
||||
}
|
||||
|
||||
|
||||
// Hidden error.
|
||||
\set_error_handler(function($errno, $errstr, $file){
|
||||
if (!Worker::$daemonize) {
|
||||
@@ -822,7 +830,7 @@ class TcpConnection extends ConnectionInterface
|
||||
}
|
||||
|
||||
$this->_status = self::STATUS_CLOSING;
|
||||
|
||||
|
||||
if ($this->_sendBuffer === '') {
|
||||
$this->destroy();
|
||||
} else {
|
||||
@@ -882,7 +890,7 @@ class TcpConnection extends ConnectionInterface
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Whether send buffer is Empty.
|
||||
*
|
||||
@@ -890,7 +898,7 @@ class TcpConnection extends ConnectionInterface
|
||||
*/
|
||||
public function bufferIsEmpty()
|
||||
{
|
||||
return empty($this->_sendBuffer);
|
||||
return empty($this->_sendBuffer);
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -546,26 +546,27 @@ class Request
|
||||
if (\preg_match('/name="(.*?)"; filename="(.*?)"/i', $value, $match)) {
|
||||
$error = 0;
|
||||
$tmp_file = '';
|
||||
$file_name = $match[2];
|
||||
$size = \strlen($boundary_value);
|
||||
$tmp_upload_dir = HTTP::uploadTmpDir();
|
||||
if (!$tmp_upload_dir) {
|
||||
$error = UPLOAD_ERR_NO_TMP_DIR;
|
||||
} else if ($boundary_value === '') {
|
||||
} else if ($boundary_value === '' && $file_name === '') {
|
||||
$error = UPLOAD_ERR_NO_FILE;
|
||||
} else {
|
||||
$tmp_file = \tempnam($tmp_upload_dir, 'workerman.upload.');
|
||||
if ($tmp_file === false || false == \file_put_contents($tmp_file, $boundary_value)) {
|
||||
if ($tmp_file === false || false === \file_put_contents($tmp_file, $boundary_value)) {
|
||||
$error = UPLOAD_ERR_CANT_WRITE;
|
||||
}
|
||||
}
|
||||
$upload_key = $match[1];
|
||||
// Parse upload files.
|
||||
$file = [
|
||||
'name' => $match[2],
|
||||
'name' => $file_name,
|
||||
'tmp_name' => $tmp_file,
|
||||
'size' => $size,
|
||||
'error' => $error,
|
||||
'type' => null,
|
||||
'type' => '',
|
||||
];
|
||||
break;
|
||||
} // Is post field.
|
||||
|
||||
315
vendor/workerman/workerman/Protocols/Websocket.php
vendored
315
vendor/workerman/workerman/Protocols/Websocket.php
vendored
@@ -11,10 +11,12 @@
|
||||
* @link http://www.workerman.net/
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
namespace Workerman\Protocols;
|
||||
|
||||
use Workerman\Connection\ConnectionInterface;
|
||||
use Workerman\Connection\TcpConnection;
|
||||
use Workerman\Protocols\Http\Request;
|
||||
use Workerman\Worker;
|
||||
|
||||
/**
|
||||
@@ -29,6 +31,13 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
|
||||
*/
|
||||
const BINARY_TYPE_BLOB = "\x81";
|
||||
|
||||
/**
|
||||
* Websocket blob type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const BINARY_TYPE_BLOB_DEFLATE = "\xc1";
|
||||
|
||||
/**
|
||||
* Websocket arraybuffer type.
|
||||
*
|
||||
@@ -36,10 +45,17 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
|
||||
*/
|
||||
const BINARY_TYPE_ARRAYBUFFER = "\x82";
|
||||
|
||||
/**
|
||||
* Websocket arraybuffer type.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const BINARY_TYPE_ARRAYBUFFER_DEFLATE = "\xc2";
|
||||
|
||||
/**
|
||||
* Check the integrity of the package.
|
||||
*
|
||||
* @param string $buffer
|
||||
* @param string $buffer
|
||||
* @param ConnectionInterface $connection
|
||||
* @return int
|
||||
*/
|
||||
@@ -53,23 +69,23 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
|
||||
}
|
||||
|
||||
// Has not yet completed the handshake.
|
||||
if (empty($connection->websocketHandshake)) {
|
||||
if (empty($connection->context->websocketHandshake)) {
|
||||
return static::dealHandshake($buffer, $connection);
|
||||
}
|
||||
|
||||
// Buffer websocket frame data.
|
||||
if ($connection->websocketCurrentFrameLength) {
|
||||
if ($connection->context->websocketCurrentFrameLength) {
|
||||
// We need more frame data.
|
||||
if ($connection->websocketCurrentFrameLength > $recv_len) {
|
||||
if ($connection->context->websocketCurrentFrameLength > $recv_len) {
|
||||
// Return 0, because it is not clear the full packet length, waiting for the frame of fin=1.
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
$firstbyte = \ord($buffer[0]);
|
||||
$secondbyte = \ord($buffer[1]);
|
||||
$data_len = $secondbyte & 127;
|
||||
$is_fin_frame = $firstbyte >> 7;
|
||||
$masked = $secondbyte >> 7;
|
||||
$first_byte = \ord($buffer[0]);
|
||||
$second_byte = \ord($buffer[1]);
|
||||
$data_len = $second_byte & 127;
|
||||
$is_fin_frame = $first_byte >> 7;
|
||||
$masked = $second_byte >> 7;
|
||||
|
||||
if (!$masked) {
|
||||
Worker::safeEcho("frame not masked so close the connection\n");
|
||||
@@ -77,7 +93,7 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
|
||||
return 0;
|
||||
}
|
||||
|
||||
$opcode = $firstbyte & 0xf;
|
||||
$opcode = $first_byte & 0xf;
|
||||
switch ($opcode) {
|
||||
case 0x0:
|
||||
break;
|
||||
@@ -90,12 +106,11 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
|
||||
// Close package.
|
||||
case 0x8:
|
||||
// Try to emit onWebSocketClose callback.
|
||||
if (isset($connection->onWebSocketClose) || isset($connection->worker->onWebSocketClose)) {
|
||||
$close_cb = $connection->onWebSocketClose ?? $connection->worker->onWebSocketClose ?? false;
|
||||
if ($close_cb) {
|
||||
try {
|
||||
\call_user_func(isset($connection->onWebSocketClose)?$connection->onWebSocketClose:$connection->worker->onWebSocketClose, $connection);
|
||||
} catch (\Exception $e) {
|
||||
Worker::stopAll(250, $e);
|
||||
} catch (\Error $e) {
|
||||
$close_cb($connection);
|
||||
} catch (\Throwable $e) {
|
||||
Worker::stopAll(250, $e);
|
||||
}
|
||||
} // Close connection.
|
||||
@@ -109,7 +124,7 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
|
||||
// Pong package.
|
||||
case 0xa:
|
||||
break;
|
||||
// Wrong opcode.
|
||||
// Wrong opcode.
|
||||
default :
|
||||
Worker::safeEcho("error opcode $opcode and close websocket connection. Buffer:" . bin2hex($buffer) . "\n");
|
||||
$connection->close();
|
||||
@@ -123,7 +138,7 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
|
||||
if ($head_len > $recv_len) {
|
||||
return 0;
|
||||
}
|
||||
$pack = \unpack('nn/ntotal_len', $buffer);
|
||||
$pack = \unpack('nn/ntotal_len', $buffer);
|
||||
$data_len = $pack['total_len'];
|
||||
} else {
|
||||
if ($data_len === 127) {
|
||||
@@ -131,13 +146,13 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
|
||||
if ($head_len > $recv_len) {
|
||||
return 0;
|
||||
}
|
||||
$arr = \unpack('n/N2c', $buffer);
|
||||
$data_len = $arr['c1']*4294967296 + $arr['c2'];
|
||||
$arr = \unpack('n/N2c', $buffer);
|
||||
$data_len = $arr['c1'] * 4294967296 + $arr['c2'];
|
||||
}
|
||||
}
|
||||
$current_frame_length = $head_len + $data_len;
|
||||
|
||||
$total_package_size = \strlen($connection->websocketDataBuffer) + $current_frame_length;
|
||||
$total_package_size = \strlen($connection->context->websocketDataBuffer) + $current_frame_length;
|
||||
if ($total_package_size > $connection->maxPackageSize) {
|
||||
Worker::safeEcho("error package. package_length=$total_package_size\n");
|
||||
$connection->close();
|
||||
@@ -151,12 +166,11 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
|
||||
$connection->consumeRecvBuffer($current_frame_length);
|
||||
$tmp_connection_type = isset($connection->websocketType) ? $connection->websocketType : static::BINARY_TYPE_BLOB;
|
||||
$connection->websocketType = "\x8a";
|
||||
if (isset($connection->onWebSocketPing) || isset($connection->worker->onWebSocketPing)) {
|
||||
$ping_cb = $connection->onWebSocketPing ?? $connection->worker->onWebSocketPing ?? false;
|
||||
if ($ping_cb) {
|
||||
try {
|
||||
\call_user_func(isset($connection->onWebSocketPing)?$connection->onWebSocketPing:$connection->worker->onWebSocketPing, $connection, $ping_data);
|
||||
} catch (\Exception $e) {
|
||||
Worker::stopAll(250, $e);
|
||||
} catch (\Error $e) {
|
||||
$ping_cb($connection, $ping_data);
|
||||
} catch (\Throwable $e) {
|
||||
Worker::stopAll(250, $e);
|
||||
}
|
||||
} else {
|
||||
@@ -175,12 +189,11 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
|
||||
$tmp_connection_type = isset($connection->websocketType) ? $connection->websocketType : static::BINARY_TYPE_BLOB;
|
||||
$connection->websocketType = "\x8a";
|
||||
// Try to emit onWebSocketPong callback.
|
||||
if (isset($connection->onWebSocketPong) || isset($connection->worker->onWebSocketPong)) {
|
||||
$pong_cb = $connection->onWebSocketPong ?? $connection->worker->onWebSocketPong ?? false;
|
||||
if ($pong_cb) {
|
||||
try {
|
||||
\call_user_func(isset($connection->onWebSocketPong)?$connection->onWebSocketPong:$connection->worker->onWebSocketPong, $connection, $pong_data);
|
||||
} catch (\Exception $e) {
|
||||
Worker::stopAll(250, $e);
|
||||
} catch (\Error $e) {
|
||||
$pong_cb($connection, $pong_data);
|
||||
} catch (\Throwable $e) {
|
||||
Worker::stopAll(250, $e);
|
||||
}
|
||||
}
|
||||
@@ -193,22 +206,22 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
|
||||
}
|
||||
return $current_frame_length;
|
||||
} else {
|
||||
$connection->websocketCurrentFrameLength = $current_frame_length;
|
||||
$connection->context->websocketCurrentFrameLength = $current_frame_length;
|
||||
}
|
||||
}
|
||||
|
||||
// Received just a frame length data.
|
||||
if ($connection->websocketCurrentFrameLength === $recv_len) {
|
||||
if ($connection->context->websocketCurrentFrameLength === $recv_len) {
|
||||
static::decode($buffer, $connection);
|
||||
$connection->consumeRecvBuffer($connection->websocketCurrentFrameLength);
|
||||
$connection->websocketCurrentFrameLength = 0;
|
||||
$connection->consumeRecvBuffer($connection->context->websocketCurrentFrameLength);
|
||||
$connection->context->websocketCurrentFrameLength = 0;
|
||||
return 0;
|
||||
} // The length of the received data is greater than the length of a frame.
|
||||
elseif ($connection->websocketCurrentFrameLength < $recv_len) {
|
||||
static::decode(\substr($buffer, 0, $connection->websocketCurrentFrameLength), $connection);
|
||||
$connection->consumeRecvBuffer($connection->websocketCurrentFrameLength);
|
||||
$current_frame_length = $connection->websocketCurrentFrameLength;
|
||||
$connection->websocketCurrentFrameLength = 0;
|
||||
elseif ($connection->context->websocketCurrentFrameLength < $recv_len) {
|
||||
static::decode(\substr($buffer, 0, $connection->context->websocketCurrentFrameLength), $connection);
|
||||
$connection->consumeRecvBuffer($connection->context->websocketCurrentFrameLength);
|
||||
$current_frame_length = $connection->context->websocketCurrentFrameLength;
|
||||
$connection->context->websocketCurrentFrameLength = 0;
|
||||
// Continue to read next frame.
|
||||
return static::input(\substr($buffer, $current_frame_length), $connection);
|
||||
} // The length of the received data is less than the length of a frame.
|
||||
@@ -220,7 +233,7 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
|
||||
/**
|
||||
* Websocket encode.
|
||||
*
|
||||
* @param string $buffer
|
||||
* @param string $buffer
|
||||
* @param ConnectionInterface $connection
|
||||
* @return string
|
||||
*/
|
||||
@@ -229,12 +242,18 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
|
||||
if (!is_scalar($buffer)) {
|
||||
throw new \Exception("You can't send(" . \gettype($buffer) . ") to client, you need to convert it to a string. ");
|
||||
}
|
||||
$len = \strlen($buffer);
|
||||
|
||||
if (empty($connection->websocketType)) {
|
||||
$connection->websocketType = static::BINARY_TYPE_BLOB;
|
||||
}
|
||||
|
||||
// permessage-deflate
|
||||
if (\ord($connection->websocketType) & 64) {
|
||||
$buffer = static::deflate($connection, $buffer);
|
||||
}
|
||||
|
||||
$first_byte = $connection->websocketType;
|
||||
$len = \strlen($buffer);
|
||||
|
||||
if ($len <= 125) {
|
||||
$encode_buffer = $first_byte . \chr($len) . $buffer;
|
||||
@@ -247,37 +266,32 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
|
||||
}
|
||||
|
||||
// Handshake not completed so temporary buffer websocket data waiting for send.
|
||||
if (empty($connection->websocketHandshake)) {
|
||||
if (empty($connection->tmpWebsocketData)) {
|
||||
$connection->tmpWebsocketData = '';
|
||||
if (empty($connection->context->websocketHandshake)) {
|
||||
if (empty($connection->context->tmpWebsocketData)) {
|
||||
$connection->context->tmpWebsocketData = '';
|
||||
}
|
||||
// If buffer has already full then discard the current package.
|
||||
if (\strlen($connection->tmpWebsocketData) > $connection->maxSendBufferSize) {
|
||||
if (\strlen($connection->context->tmpWebsocketData) > $connection->maxSendBufferSize) {
|
||||
if ($connection->onError) {
|
||||
try {
|
||||
\call_user_func($connection->onError, $connection, \WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
|
||||
} catch (\Exception $e) {
|
||||
Worker::stopAll(250, $e);
|
||||
} catch (\Error $e) {
|
||||
($connection->onError)($connection, ConnectionInterface::SEND_FAIL, 'send buffer full and drop package');
|
||||
} catch (\Throwable $e) {
|
||||
Worker::stopAll(250, $e);
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
$connection->tmpWebsocketData .= $encode_buffer;
|
||||
$connection->context->tmpWebsocketData .= $encode_buffer;
|
||||
// Check buffer is full.
|
||||
if ($connection->maxSendBufferSize <= \strlen($connection->tmpWebsocketData)) {
|
||||
if ($connection->maxSendBufferSize <= \strlen($connection->context->tmpWebsocketData)) {
|
||||
if ($connection->onBufferFull) {
|
||||
try {
|
||||
\call_user_func($connection->onBufferFull, $connection);
|
||||
} catch (\Exception $e) {
|
||||
Worker::stopAll(250, $e);
|
||||
} catch (\Error $e) {
|
||||
($connection->onBufferFull)($connection);
|
||||
} catch (\Throwable $e) {
|
||||
Worker::stopAll(250, $e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Return empty string.
|
||||
return '';
|
||||
}
|
||||
@@ -288,64 +302,125 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
|
||||
/**
|
||||
* Websocket decode.
|
||||
*
|
||||
* @param string $buffer
|
||||
* @param string $buffer
|
||||
* @param ConnectionInterface $connection
|
||||
* @return string
|
||||
*/
|
||||
public static function decode($buffer, ConnectionInterface $connection)
|
||||
{
|
||||
$len = \ord($buffer[1]) & 127;
|
||||
$first_byte = \ord($buffer[0]);
|
||||
$second_byte = \ord($buffer[1]);
|
||||
$len = $second_byte & 127;
|
||||
$is_fin_frame = $first_byte >> 7;
|
||||
$rsv1 = 64 === ($first_byte & 64);
|
||||
|
||||
if ($len === 126) {
|
||||
$masks = \substr($buffer, 4, 4);
|
||||
$data = \substr($buffer, 8);
|
||||
$data = \substr($buffer, 8);
|
||||
} else {
|
||||
if ($len === 127) {
|
||||
$masks = \substr($buffer, 10, 4);
|
||||
$data = \substr($buffer, 14);
|
||||
$data = \substr($buffer, 14);
|
||||
} else {
|
||||
$masks = \substr($buffer, 2, 4);
|
||||
$data = \substr($buffer, 6);
|
||||
$data = \substr($buffer, 6);
|
||||
}
|
||||
}
|
||||
$dataLength = \strlen($data);
|
||||
$masks = \str_repeat($masks, \floor($dataLength / 4)) . \substr($masks, 0, $dataLength % 4);
|
||||
$decoded = $data ^ $masks;
|
||||
if ($connection->websocketCurrentFrameLength) {
|
||||
$connection->websocketDataBuffer .= $decoded;
|
||||
return $connection->websocketDataBuffer;
|
||||
if ($connection->context->websocketCurrentFrameLength) {
|
||||
$connection->context->websocketDataBuffer .= $decoded;
|
||||
if ($rsv1) {
|
||||
return static::inflate($connection, $connection->context->websocketDataBuffer, $is_fin_frame);
|
||||
}
|
||||
return $connection->context->websocketDataBuffer;
|
||||
} else {
|
||||
if ($connection->websocketDataBuffer !== '') {
|
||||
$decoded = $connection->websocketDataBuffer . $decoded;
|
||||
$connection->websocketDataBuffer = '';
|
||||
if ($connection->context->websocketDataBuffer !== '') {
|
||||
$decoded = $connection->context->websocketDataBuffer . $decoded;
|
||||
$connection->context->websocketDataBuffer = '';
|
||||
}
|
||||
if ($rsv1) {
|
||||
return static::inflate($connection, $decoded, $is_fin_frame);
|
||||
}
|
||||
return $decoded;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Inflate.
|
||||
*
|
||||
* @param $connection
|
||||
* @param $buffer
|
||||
* @param $is_fin_frame
|
||||
* @return false|string
|
||||
*/
|
||||
protected static function inflate($connection, $buffer, $is_fin_frame)
|
||||
{
|
||||
if (!isset($connection->context->inflator)) {
|
||||
$connection->context->inflator = \inflate_init(
|
||||
\ZLIB_ENCODING_RAW,
|
||||
[
|
||||
'level' => -1,
|
||||
'memory' => 8,
|
||||
'window' => 9,
|
||||
'strategy' => \ZLIB_DEFAULT_STRATEGY
|
||||
]
|
||||
);
|
||||
}
|
||||
if ($is_fin_frame) {
|
||||
$buffer .= "\x00\x00\xff\xff";
|
||||
}
|
||||
return \inflate_add($connection->context->inflator, $buffer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deflate.
|
||||
*
|
||||
* @param $connection
|
||||
* @param $buffer
|
||||
* @return false|string
|
||||
*/
|
||||
protected static function deflate($connection, $buffer)
|
||||
{
|
||||
if (!isset($connection->context->deflator)) {
|
||||
$connection->context->deflator = \deflate_init(
|
||||
\ZLIB_ENCODING_RAW,
|
||||
[
|
||||
'level' => -1,
|
||||
'memory' => 8,
|
||||
'window' => 9,
|
||||
'strategy' => \ZLIB_DEFAULT_STRATEGY
|
||||
]
|
||||
);
|
||||
}
|
||||
return \substr(\deflate_add($connection->context->deflator, $buffer), 0, -4);
|
||||
}
|
||||
|
||||
/**
|
||||
* Websocket handshake.
|
||||
*
|
||||
* @param string $buffer
|
||||
* @param string $buffer
|
||||
* @param TcpConnection $connection
|
||||
* @return int
|
||||
*/
|
||||
public static function dealHandshake($buffer, TcpConnection $connection)
|
||||
public static function dealHandshake($buffer, $connection)
|
||||
{
|
||||
// HTTP protocol.
|
||||
if (0 === \strpos($buffer, 'GET')) {
|
||||
// Find \r\n\r\n.
|
||||
$heder_end_pos = \strpos($buffer, "\r\n\r\n");
|
||||
if (!$heder_end_pos) {
|
||||
$header_end_pos = \strpos($buffer, "\r\n\r\n");
|
||||
if (!$header_end_pos) {
|
||||
return 0;
|
||||
}
|
||||
$header_length = $heder_end_pos + 4;
|
||||
$header_length = $header_end_pos + 4;
|
||||
|
||||
// Get Sec-WebSocket-Key.
|
||||
$Sec_WebSocket_Key = '';
|
||||
if (\preg_match("/Sec-WebSocket-Key: *(.*?)\r\n/i", $buffer, $match)) {
|
||||
$Sec_WebSocket_Key = $match[1];
|
||||
} else {
|
||||
$connection->close("HTTP/1.1 200 WebSocket\r\nServer: workerman/".Worker::VERSION."\r\n\r\n<div style=\"text-align:center\"><h1>WebSocket</h1><hr>workerman/".Worker::VERSION."</div>",
|
||||
$connection->close("HTTP/1.1 200 WebSocket\r\nServer: workerman/" . Worker::VERSION . "\r\n\r\n<div style=\"text-align:center\"><h1>WebSocket</h1><hr>workerman/" . Worker::VERSION . "</div>",
|
||||
true);
|
||||
return 0;
|
||||
}
|
||||
@@ -353,54 +428,22 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
|
||||
$new_key = \base64_encode(\sha1($Sec_WebSocket_Key . "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true));
|
||||
// Handshake response data.
|
||||
$handshake_message = "HTTP/1.1 101 Switching Protocols\r\n"
|
||||
."Upgrade: websocket\r\n"
|
||||
."Sec-WebSocket-Version: 13\r\n"
|
||||
."Connection: Upgrade\r\n"
|
||||
."Sec-WebSocket-Accept: " . $new_key . "\r\n";
|
||||
. "Upgrade: websocket\r\n"
|
||||
. "Sec-WebSocket-Version: 13\r\n"
|
||||
. "Connection: Upgrade\r\n"
|
||||
. "Sec-WebSocket-Accept: " . $new_key . "\r\n";
|
||||
|
||||
// Websocket data buffer.
|
||||
$connection->websocketDataBuffer = '';
|
||||
$connection->context->websocketDataBuffer = '';
|
||||
// Current websocket frame length.
|
||||
$connection->websocketCurrentFrameLength = 0;
|
||||
$connection->context->websocketCurrentFrameLength = 0;
|
||||
// Current websocket frame data.
|
||||
$connection->websocketCurrentFrameBuffer = '';
|
||||
$connection->context->websocketCurrentFrameBuffer = '';
|
||||
// Consume handshake data.
|
||||
$connection->consumeRecvBuffer($header_length);
|
||||
|
||||
// blob or arraybuffer
|
||||
if (empty($connection->websocketType)) {
|
||||
$connection->websocketType = static::BINARY_TYPE_BLOB;
|
||||
}
|
||||
|
||||
$has_server_header = false;
|
||||
|
||||
if (isset($connection->headers)) {
|
||||
if (\is_array($connection->headers)) {
|
||||
foreach ($connection->headers as $header) {
|
||||
if (\stripos($header, 'Server:') === 0) {
|
||||
$has_server_header = true;
|
||||
}
|
||||
$handshake_message .= "$header\r\n";
|
||||
}
|
||||
} else {
|
||||
if (\stripos($connection->headers, 'Server:') !== false) {
|
||||
$has_server_header = true;
|
||||
}
|
||||
$handshake_message .= "$connection->headers\r\n";
|
||||
}
|
||||
}
|
||||
if (!$has_server_header) {
|
||||
$handshake_message .= "Server: workerman/".Worker::VERSION."\r\n";
|
||||
}
|
||||
$handshake_message .= "\r\n";
|
||||
// Send handshake response.
|
||||
$connection->send($handshake_message, true);
|
||||
// Mark handshake complete..
|
||||
$connection->websocketHandshake = true;
|
||||
|
||||
// Try to emit onWebSocketConnect callback.
|
||||
$on_websocket_connect = isset($connection->onWebSocketConnect) ? $connection->onWebSocketConnect :
|
||||
(isset($connection->worker->onWebSocketConnect) ? $connection->worker->onWebSocketConnect : false);
|
||||
$on_websocket_connect = $connection->onWebSocketConnect ?? $connection->worker->onWebSocketConnect ?? false;
|
||||
if ($on_websocket_connect) {
|
||||
static::parseHttpHeader($buffer);
|
||||
try {
|
||||
@@ -416,24 +459,49 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
|
||||
$_GET = $_SERVER = $_SESSION = $_COOKIE = array();
|
||||
}
|
||||
|
||||
// blob or arraybuffer
|
||||
if (empty($connection->websocketType)) {
|
||||
$connection->websocketType = static::BINARY_TYPE_BLOB;
|
||||
}
|
||||
|
||||
$has_server_header = false;
|
||||
|
||||
if (isset($connection->headers)) {
|
||||
if (\is_array($connection->headers)) {
|
||||
foreach ($connection->headers as $header) {
|
||||
if (\stripos($header, 'Server:') === 0) {
|
||||
$has_server_header = true;
|
||||
}
|
||||
$handshake_message .= "$header\r\n";
|
||||
}
|
||||
} else {
|
||||
if (\stripos($connection->headers, 'Server:') !== false) {
|
||||
$has_server_header = true;
|
||||
}
|
||||
$handshake_message .= "$connection->headers\r\n";
|
||||
}
|
||||
}
|
||||
if (!$has_server_header) {
|
||||
$handshake_message .= "Server: workerman/" . Worker::VERSION . "\r\n";
|
||||
}
|
||||
$handshake_message .= "\r\n";
|
||||
// Send handshake response.
|
||||
$connection->send($handshake_message, true);
|
||||
// Mark handshake complete..
|
||||
$connection->context->websocketHandshake = true;
|
||||
|
||||
// There are data waiting to be sent.
|
||||
if (!empty($connection->tmpWebsocketData)) {
|
||||
$connection->send($connection->tmpWebsocketData, true);
|
||||
$connection->tmpWebsocketData = '';
|
||||
if (!empty($connection->context->tmpWebsocketData)) {
|
||||
$connection->send($connection->context->tmpWebsocketData, true);
|
||||
$connection->context->tmpWebsocketData = '';
|
||||
}
|
||||
if (\strlen($buffer) > $header_length) {
|
||||
return static::input(\substr($buffer, $header_length), $connection);
|
||||
}
|
||||
return 0;
|
||||
} // Is flash policy-file-request.
|
||||
elseif (0 === \strpos($buffer, '<polic')) {
|
||||
$policy_xml = '<?xml version="1.0"?><cross-domain-policy><site-control permitted-cross-domain-policies="all"/><allow-access-from domain="*" to-ports="*"/></cross-domain-policy>' . "\0";
|
||||
$connection->send($policy_xml, true);
|
||||
$connection->consumeRecvBuffer(\strlen($buffer));
|
||||
return 0;
|
||||
}
|
||||
// Bad websocket handshake request.
|
||||
$connection->close("HTTP/1.1 200 WebSocket\r\nServer: workerman/".Worker::VERSION."\r\n\r\n<div style=\"text-align:center\"><h1>WebSocket</h1><hr>workerman/".Worker::VERSION."</div>",
|
||||
$connection->close("HTTP/1.1 200 WebSocket\r\nServer: workerman/" . Worker::VERSION . "\r\n\r\n<div style=\"text-align:center\"><h1>WebSocket</h1><hr>workerman/" . Worker::VERSION . "</div>",
|
||||
true);
|
||||
return 0;
|
||||
}
|
||||
@@ -492,4 +560,5 @@ class Websocket implements \Workerman\Protocols\ProtocolInterface
|
||||
$_SERVER['QUERY_STRING'] = '';
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
318
vendor/workerman/workerman/Protocols/Ws.php
vendored
318
vendor/workerman/workerman/Protocols/Ws.php
vendored
@@ -11,10 +11,11 @@
|
||||
* @link http://www.workerman.net/
|
||||
* @license http://www.opensource.org/licenses/mit-license.php MIT License
|
||||
*/
|
||||
|
||||
namespace Workerman\Protocols;
|
||||
|
||||
use Workerman\Worker;
|
||||
use Workerman\Lib\Timer;
|
||||
use Workerman\Timer;
|
||||
use Workerman\Connection\TcpConnection;
|
||||
use Workerman\Connection\ConnectionInterface;
|
||||
|
||||
@@ -40,38 +41,38 @@ class Ws
|
||||
/**
|
||||
* Check the integrity of the package.
|
||||
*
|
||||
* @param string $buffer
|
||||
* @param string $buffer
|
||||
* @param ConnectionInterface $connection
|
||||
* @return int
|
||||
*/
|
||||
public static function input($buffer, ConnectionInterface $connection)
|
||||
{
|
||||
if (empty($connection->handshakeStep)) {
|
||||
if (empty($connection->context->handshakeStep)) {
|
||||
Worker::safeEcho("recv data before handshake. Buffer:" . \bin2hex($buffer) . "\n");
|
||||
return false;
|
||||
}
|
||||
// Recv handshake response
|
||||
if ($connection->handshakeStep === 1) {
|
||||
if ($connection->context->handshakeStep === 1) {
|
||||
return self::dealHandshake($buffer, $connection);
|
||||
}
|
||||
$recv_len = \strlen($buffer);
|
||||
if ($recv_len < 2) {
|
||||
$recvLen = \strlen($buffer);
|
||||
if ($recvLen < 2) {
|
||||
return 0;
|
||||
}
|
||||
// Buffer websocket frame data.
|
||||
if ($connection->websocketCurrentFrameLength) {
|
||||
if ($connection->context->websocketCurrentFrameLength) {
|
||||
// We need more frame data.
|
||||
if ($connection->websocketCurrentFrameLength > $recv_len) {
|
||||
if ($connection->context->websocketCurrentFrameLength > $recvLen) {
|
||||
// Return 0, because it is not clear the full packet length, waiting for the frame of fin=1.
|
||||
return 0;
|
||||
}
|
||||
} else {
|
||||
|
||||
$firstbyte = \ord($buffer[0]);
|
||||
$secondbyte = \ord($buffer[1]);
|
||||
$data_len = $secondbyte & 127;
|
||||
$is_fin_frame = $firstbyte >> 7;
|
||||
$masked = $secondbyte >> 7;
|
||||
$firstbyte = \ord($buffer[0]);
|
||||
$secondbyte = \ord($buffer[1]);
|
||||
$dataLen = $secondbyte & 127;
|
||||
$isFinFrame = $firstbyte >> 7;
|
||||
$masked = $secondbyte >> 7;
|
||||
|
||||
if ($masked) {
|
||||
Worker::safeEcho("frame masked so close the connection\n");
|
||||
@@ -79,26 +80,26 @@ class Ws
|
||||
return 0;
|
||||
}
|
||||
|
||||
$opcode = $firstbyte & 0xf;
|
||||
$opcode = $firstbyte & 0xf;
|
||||
|
||||
switch ($opcode) {
|
||||
case 0x0:
|
||||
break;
|
||||
// Blob type.
|
||||
// Blob type.
|
||||
case 0x1:
|
||||
break;
|
||||
// Arraybuffer type.
|
||||
// Arraybuffer type.
|
||||
case 0x2:
|
||||
// Ping package.
|
||||
case 0x9:
|
||||
// Pong package.
|
||||
case 0xa:
|
||||
break;
|
||||
// Close package.
|
||||
case 0x8:
|
||||
// Try to emit onWebSocketClose callback.
|
||||
if (isset($connection->onWebSocketClose)) {
|
||||
try {
|
||||
\call_user_func($connection->onWebSocketClose, $connection);
|
||||
} catch (\Exception $e) {
|
||||
Worker::stopAll(250, $e);
|
||||
} catch (\Error $e) {
|
||||
($connection->onWebSocketClose)($connection);
|
||||
} catch (\Throwable $e) {
|
||||
Worker::stopAll(250, $e);
|
||||
}
|
||||
} // Close connection.
|
||||
@@ -106,12 +107,6 @@ class Ws
|
||||
$connection->close();
|
||||
}
|
||||
return 0;
|
||||
// Ping package.
|
||||
case 0x9:
|
||||
break;
|
||||
// Pong package.
|
||||
case 0xa:
|
||||
break;
|
||||
// Wrong opcode.
|
||||
default :
|
||||
Worker::safeEcho("error opcode $opcode and close websocket connection. Buffer:" . $buffer . "\n");
|
||||
@@ -119,96 +114,92 @@ class Ws
|
||||
return 0;
|
||||
}
|
||||
// Calculate packet length.
|
||||
if ($data_len === 126) {
|
||||
if ($dataLen === 126) {
|
||||
if (\strlen($buffer) < 4) {
|
||||
return 0;
|
||||
}
|
||||
$pack = \unpack('nn/ntotal_len', $buffer);
|
||||
$current_frame_length = $pack['total_len'] + 4;
|
||||
} else if ($data_len === 127) {
|
||||
$currentFrameLength = $pack['total_len'] + 4;
|
||||
} else if ($dataLen === 127) {
|
||||
if (\strlen($buffer) < 10) {
|
||||
return 0;
|
||||
}
|
||||
$arr = \unpack('n/N2c', $buffer);
|
||||
$current_frame_length = $arr['c1']*4294967296 + $arr['c2'] + 10;
|
||||
$currentFrameLength = $arr['c1'] * 4294967296 + $arr['c2'] + 10;
|
||||
} else {
|
||||
$current_frame_length = $data_len + 2;
|
||||
$currentFrameLength = $dataLen + 2;
|
||||
}
|
||||
|
||||
$total_package_size = \strlen($connection->websocketDataBuffer) + $current_frame_length;
|
||||
if ($total_package_size > $connection->maxPackageSize) {
|
||||
Worker::safeEcho("error package. package_length=$total_package_size\n");
|
||||
$totalPackageSize = \strlen($connection->context->websocketDataBuffer) + $currentFrameLength;
|
||||
if ($totalPackageSize > $connection->maxPackageSize) {
|
||||
Worker::safeEcho("error package. package_length=$totalPackageSize\n");
|
||||
$connection->close();
|
||||
return 0;
|
||||
}
|
||||
|
||||
if ($is_fin_frame) {
|
||||
if ($isFinFrame) {
|
||||
if ($opcode === 0x9) {
|
||||
if ($recv_len >= $current_frame_length) {
|
||||
$ping_data = static::decode(\substr($buffer, 0, $current_frame_length), $connection);
|
||||
$connection->consumeRecvBuffer($current_frame_length);
|
||||
$tmp_connection_type = isset($connection->websocketType) ? $connection->websocketType : static::BINARY_TYPE_BLOB;
|
||||
if ($recvLen >= $currentFrameLength) {
|
||||
$pingData = static::decode(\substr($buffer, 0, $currentFrameLength), $connection);
|
||||
$connection->consumeRecvBuffer($currentFrameLength);
|
||||
$tmpConnectionType = isset($connection->websocketType) ? $connection->websocketType : static::BINARY_TYPE_BLOB;
|
||||
$connection->websocketType = "\x8a";
|
||||
if (isset($connection->onWebSocketPing)) {
|
||||
try {
|
||||
\call_user_func($connection->onWebSocketPing, $connection, $ping_data);
|
||||
} catch (\Exception $e) {
|
||||
Worker::stopAll(250, $e);
|
||||
} catch (\Error $e) {
|
||||
($connection->onWebSocketPing)($connection, $pingData);
|
||||
} catch (\Throwable $e) {
|
||||
Worker::stopAll(250, $e);
|
||||
}
|
||||
} else {
|
||||
$connection->send($ping_data);
|
||||
$connection->send($pingData);
|
||||
}
|
||||
$connection->websocketType = $tmp_connection_type;
|
||||
if ($recv_len > $current_frame_length) {
|
||||
return static::input(\substr($buffer, $current_frame_length), $connection);
|
||||
$connection->websocketType = $tmpConnectionType;
|
||||
if ($recvLen > $currentFrameLength) {
|
||||
return static::input(\substr($buffer, $currentFrameLength), $connection);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
|
||||
} else if ($opcode === 0xa) {
|
||||
if ($recv_len >= $current_frame_length) {
|
||||
$pong_data = static::decode(\substr($buffer, 0, $current_frame_length), $connection);
|
||||
$connection->consumeRecvBuffer($current_frame_length);
|
||||
$tmp_connection_type = isset($connection->websocketType) ? $connection->websocketType : static::BINARY_TYPE_BLOB;
|
||||
if ($recvLen >= $currentFrameLength) {
|
||||
$pongData = static::decode(\substr($buffer, 0, $currentFrameLength), $connection);
|
||||
$connection->consumeRecvBuffer($currentFrameLength);
|
||||
$tmpConnectionType = isset($connection->websocketType) ? $connection->websocketType : static::BINARY_TYPE_BLOB;
|
||||
$connection->websocketType = "\x8a";
|
||||
// Try to emit onWebSocketPong callback.
|
||||
if (isset($connection->onWebSocketPong)) {
|
||||
try {
|
||||
\call_user_func($connection->onWebSocketPong, $connection, $pong_data);
|
||||
} catch (\Exception $e) {
|
||||
Worker::stopAll(250, $e);
|
||||
} catch (\Error $e) {
|
||||
($connection->onWebSocketPong)($connection, $pongData);
|
||||
} catch (\Throwable $e) {
|
||||
Worker::stopAll(250, $e);
|
||||
}
|
||||
}
|
||||
$connection->websocketType = $tmp_connection_type;
|
||||
if ($recv_len > $current_frame_length) {
|
||||
return static::input(\substr($buffer, $current_frame_length), $connection);
|
||||
$connection->websocketType = $tmpConnectionType;
|
||||
if ($recvLen > $currentFrameLength) {
|
||||
return static::input(\substr($buffer, $currentFrameLength), $connection);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
return $current_frame_length;
|
||||
return $currentFrameLength;
|
||||
} else {
|
||||
$connection->websocketCurrentFrameLength = $current_frame_length;
|
||||
$connection->context->websocketCurrentFrameLength = $currentFrameLength;
|
||||
}
|
||||
}
|
||||
// Received just a frame length data.
|
||||
if ($connection->websocketCurrentFrameLength === $recv_len) {
|
||||
if ($connection->context->websocketCurrentFrameLength === $recvLen) {
|
||||
self::decode($buffer, $connection);
|
||||
$connection->consumeRecvBuffer($connection->websocketCurrentFrameLength);
|
||||
$connection->websocketCurrentFrameLength = 0;
|
||||
$connection->consumeRecvBuffer($connection->context->websocketCurrentFrameLength);
|
||||
$connection->context->websocketCurrentFrameLength = 0;
|
||||
return 0;
|
||||
} // The length of the received data is greater than the length of a frame.
|
||||
elseif ($connection->websocketCurrentFrameLength < $recv_len) {
|
||||
self::decode(\substr($buffer, 0, $connection->websocketCurrentFrameLength), $connection);
|
||||
$connection->consumeRecvBuffer($connection->websocketCurrentFrameLength);
|
||||
$current_frame_length = $connection->websocketCurrentFrameLength;
|
||||
$connection->websocketCurrentFrameLength = 0;
|
||||
elseif ($connection->context->websocketCurrentFrameLength < $recvLen) {
|
||||
self::decode(\substr($buffer, 0, $connection->context->websocketCurrentFrameLength), $connection);
|
||||
$connection->consumeRecvBuffer($connection->context->websocketCurrentFrameLength);
|
||||
$currentFrameLength = $connection->context->websocketCurrentFrameLength;
|
||||
$connection->context->websocketCurrentFrameLength = 0;
|
||||
// Continue to read next frame.
|
||||
return self::input(\substr($buffer, $current_frame_length), $connection);
|
||||
return self::input(\substr($buffer, $currentFrameLength), $connection);
|
||||
} // The length of the received data is less than the length of a frame.
|
||||
else {
|
||||
return 0;
|
||||
@@ -218,7 +209,7 @@ class Ws
|
||||
/**
|
||||
* Websocket encode.
|
||||
*
|
||||
* @param string $buffer
|
||||
* @param string $buffer
|
||||
* @param ConnectionInterface $connection
|
||||
* @return string
|
||||
*/
|
||||
@@ -228,52 +219,44 @@ class Ws
|
||||
$connection->websocketType = self::BINARY_TYPE_BLOB;
|
||||
}
|
||||
$payload = (string)$payload;
|
||||
if (empty($connection->handshakeStep)) {
|
||||
if (empty($connection->context->handshakeStep)) {
|
||||
static::sendHandshake($connection);
|
||||
}
|
||||
$mask = 1;
|
||||
$mask_key = "\x00\x00\x00\x00";
|
||||
|
||||
$pack = '';
|
||||
$length = $length_flag = \strlen($payload);
|
||||
if (65535 < $length) {
|
||||
$pack = \pack('NN', ($length & 0xFFFFFFFF00000000) >> 32, $length & 0x00000000FFFFFFFF);
|
||||
$length_flag = 127;
|
||||
} else if (125 < $length) {
|
||||
$pack = \pack('n*', $length);
|
||||
$length_flag = 126;
|
||||
$maskKey = "\x00\x00\x00\x00";
|
||||
$length = \strlen($payload);
|
||||
|
||||
if (strlen($payload) < 126) {
|
||||
$head = chr(0x80 | $length);
|
||||
} elseif ($length < 0xFFFF) {
|
||||
$head = chr(0x80 | 126) . pack("n", $length);
|
||||
} else {
|
||||
$head = chr(0x80 | 127) . pack("N", 0) . pack("N", $length);
|
||||
}
|
||||
|
||||
$head = ($mask << 7) | $length_flag;
|
||||
$head = $connection->websocketType . \chr($head) . $pack;
|
||||
|
||||
$frame = $head . $mask_key;
|
||||
$frame = $connection->websocketType . $head . $maskKey;
|
||||
// append payload to frame:
|
||||
$mask_key = \str_repeat($mask_key, \floor($length / 4)) . \substr($mask_key, 0, $length % 4);
|
||||
$frame .= $payload ^ $mask_key;
|
||||
if ($connection->handshakeStep === 1) {
|
||||
$maskKey = \str_repeat($maskKey, \floor($length / 4)) . \substr($maskKey, 0, $length % 4);
|
||||
$frame .= $payload ^ $maskKey;
|
||||
if ($connection->context->handshakeStep === 1) {
|
||||
// If buffer has already full then discard the current package.
|
||||
if (\strlen($connection->tmpWebsocketData) > $connection->maxSendBufferSize) {
|
||||
if (\strlen($connection->context->tmpWebsocketData) > $connection->maxSendBufferSize) {
|
||||
if ($connection->onError) {
|
||||
try {
|
||||
\call_user_func($connection->onError, $connection, \WORKERMAN_SEND_FAIL, 'send buffer full and drop package');
|
||||
} catch (\Exception $e) {
|
||||
Worker::stopAll(250, $e);
|
||||
} catch (\Error $e) {
|
||||
($connection->onError)($connection, ConnectionInterface::SEND_FAIL, 'send buffer full and drop package');
|
||||
} catch (\Throwable $e) {
|
||||
Worker::stopAll(250, $e);
|
||||
}
|
||||
}
|
||||
return '';
|
||||
}
|
||||
$connection->tmpWebsocketData = $connection->tmpWebsocketData . $frame;
|
||||
$connection->context->tmpWebsocketData = $connection->context->tmpWebsocketData . $frame;
|
||||
// Check buffer is full.
|
||||
if ($connection->maxSendBufferSize <= \strlen($connection->tmpWebsocketData)) {
|
||||
if ($connection->maxSendBufferSize <= \strlen($connection->context->tmpWebsocketData)) {
|
||||
if ($connection->onBufferFull) {
|
||||
try {
|
||||
\call_user_func($connection->onBufferFull, $connection);
|
||||
} catch (\Exception $e) {
|
||||
Worker::stopAll(250, $e);
|
||||
} catch (\Error $e) {
|
||||
($connection->onBufferFull)($connection);
|
||||
} catch (\Throwable $e) {
|
||||
Worker::stopAll(250, $e);
|
||||
}
|
||||
}
|
||||
@@ -286,30 +269,30 @@ class Ws
|
||||
/**
|
||||
* Websocket decode.
|
||||
*
|
||||
* @param string $buffer
|
||||
* @param string $buffer
|
||||
* @param ConnectionInterface $connection
|
||||
* @return string
|
||||
*/
|
||||
public static function decode($bytes, ConnectionInterface $connection)
|
||||
{
|
||||
$data_length = \ord($bytes[1]);
|
||||
$dataLength = \ord($bytes[1]);
|
||||
|
||||
if ($data_length === 126) {
|
||||
$decoded_data = \substr($bytes, 4);
|
||||
} else if ($data_length === 127) {
|
||||
$decoded_data = \substr($bytes, 10);
|
||||
if ($dataLength === 126) {
|
||||
$decodedData = \substr($bytes, 4);
|
||||
} else if ($dataLength === 127) {
|
||||
$decodedData = \substr($bytes, 10);
|
||||
} else {
|
||||
$decoded_data = \substr($bytes, 2);
|
||||
$decodedData = \substr($bytes, 2);
|
||||
}
|
||||
if ($connection->websocketCurrentFrameLength) {
|
||||
$connection->websocketDataBuffer .= $decoded_data;
|
||||
return $connection->websocketDataBuffer;
|
||||
if ($connection->context->websocketCurrentFrameLength) {
|
||||
$connection->context->websocketDataBuffer .= $decodedData;
|
||||
return $connection->context->websocketDataBuffer;
|
||||
} else {
|
||||
if ($connection->websocketDataBuffer !== '') {
|
||||
$decoded_data = $connection->websocketDataBuffer . $decoded_data;
|
||||
$connection->websocketDataBuffer = '';
|
||||
if ($connection->context->websocketDataBuffer !== '') {
|
||||
$decodedData = $connection->context->websocketDataBuffer . $decodedData;
|
||||
$connection->context->websocketDataBuffer = '';
|
||||
}
|
||||
return $decoded_data;
|
||||
return $decodedData;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -330,13 +313,13 @@ class Ws
|
||||
*/
|
||||
public static function onClose($connection)
|
||||
{
|
||||
$connection->handshakeStep = null;
|
||||
$connection->websocketCurrentFrameLength = 0;
|
||||
$connection->tmpWebsocketData = '';
|
||||
$connection->websocketDataBuffer = '';
|
||||
if (!empty($connection->websocketPingTimer)) {
|
||||
Timer::del($connection->websocketPingTimer);
|
||||
$connection->websocketPingTimer = null;
|
||||
$connection->context->handshakeStep = null;
|
||||
$connection->context->websocketCurrentFrameLength = 0;
|
||||
$connection->context->tmpWebsocketData = '';
|
||||
$connection->context->websocketDataBuffer = '';
|
||||
if (!empty($connection->context->websocketPingTimer)) {
|
||||
Timer::del($connection->context->websocketPingTimer);
|
||||
$connection->context->websocketPingTimer = null;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -346,58 +329,57 @@ class Ws
|
||||
* @param TcpConnection $connection
|
||||
* @return void
|
||||
*/
|
||||
public static function sendHandshake(TcpConnection $connection)
|
||||
public static function sendHandshake(ConnectionInterface $connection)
|
||||
{
|
||||
if (!empty($connection->handshakeStep)) {
|
||||
if (!empty($connection->context->handshakeStep)) {
|
||||
return;
|
||||
}
|
||||
// Get Host.
|
||||
$port = $connection->getRemotePort();
|
||||
$host = $port === 80 ? $connection->getRemoteHost() : $connection->getRemoteHost() . ':' . $port;
|
||||
// Handshake header.
|
||||
$connection->websocketSecKey = \base64_encode(random_bytes(16));
|
||||
$user_header = isset($connection->headers) ? $connection->headers :
|
||||
(isset($connection->wsHttpHeader) ? $connection->wsHttpHeader : null);
|
||||
$user_header_str = '';
|
||||
if (!empty($user_header)) {
|
||||
if (\is_array($user_header)){
|
||||
foreach($user_header as $k=>$v){
|
||||
$user_header_str .= "$k: $v\r\n";
|
||||
$connection->context->websocketSecKey = \base64_encode(random_bytes(16));
|
||||
$userHeader = $connection->headers ?? null;
|
||||
$userHeaderStr = '';
|
||||
if (!empty($userHeader)) {
|
||||
if (\is_array($userHeader)) {
|
||||
foreach ($userHeader as $k => $v) {
|
||||
$userHeaderStr .= "$k: $v\r\n";
|
||||
}
|
||||
} else {
|
||||
$user_header_str .= $user_header;
|
||||
$userHeaderStr .= $userHeader;
|
||||
}
|
||||
$user_header_str = "\r\n".\trim($user_header_str);
|
||||
$userHeaderStr = "\r\n" . \trim($userHeaderStr);
|
||||
}
|
||||
$header = 'GET ' . $connection->getRemoteURI() . " HTTP/1.1\r\n".
|
||||
(!\preg_match("/\nHost:/i", $user_header_str) ? "Host: $host\r\n" : '').
|
||||
"Connection: Upgrade\r\n".
|
||||
"Upgrade: websocket\r\n".
|
||||
(isset($connection->websocketOrigin) ? "Origin: ".$connection->websocketOrigin."\r\n":'').
|
||||
(isset($connection->WSClientProtocol)?"Sec-WebSocket-Protocol: ".$connection->WSClientProtocol."\r\n":'').
|
||||
"Sec-WebSocket-Version: 13\r\n".
|
||||
"Sec-WebSocket-Key: " . $connection->websocketSecKey . $user_header_str . "\r\n\r\n";
|
||||
$header = 'GET ' . $connection->getRemoteURI() . " HTTP/1.1\r\n" .
|
||||
(!\preg_match("/\nHost:/i", $userHeaderStr) ? "Host: $host\r\n" : '') .
|
||||
"Connection: Upgrade\r\n" .
|
||||
"Upgrade: websocket\r\n" .
|
||||
(isset($connection->websocketOrigin) ? "Origin: " . $connection->websocketOrigin . "\r\n" : '') .
|
||||
(isset($connection->websocketClientProtocol) ? "Sec-WebSocket-Protocol: " . $connection->websocketClientProtocol . "\r\n" : '') .
|
||||
"Sec-WebSocket-Version: 13\r\n" .
|
||||
"Sec-WebSocket-Key: " . $connection->context->websocketSecKey . $userHeaderStr . "\r\n\r\n";
|
||||
$connection->send($header, true);
|
||||
$connection->handshakeStep = 1;
|
||||
$connection->websocketCurrentFrameLength = 0;
|
||||
$connection->websocketDataBuffer = '';
|
||||
$connection->tmpWebsocketData = '';
|
||||
$connection->context->handshakeStep = 1;
|
||||
$connection->context->websocketCurrentFrameLength = 0;
|
||||
$connection->context->websocketDataBuffer = '';
|
||||
$connection->context->tmpWebsocketData = '';
|
||||
}
|
||||
|
||||
/**
|
||||
* Websocket handshake.
|
||||
*
|
||||
* @param string $buffer
|
||||
* @param string $buffer
|
||||
* @param TcpConnection $connection
|
||||
* @return int
|
||||
*/
|
||||
public static function dealHandshake($buffer, TcpConnection $connection)
|
||||
public static function dealHandshake($buffer, ConnectionInterface $connection)
|
||||
{
|
||||
$pos = \strpos($buffer, "\r\n\r\n");
|
||||
if ($pos) {
|
||||
//checking Sec-WebSocket-Accept
|
||||
if (\preg_match("/Sec-WebSocket-Accept: *(.*?)\r\n/i", $buffer, $match)) {
|
||||
if ($match[1] !== \base64_encode(\sha1($connection->websocketSecKey . "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true))) {
|
||||
if ($match[1] !== \base64_encode(\sha1($connection->context->websocketSecKey . "258EAFA5-E914-47DA-95CA-C5AB0DC85B11", true))) {
|
||||
Worker::safeEcho("Sec-WebSocket-Accept not match. Header:\n" . \substr($buffer, 0, $pos) . "\n");
|
||||
$connection->close();
|
||||
return 0;
|
||||
@@ -412,49 +394,39 @@ class Ws
|
||||
|
||||
// Get WebSocket subprotocol (if specified by server)
|
||||
if (\preg_match("/Sec-WebSocket-Protocol: *(.*?)\r\n/i", $buffer, $match)) {
|
||||
$connection->WSServerProtocol = \trim($match[1]);
|
||||
$connection->websocketServerProtocol = \trim($match[1]);
|
||||
}
|
||||
|
||||
$connection->handshakeStep = 2;
|
||||
$handshake_response_length = $pos + 4;
|
||||
$connection->context->handshakeStep = 2;
|
||||
$handshakeResponseLength = $pos + 4;
|
||||
// Try to emit onWebSocketConnect callback.
|
||||
if (isset($connection->onWebSocketConnect)) {
|
||||
try {
|
||||
\call_user_func($connection->onWebSocketConnect, $connection, \substr($buffer, 0, $handshake_response_length));
|
||||
} catch (\Exception $e) {
|
||||
Worker::stopAll(250, $e);
|
||||
} catch (\Error $e) {
|
||||
($connection->onWebSocketConnect)($connection, \substr($buffer, 0, $handshakeResponseLength));
|
||||
} catch (\Throwable $e) {
|
||||
Worker::stopAll(250, $e);
|
||||
}
|
||||
}
|
||||
// Headbeat.
|
||||
if (!empty($connection->websocketPingInterval)) {
|
||||
$connection->websocketPingTimer = Timer::add($connection->websocketPingInterval, function() use ($connection){
|
||||
$connection->context->websocketPingTimer = Timer::add($connection->websocketPingInterval, function () use ($connection) {
|
||||
if (false === $connection->send(\pack('H*', '898000000000'), true)) {
|
||||
Timer::del($connection->websocketPingTimer);
|
||||
$connection->websocketPingTimer = null;
|
||||
Timer::del($connection->context->websocketPingTimer);
|
||||
$connection->context->websocketPingTimer = null;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
$connection->consumeRecvBuffer($handshake_response_length);
|
||||
if (!empty($connection->tmpWebsocketData)) {
|
||||
$connection->send($connection->tmpWebsocketData, true);
|
||||
$connection->tmpWebsocketData = '';
|
||||
$connection->consumeRecvBuffer($handshakeResponseLength);
|
||||
if (!empty($connection->context->tmpWebsocketData)) {
|
||||
$connection->send($connection->context->tmpWebsocketData, true);
|
||||
$connection->context->tmpWebsocketData = '';
|
||||
}
|
||||
if (\strlen($buffer) > $handshake_response_length) {
|
||||
return self::input(\substr($buffer, $handshake_response_length), $connection);
|
||||
if (\strlen($buffer) > $handshakeResponseLength) {
|
||||
return self::input(\substr($buffer, $handshakeResponseLength), $connection);
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
public static function WSSetProtocol($connection, $params) {
|
||||
$connection->WSClientProtocol = $params[0];
|
||||
}
|
||||
|
||||
public static function WSGetServerProtocol($connection) {
|
||||
return (\property_exists($connection, 'WSServerProtocol') ? $connection->WSServerProtocol : null);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
2
vendor/workerman/workerman/README.md
vendored
2
vendor/workerman/workerman/README.md
vendored
@@ -12,7 +12,7 @@ Workerman supports HTTP, Websocket, SSL and other custom protocols.
|
||||
Workerman supports event extension.
|
||||
|
||||
## Requires
|
||||
PHP 5.4 or Higher
|
||||
PHP 7.0 or Higher
|
||||
A POSIX compatible operating system (Linux, OSX, BSD)
|
||||
POSIX and PCNTL extensions required
|
||||
Event extension recommended for better performance
|
||||
|
||||
4
vendor/workerman/workerman/Timer.php
vendored
4
vendor/workerman/workerman/Timer.php
vendored
@@ -210,7 +210,9 @@ class Timer
|
||||
public static function delAll()
|
||||
{
|
||||
self::$_tasks = self::$_status = array();
|
||||
\pcntl_alarm(0);
|
||||
if (\function_exists('pcntl_alarm')) {
|
||||
\pcntl_alarm(0);
|
||||
}
|
||||
if (self::$_event) {
|
||||
self::$_event->clearAllTimer();
|
||||
}
|
||||
|
||||
174
vendor/workerman/workerman/Worker.php
vendored
174
vendor/workerman/workerman/Worker.php
vendored
@@ -34,7 +34,7 @@ class Worker
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
const VERSION = '4.1.4';
|
||||
const VERSION = '4.1.9';
|
||||
|
||||
/**
|
||||
* Status starting.
|
||||
@@ -549,8 +549,8 @@ class Worker
|
||||
{
|
||||
static::checkSapiEnv();
|
||||
static::init();
|
||||
static::lock();
|
||||
static::parseCommand();
|
||||
static::lock();
|
||||
static::daemonize();
|
||||
static::initWorkers();
|
||||
static::installSignal();
|
||||
@@ -767,7 +767,7 @@ class Worker
|
||||
protected static function getCurrentUser()
|
||||
{
|
||||
$user_info = \posix_getpwuid(\posix_getuid());
|
||||
return $user_info['name'];
|
||||
return $user_info['name'] ?? 'unknown';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -943,7 +943,7 @@ class Worker
|
||||
exit;
|
||||
}
|
||||
|
||||
$statistics_file = static::$statusFile ? static::$statusFile : __DIR__ . "/../workerman-$master_pid.status";
|
||||
$statistics_file = static::$statusFile ? static::$statusFile : __DIR__ . "/../workerman-$master_pid.$command";
|
||||
|
||||
// execute command.
|
||||
switch ($command) {
|
||||
@@ -1218,7 +1218,7 @@ class Worker
|
||||
case \SIGINT:
|
||||
case \SIGTERM:
|
||||
case \SIGHUP:
|
||||
case \SIGTSTP;
|
||||
case \SIGTSTP:
|
||||
static::$_gracefulStop = false;
|
||||
static::stopAll();
|
||||
break;
|
||||
@@ -1230,6 +1230,9 @@ class Worker
|
||||
// Reload.
|
||||
case \SIGUSR2:
|
||||
case \SIGUSR1:
|
||||
if (static::$_status === static::STATUS_SHUTDOWN || static::$_status === static::STATUS_RELOADING) {
|
||||
return;
|
||||
}
|
||||
static::$_gracefulStop = $signal === \SIGUSR2;
|
||||
static::$_pidsToRestart = static::getAllWorkerPids();
|
||||
static::reload();
|
||||
@@ -1304,7 +1307,7 @@ class Worker
|
||||
$STDOUT = \fopen(static::$stdoutFile, "a");
|
||||
$STDERR = \fopen(static::$stdoutFile, "a");
|
||||
// Fix standard output cannot redirect of PHP 8.1.8's bug
|
||||
if (\posix_isatty(2)) {
|
||||
if (\function_exists('posix_isatty') && \posix_isatty(2)) {
|
||||
\ob_start(function ($string) {
|
||||
\file_put_contents(static::$stdoutFile, $string, FILE_APPEND);
|
||||
}, 1);
|
||||
@@ -1447,11 +1450,39 @@ class Worker
|
||||
/** @var Worker $worker */
|
||||
$worker = current(static::$_workers);
|
||||
|
||||
\Workerman\Timer::delAll();
|
||||
|
||||
//Update process state.
|
||||
static::$_status = static::STATUS_RUNNING;
|
||||
|
||||
// Register shutdown function for checking errors.
|
||||
\register_shutdown_function([__CLASS__, 'checkErrors']);
|
||||
|
||||
// Create a global event loop.
|
||||
if (!static::$globalEvent) {
|
||||
$eventLoopClass = static::getEventLoopName();
|
||||
static::$globalEvent = new $eventLoopClass;
|
||||
}
|
||||
|
||||
// Reinstall signal.
|
||||
static::reinstallSignal();
|
||||
|
||||
// Init Timer.
|
||||
Timer::init(static::$globalEvent);
|
||||
|
||||
\restore_error_handler();
|
||||
|
||||
// Display UI.
|
||||
static::safeEcho(\str_pad($worker->name, 30) . \str_pad($worker->getSocketName(), 36) . \str_pad($worker->count, 10) . "[ok]\n");
|
||||
static::safeEcho(\str_pad($worker->name, 21) . \str_pad($worker->getSocketName(), 36) . \str_pad((string)$worker->count, 10) . "[ok]\n");
|
||||
$worker->listen();
|
||||
$worker->run();
|
||||
exit("@@@child exit@@@\r\n");
|
||||
static::$globalEvent->loop();
|
||||
if (static::$_status !== self::STATUS_SHUTDOWN) {
|
||||
$err = new Exception('event-loop exited');
|
||||
static::log($err);
|
||||
exit(250);
|
||||
}
|
||||
exit(0);
|
||||
}
|
||||
else
|
||||
{
|
||||
@@ -1557,9 +1588,7 @@ class Worker
|
||||
elseif (0 === $pid) {
|
||||
\srand();
|
||||
\mt_srand();
|
||||
if ($worker->reusePort) {
|
||||
$worker->listen();
|
||||
}
|
||||
static::$_gracefulStop = false;
|
||||
if (static::$_status === static::STATUS_STARTING) {
|
||||
static::resetStd();
|
||||
}
|
||||
@@ -1572,10 +1601,32 @@ class Worker
|
||||
}
|
||||
}
|
||||
Timer::delAll();
|
||||
//Update process state.
|
||||
static::$_status = static::STATUS_RUNNING;
|
||||
|
||||
// Register shutdown function for checking errors.
|
||||
\register_shutdown_function(array("\\Workerman\\Worker", 'checkErrors'));
|
||||
|
||||
// Create a global event loop.
|
||||
if (!static::$globalEvent) {
|
||||
$event_loop_class = static::getEventLoopName();
|
||||
static::$globalEvent = new $event_loop_class;
|
||||
}
|
||||
|
||||
// Reinstall signal.
|
||||
static::reinstallSignal();
|
||||
|
||||
// Init Timer.
|
||||
Timer::init(static::$globalEvent);
|
||||
|
||||
\restore_error_handler();
|
||||
|
||||
static::setProcessTitle(self::$processTitle . ': worker process ' . $worker->name . ' ' . $worker->getSocketName());
|
||||
$worker->setUserAndGroup();
|
||||
$worker->id = $id;
|
||||
$worker->run();
|
||||
// Main loop.
|
||||
static::$globalEvent->loop();
|
||||
if (strpos(static::$eventLoopClass, 'Workerman\Events\Swoole') !== false) {
|
||||
exit(0);
|
||||
}
|
||||
@@ -1610,7 +1661,7 @@ class Worker
|
||||
// Get uid.
|
||||
$user_info = \posix_getpwnam($this->user);
|
||||
if (!$user_info) {
|
||||
static::log("Warning: User {$this->user} not exsits");
|
||||
static::log("Warning: User {$this->user} not exists");
|
||||
return;
|
||||
}
|
||||
$uid = $user_info['uid'];
|
||||
@@ -1618,7 +1669,7 @@ class Worker
|
||||
if ($this->group) {
|
||||
$group_info = \posix_getgrnam($this->group);
|
||||
if (!$group_info) {
|
||||
static::log("Warning: Group {$this->group} not exsits");
|
||||
static::log("Warning: Group {$this->group} not exists");
|
||||
return;
|
||||
}
|
||||
$gid = $group_info['gid'];
|
||||
@@ -1689,6 +1740,10 @@ class Worker
|
||||
foreach (static::$_pidMap as $worker_id => $worker_pid_array) {
|
||||
if (isset($worker_pid_array[$pid])) {
|
||||
$worker = static::$_workers[$worker_id];
|
||||
// Fix exit with status 2 for php8.2
|
||||
if ($status === \SIGINT && static::$_status === static::STATUS_SHUTDOWN) {
|
||||
$status = 0;
|
||||
}
|
||||
// Exit status.
|
||||
if ($status !== 0) {
|
||||
static::log("worker[{$worker->name}:$pid] exit with status $status");
|
||||
@@ -1781,6 +1836,11 @@ class Worker
|
||||
{
|
||||
// For master process.
|
||||
if (static::$_masterPid === \posix_getpid()) {
|
||||
if (static::$_gracefulStop) {
|
||||
$sig = \SIGUSR2;
|
||||
} else {
|
||||
$sig = \SIGUSR1;
|
||||
}
|
||||
// Set reloading state.
|
||||
if (static::$_status !== static::STATUS_RELOADING && static::$_status !== static::STATUS_SHUTDOWN) {
|
||||
static::log("Workerman[" . \basename(static::$_startFile) . "] reloading");
|
||||
@@ -1796,32 +1856,27 @@ class Worker
|
||||
}
|
||||
static::initId();
|
||||
}
|
||||
}
|
||||
|
||||
if (static::$_gracefulStop) {
|
||||
$sig = \SIGUSR2;
|
||||
} else {
|
||||
$sig = \SIGUSR1;
|
||||
}
|
||||
|
||||
// Send reload signal to all child processes.
|
||||
$reloadable_pid_array = array();
|
||||
foreach (static::$_pidMap as $worker_id => $worker_pid_array) {
|
||||
$worker = static::$_workers[$worker_id];
|
||||
if ($worker->reloadable) {
|
||||
foreach ($worker_pid_array as $pid) {
|
||||
$reloadable_pid_array[$pid] = $pid;
|
||||
}
|
||||
} else {
|
||||
foreach ($worker_pid_array as $pid) {
|
||||
// Send reload signal to a worker process which reloadable is false.
|
||||
\posix_kill($pid, $sig);
|
||||
// Send reload signal to all child processes.
|
||||
$reloadable_pid_array = array();
|
||||
foreach (static::$_pidMap as $worker_id => $worker_pid_array) {
|
||||
$worker = static::$_workers[$worker_id];
|
||||
if ($worker->reloadable) {
|
||||
foreach ($worker_pid_array as $pid) {
|
||||
$reloadable_pid_array[$pid] = $pid;
|
||||
}
|
||||
} else {
|
||||
foreach ($worker_pid_array as $pid) {
|
||||
// Send reload signal to a worker process which reloadable is false.
|
||||
\posix_kill($pid, $sig);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get all pids that are waiting reload.
|
||||
static::$_pidsToRestart = \array_intersect(static::$_pidsToRestart, $reloadable_pid_array);
|
||||
// Get all pids that are waiting reload.
|
||||
static::$_pidsToRestart = \array_intersect(static::$_pidsToRestart, $reloadable_pid_array);
|
||||
|
||||
}
|
||||
|
||||
// Reload complete.
|
||||
if (empty(static::$_pidsToRestart)) {
|
||||
@@ -1883,7 +1938,11 @@ class Worker
|
||||
$sig = \SIGINT;
|
||||
}
|
||||
foreach ($worker_pid_array as $worker_pid) {
|
||||
\posix_kill($worker_pid, $sig);
|
||||
if (static::$daemonize) {
|
||||
\posix_kill($worker_pid, $sig);
|
||||
} else {
|
||||
Timer::add(1, '\posix_kill', array($worker_pid, $sig), false);
|
||||
}
|
||||
if(!static::$_gracefulStop){
|
||||
Timer::add(static::$stopTimeout, '\posix_kill', array($worker_pid, \SIGKILL), false);
|
||||
}
|
||||
@@ -1896,7 +1955,8 @@ class Worker
|
||||
} // For child processes.
|
||||
else {
|
||||
// Execute exit.
|
||||
foreach (static::$_workers as $worker) {
|
||||
$workers = array_reverse(static::$_workers);
|
||||
foreach ($workers as $worker) {
|
||||
if(!$worker->stopping){
|
||||
$worker->stop();
|
||||
$worker->stopping = true;
|
||||
@@ -2416,37 +2476,11 @@ class Worker
|
||||
* Run worker instance.
|
||||
*
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function run()
|
||||
{
|
||||
//Update process state.
|
||||
static::$_status = static::STATUS_RUNNING;
|
||||
|
||||
// Register shutdown function for checking errors.
|
||||
\register_shutdown_function(array("\\Workerman\\Worker", 'checkErrors'));
|
||||
|
||||
// Set autoload root path.
|
||||
Autoloader::setRootPath($this->_autoloadRootPath);
|
||||
|
||||
// Create a global event loop.
|
||||
if (!static::$globalEvent) {
|
||||
$event_loop_class = static::getEventLoopName();
|
||||
static::$globalEvent = new $event_loop_class;
|
||||
$this->resumeAccept();
|
||||
}
|
||||
|
||||
// Reinstall signal.
|
||||
static::reinstallSignal();
|
||||
|
||||
// Init Timer.
|
||||
Timer::init(static::$globalEvent);
|
||||
|
||||
// Set an empty onMessage callback.
|
||||
if (empty($this->onMessage)) {
|
||||
$this->onMessage = function () {};
|
||||
}
|
||||
|
||||
\restore_error_handler();
|
||||
$this->listen();
|
||||
|
||||
// Try to emit onWorkerStart callback.
|
||||
if ($this->onWorkerStart) {
|
||||
@@ -2462,9 +2496,6 @@ class Worker
|
||||
static::stopAll(250, $e);
|
||||
}
|
||||
}
|
||||
|
||||
// Main loop.
|
||||
static::$globalEvent->loop();
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -2492,6 +2523,13 @@ class Worker
|
||||
$connection->close();
|
||||
}
|
||||
}
|
||||
// Remove worker.
|
||||
foreach(static::$_workers as $key => $one_worker) {
|
||||
if ($one_worker->workerId === $this->workerId) {
|
||||
unset(static::$_workers[$key]);
|
||||
}
|
||||
}
|
||||
|
||||
// Clear callback.
|
||||
$this->onMessage = $this->onClose = $this->onError = $this->onBufferDrain = $this->onBufferFull = null;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user