This commit is contained in:
as2252258@163.com
2021-09-24 02:23:24 +08:00
parent b9a1178fbe
commit 3d9bea7fb6
31 changed files with 110 additions and 78 deletions
+3 -3
View File
@@ -28,7 +28,7 @@ use Kiri\Jwt\Jwt;
use Kiri\Kiri; use Kiri\Kiri;
use ReflectionException; use ReflectionException;
use Server\ServerManager; use Server\ServerManager;
use Server\SInterface\TaskExecute; use Server\SInterface\OnTaskInterface;
use Swoole\Table; use Swoole\Table;
/** /**
@@ -205,10 +205,10 @@ abstract class BaseApplication extends Component
/** /**
* @param TaskExecute $execute * @param OnTaskInterface $execute
* @throws ReflectionException * @throws ReflectionException
*/ */
public function task(TaskExecute $execute): void public function task(OnTaskInterface $execute): void
{ {
di(ServerManager::class)->task($execute); di(ServerManager::class)->task($execute);
} }
+2 -2
View File
@@ -11,13 +11,13 @@ use Kiri\Exception\ComponentException;
use Kiri\Kiri; use Kiri\Kiri;
use Swoole\Coroutine; use Swoole\Coroutine;
use Swoole\Process; use Swoole\Process;
use Server\Abstracts\CustomProcess; use Server\Abstracts\OnProcessInterface;
/** /**
* Class LoggerProcess * Class LoggerProcess
* @package Kiri\Error * @package Kiri\Error
*/ */
class LoggerProcess extends CustomProcess class LoggerProcess extends OnProcessInterface
{ {
/** /**
+1 -1
View File
@@ -71,7 +71,7 @@ class Event extends BaseObject
const SERVER_ON_CLOSE = 'Close'; const SERVER_ON_CLOSE = 'Close';
const SERVER_ON_TASK = 'Task'; const SERVER_ON_TASK = 'Task';
const SERVER_ON_FINISH = 'Finish'; const SERVER_ON_FINISH = 'Finish';
const SERVER_ON_PIPE_MESSAGE = 'PipeMessage'; const SERVER_ON_PIPE_MESSAGE = 'OnPipeMessageInterface';
const SERVER_ON_WORKER_ERROR = 'WorkerError'; const SERVER_ON_WORKER_ERROR = 'WorkerError';
const SERVER_ON_MANAGER_START = 'ManagerStart'; const SERVER_ON_MANAGER_START = 'ManagerStart';
const SERVER_ON_MANAGER_STOP = 'ManagerStop'; const SERVER_ON_MANAGER_STOP = 'ManagerStop';
+2 -2
View File
@@ -6,14 +6,14 @@ use Exception;
use Kiri\Abstracts\Config; use Kiri\Abstracts\Config;
use Kiri\Kiri; use Kiri\Kiri;
use ReflectionException; use ReflectionException;
use Server\Abstracts\CustomProcess; use Server\Abstracts\OnProcessInterface;
use Swoole\Process; use Swoole\Process;
/** /**
* *
*/ */
class FileChangeCustomProcess extends CustomProcess class FileChangeCustomProcess extends OnProcessInterface
{ {
@@ -2,10 +2,10 @@
namespace Http\Message; namespace Http\Message;
use Server\SInterface\DownloadInterface; use Server\SInterface\OnDownloadInterface;
class Download extends Response implements DownloadInterface class OnDownload extends Response implements OnDownloadInterface
{ {
use Message; use Message;
@@ -34,7 +34,7 @@ class Download extends Response implements DownloadInterface
* @param int $offset * @param int $offset
* @return $this * @return $this
*/ */
public function path(string $path, bool $isChunk = false, int $size = -1, int $offset = 0): Download public function path(string $path, bool $isChunk = false, int $size = -1, int $offset = 0): OnDownload
{ {
$this->path = $path; $this->path = $path;
$this->isChunk = $isChunk; $this->isChunk = $isChunk;
+4 -4
View File
@@ -6,7 +6,7 @@ use Exception;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Kiri\Core\Help; use Kiri\Core\Help;
use Server\Constrict\ResponseInterface; use Server\Constrict\ResponseInterface;
use Server\SInterface\DownloadInterface; use Server\SInterface\OnDownloadInterface;
/** /**
@@ -208,15 +208,15 @@ class Response implements ResponseInterface
* @param bool $isChunk * @param bool $isChunk
* @param int $size * @param int $size
* @param int $offset * @param int $offset
* @return DownloadInterface * @return OnDownloadInterface
* @throws Exception * @throws Exception
*/ */
public function file($path, bool $isChunk = false, int $size = -1, int $offset = 0): DownloadInterface public function file($path, bool $isChunk = false, int $size = -1, int $offset = 0): OnDownloadInterface
{ {
$path = realpath($path); $path = realpath($path);
if (!file_exists($path) || !is_readable($path)) { if (!file_exists($path) || !is_readable($path)) {
throw new Exception('Cannot read file "' . $path . '", no permission'); throw new Exception('Cannot read file "' . $path . '", no permission');
} }
return (new Download())->path($path, $isChunk, $size, $offset); return (new OnDownload())->path($path, $isChunk, $size, $offset);
} }
} }
@@ -11,7 +11,7 @@ use Swoole\Process;
/** /**
* *
*/ */
abstract class CustomProcess implements \Server\SInterface\CustomProcess abstract class OnProcessInterface implements \Server\SInterface\OnProcessInterface
{ {
/** @var bool */ /** @var bool */
+1 -1
View File
@@ -26,7 +26,7 @@ class Constant
const CLOSE = 'Close'; const CLOSE = 'Close';
const TASK = 'Task'; const TASK = 'Task';
const FINISH = 'Finish'; const FINISH = 'Finish';
const PIPE_MESSAGE = 'PipeMessage'; const PIPE_MESSAGE = 'OnPipeMessageInterface';
const WORKER_ERROR = 'WorkerError'; const WORKER_ERROR = 'WorkerError';
const MANAGER_START = 'ManagerStart'; const MANAGER_START = 'ManagerStart';
const MANAGER_STOP = 'ManagerStop'; const MANAGER_STOP = 'ManagerStop';
+3 -3
View File
@@ -14,7 +14,7 @@ use Psr\Http\Message\StreamInterface;
use Http\Message\ServerRequest as RequestMessage; use Http\Message\ServerRequest as RequestMessage;
use Http\Message\Response as Psr7Response; use Http\Message\Response as Psr7Response;
use Server\ServerManager; use Server\ServerManager;
use Server\SInterface\DownloadInterface; use Server\SInterface\OnDownloadInterface;
/** /**
@@ -201,9 +201,9 @@ class Response implements ResponseInterface
/** /**
* @param string $path * @param string $path
* @return DownloadInterface * @return OnDownloadInterface
*/ */
public function file(string $path): DownloadInterface public function file(string $path): OnDownloadInterface
{ {
return $this->__call__()->{__FUNCTION__}($path); return $this->__call__()->{__FUNCTION__}($path);
} }
+2 -2
View File
@@ -3,7 +3,7 @@
namespace Server\Constrict; namespace Server\Constrict;
use Annotation\Inject; use Annotation\Inject;
use Server\SInterface\DownloadInterface; use Server\SInterface\OnDownloadInterface;
use Swoole\Server; use Swoole\Server;
@@ -42,7 +42,7 @@ class ResponseEmitter implements Emitter
$response->header('Server', 'swoole'); $response->header('Server', 'swoole');
$response->header('Swoole-Version', swoole_version()); $response->header('Swoole-Version', swoole_version());
if (!($emitter instanceof DownloadInterface)) { if (!($emitter instanceof OnDownloadInterface)) {
$response->end($emitter->getBody()->getContents()); $response->end($emitter->getBody()->getContents());
} else { } else {
$emitter->dispatch($response); $emitter->dispatch($response);
+3 -3
View File
@@ -5,7 +5,7 @@ namespace Server\Constrict;
use JetBrains\PhpStorm\Pure; use JetBrains\PhpStorm\Pure;
use Http\Message\Response; use Http\Message\Response;
use Server\SInterface\DownloadInterface; use Server\SInterface\OnDownloadInterface;
/** /**
* @mixin Response * @mixin Response
@@ -16,9 +16,9 @@ interface ResponseInterface extends \Psr\Http\Message\ResponseInterface
/** /**
* @param string $path * @param string $path
* @return DownloadInterface * @return OnDownloadInterface
*/ */
public function file(string $path): DownloadInterface; public function file(string $path): OnDownloadInterface;
/** /**
+2 -2
View File
@@ -6,7 +6,7 @@ use Annotation\Inject;
use Server\Abstracts\Server; use Server\Abstracts\Server;
use Exception; use Exception;
use Server\Events\OnAfterRequest; use Server\Events\OnAfterRequest;
use Server\SInterface\PipeMessage; use Server\SInterface\OnPipeMessageInterface;
use Kiri\Events\EventDispatch; use Kiri\Events\EventDispatch;
/** /**
@@ -29,7 +29,7 @@ class OnPipeMessage extends Server
*/ */
public function onPipeMessage(\Swoole\Server $server, int $src_worker_id, mixed $message) public function onPipeMessage(\Swoole\Server $server, int $src_worker_id, mixed $message)
{ {
if (!is_object($message) || !($message instanceof PipeMessage)) { if (!is_object($message) || !($message instanceof OnPipeMessageInterface)) {
return; return;
} }
@@ -8,7 +8,7 @@ use Swoole\Server;
/** /**
* *
*/ */
interface OnClose interface OnCloseInterface
{ {
@@ -19,11 +19,4 @@ interface OnClose
public function onClose(Server $server, int $fd): void; public function onClose(Server $server, int $fd): void;
/**
* @param Server $server
* @param int $fd
*/
public function onDisconnect(Server $server, int $fd): void;
} }
@@ -4,7 +4,7 @@ namespace Server\SInterface;
use Swoole\Server; use Swoole\Server;
interface OnConnect interface OnConnectInterface
{ {
@@ -0,0 +1,19 @@
<?php
namespace Server\SInterface;
use Swoole\Server;
interface OnDisconnectInterface
{
/**
* @param Server $server
* @param int $fd
*/
public function onDisconnect(Server $server, int $fd): void;
}
@@ -4,7 +4,7 @@ namespace Server\SInterface;
use Swoole\Http\Response; use Swoole\Http\Response;
interface DownloadInterface interface OnDownloadInterface
{ {
public function dispatch(Response $response); public function dispatch(Response $response);
@@ -9,7 +9,7 @@ use Swoole\Http\Response;
/** /**
* *
*/ */
interface OnHandshake interface OnHandshakeInterface
{ {
@@ -5,7 +5,7 @@ namespace Server\SInterface;
use Swoole\Server; use Swoole\Server;
use Swoole\WebSocket\Frame; use Swoole\WebSocket\Frame;
interface OnMessage interface OnMessageInterface
{ {
@@ -4,7 +4,7 @@ namespace Server\SInterface;
use Server\Abstracts\Server; use Server\Abstracts\Server;
interface OnPacket interface OnPacketInterface
{ {
@@ -5,10 +5,10 @@ namespace Server\SInterface;
/** /**
* Interface PipeMessage * Interface OnPipeMessageInterface
* @package Server\SInterface * @package Server\SInterface
*/ */
interface PipeMessage interface OnPipeMessageInterface
{ {
/** /**
@@ -8,10 +8,10 @@ use Swoole\Process;
/** /**
* Interface CustomProcess * Interface OnProcessInterface
* @package SInterface * @package SInterface
*/ */
interface CustomProcess interface OnProcessInterface
{ {
@@ -8,7 +8,7 @@ use Swoole\Server;
/** /**
* *
*/ */
interface OnReceive interface OnReceiveInterface
{ {
@@ -5,7 +5,7 @@ namespace Server\SInterface;
use Swoole\Http\Request; use Swoole\Http\Request;
use Swoole\Http\Response; use Swoole\Http\Response;
interface OnRequest interface OnRequestInterface
{ {
@@ -6,7 +6,7 @@ namespace Server\SInterface;
use Swoole\Server; use Swoole\Server;
interface TaskExecute interface OnTaskInterface
{ {
public function execute(); public function execute();
+29 -9
View File
@@ -14,8 +14,15 @@ use Server\Manager\OnPipeMessage;
use Server\Manager\OnServer; use Server\Manager\OnServer;
use Server\Manager\OnServerManager; use Server\Manager\OnServerManager;
use Server\Manager\OnServerReload; use Server\Manager\OnServerReload;
use Server\SInterface\CustomProcess; use Server\SInterface\OnProcessInterface;
use Server\SInterface\TaskExecute; use Server\SInterface\OnCloseInterface;
use Server\SInterface\OnConnectInterface;
use Server\SInterface\OnDisconnectInterface;
use Server\SInterface\OnHandshakeInterface;
use Server\SInterface\OnMessageInterface;
use Server\SInterface\OnPacketInterface;
use Server\SInterface\OnReceiveInterface;
use Server\SInterface\OnTaskInterface;
use Server\Task\OnServerTask; use Server\Task\OnServerTask;
use Server\Worker\OnServerWorker; use Server\Worker\OnServerWorker;
use Swoole\Http\Server as HServer; use Swoole\Http\Server as HServer;
@@ -68,6 +75,19 @@ class ServerManager
]; ];
private array $eventInterface = [
OnReceiveInterface::class => 'receive',
OnPacketInterface::class =>'packet',
OnHandshakeInterface::class => 'handshake',
OnMessageInterface::class =>'message',
OnConnectInterface::class =>'connect',
OnCloseInterface::class =>'close',
OnDisconnectInterface::class =>'disconnect'
];
/** /**
* @return Server|WServer|HServer|null * @return Server|WServer|HServer|null
*/ */
@@ -134,17 +154,17 @@ class ServerManager
/** /**
* @param string|CustomProcess $customProcess * @param string|OnProcessInterface $customProcess
* @param null $redirect_stdin_and_stdout * @param null $redirect_stdin_and_stdout
* @param int|null $pipe_type * @param int|null $pipe_type
* @param bool $enable_coroutine * @param bool $enable_coroutine
* @throws Exception * @throws Exception
*/ */
public function addProcess(string|CustomProcess $customProcess, $redirect_stdin_and_stdout = null, ?int $pipe_type = SOCK_DGRAM, bool $enable_coroutine = true) public function addProcess(string|OnProcessInterface $customProcess, $redirect_stdin_and_stdout = null, ?int $pipe_type = SOCK_DGRAM, bool $enable_coroutine = true)
{ {
$process = $this->initProcess($customProcess, $redirect_stdin_and_stdout, $pipe_type, $enable_coroutine); $process = $this->initProcess($customProcess, $redirect_stdin_and_stdout, $pipe_type, $enable_coroutine);
$this->server->addProcess($process); $this->server->addProcess($process);
if ($customProcess instanceof CustomProcess) { if ($customProcess instanceof OnProcessInterface) {
Kiri::app()->addProcess($customProcess::class, $process); Kiri::app()->addProcess($customProcess::class, $process);
} else { } else {
Kiri::app()->addProcess($customProcess, $process); Kiri::app()->addProcess($customProcess, $process);
@@ -445,13 +465,13 @@ class ServerManager
/** /**
* @param TaskExecute|string $handler * @param OnTaskInterface|string $handler
* @param array $params * @param array $params
* @param int|null $workerId * @param int|null $workerId
* @throws ReflectionException * @throws ReflectionException
* @throws Exception * @throws Exception
*/ */
public function task(TaskExecute|string $handler, array $params = [], int $workerId = null) public function task(OnTaskInterface|string $handler, array $params = [], int $workerId = null)
{ {
if ($workerId === null || $workerId <= $this->server->setting['worker_num']) { if ($workerId === null || $workerId <= $this->server->setting['worker_num']) {
$workerId = random_int($this->server->setting['worker_num'] + 1, $workerId = random_int($this->server->setting['worker_num'] + 1,
@@ -459,8 +479,8 @@ class ServerManager
} }
if (is_string($handler)) { if (is_string($handler)) {
$implements = $this->container->getReflect($handler); $implements = $this->container->getReflect($handler);
if (!in_array(TaskExecute::class, $implements->getInterfaceNames())) { if (!in_array(OnTaskInterface::class, $implements->getInterfaceNames())) {
throw new Exception('Task must instance ' . TaskExecute::class); throw new Exception('Task must instance ' . OnTaskInterface::class);
} }
$handler = $implements->newInstanceArgs($params); $handler = $implements->newInstanceArgs($params);
} }
+4 -4
View File
@@ -26,9 +26,9 @@ use Server\Constrict\ResponseInterface;
use Server\Events\OnAfterRequest; use Server\Events\OnAfterRequest;
use Server\ExceptionHandlerDispatcher; use Server\ExceptionHandlerDispatcher;
use Server\ExceptionHandlerInterface; use Server\ExceptionHandlerInterface;
use Server\SInterface\OnClose; use Server\SInterface\OnCloseInterface;
use Server\SInterface\OnConnect; use Server\SInterface\OnConnectInterface;
use Server\SInterface\OnRequest; use Server\SInterface\OnRequestInterface;
use Swoole\Http\Request; use Swoole\Http\Request;
use Swoole\Http\Response; use Swoole\Http\Response;
use Swoole\Server; use Swoole\Server;
@@ -36,7 +36,7 @@ use Swoole\Server;
/** /**
* *
*/ */
class Http implements OnClose, OnConnect, OnRequest class Http implements OnCloseInterface, OnConnectInterface, OnRequestInterface
{ {
use EventDispatchHelper; use EventDispatchHelper;
+4 -4
View File
@@ -10,16 +10,16 @@ use Server\Abstracts\Utility\ResponseHelper;
use Server\Constrict\TcpEmitter; use Server\Constrict\TcpEmitter;
use Server\ExceptionHandlerDispatcher; use Server\ExceptionHandlerDispatcher;
use Server\ExceptionHandlerInterface; use Server\ExceptionHandlerInterface;
use Server\SInterface\OnClose; use Server\SInterface\OnCloseInterface;
use Server\SInterface\OnConnect; use Server\SInterface\OnConnectInterface;
use Server\SInterface\OnReceive; use Server\SInterface\OnReceiveInterface;
use Swoole\Server; use Swoole\Server;
/** /**
* *
*/ */
class Tcp implements OnConnect, OnClose, OnReceive class Tcp implements OnConnectInterface, OnCloseInterface, OnReceiveInterface
{ {
use EventDispatchHelper; use EventDispatchHelper;
+2 -2
View File
@@ -13,13 +13,13 @@ use Server\Abstracts\Utility\ResponseHelper;
use Server\Constrict\UdpEmitter; use Server\Constrict\UdpEmitter;
use Server\ExceptionHandlerDispatcher; use Server\ExceptionHandlerDispatcher;
use Server\ExceptionHandlerInterface; use Server\ExceptionHandlerInterface;
use Server\SInterface\OnPacket; use Server\SInterface\OnPacketInterface;
/** /**
* *
*/ */
class Udp implements OnPacket class Udp implements OnPacketInterface
{ {
use EventDispatchHelper; use EventDispatchHelper;
+9 -9
View File
@@ -12,10 +12,10 @@ use Server\Abstracts\Utility\ResponseHelper;
use Server\Constrict\WebSocketEmitter; use Server\Constrict\WebSocketEmitter;
use Server\ExceptionHandlerDispatcher; use Server\ExceptionHandlerDispatcher;
use Server\ExceptionHandlerInterface; use Server\ExceptionHandlerInterface;
use Server\SInterface\OnClose; use Server\SInterface\OnCloseInterface;
use Server\SInterface\OnHandshake; use Server\SInterface\OnHandshakeInterface;
use Server\SInterface\OnMessage; use Server\SInterface\OnMessageInterface;
use Server\SInterface\OnRequest; use Server\SInterface\OnRequestInterface;
use Swoole\Http\Request; use Swoole\Http\Request;
use Swoole\Http\Response; use Swoole\Http\Response;
use Swoole\Server; use Swoole\Server;
@@ -24,7 +24,7 @@ use Swoole\WebSocket\Frame;
/** /**
* *
*/ */
class WebSocket implements OnHandshake, OnMessage, OnClose class WebSocket implements OnHandshakeInterface, OnMessageInterface, OnCloseInterface
{ {
@@ -60,7 +60,7 @@ class WebSocket implements OnHandshake, OnMessage, OnClose
*/ */
public function onHandshake(Request $request, Response $response): void public function onHandshake(Request $request, Response $response): void
{ {
// TODO: Implement OnHandshake() method. // TODO: Implement OnHandshakeInterface() method.
$secWebSocketKey = $request->header['sec-websocket-key']; $secWebSocketKey = $request->header['sec-websocket-key'];
$patten = '#^[+/0-9A-Za-z]{21}[AQgw]==$#'; $patten = '#^[+/0-9A-Za-z]{21}[AQgw]==$#';
if (0 === preg_match($patten, $secWebSocketKey) || 16 !== strlen(base64_decode($secWebSocketKey))) { if (0 === preg_match($patten, $secWebSocketKey) || 16 !== strlen(base64_decode($secWebSocketKey))) {
@@ -91,7 +91,7 @@ class WebSocket implements OnHandshake, OnMessage, OnClose
*/ */
public function onMessage(Server $server, Frame $frame): void public function onMessage(Server $server, Frame $frame): void
{ {
// TODO: Implement OnMessage() method. // TODO: Implement OnMessageInterface() method.
} }
@@ -101,7 +101,7 @@ class WebSocket implements OnHandshake, OnMessage, OnClose
*/ */
public function onClose(Server $server, int $fd): void public function onClose(Server $server, int $fd): void
{ {
// TODO: Implement OnClose() method. // TODO: Implement OnCloseInterface() method.
} }
@@ -111,6 +111,6 @@ class WebSocket implements OnHandshake, OnMessage, OnClose
*/ */
public function onDisconnect(Server $server, int $fd): void public function onDisconnect(Server $server, int $fd): void
{ {
// TODO: Implement OnDisconnect() method. // TODO: Implement OnDisconnectInterface() method.
} }
} }
+2 -2
View File
@@ -12,7 +12,7 @@ use Server\Constrict\Response;
use Server\Constrict\ResponseInterface; use Server\Constrict\ResponseInterface;
use Server\ExceptionHandlerDispatcher; use Server\ExceptionHandlerDispatcher;
use Server\ExceptionHandlerInterface; use Server\ExceptionHandlerInterface;
use Server\SInterface\TaskExecute; use Server\SInterface\OnTaskInterface;
use Swoole\Server; use Swoole\Server;
@@ -108,7 +108,7 @@ class OnServerTask
*/ */
public function onFinish(Server $server, int $task_id, mixed $data) public function onFinish(Server $server, int $task_id, mixed $data)
{ {
if (!($data instanceof TaskExecute)) { if (!($data instanceof OnTaskInterface)) {
return; return;
} }
$data->finish($server, $task_id); $data->finish($server, $task_id);
+1 -1
View File
@@ -31,7 +31,7 @@ class GiiTask extends GiiBase
namespace App\Async; namespace App\Async;
use Server\SInterface\TaskExecute; use Server\SInterface\OnTaskInterface;
'; ';