改名
This commit is contained in:
@@ -1,108 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Abstracts;
|
||||
|
||||
|
||||
use Database\Connection;
|
||||
use Exception;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Event;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Kiri;
|
||||
|
||||
|
||||
/**
|
||||
* Class Callback
|
||||
* @package HttpServer\Abstracts
|
||||
*/
|
||||
abstract class Callback extends HttpService
|
||||
{
|
||||
|
||||
|
||||
const EVENT_ERROR = 'WORKER:ERROR';
|
||||
const EVENT_STOP = 'WORKER:STOP';
|
||||
const EVENT_EXIT = 'WORKER:EXIT';
|
||||
|
||||
|
||||
private array $_MESSAGE = [
|
||||
self::EVENT_ERROR => 'The server error. at No.',
|
||||
self::EVENT_STOP => 'The server stop. at No.',
|
||||
self::EVENT_EXIT => 'The server exit. at No.',
|
||||
];
|
||||
|
||||
/**
|
||||
* @param $messageContent
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function system_mail($messageContent)
|
||||
{
|
||||
try {
|
||||
$email = Config::get('email');
|
||||
if (empty($email) || !$email['enable']) {
|
||||
return;
|
||||
}
|
||||
$transport = (new \Swift_SmtpTransport($email['host'], $email['465']))
|
||||
->setUsername($email['username'])
|
||||
->setPassword($email['password']);
|
||||
$mailer = new \Swift_Mailer($transport);
|
||||
|
||||
// Create a message
|
||||
$message = (new \Swift_Message('Wonderful Subject'))
|
||||
->setFrom([$email['send']['address'] => $email['send']['nickname']])
|
||||
->setBody('Here is the message itself');
|
||||
|
||||
foreach ($email['receive'] as $item) {
|
||||
$message->setTo([$item['address'], $item['address'] => $item['nickname']]);
|
||||
}
|
||||
$mailer->send($messageContent);
|
||||
} catch (\Throwable $e) {
|
||||
$this->addError($e, 'email');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function clearMysqlClient()
|
||||
{
|
||||
$databases = Config::get('databases', []);
|
||||
if (empty($databases)) {
|
||||
return;
|
||||
}
|
||||
$application = Kiri::app();
|
||||
foreach ($databases as $name => $database) {
|
||||
/** @var Connection $connection */
|
||||
$connection = $application->get('databases.' . $name, false);
|
||||
if (empty($connection)) {
|
||||
continue;
|
||||
}
|
||||
$connection->disconnect();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $clientInfo
|
||||
* @param string $event
|
||||
* @return string
|
||||
*/
|
||||
protected function getName(array $clientInfo, string $event): string
|
||||
{
|
||||
return 'listen ' . $clientInfo['server_port'] . ' ' . Event::SERVER_CONNECT;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function clearRedisClient()
|
||||
{
|
||||
$redis = Kiri::app()->getRedis();
|
||||
$redis->destroy();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
<?php
|
||||
/**
|
||||
* Created by PhpStorm.
|
||||
* User: whwyy
|
||||
* Date: 2018/11/8 0008
|
||||
* Time: 18:37
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
namespace HttpServer\Abstracts;
|
||||
|
||||
|
||||
use Swoole\WebSocket\Server;
|
||||
|
||||
/**
|
||||
* Class OnServerDefault
|
||||
* @package Kiri\Kiri\Server
|
||||
*/
|
||||
abstract class ServerBase extends HttpService
|
||||
{
|
||||
|
||||
/** @var Server */
|
||||
protected Server $server;
|
||||
|
||||
/**
|
||||
* @return Server
|
||||
*/
|
||||
public function getServer(): Server
|
||||
{
|
||||
return $this->server;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $server
|
||||
*/
|
||||
public function setServer($server)
|
||||
{
|
||||
$this->server = $server;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use Kiri\Event;
|
||||
use Kiri\Exception\ComponentException;
|
||||
use Kiri\Kiri;
|
||||
use Swoole\Server;
|
||||
|
||||
/**
|
||||
* Class OnAfterReload
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnAfterReload extends Callback
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler(Server $server)
|
||||
{
|
||||
Event::trigger(Event::SERVER_AFTER_RELOAD, [$server]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use Kiri\Event;
|
||||
use Kiri\Kiri;
|
||||
use Swoole\Server;
|
||||
use Swoole\Timer;
|
||||
|
||||
/**
|
||||
* Class OnBeforeReload
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnBeforeReload extends Callback
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler(Server $server)
|
||||
{
|
||||
Event::trigger(Event::SERVER_BEFORE_RELOAD, [$server]);
|
||||
|
||||
Kiri::clearWorkerPid();
|
||||
Kiri::clearTaskPid();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use Kiri\Event;
|
||||
use Swoole\Server;
|
||||
|
||||
/**
|
||||
* Class OnClose
|
||||
* @package HttpServer\Events
|
||||
*
|
||||
*/
|
||||
class OnClose extends Callback
|
||||
{
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler(Server $server, int $fd)
|
||||
{
|
||||
try {
|
||||
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
|
||||
|
||||
Event::trigger(Event::SERVER_ON_CLOSE, [$server, $fd]);
|
||||
} catch (\Throwable $exception) {
|
||||
$this->addError($exception, 'throwable');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,43 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use Kiri\Event;
|
||||
use Swoole\Server;
|
||||
|
||||
/**
|
||||
* Class OnConnect
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnConnect extends Callback
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $fd
|
||||
* @param int $reactorId
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler(Server $server, int $fd, int $reactorId)
|
||||
{
|
||||
try {
|
||||
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
|
||||
if (($clientInfo = $server->getClientInfo($fd, $reactorId)) === false) {
|
||||
return;
|
||||
}
|
||||
if (isset($clientInfo['websocket_status'])) {
|
||||
return;
|
||||
}
|
||||
fire($this->getName($clientInfo, Event::SERVER_CONNECT), [$server, $fd, $reactorId]);
|
||||
} catch (\Throwable $throwable) {
|
||||
$this->addError($throwable, 'connect');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,38 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use Kiri\Event;
|
||||
use Kiri\Kiri;
|
||||
use Swoole\Server;
|
||||
|
||||
/**
|
||||
* Class OnFinish
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnFinish extends Callback
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param $task_id
|
||||
* @param $data
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler(Server $server, $task_id, $data)
|
||||
{
|
||||
try {
|
||||
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
|
||||
|
||||
fire(Event::TASK_FINISH, [$task_id, $data]);
|
||||
} catch (\Throwable $exception) {
|
||||
$this->addError($exception, 'task');
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,135 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Annotation\Route\Socket;
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use HttpServer\Http\HttpHeaders;
|
||||
use HttpServer\Http\HttpParams;
|
||||
use HttpServer\Http\Request;
|
||||
use HttpServer\Http\Response;
|
||||
use HttpServer\Route\Router;
|
||||
use ReflectionException;
|
||||
use Kiri\Core\ArrayAccess;
|
||||
use Kiri\Event;
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
use Kiri\Kiri;
|
||||
use Swoole\Http\Request as SRequest;
|
||||
use Swoole\Http\Response as SResponse;
|
||||
use Swoole\WebSocket\Server;
|
||||
|
||||
|
||||
/**
|
||||
* Class OnHandshake
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnHandshake extends Callback
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param $request
|
||||
* @param $response
|
||||
* @return Router
|
||||
* @throws Exception
|
||||
*/
|
||||
private function _protocol($request, $response): Router
|
||||
{
|
||||
/** @var Server $server */
|
||||
$secWebSocketKey = $request->header['sec-websocket-key'];
|
||||
$patten = '#^[+/0-9A-Za-z]{21}[AQgw]==$#';
|
||||
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 = [
|
||||
'Upgrade' => 'websocket',
|
||||
'Connection' => 'Upgrade',
|
||||
'Sec-websocket-Accept' => $key,
|
||||
'Sec-websocket-Version' => '13',
|
||||
];
|
||||
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);
|
||||
}
|
||||
return Kiri::app()->getRouter();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param SResponse $response
|
||||
* @param int $code
|
||||
* @return void
|
||||
*/
|
||||
private function disconnect(SResponse $response, int $code = 500): void
|
||||
{
|
||||
$server = Kiri::getWebSocket();
|
||||
if (!$server->isEstablished($response->fd)) {
|
||||
return;
|
||||
}
|
||||
$response->status($code);
|
||||
$response->end();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param SRequest $request
|
||||
* @param SResponse $response
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler(SRequest $request, SResponse $response): void
|
||||
{
|
||||
try {
|
||||
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 $request
|
||||
* @param SResponse $response
|
||||
* @return array
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
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,37 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Event;
|
||||
use Kiri\Kiri;
|
||||
use Swoole\Server;
|
||||
|
||||
|
||||
/**
|
||||
* Class OnManagerStart
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnManagerStart extends Callback
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler(Server $server)
|
||||
{
|
||||
Kiri::setWorkerId($server->manager_pid);
|
||||
|
||||
fire(Event::SERVER_MANAGER_START, [$server]);
|
||||
|
||||
name($server->manager_pid, 'manager');
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,31 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use Kiri\Event;
|
||||
use Kiri\Kiri;
|
||||
use Swoole\Server;
|
||||
|
||||
/**
|
||||
* Class OnManagerStop
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnManagerStop extends Callback
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler(Server $server)
|
||||
{
|
||||
$this->warning('manager stop.');
|
||||
|
||||
fire(Event::SERVER_MANAGER_STOP, [$server]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,44 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use Kiri\Event;
|
||||
use Swoole\WebSocket\Frame;
|
||||
use Swoole\WebSocket\Server;
|
||||
|
||||
/**
|
||||
* Class OnMessage
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnMessage extends Callback
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param Frame $frame
|
||||
* @throws
|
||||
*/
|
||||
public function onHandler(Server $server, Frame $frame)
|
||||
{
|
||||
try {
|
||||
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
|
||||
if ($frame->opcode === 0x08) {
|
||||
return;
|
||||
}
|
||||
|
||||
$clientInfo = $this->getName($server->getClientInfo($frame->fd), Event::SERVER_MESSAGE);
|
||||
|
||||
Event::trigger($clientInfo, [$frame, $server]);
|
||||
} catch (\Throwable $exception) {
|
||||
$this->addError($exception, 'websocket');
|
||||
if (!swoole()->isEstablished($frame->fd)) {
|
||||
return;
|
||||
}
|
||||
$server->send($frame->fd, $exception->getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,48 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use Kiri\Core\Json;
|
||||
use Kiri\Event;
|
||||
use Swoole\Server;
|
||||
|
||||
/**
|
||||
* Class OnPacket
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnPacket extends Callback
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param string $data
|
||||
* @param array $client
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler(Server $server, string $data, array $client): mixed
|
||||
{
|
||||
try {
|
||||
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
|
||||
|
||||
$client['server_port'] = $client['port'];
|
||||
$name = $this->getName($client, Event::SERVER_RECEIVE);
|
||||
|
||||
$result = Event::trigger($name, [$server, $data, $client]);
|
||||
} catch (\Throwable $exception) {
|
||||
$result = logger()->exception($exception);
|
||||
} finally {
|
||||
if (is_array($result) || is_object($result)) {
|
||||
$result = Json::encode($result);
|
||||
}
|
||||
$sendData = [$client['address'], $client['port'], $result];
|
||||
return $server->sendto(...$sendData);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,85 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use Kafka\Struct;
|
||||
use Kiri\Crontab\Crontab;
|
||||
use Kiri\Crontab\Producer;
|
||||
use Kiri\Event;
|
||||
use Kiri\Kiri;
|
||||
use Swoole\Server;
|
||||
|
||||
/**
|
||||
* Class OnPipeMessage
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnPipeMessage extends Callback
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $src_worker_id
|
||||
* @param $swollen_universalize
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler(Server $server, int $src_worker_id, $swollen_universalize)
|
||||
{
|
||||
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
|
||||
|
||||
match ($swollen_universalize['action'] ?? null) {
|
||||
'kafka' => $this->onKafkaWorker($swollen_universalize),
|
||||
'crontab' => $this->onCrontabWorker($swollen_universalize),
|
||||
default => $this->onMessageWorker($server, $src_worker_id, $swollen_universalize)
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $message
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
private function onCrontabWorker(array $message): string
|
||||
{
|
||||
if (empty($message['handler'] ?? null)) {
|
||||
throw new Exception('unknown handler');
|
||||
}
|
||||
$message['handler']->increment()->execute();
|
||||
return 'success';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $server
|
||||
* @param $src_worker_id
|
||||
* @param $message
|
||||
* @return string
|
||||
* @throws Exception
|
||||
*/
|
||||
private function onMessageWorker($server, $src_worker_id, $message): string
|
||||
{
|
||||
fire(Event::PIPE_MESSAGE, [$server, $src_worker_id, $message]);
|
||||
|
||||
return 'success';
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $message
|
||||
* @return string
|
||||
*/
|
||||
private function onKafkaWorker(array $message): string
|
||||
{
|
||||
[$topic, $rdMessage] = $message['body'];
|
||||
|
||||
call_user_func($message['handler'], new Struct($topic, $rdMessage));
|
||||
|
||||
return 'success';
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,50 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use HttpServer\Http\Request;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Core\Json;
|
||||
use Kiri\Event;
|
||||
use Swoole\Server;
|
||||
|
||||
/**
|
||||
* Class OnReceive
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnReceive extends Callback
|
||||
{
|
||||
|
||||
/**
|
||||
* @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);
|
||||
|
||||
$result = Event::trigger($name, [$server, $data, $client]);
|
||||
if (is_array($result) || is_object($result)) {
|
||||
$result = Json::encode($result);
|
||||
}
|
||||
} catch (\Throwable $exception) {
|
||||
$result = logger()->exception($exception);
|
||||
} finally {
|
||||
return $server->send($fd, $result);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,112 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use HttpServer\Exception\ExitException;
|
||||
use HttpServer\Http\Request as HRequest;
|
||||
use HttpServer\Http\Response as HResponse;
|
||||
use HttpServer\Route\Router;
|
||||
use Kiri\Error\Logger;
|
||||
use Kiri\Event;
|
||||
use Kiri\Kiri;
|
||||
use Swoole\Error;
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Http\Response;
|
||||
use Throwable;
|
||||
|
||||
/**
|
||||
* Class OnRequest
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnRequest extends Callback
|
||||
{
|
||||
|
||||
|
||||
public Event $event;
|
||||
public Logger $logger;
|
||||
|
||||
|
||||
public Router $router;
|
||||
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->router = Kiri::app()->getRouter();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
* @return void
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler(Request $request, Response $response): void
|
||||
{
|
||||
try {
|
||||
// defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
|
||||
|
||||
/** @var HResponse $response */
|
||||
[$request, $response] = OnRequest::createContext($request, $response);
|
||||
|
||||
if ($request->is('favicon.ico')) {
|
||||
$response->close(404);
|
||||
} else {
|
||||
$this->router->dispatch();
|
||||
}
|
||||
} catch (ExitException | Error | Throwable $exception) {
|
||||
$this->addError($exception, 'throwable');
|
||||
$this->sendErrorMessage($request, $response, $exception);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $request
|
||||
* @param $response
|
||||
* @return array
|
||||
* @throws Exception
|
||||
*/
|
||||
public static function createContext($request, $response): array
|
||||
{
|
||||
return [HRequest::create($request), HResponse::create($response)];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $sRequest
|
||||
* @param $sResponse
|
||||
* @param Throwable $exception
|
||||
* @return bool|string
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function sendErrorMessage($sRequest, $sResponse, Throwable $exception): bool|string
|
||||
{
|
||||
$this->addError($exception, 'throwable');
|
||||
if ($sResponse instanceof Response) {
|
||||
[$sRequest, $sResponse] = [HRequest::create($sRequest), HResponse::create($sResponse)];
|
||||
}
|
||||
|
||||
$headers = $sRequest->headers->get('access-control-request-headers');
|
||||
$methods = $sRequest->headers->get('access-control-request-method');
|
||||
|
||||
/** @var HResponse $sResponse */
|
||||
$sResponse->addHeader('Access-Control-Allow-Origin', '*');
|
||||
$sResponse->addHeader('Access-Control-Allow-Headers', $headers);
|
||||
$sResponse->addHeader('Access-Control-Request-Method', $methods);
|
||||
|
||||
if (!($exception instanceof ExitException)) {
|
||||
return $sResponse->send(\logger()->exception($exception), 200);
|
||||
} else {
|
||||
return $sResponse->send($exception->getMessage(), 200);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use Kiri\Event;
|
||||
use Kiri\Exception\ComponentException;
|
||||
use Kiri\Kiri;
|
||||
use Swoole\Server;
|
||||
|
||||
/**
|
||||
* Class OnShutdown
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnShutdown extends Callback
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler(Server $server)
|
||||
{
|
||||
$this->debug('server shutdown~');
|
||||
|
||||
$this->system_mail('server shutdown~');
|
||||
|
||||
fire(Event::SERVER_SHUTDOWN);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,33 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Event;
|
||||
use Kiri\Kiri;
|
||||
use Swoole\Server;
|
||||
|
||||
/**
|
||||
* Class OnStart
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnStart extends Callback
|
||||
{
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler(Server $server)
|
||||
{
|
||||
if (Kiri::getPlatform()->isLinux()) {
|
||||
name($server->master_pid, 'master');
|
||||
}
|
||||
fire(Event::SERVER_EVENT_START, [$server]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,154 +0,0 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use HttpServer\IInterface\Task;
|
||||
use HttpServer\IInterface\Task as ITask;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Event;
|
||||
use Kiri\Kiri;
|
||||
use Swoole\Coroutine;
|
||||
use Swoole\Process;
|
||||
use Swoole\Server;
|
||||
use Swoole\Timer;
|
||||
use Exception;
|
||||
|
||||
/**
|
||||
* Class OnTask
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnTask extends Callback
|
||||
{
|
||||
|
||||
/**
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler()
|
||||
{
|
||||
$setting = Kiri::app()->getSwoole();
|
||||
|
||||
$isCoroutineTask = $setting->setting['task_enable_coroutine'] ?? false;
|
||||
if ($isCoroutineTask === true) {
|
||||
call_user_func([$this, 'onContinueTask'], ...func_get_args());
|
||||
} else {
|
||||
call_user_func([$this, 'onTask'], ...func_get_args());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $task_id
|
||||
* @param int $from_id
|
||||
* @param string $data
|
||||
*
|
||||
* @return mixed
|
||||
* @throws Exception 异步任务
|
||||
*/
|
||||
public function onTask(Server $server, int $task_id, int $from_id, string $data): mixed
|
||||
{
|
||||
if (empty($data)) {
|
||||
return $server->finish('null data');
|
||||
}
|
||||
|
||||
$time = microtime(TRUE);
|
||||
$finish = $this->runTaskHandler($data);
|
||||
if (!$finish) {
|
||||
$finish = [];
|
||||
}
|
||||
$finish['runTime'] = [
|
||||
'startTime' => $time,
|
||||
'runTime' => microtime(TRUE) - $time,
|
||||
'endTime' => microtime(TRUE),
|
||||
];
|
||||
return $server->finish(json_encode($finish));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param Server\Task $task
|
||||
* @return mixed
|
||||
* @throws Exception 异步任务
|
||||
*/
|
||||
public function onContinueTask(Server $server, Server\Task $task): mixed
|
||||
{
|
||||
if (empty($task->data)) {
|
||||
return $task->finish('null data');
|
||||
}
|
||||
|
||||
$time = microtime(TRUE);
|
||||
$finish = $this->runTaskHandler($task->data);
|
||||
if (!$finish) {
|
||||
$finish = [];
|
||||
}
|
||||
$finish['runTime'] = [
|
||||
'startTime' => $time,
|
||||
'runTime' => microtime(TRUE) - $time,
|
||||
'endTime' => microtime(TRUE),
|
||||
];
|
||||
return $task->finish(json_encode($finish));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return array|null
|
||||
* @throws Exception
|
||||
*/
|
||||
private function runTaskHandler($data): ?array
|
||||
{
|
||||
try {
|
||||
defer(fn() => fire(Event::SYSTEM_RESOURCE_CLEAN));
|
||||
|
||||
$serialize = $this->before($data);
|
||||
if ($serialize === null) {
|
||||
throw new Exception('unpack error.');
|
||||
}
|
||||
$params = $serialize->getParams();
|
||||
if (is_object($params)) {
|
||||
$params = get_object_vars($params);
|
||||
}
|
||||
$finish['class'] = $serialize::class;
|
||||
$finish['params'] = $params;
|
||||
$finish['status'] = 'success';
|
||||
$finish['info'] = $serialize->onHandler();
|
||||
return $finish;
|
||||
} catch (\Throwable $exception) {
|
||||
$finish['status'] = 'error';
|
||||
$finish['info'] = $this->format($exception);
|
||||
$this->addError($exception, 'Task');
|
||||
|
||||
return $finish;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $data
|
||||
* @return ITask|null
|
||||
*/
|
||||
protected function before($data): ?Task
|
||||
{
|
||||
if (empty($serialize = swoole_unserialize($data))) {
|
||||
return null;
|
||||
}
|
||||
if (!($serialize instanceof ITask)) {
|
||||
return null;
|
||||
}
|
||||
return $serialize;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $exception
|
||||
* @return string
|
||||
*/
|
||||
private function format($exception): string
|
||||
{
|
||||
return $exception->getMessage() . " on line " . $exception->getLine() . " at file " . $exception->getFile();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Event;
|
||||
use Kiri\Kiri;
|
||||
use Swoole\Server;
|
||||
|
||||
/**
|
||||
* Class OnWorkerError
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnWorkerError extends Callback
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $worker_id
|
||||
* @param int $worker_pid
|
||||
* @param int $exit_code
|
||||
* @param int $signal
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal)
|
||||
{
|
||||
Event::trigger(Event::SERVER_WORKER_ERROR);
|
||||
|
||||
$message = sprintf('Worker#%d::%d error stop. signal %d, exit_code %d, msg %s',
|
||||
$worker_id, $worker_pid, $signal, $exit_code, swoole_strerror(swoole_last_error(), 9)
|
||||
);
|
||||
write($message, 'worker-exit');
|
||||
|
||||
$this->system_mail($message);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use Kiri\Event;
|
||||
use Kiri\Kiri;
|
||||
use Swoole\Timer;
|
||||
|
||||
/**
|
||||
* Class OnWorkerExit
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnWorkerExit extends Callback
|
||||
{
|
||||
|
||||
/**
|
||||
* @param $server
|
||||
* @param $worker_id
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler($server, $worker_id)
|
||||
{
|
||||
putenv('state=exit');
|
||||
|
||||
Event::trigger(Event::SERVER_WORKER_EXIT, [$server, $worker_id]);
|
||||
|
||||
Kiri::getApp('logger')->insert();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,115 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
use Annotation\Annotation;
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use Kiri\Abstracts\Config;
|
||||
use Kiri\Event;
|
||||
use Kiri\Exception\ConfigException;
|
||||
use Kiri\Runtime;
|
||||
use Kiri\Kiri;
|
||||
use Swoole\Server;
|
||||
|
||||
/**
|
||||
* Class OnWorkerStart
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnWorkerStart extends Callback
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $worker_id
|
||||
*
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler(Server $server, int $worker_id): void
|
||||
{
|
||||
$this->setConfigs($worker_id);
|
||||
|
||||
$annotation = Kiri::app()->getAnnotation();
|
||||
$annotation->setLoader(unserialize(file_get_contents(storage(Runtime::CACHE_NAME))));
|
||||
if ($worker_id >= $server->setting['worker_num']) {
|
||||
$this->onTask($server, $annotation);
|
||||
} else {
|
||||
$this->onWorker($server, $annotation);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $worker_id
|
||||
* @throws Exception
|
||||
*/
|
||||
private function setConfigs($worker_id)
|
||||
{
|
||||
putenv('state=start');
|
||||
putenv('worker=' . $worker_id);
|
||||
|
||||
$serialize = file_get_contents(storage(Runtime::CONFIG_NAME));
|
||||
if (empty($serialize)) {
|
||||
return;
|
||||
}
|
||||
Config::sets(unserialize($serialize));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $worker_id
|
||||
* @return bool
|
||||
*/
|
||||
private function isWorker(Server $server, int $worker_id): bool
|
||||
{
|
||||
return $worker_id < $server->setting['worker_num'];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param Annotation $annotation
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onTask(Server $server, Annotation $annotation)
|
||||
{
|
||||
putenv('environmental=' . Kiri::TASK);
|
||||
|
||||
$annotation->runtime(APP_PATH, [CONTROLLER_PATH, TASK_PATH, LISTENER_PATH]);
|
||||
|
||||
name($server->worker_pid, 'Task#' . $server->worker_id);
|
||||
|
||||
Kiri::setTaskId($server->worker_pid);
|
||||
|
||||
fire(Event::SERVER_TASK_START);
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param Annotation $annotation
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onWorker(Server $server, Annotation $annotation)
|
||||
{
|
||||
name($server->worker_pid, 'Worker#' . $server->worker_id);
|
||||
|
||||
$time = microtime(true);
|
||||
|
||||
$annotation->runtime(CONTROLLER_PATH);
|
||||
$this->debug('use time.' . (microtime(true) - $time));
|
||||
$annotation->runtime(directory('app'), [CONTROLLER_PATH]);
|
||||
|
||||
Kiri::setWorkerId($server->worker_pid);
|
||||
putenv('environmental=' . Kiri::WORKER);
|
||||
|
||||
fire(Event::SERVER_WORKER_START, [getenv('worker')]);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,34 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Events;
|
||||
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\Callback;
|
||||
use Kiri\Event;
|
||||
use Swoole\Timer;
|
||||
|
||||
/**
|
||||
* Class OnWorkerStop
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class OnWorkerStop extends Callback
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param $server
|
||||
* @param $worker_id
|
||||
* @throws Exception
|
||||
*/
|
||||
public function onHandler($server, $worker_id)
|
||||
{
|
||||
Event::trigger(Event::SERVER_WORKER_STOP);
|
||||
|
||||
fire(Event::SYSTEM_RESOURCE_CLEAN);
|
||||
|
||||
Timer::clearAll();
|
||||
}
|
||||
|
||||
}
|
||||
+2
-61
@@ -5,10 +5,6 @@ namespace HttpServer;
|
||||
|
||||
use Exception;
|
||||
use HttpServer\Abstracts\HttpService;
|
||||
use HttpServer\Service\Http;
|
||||
use HttpServer\Service\Packet;
|
||||
use HttpServer\Service\Receive;
|
||||
use HttpServer\Service\Websocket;
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use ReflectionException;
|
||||
use Rpc\Service;
|
||||
@@ -39,6 +35,7 @@ class Server extends HttpService
|
||||
|
||||
|
||||
private ServerManager $manager;
|
||||
private mixed $daemon = 0;
|
||||
|
||||
|
||||
/**
|
||||
@@ -118,60 +115,6 @@ class Server extends HttpService
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $host
|
||||
* @param $Port
|
||||
* @return Packet|Websocket|Receive|Http|null
|
||||
* @throws Exception
|
||||
*/
|
||||
public function error_stop($host, $Port): Packet|Websocket|Receive|Http|null
|
||||
{
|
||||
$this->error(sprintf('Port %s::%d is already.', $host, $Port));
|
||||
if ($this->swoole) {
|
||||
$this->swoole->shutdown();
|
||||
} else {
|
||||
$this->shutdown();
|
||||
}
|
||||
return $this->swoole;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return bool
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
public function isRunner(): bool
|
||||
{
|
||||
$port = Config::get('servers');
|
||||
if (empty($port)) {
|
||||
return false;
|
||||
}
|
||||
foreach ($port as $value) {
|
||||
if ($this->checkPort($value['port'])) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $port
|
||||
* @return bool
|
||||
* @throws Exception
|
||||
*/
|
||||
private function checkPort($port): bool
|
||||
{
|
||||
if (Kiri::getPlatform()->isLinux()) {
|
||||
exec('netstat -tunlp | grep ' . $port, $output);
|
||||
} else {
|
||||
exec('lsof -i :' . $port . ' | grep -i "LISTEN"', $output);
|
||||
}
|
||||
return !empty($output);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*
|
||||
@@ -180,9 +123,7 @@ class Server extends HttpService
|
||||
*/
|
||||
public function shutdown()
|
||||
{
|
||||
/** @var Shutdown $shutdown */
|
||||
$shutdown = Kiri::app()->get('shutdown');
|
||||
$shutdown->shutdown();
|
||||
$this->manager->stopServer(0);
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -1,17 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Service\Abstracts;
|
||||
|
||||
|
||||
use HttpServer\IInterface\Service;
|
||||
use Swoole\Http\Server;
|
||||
|
||||
abstract class Http extends Server implements Service
|
||||
{
|
||||
|
||||
use \HttpServer\Service\Abstracts\Server;
|
||||
|
||||
|
||||
|
||||
}
|
||||
@@ -1,122 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Service\Abstracts;
|
||||
|
||||
|
||||
use Exception;
|
||||
use ReflectionException;
|
||||
use Kiri\Application;
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
use Kiri\Kiri;
|
||||
|
||||
|
||||
/**
|
||||
* Trait Server
|
||||
* @package HttpServer\Service\Abstracts
|
||||
*/
|
||||
trait Server
|
||||
{
|
||||
|
||||
public ?Application $application = null;
|
||||
|
||||
|
||||
/**
|
||||
* Server constructor.
|
||||
* @param $host
|
||||
* @param null $port
|
||||
* @param null $mode
|
||||
* @param null $sock_type
|
||||
*/
|
||||
public function __construct($host, $port = null, $mode = null, $sock_type = null)
|
||||
{
|
||||
parent::__construct($host, $port, $mode, $sock_type);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param array $settings
|
||||
*/
|
||||
public function set(array $settings)
|
||||
{
|
||||
parent::set($settings); // TODO: Change the autogenerated stub
|
||||
$this->onInit();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function onHandlerListener(): void
|
||||
{
|
||||
$this->on('Shutdown', $this->createHandler('shutdown'));
|
||||
$this->on('Start', $this->createHandler('start'));
|
||||
if (($this->setting['task_worker_num'] ?? 0) > 0) {
|
||||
$this->on('Finish', $this->createHandler('finish'));
|
||||
$this->on('Task', $this->createHandler('task'));
|
||||
}
|
||||
$this->onManager();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
private function onManager()
|
||||
{
|
||||
$this->on('ManagerStart', $this->createHandler('managerStart'));
|
||||
$this->on('ManagerStop', $this->createHandler('managerStop'));
|
||||
|
||||
$this->onWorker();
|
||||
$this->onOther();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
private function onWorker()
|
||||
{
|
||||
$this->on('WorkerStop', $this->createHandler('workerStop'));
|
||||
$this->on('WorkerExit', $this->createHandler('workerExit'));
|
||||
$this->on('WorkerStart', $this->createHandler('workerStart'));
|
||||
$this->on('WorkerError', $this->createHandler('workerError'));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
private function onOther()
|
||||
{
|
||||
$this->on('PipeMessage', $this->createHandler('pipeMessage'));
|
||||
$this->on('BeforeReload', $this->createHandler('BeforeReload'));
|
||||
$this->on('AfterReload', $this->createHandler('AfterReload'));
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $eventName
|
||||
* @return array
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function createHandler($eventName): array
|
||||
{
|
||||
$classPrefix = 'HttpServer\Events\On' . ucfirst($eventName);
|
||||
if (!class_exists($classPrefix)) {
|
||||
throw new Exception('class not found.');
|
||||
}
|
||||
|
||||
$class = Kiri::createObject($classPrefix, [Kiri::app()]);
|
||||
return [$class, 'onHandler'];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Service\Abstracts;
|
||||
|
||||
|
||||
use Closure;
|
||||
use HttpServer\IInterface\Service;
|
||||
use Swoole\Server;
|
||||
|
||||
abstract class Tcp extends Server implements Service
|
||||
{
|
||||
|
||||
use \HttpServer\Service\Abstracts\Server;
|
||||
|
||||
}
|
||||
@@ -1,16 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Service\Abstracts;
|
||||
|
||||
|
||||
use HttpServer\IInterface\Service;
|
||||
use Swoole\WebSocket\Server;
|
||||
|
||||
abstract class Websocket extends Server implements Service
|
||||
{
|
||||
|
||||
use \HttpServer\Service\Abstracts\Server;
|
||||
|
||||
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Service;
|
||||
|
||||
|
||||
use ReflectionException;
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
use HttpServer\Service\Abstracts\Http as AHttp;
|
||||
|
||||
class Http extends AHttp
|
||||
{
|
||||
|
||||
/**
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
public function onInit()
|
||||
{
|
||||
$this->onHandlerListener();
|
||||
$this->onBaseListener();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function onBaseListener()
|
||||
{
|
||||
$this->on('request', $this->createHandler('request'));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Service;
|
||||
|
||||
|
||||
use HttpServer\Service\Abstracts\Tcp;
|
||||
use ReflectionException;
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
|
||||
/**
|
||||
* Class OnPacket
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class Packet extends Tcp
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
public function onInit()
|
||||
{
|
||||
$this->onHandlerListener();
|
||||
$this->onBaseListener();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function onBaseListener()
|
||||
{
|
||||
$this->on('packet', $this->createHandler('packet'));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Service;
|
||||
|
||||
|
||||
use HttpServer\Service\Abstracts\Tcp;
|
||||
use ReflectionException;
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
|
||||
/**
|
||||
* Class Receive
|
||||
* @package HttpServer\Events
|
||||
*/
|
||||
class Receive extends Tcp
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
public function onInit()
|
||||
{
|
||||
$this->onHandlerListener();
|
||||
$this->onBaseListener();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function onBaseListener()
|
||||
{
|
||||
$this->on('connect', $this->createHandler('connect'));
|
||||
$this->on('receive', $this->createHandler('receive'));
|
||||
$this->on('close', $this->createHandler('close'));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,42 +0,0 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace HttpServer\Service;
|
||||
|
||||
|
||||
use ReflectionException;
|
||||
use Kiri\Exception\NotFindClassException;
|
||||
use HttpServer\Service\Abstracts\Websocket as HAWebsocket;
|
||||
|
||||
|
||||
/**
|
||||
* Class Websocket
|
||||
* @package HttpServer\Service
|
||||
*/
|
||||
class Websocket extends HAWebsocket
|
||||
{
|
||||
|
||||
/**
|
||||
* @throws ReflectionException
|
||||
* @throws NotFindClassException
|
||||
*/
|
||||
public function onInit()
|
||||
{
|
||||
$this->onHandlerListener();
|
||||
$this->onBaseListener();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @throws NotFindClassException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
public function onBaseListener()
|
||||
{
|
||||
$this->on('connect', function () {});
|
||||
$this->on('handshake', $this->createHandler('handshake'));
|
||||
$this->on('message', $this->createHandler('message'));
|
||||
$this->on('close', $this->createHandler('close'));
|
||||
}
|
||||
|
||||
}
|
||||
@@ -308,6 +308,12 @@ class ServerManager
|
||||
*/
|
||||
private function portIsAlready($port): bool|string
|
||||
{
|
||||
if (!Kiri::getPlatform()->isLinux()) {
|
||||
exec('lsof -i :' . $port . ' | grep -i "LISTEN" | awk "{print $2}"', $output);
|
||||
if (empty($output)) return false;
|
||||
$output = explode(PHP_EOL, $output[0]);
|
||||
return $output[0];
|
||||
}
|
||||
exec('netstat -lnp | grep ' . $port . ' | grep "LISTEN" | awk \'{print $7}\'', $output);
|
||||
if (empty($output)) {
|
||||
return false;
|
||||
|
||||
Reference in New Issue
Block a user