改名
This commit is contained in:
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
|
||||
class OnAfterRequest
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
|
||||
class OnBeforeRequest
|
||||
{
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
|
||||
use Swoole\Server;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class OnWorkerError
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $workerId
|
||||
*/
|
||||
public function __construct(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
|
||||
use Swoole\Server;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class OnWorkerExit
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $workerId
|
||||
*/
|
||||
public function __construct(Server $server, int $workerId)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
|
||||
use Swoole\Server;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class OnWorkerStart
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $workerId
|
||||
*/
|
||||
public function __construct(Server $server, int $workerId)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
|
||||
namespace Server\Events;
|
||||
|
||||
use Swoole\Server;
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class OnWorkerStop
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $workerId
|
||||
*/
|
||||
public function __construct(Server $server, int $workerId)
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -2,10 +2,14 @@
|
||||
|
||||
namespace Server;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use HttpServer\Http\Context;
|
||||
use HttpServer\Http\Request as HSRequest;
|
||||
use HttpServer\Route\Router;
|
||||
use Server\Events\OnAfterRequest;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Events\EventDispatch;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Error;
|
||||
use Swoole\Http\Request;
|
||||
@@ -26,21 +30,19 @@ class HTTPServerListener extends Abstracts\Server
|
||||
|
||||
use ListenerHelper;
|
||||
|
||||
/** @var Router|mixed */
|
||||
#[Inject('router')]
|
||||
private Router $router;
|
||||
|
||||
/** @var \HttpServer\Http\Response|mixed */
|
||||
#[Inject(\HttpServer\Http\Response::class)]
|
||||
private \HttpServer\Http\Response $response;
|
||||
|
||||
|
||||
/**
|
||||
* HTTPServerListener constructor.
|
||||
* @throws Exception
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
$this->router = Snowflake::getApp('router');
|
||||
$this->response = di(\HttpServer\Http\Response::class);
|
||||
parent::__construct();
|
||||
}
|
||||
/** @var EventDispatch */
|
||||
#[Inject(EventDispatch::class)]
|
||||
public EventDispatch $eventDispatch;
|
||||
|
||||
|
||||
/**
|
||||
* UDPServerListener constructor.
|
||||
@@ -93,6 +95,8 @@ class HTTPServerListener extends Abstracts\Server
|
||||
$this->router->dispatch(HSRequest::create($request));
|
||||
} catch (Error | Throwable $exception) {
|
||||
$this->response->send(jTraceEx($exception), 500);
|
||||
} finally {
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -2,11 +2,15 @@
|
||||
|
||||
namespace Server\Manager;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use Server\Abstracts\Server;
|
||||
use Server\Constant;
|
||||
use Server\Events\OnAfterRequest;
|
||||
use Server\SInterface\PipeMessage;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Events\EventDispatch;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
|
||||
|
||||
/**
|
||||
@@ -17,9 +21,14 @@ class ServerDefaultEvent extends Server
|
||||
{
|
||||
|
||||
|
||||
/** @var EventDispatch */
|
||||
#[Inject(EventDispatch::class)]
|
||||
public EventDispatch $eventDispatch;
|
||||
|
||||
|
||||
/**
|
||||
* @param \Swoole\Server $server
|
||||
* @throws \Snowflake\Exception\ConfigException
|
||||
* @throws ConfigException
|
||||
*/
|
||||
public function onStart(\Swoole\Server $server)
|
||||
{
|
||||
@@ -49,7 +58,7 @@ class ServerDefaultEvent extends Server
|
||||
if (!is_object($message) || !($message instanceof PipeMessage)) {
|
||||
return;
|
||||
}
|
||||
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
|
||||
defer(fn() => $this->eventDispatch->dispatch(new OnAfterRequest()));
|
||||
$this->runEvent(Constant::PIPE_MESSAGE,
|
||||
function (\Swoole\Server $server, $src_worker_id, $message) {
|
||||
call_user_func([$message, 'execute']);
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
|
||||
namespace Server;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use ReflectionException;
|
||||
use Server\Events\OnAfterRequest;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Events\EventDispatch;
|
||||
use Swoole\Server;
|
||||
use Swoole\Server\Port;
|
||||
|
||||
@@ -21,6 +23,11 @@ class TCPServerListener extends Abstracts\Server
|
||||
protected static bool|Port $_tcp;
|
||||
|
||||
|
||||
/** @var EventDispatch */
|
||||
#[Inject(EventDispatch::class)]
|
||||
public EventDispatch $eventDispatch;
|
||||
|
||||
|
||||
/**
|
||||
* UDPServerListener constructor.
|
||||
* @param Server|Port $server
|
||||
@@ -51,7 +58,7 @@ class TCPServerListener extends Abstracts\Server
|
||||
{
|
||||
$this->runEvent(Constant::CONNECT, null, [$server, $fd]);
|
||||
|
||||
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
|
||||
|
||||
@@ -66,7 +73,7 @@ class TCPServerListener extends Abstracts\Server
|
||||
{
|
||||
$this->runEvent(Constant::RECEIVE, null, [$server, $fd, $reactor_id, $data]);
|
||||
|
||||
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
|
||||
|
||||
@@ -79,7 +86,8 @@ class TCPServerListener extends Abstracts\Server
|
||||
{
|
||||
$this->runEvent(Constant::CLOSE, null, [$server, $fd]);
|
||||
|
||||
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,9 +2,12 @@
|
||||
|
||||
namespace Server;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use ReflectionException;
|
||||
use Server\Events\OnAfterRequest;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Events\EventDispatch;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Swoole\Server;
|
||||
use Swoole\Server\Port;
|
||||
@@ -17,7 +20,10 @@ use Swoole\Server\Port;
|
||||
class UDPServerListener extends Abstracts\Server
|
||||
{
|
||||
|
||||
protected static bool|Port $_udp;
|
||||
|
||||
/** @var EventDispatch */
|
||||
#[Inject(EventDispatch::class)]
|
||||
public EventDispatch $eventDispatch;
|
||||
|
||||
|
||||
use ListenerHelper;
|
||||
@@ -50,7 +56,7 @@ class UDPServerListener extends Abstracts\Server
|
||||
{
|
||||
$this->runEvent(Constant::MESSAGE, null, [$server, $data, $clientInfo]);
|
||||
|
||||
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -2,9 +2,11 @@
|
||||
|
||||
namespace Server;
|
||||
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use ReflectionException;
|
||||
use Snowflake\Event;
|
||||
use Server\Events\OnAfterRequest;
|
||||
use Snowflake\Events\EventDispatch;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
use Swoole\Http\Request;
|
||||
@@ -25,6 +27,11 @@ class WebSocketServerListener extends Abstracts\Server
|
||||
|
||||
use ListenerHelper;
|
||||
|
||||
|
||||
#[Inject(EventDispatch::class)]
|
||||
public EventDispatch $eventDispatch;
|
||||
|
||||
|
||||
/**
|
||||
* @param mixed $server
|
||||
* @param array|null $settings
|
||||
@@ -88,7 +95,7 @@ class WebSocketServerListener extends Abstracts\Server
|
||||
}
|
||||
$this->runEvent(Constant::HANDSHAKE, fn() => $this->disconnect($request, $response), [$request, $response]);
|
||||
|
||||
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
|
||||
|
||||
@@ -112,7 +119,7 @@ class WebSocketServerListener extends Abstracts\Server
|
||||
{
|
||||
$this->runEvent(Constant::CONNECT, fn() => $server->confirm($fd), [$server, $fd]);
|
||||
|
||||
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
|
||||
|
||||
@@ -125,7 +132,7 @@ class WebSocketServerListener extends Abstracts\Server
|
||||
{
|
||||
$this->runEvent(Constant::MESSAGE, fn() => $server->push($frame->fd, '.'), [$server, $frame]);
|
||||
|
||||
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
|
||||
|
||||
@@ -138,7 +145,7 @@ class WebSocketServerListener extends Abstracts\Server
|
||||
{
|
||||
$this->runEvent(Constant::CLOSE, fn() => $server->confirm($fd), [$server, $fd]);
|
||||
|
||||
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
|
||||
|
||||
@@ -151,7 +158,7 @@ class WebSocketServerListener extends Abstracts\Server
|
||||
{
|
||||
$this->runEvent(Constant::DISCONNECT, fn() => $server->confirm($fd), [$server, $fd]);
|
||||
|
||||
$this->_event->dispatch(Event::SYSTEM_RESOURCE_RELEASES);
|
||||
$this->eventDispatch->dispatch(new OnAfterRequest());
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -3,13 +3,17 @@
|
||||
namespace Server\Worker;
|
||||
|
||||
use Annotation\Annotation;
|
||||
use Annotation\Inject;
|
||||
use Exception;
|
||||
use ReflectionException;
|
||||
use Server\ApplicationStore;
|
||||
use Server\Constant;
|
||||
use Server\Events\OnWorkerError;
|
||||
use Server\Events\OnWorkerExit;
|
||||
use Server\Events\OnWorkerStart;
|
||||
use Server\Events\OnWorkerStop;
|
||||
use Snowflake\Abstracts\Config;
|
||||
use Snowflake\Core\Help;
|
||||
use Snowflake\Event;
|
||||
use Snowflake\Events\EventDispatch;
|
||||
use Snowflake\Exception\ConfigException;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Runtime;
|
||||
@@ -26,6 +30,13 @@ class ServerWorker extends \Server\Abstracts\Server
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var EventDispatch
|
||||
*/
|
||||
#[Inject(EventDispatch::class)]
|
||||
public EventDispatch $eventDispatch;
|
||||
|
||||
|
||||
/**
|
||||
* @param Server $server
|
||||
* @param int $workerId
|
||||
@@ -34,18 +45,15 @@ class ServerWorker extends \Server\Abstracts\Server
|
||||
public function onWorkerStart(Server $server, int $workerId)
|
||||
{
|
||||
$this->_setConfigCache($workerId);
|
||||
|
||||
$store = ApplicationStore::getStore()->instance();
|
||||
|
||||
$annotation = Snowflake::app()->getAnnotation();
|
||||
$annotation->read(APP_PATH . 'app');
|
||||
|
||||
$this->eventDispatch->dispatch(new OnWorkerStart($server, $workerId));
|
||||
|
||||
$this->runEvent(Constant::WORKER_START, null, [$server, $workerId]);
|
||||
|
||||
$this->workerInitExecutor($server, $annotation, $workerId);
|
||||
$this->interpretDirectory($annotation);
|
||||
|
||||
$store->close();
|
||||
}
|
||||
|
||||
|
||||
@@ -128,9 +136,7 @@ class ServerWorker extends \Server\Abstracts\Server
|
||||
{
|
||||
$this->runEvent(Constant::WORKER_STOP, null, [$server, $workerId]);
|
||||
|
||||
Event::trigger(Event::SERVER_WORKER_STOP);
|
||||
|
||||
fire(Event::SYSTEM_RESOURCE_CLEAN);
|
||||
$this->eventDispatch->dispatch(new OnWorkerStop($server, $workerId));
|
||||
|
||||
Timer::clearAll();
|
||||
}
|
||||
@@ -147,7 +153,7 @@ class ServerWorker extends \Server\Abstracts\Server
|
||||
|
||||
putenv('state=exit');
|
||||
|
||||
Event::trigger(Event::SERVER_WORKER_EXIT, [$server, $workerId]);
|
||||
$this->eventDispatch->dispatch(new OnWorkerExit($server, $workerId));
|
||||
|
||||
Snowflake::getApp('logger')->insert();
|
||||
}
|
||||
@@ -165,7 +171,7 @@ class ServerWorker extends \Server\Abstracts\Server
|
||||
{
|
||||
$this->runEvent(Constant::WORKER_ERROR, null, [$server, $worker_id, $worker_pid, $exit_code, $signal]);
|
||||
|
||||
Event::trigger(Event::SERVER_WORKER_ERROR);
|
||||
$this->eventDispatch->dispatch(new OnWorkerError($server, $worker_id, $worker_pid, $exit_code, $signal));
|
||||
|
||||
$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)
|
||||
|
||||
Reference in New Issue
Block a user