This commit is contained in:
2021-06-25 11:20:38 +08:00
parent fbe83aff27
commit aa72ac2eb9
11 changed files with 261 additions and 430 deletions
+4 -11
View File
@@ -7,10 +7,6 @@ namespace Annotation\Route;
use Annotation\Attribute; use Annotation\Attribute;
use Exception; use Exception;
use HttpServer\Route\Router; use HttpServer\Route\Router;
use ReflectionException;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
/** /**
@@ -30,21 +26,18 @@ use Snowflake\Snowflake;
* @param string|null $uri * @param string|null $uri
* @param string $version * @param string $version
*/ */
public function __construct( public function __construct(public string $event, public ?string $uri = null, public string $version = 'v.1.0')
public string $event,
public ?string $uri = null,
public string $version = 'v.1.0'
)
{ {
} }
/** /**
* @param array $handler * @param mixed $class
* @param mixed|null $method
* @return Router * @return Router
* @throws Exception * @throws Exception
*/ */
public function execute(mixed $class, mixed $method = null): Router public function execute(mixed $class, mixed $method = null): Router
{ {
// TODO: Implement setHandler() method. // TODO: Implement setHandler() method.
$router = Snowflake::app()->getRouter(); $router = Snowflake::app()->getRouter();
+71 -108
View File
@@ -6,20 +6,10 @@ namespace HttpServer\Abstracts;
use Database\Connection; use Database\Connection;
use Exception; use Exception;
use HttpServer\Http\Request;
use PHPMailer\PHPMailer\PHPMailer;
use PHPMailer\PHPMailer\SMTP;
use ReflectionException;
use Snowflake\Abstracts\Config; use Snowflake\Abstracts\Config;
use Snowflake\Core\Json;
use Snowflake\Error\LoggerProcess;
use Snowflake\Event; use Snowflake\Event;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException; use Snowflake\Exception\ConfigException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake; use Snowflake\Snowflake;
use Swoole\Process;
use Swoole\Server;
/** /**
@@ -30,116 +20,89 @@ abstract class Callback extends HttpService
{ {
const EVENT_ERROR = 'WORKER:ERROR';
const EVENT_ERROR = 'WORKER:ERROR'; const EVENT_STOP = 'WORKER:STOP';
const EVENT_STOP = 'WORKER:STOP'; const EVENT_EXIT = 'WORKER:EXIT';
const EVENT_EXIT = 'WORKER:EXIT';
private array $_MESSAGE = [ private array $_MESSAGE = [
self::EVENT_ERROR => 'The server error. at No.', self::EVENT_ERROR => 'The server error. at No.',
self::EVENT_STOP => 'The server stop. at No.', self::EVENT_STOP => 'The server stop. at No.',
self::EVENT_EXIT => 'The server exit. at No.', self::EVENT_EXIT => 'The server exit. at No.',
]; ];
/**
* @return PHPMailer
* @throws \PHPMailer\PHPMailer\Exception
* @throws ConfigException
*/
private function createEmail(): PHPMailer
{
$mail = new PHPMailer(true);
$mail->SMTPDebug = SMTP::DEBUG_SERVER; // Enable verbose debug output
$mail->isSMTP(); // Send using SMTP
$mail->Host = Config::get('email.host'); // Set the SMTP server to send through
$mail->SMTPAuth = true; // Enable SMTP authentication
$mail->Debugoutput = false; // Enable SMTP authentication
$mail->CharSet = "UTF8"; // Enable SMTP authentication
$mail->Username = Config::get('email.username'); // SMTP username
$mail->Password = Config::get('email.password'); // SMTP password
$mail->SMTPSecure = PHPMailer::ENCRYPTION_SMTPS; // Enable TLS encryption; `PHPMailer::ENCRYPTION_SMTPS` encouraged
$mail->Port = Config::get('email.port'); // TCP port to connect to, use 465 for `PHPMailer::ENCRYPTION_SMTPS` above
$mail->setFrom(Config::get('email.send.address'), Config::get('email.send.nickname'));
return $mail;
}
/**
* @param $fd
* @param $data
* @param $reID
* @return Request
* @throws Exception
*/
protected function _request($fd, $data, $reID): Request
{
return Request::createListenRequest($fd, $data, $reID);
}
/** /**
* @param $messageContent * @param $messageContent
* @throws Exception * @throws Exception
*/ */
protected function system_mail($messageContent) protected function system_mail($messageContent)
{ {
try { try {
$email = Config::get('email'); $email = Config::get('email');
if (empty($email) || !$email['enable']) { if (empty($email) || !$email['enable']) {
return; return;
} }
$transport = (new \Swift_SmtpTransport($email['host'], $email['465'])) $transport = (new \Swift_SmtpTransport($email['host'], $email['465']))
->setUsername($email['username']) ->setUsername($email['username'])
->setPassword($email['password']); ->setPassword($email['password']);
$mailer = new \Swift_Mailer($transport); $mailer = new \Swift_Mailer($transport);
// Create a message // Create a message
$message = (new \Swift_Message('Wonderful Subject')) $message = (new \Swift_Message('Wonderful Subject'))
->setFrom([$email['send']['address'] => $email['send']['nickname']]) ->setFrom([$email['send']['address'] => $email['send']['nickname']])
->setBody('Here is the message itself'); ->setBody('Here is the message itself');
foreach ($email['receive'] as $item) { foreach ($email['receive'] as $item) {
$message->setTo([$item['address'], $item['address'] => $item['nickname']]); $message->setTo([$item['address'], $item['address'] => $item['nickname']]);
} }
$mailer->send($messageContent); $mailer->send($messageContent);
} catch (\Throwable $e) { } catch (\Throwable $e) {
$this->addError($e, 'email'); $this->addError($e, 'email');
} }
} }
/** /**
* @throws ConfigException * @throws ConfigException
* @throws Exception * @throws Exception
*/ */
protected function clearMysqlClient() protected function clearMysqlClient()
{ {
$databases = Config::get('databases', []); $databases = Config::get('databases', []);
if (empty($databases)) { if (empty($databases)) {
return; return;
} }
$application = Snowflake::app(); $application = Snowflake::app();
foreach ($databases as $name => $database) { foreach ($databases as $name => $database) {
/** @var Connection $connection */ /** @var Connection $connection */
$connection = $application->get('databases.' . $name, false); $connection = $application->get('databases.' . $name, false);
if (empty($connection)) { if (empty($connection)) {
continue; continue;
} }
$connection->disconnect(); $connection->disconnect();
} }
} }
/** /**
* @throws ConfigException * @param array $clientInfo
* @throws Exception * @param string $event
*/ * @return string
protected function clearRedisClient() */
{ protected function getName(array $clientInfo, string $event): string
$redis = Snowflake::app()->getRedis(); {
$redis->destroy(); return 'listen ' . $clientInfo['server_port'] . ' ' . Event::SERVER_CONNECT;
} }
/**
* @throws ConfigException
* @throws Exception
*/
protected function clearRedisClient()
{
$redis = Snowflake::app()->getRedis();
$redis->destroy();
}
} }
+15 -28
View File
@@ -7,7 +7,6 @@ namespace HttpServer\Events;
use Exception; use Exception;
use HttpServer\Abstracts\Callback; use HttpServer\Abstracts\Callback;
use Snowflake\Event; use Snowflake\Event;
use Snowflake\Snowflake;
use Swoole\Server; use Swoole\Server;
/** /**
@@ -19,33 +18,21 @@ class OnClose extends Callback
{ {
/** /**
* @param Server $server * @param Server $server
* @param int $fd * @param int $fd
* @throws Exception * @throws Exception
*/ */
public function onHandler(Server $server, int $fd) public function onHandler(Server $server, int $fd)
{ {
try { try {
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES)); defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
$clientInfo = $server->getClientInfo($fd); $clientInfo = $server->getClientInfo($fd);
if (!Event::exists(($name = $this->getName($clientInfo)))) {
return;
}
Event::trigger($name, [$server, $fd]);
} catch (\Throwable $exception) {
$this->addError($exception, 'throwable');
}
}
Event::trigger($this->getName($clientInfo, Event::SERVER_CLIENT_CLOSE), [$server, $fd]);
/** } catch (\Throwable $exception) {
* @param $server_port $this->addError($exception, 'throwable');
* @return string }
*/ }
private function getName($server_port): string
{
return 'listen ' . $server_port['server_port'] . ' ' . Event::SERVER_CLIENT_CLOSE;
}
} }
+1 -11
View File
@@ -33,21 +33,11 @@ class OnConnect extends Callback
if (isset($clientInfo['websocket_status'])) { if (isset($clientInfo['websocket_status'])) {
return; return;
} }
fire($this->getName($clientInfo), [$server, $fd, $reactorId]); fire($this->getName($clientInfo, Event::SERVER_CONNECT), [$server, $fd, $reactorId]);
} catch (\Throwable $throwable) { } catch (\Throwable $throwable) {
$this->addError($throwable, 'connect'); $this->addError($throwable, 'connect');
} }
} }
/**
* @param array $clientInfo
* @return string
*/
private function getName(array $clientInfo): string
{
return 'listen ' . $clientInfo['server_port'] . ' ' . Event::SERVER_CONNECT;
}
} }
+90 -113
View File
@@ -11,6 +11,7 @@ use HttpServer\Http\HttpHeaders;
use HttpServer\Http\HttpParams; use HttpServer\Http\HttpParams;
use HttpServer\Http\Request; use HttpServer\Http\Request;
use HttpServer\Http\Response; use HttpServer\Http\Response;
use HttpServer\Route\Router;
use ReflectionException; use ReflectionException;
use Snowflake\Core\ArrayAccess; use Snowflake\Core\ArrayAccess;
use Snowflake\Event; use Snowflake\Event;
@@ -29,130 +30,106 @@ class OnHandshake extends Callback
{ {
/** /**
* @param $request * @param $request
* @param $response * @param $response
* @throws Exception * @return Router
*/ * @throws Exception
private function resolveParse($request, $response) */
{ private function _protocol($request, $response): Router
/** @var Server $server */ {
$secWebSocketKey = $request->header['sec-websocket-key']; /** @var Server $server */
$patten = '#^[+/0-9A-Za-z]{21}[AQgw]==$#'; $secWebSocketKey = $request->header['sec-websocket-key'];
if (0 === preg_match($patten, $secWebSocketKey) || 16 !== strlen(base64_decode($secWebSocketKey))) { $patten = '#^[+/0-9A-Za-z]{21}[AQgw]==$#';
throw new Exception('protocol error.', 500); if (0 === preg_match($patten, $secWebSocketKey) || 16 !== strlen(base64_decode($secWebSocketKey))) {
} throw new Exception('protocol error.', 500);
$key = base64_encode(sha1($request->header['sec-websocket-key'] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', TRUE)); }
$headers = [ $key = base64_encode(sha1($request->header['sec-websocket-key'] . '258EAFA5-E914-47DA-95CA-C5AB0DC85B11', TRUE));
'Upgrade' => 'websocket', $headers = [
'Connection' => 'Upgrade', 'Upgrade' => 'websocket',
'Sec-websocket-Accept' => $key, 'Connection' => 'Upgrade',
'Sec-websocket-Version' => '13', 'Sec-websocket-Accept' => $key,
]; 'Sec-websocket-Version' => '13',
if (isset($request->header['sec-websocket-protocol'])) { ];
$headers['Sec-websocket-Protocol'] = $request->header['sec-websocket-protocol']; if (isset($request->header['sec-websocket-protocol'])) {
} $headers['Sec-websocket-Protocol'] = $request->header['sec-websocket-protocol'];
foreach ($headers as $key => $val) { }
$response->header($key, $val); foreach ($headers as $key => $val) {
} $response->header($key, $val);
} }
return Snowflake::app()->getRouter();
}
/** /**
* @param SResponse $response * @param SResponse $response
* @param int $code * @param int $code
* @return false * @return void
*/ */
private function disconnect(SResponse $response, $code = 500): bool private function disconnect(SResponse $response, int $code = 500): void
{ {
$server = Snowflake::getWebSocket(); $server = Snowflake::getWebSocket();
if (!$server->exist($response->fd)) { if (!$server->isEstablished($response->fd)) {
return false; return;
} }
$response->status($code); $response->status($code);
$response->end(); $response->end();
return false; }
}
/** /**
* @param $response * @param SRequest $request
* @param int $code * @param SResponse $response
* @return false * @return void
*/ * @throws Exception
private function connect($response, $code = 101): bool */
{ public function onHandler(SRequest $request, SResponse $response): void
$response->status($code); {
$response->end(); try {
return false; defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
}
$router = $this->_protocol($request, $response);
[$sRequest, $sResponse] = $this->sRequest($request, $response);
if (($node = $router->find_path($sRequest)) !== null) {
$node->dispatch($sRequest, $sResponse);
} else {
$this->disconnect($response, 404);
}
} catch (\Throwable $exception) {
$this->addError($exception, 'throwable');
$response->status(500);
$response->end($exception->getMessage());
}
}
/** /**
* @param SRequest $request * @param $request
* @param SResponse $response * @param SResponse $response
* @return void * @return array
* @throws Exception * @throws NotFindClassException
*/ * @throws ReflectionException
public function onHandler(SRequest $request, SResponse $response): void * @throws Exception
{ */
try { private function sRequest($request, SResponse $response): array
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES)); {
/** @var Request $sRequest */
$sRequest = Request::create($request);
$sRequest->uri = '/' . Socket::HANDSHAKE . '::event';
$this->execute($request, $response); $sRequest->headers = new HttpHeaders(ArrayAccess::merge($request->server, $request->header));
} catch (\Throwable $exception) {
$this->addError($exception, 'throwable');
$response->status(500);
$response->end($exception->getMessage());
}
}
$sRequest->headers->replace('request_method', 'sw::socket');
$sRequest->headers->replace('request_uri', $sRequest->uri);
/** $sRequest->params = new HttpParams([], $request->get, []);
* @param SRequest $request
* @param SResponse $response
* @return mixed
* @throws Exception
*/
private function execute(SRequest $request, SResponse $response): mixed
{
$this->resolveParse($request, $response);
$router = Snowflake::app()->getRouter(); $sRequest->parseUri();
[$sRequest, $sResponse] = $this->sRequest($request, $response); return [$sRequest, Response::create($response)];
}
if (($node = $router->find_path($sRequest)) !== null) {
return $node->dispatch($sRequest, $sResponse);
}
return $this->disconnect($response, 404);
}
/**
* @param $request
* @param SResponse $response
* @return array
* @throws NotFindClassException
* @throws ReflectionException
*/
private function sRequest($request, SResponse $response): array
{
/** @var Request $sRequest */
$sRequest = Request::create($request);
$sRequest->uri = '/' . Socket::HANDSHAKE . '::event';
$sRequest->headers = new HttpHeaders(ArrayAccess::merge($request->server, $request->header));
$sRequest->headers->replace('request_method', 'sw::socket');
$sRequest->headers->replace('request_uri', $sRequest->uri);
$sRequest->params = new HttpParams([], $request->get, []);
$sRequest->parseUri();
return [$sRequest, Response::create($response)];
}
} }
+1 -1
View File
@@ -18,7 +18,7 @@ class OnManagerStop extends Callback
{ {
/** /**
* @param $server * @param Server $server
* @throws Exception * @throws Exception
*/ */
public function onHandler(Server $server) public function onHandler(Server $server)
+22 -45
View File
@@ -4,22 +4,8 @@ declare(strict_types=1);
namespace HttpServer\Events; namespace HttpServer\Events;
use Annotation\Route\Socket;
use Exception;
use HttpServer\Abstracts\Callback; use HttpServer\Abstracts\Callback;
use HttpServer\Http\Context;
use HttpServer\Http\HttpHeaders;
use HttpServer\Http\HttpParams;
use HttpServer\Http\Request;
use ReflectionException;
use Snowflake\Abstracts\Config;
use Snowflake\Core\Json;
use Snowflake\Event; use Snowflake\Event;
use Snowflake\Exception\ComponentException;
use Snowflake\Exception\ConfigException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
use Swoole\Coroutine;
use Swoole\WebSocket\Frame; use Swoole\WebSocket\Frame;
use Swoole\WebSocket\Server; use Swoole\WebSocket\Server;
@@ -30,38 +16,29 @@ use Swoole\WebSocket\Server;
class OnMessage extends Callback class OnMessage extends Callback
{ {
/** /**
* @param Server $server * @param Server $server
* @param Frame $frame * @param Frame $frame
* @throws * @throws
*/ */
public function onHandler(Server $server, Frame $frame) public function onHandler(Server $server, Frame $frame)
{ {
try { try {
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES)); defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
if ($frame->opcode === 0x08) { if ($frame->opcode === 0x08) {
return; return;
} }
Event::trigger($this->getName($server, $frame), [$frame, $server]);
} catch (\Throwable $exception) {
$this->addError($exception, 'websocket');
if (!swoole()->isEstablished($frame->fd)) {
return;
}
$server->send($frame->fd, $exception->getMessage());
}
}
$clientInfo = $this->getName($server->getClientInfo($frame->fd), Event::SERVER_MESSAGE);
/** Event::trigger($clientInfo, [$frame, $server]);
* @param $clientInfo } catch (\Throwable $exception) {
* @return string $this->addError($exception, 'websocket');
*/ if (!swoole()->isEstablished($frame->fd)) {
private function getName(Server $server, Frame $frame): string return;
{ }
$clientInfo = $server->getClientInfo($frame->fd); $server->send($frame->fd, $exception->getMessage());
}
return 'listen ' . $clientInfo['server_port'] . ' ' . Event::SERVER_MESSAGE; }
}
} }
+14 -30
View File
@@ -8,7 +8,6 @@ use Exception;
use HttpServer\Abstracts\Callback; use HttpServer\Abstracts\Callback;
use Snowflake\Core\Json; use Snowflake\Core\Json;
use Snowflake\Event; use Snowflake\Event;
use Snowflake\Snowflake;
use Swoole\Server; use Swoole\Server;
/** /**
@@ -18,45 +17,30 @@ use Swoole\Server;
class OnPacket extends Callback class OnPacket extends Callback
{ {
public int $port = 0;
public string $host = '';
/** /**
* @param Server $server * @param Server $server
* @param $data * @param string $data
* @param $clientInfo * @param array $client
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
public function onHandler(Server $server, string $data, array $clientInfo): mixed public function onHandler(Server $server, string $data, array $client): mixed
{ {
[$host, $port] = [$clientInfo['address'], $clientInfo['port']];
try { try {
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES)); defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
$request = $this->_request($clientInfo, $server, $data); $client['server_port'] = $client['port'];
$name = $this->getName($client, Event::SERVER_RECEIVE);
$router = Snowflake::app()->getRouter(); $result = Event::trigger($name, [$server, $data, $client]);
if (($node = $router->find_path($request)) === null) {
return $server->sendto($host, $port, Json::encode(['state' => 404]));
}
$dispatch = $node->dispatch();
if (!is_string($dispatch)) $dispatch = Json::encode($dispatch);
if (empty($dispatch)) {
$dispatch = Json::encode(['state' => 0, 'message' => 'ok']);
}
return $server->sendto($host, $port, $dispatch);
} catch (\Throwable $exception) { } catch (\Throwable $exception) {
$this->addError($exception, 'packet'); $result = logger()->exception($exception);
} finally {
$response = Json::encode(['state' => 500, 'message' => $exception->getMessage()]); if (is_array($result) || is_object($result)) {
$result = Json::encode($result);
return $server->sendto($host, $port, $response); }
$sendData = [$client['address'], $client['port'], $result];
return $server->sendto(...$sendData);
} }
} }
+29 -52
View File
@@ -7,12 +7,9 @@ namespace HttpServer\Events;
use Exception; use Exception;
use HttpServer\Abstracts\Callback; use HttpServer\Abstracts\Callback;
use HttpServer\Http\Request; use HttpServer\Http\Request;
use HttpServer\Route\Router; use Snowflake\Abstracts\Config;
use ReflectionException;
use Snowflake\Core\Json; use Snowflake\Core\Json;
use Snowflake\Event; use Snowflake\Event;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
use Swoole\Server; use Swoole\Server;
/** /**
@@ -22,56 +19,36 @@ use Swoole\Server;
class OnReceive extends Callback class OnReceive extends Callback
{ {
public int $port = 0; /**
* @param Server $server
* @param int $fd
* @param int $reID
* @param string $data
* @return mixed
* @throws Exception
*/
public function onHandler(Server $server, int $fd, int $reID, string $data): mixed
{
try {
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
$client = $server->getClientInfo($fd, $reID);
$name = $this->getName($client, Event::SERVER_RECEIVE);
public string $host = ''; if (Config::get('rpc.port', 0) == $client['server_port']) {
$result = router()->find_path(Request::rpcRequest($fd, $data, $reID))?->dispatch();
} else {
private Router $router; $result = Event::trigger($name, [$server, $data, $client]);
}
if (is_array($result) || is_object($result)) {
public function init() $result = Json::encode($result);
{ }
$this->router = Snowflake::app()->getRouter(); } catch (\Throwable $exception) {
} $result = logger()->exception($exception);
} finally {
return $server->send($fd, $result);
/** }
* @param Server $server }
* @param int $fd
* @param int $reID
* @param string $data
* @return mixed
* @throws Exception
*/
public function onHandler(Server $server, int $fd, int $reID, string $data): mixed
{
try {
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
$request = $this->_request($fd, $data, $reID);
if (($node = $this->router->find_path($request)) === null) {
return $server->send($fd, Json::encode(['state' => 404]));
}
$dispatch = $node->dispatch();
if (!is_string($dispatch)) $dispatch = Json::encode($dispatch);
if (empty($dispatch)) {
$dispatch = Json::encode(['state' => 0, 'message' => 'ok']);
}
if ($server->exist($fd)) {
return $server->send($fd, $dispatch);
}
return $dispatch;
} catch (\Throwable $exception) {
$this->addError($exception, 'receive');
$error = ['state' => 500, 'message' => $exception->getMessage()];
if ($server->exist($fd)) {
return $server->send($fd, Json::encode($error));
}
return Json::encode($error);
}
}
} }
-2
View File
@@ -6,9 +6,7 @@ namespace HttpServer\Events;
use Exception; use Exception;
use HttpServer\Abstracts\Callback; use HttpServer\Abstracts\Callback;
use Snowflake\Error\Logger;
use Snowflake\Event; use Snowflake\Event;
use Snowflake\Snowflake;
use Swoole\Timer; use Swoole\Timer;
/** /**
+14 -29
View File
@@ -415,10 +415,10 @@ class Request extends HttpService
{ {
$mainstay = sprintf("%.6f", microtime(true)); // 带毫秒的时间戳 $mainstay = sprintf("%.6f", microtime(true)); // 带毫秒的时间戳
$timestamp = floor($mainstay); // 时间戳 $timestamp = floatval($mainstay); // 时间戳
$milliseconds = round(($mainstay - $timestamp) * 1000); // 毫秒 $milliseconds = round(($mainstay - $timestamp) * 1000); // 毫秒
$datetime = date("Y-m-d H:i:s", $timestamp) . '.' . $milliseconds; $datetime = date("Y-m-d H:i:s", (int)$timestamp) . '.' . $milliseconds;
$tmp = [ $tmp = [
'[Debug ' . $datetime . '] ', '[Debug ' . $datetime . '] ',
@@ -427,7 +427,6 @@ class Request extends HttpService
'`' . $this->headers->getHeader('user-agent') . '`', '`' . $this->headers->getHeader('user-agent') . '`',
$this->getRuntime() $this->getRuntime()
]; ];
return implode(' ', $tmp); return implode(' ', $tmp);
} }
@@ -500,10 +499,11 @@ class Request extends HttpService
* @param $fd * @param $fd
* @param $data * @param $data
* @param int $reID * @param int $reID
* @return Request * @return mixed|null
* @throws ConfigException
* @throws Exception * @throws Exception
*/ */
public static function createListenRequest($fd, $data, $reID = 0): Request public static function rpcRequest($fd, $data, int $reID = 0): Request|null
{ {
$sRequest = new Request(); $sRequest = new Request();
@@ -514,28 +514,11 @@ class Request extends HttpService
$sRequest->params = new HttpParams($data, [], []); $sRequest->params = new HttpParams($data, [], []);
$port = $sRequest->clientInfo['server_port']; $port = $sRequest->clientInfo['server_port'];
$sRequest->headers->setRequestUri('add-port-listen/port_' . $port);
$sRequest->headers->setRequestMethod(self::HTTP_LISTEN);
$sRequest->checkIsRpcClient()->parseUri();
return Context::setContext('request', $sRequest);
}
/**
* @throws ConfigException
* @throws Exception
*/
private function checkIsRpcClient(): static
{
$port = $this->clientInfo['server_port'];
if (($rpc = Config::get('rpc.port', 0)) !== $port) { if (($rpc = Config::get('rpc.port', 0)) !== $port) {
return $this; return null;
} }
[$cmd, $repeat, $body] = explode("\n", $this->params->getBody()); [$cmd, $repeat, $body] = explode("\n", $sRequest->params->getBody());
if (is_null($body) || is_null($cmd) || !empty($repeat)) { if (is_null($body) || is_null($cmd) || !empty($repeat)) {
throw new Exception('Protocol format error.'); throw new Exception('Protocol format error.');
} }
@@ -543,10 +526,12 @@ class Request extends HttpService
if (is_string($body) && is_null($data = Json::decode($body))) { if (is_string($body) && is_null($data = Json::decode($body))) {
throw new Exception('Protocol format error.'); throw new Exception('Protocol format error.');
} }
$this->headers->setRequestUri('rpc/p' . $rpc . '/' . ltrim($cmd, '/'));
$this->headers->setRequestMethod(Request::HTTP_CMD);
return $this; $sRequest->params->setPosts($data);
$sRequest->headers->setRequestUri('rpc/p' . $rpc . '/' . ltrim($cmd, '/'));
$sRequest->headers->setRequestMethod(Request::HTTP_CMD);
return Context::setContext('request', $sRequest);
} }
@@ -556,7 +541,7 @@ class Request extends HttpService
* @return mixed * @return mixed
* @throws Exception * @throws Exception
*/ */
private static function getClientInfo($fd, $re = 0): mixed private static function getClientInfo($fd, int $re = 0): mixed
{ {
$server = Snowflake::app()->getSwoole(); $server = Snowflake::app()->getSwoole();
if (!is_array($fd)) { if (!is_array($fd)) {