modify
This commit is contained in:
@@ -25,12 +25,10 @@ class OnWorkerExit extends Callback
|
||||
public function onHandler($server, $worker_id)
|
||||
{
|
||||
putenv('state=exit');
|
||||
$channel = Snowflake::app()->getChannel();
|
||||
$channel->cleanAll();
|
||||
|
||||
Event::trigger(Event::SERVER_WORKER_EXIT);
|
||||
Event::trigger(Event::SERVER_WORKER_EXIT, [$server, $worker_id]);
|
||||
|
||||
logger()->insert();
|
||||
}
|
||||
Snowflake::getApp('logger')->insert();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -26,9 +26,6 @@ class OnWorkerStop extends Callback
|
||||
{
|
||||
Event::trigger(Event::SERVER_WORKER_STOP);
|
||||
|
||||
$this->clearMysqlClient();
|
||||
$this->clearRedisClient();
|
||||
|
||||
fire(Event::SYSTEM_RESOURCE_CLEAN);
|
||||
|
||||
Timer::clearAll();
|
||||
|
||||
+636
-623
File diff suppressed because it is too large
Load Diff
+18
-35
@@ -276,7 +276,7 @@ class Server extends HttpService
|
||||
|
||||
/**
|
||||
* @param $config
|
||||
* @return \Swoole\Server|Packet|Receive|Http|Websocket|null
|
||||
* @return mixed
|
||||
* @throws ConfigException
|
||||
* @throws Exception
|
||||
*/
|
||||
@@ -288,7 +288,7 @@ class Server extends HttpService
|
||||
}
|
||||
$server = $this->dispatchCreate($config, $settings);
|
||||
if (isset($config['events'])) {
|
||||
$this->createEventListen($config);
|
||||
$this->createEventListen($server, $config);
|
||||
}
|
||||
return $server;
|
||||
}
|
||||
@@ -298,33 +298,26 @@ class Server extends HttpService
|
||||
* @param $config
|
||||
* @throws Exception
|
||||
*/
|
||||
protected function createEventListen($config)
|
||||
protected function createEventListen($server, $config)
|
||||
{
|
||||
if (!is_array($config['events'])) {
|
||||
return;
|
||||
}
|
||||
foreach ($config['events'] as $name => $_event) {
|
||||
if ($name !== Event::SERVER_CLIENT_CLOSE) {
|
||||
Event::on('listen ' . $config['port'] . ' ' . $name, $_event);
|
||||
} else {
|
||||
Event::on($name, $_event);
|
||||
}
|
||||
$server->on($name, $_event);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param $config
|
||||
* @param $settings
|
||||
* @return \Swoole\Server|Packet|Receive|Http|Websocket|null
|
||||
* @return mixed
|
||||
* @throws Exception
|
||||
*/
|
||||
private function dispatchCreate($config, $settings): \Swoole\Server|Packet|Receive|Http|Websocket|null
|
||||
private function dispatchCreate($config, $settings): mixed
|
||||
{
|
||||
if (Snowflake::port_already($config['port'])) {
|
||||
return $this->error_stop($config['host'], $config['port']);
|
||||
}
|
||||
if (!($this->swoole instanceof \Swoole\Server)) {
|
||||
return $this->parseServer($config, $settings);
|
||||
$this->parseServer($config, $settings);
|
||||
}
|
||||
return $this->addListener($config);
|
||||
}
|
||||
@@ -405,7 +398,17 @@ class Server extends HttpService
|
||||
*/
|
||||
private function onListenerBind($server, $config): Packet|Websocket|Receive|Http|null
|
||||
{
|
||||
$this->bindServerEvent($server, $config['type']);
|
||||
if (self::PACKAGE == $config['type']) {
|
||||
$this->onBindCallback($server, 'packet', $config['events'][Event::SERVER_ON_PACKET] ?? [make(OnPacket::class), 'onHandler']);
|
||||
} else if ($config['type'] == self::TCP) {
|
||||
$this->onBindCallback($server, 'connect', $config['events'][Event::SERVER_ON_CONNECT] ?? [make(OnConnect::class), 'onHandler']);
|
||||
$this->onBindCallback($server, 'close', $config['events'][Event::SERVER_ON_CLOSE] ?? [make(OnClose::class), 'onHandler']);
|
||||
$this->onBindCallback($server, 'receive', $config['events'][Event::SERVER_ON_RECEIVE] ?? [make(OnReceive::class), 'onHandler']);
|
||||
} else if ($config['type'] === self::HTTP) {
|
||||
$this->onBindCallback($server, 'request', $config['events'][Event::SERVER_ON_REQUEST] ?? [make(OnRequest::class), 'onHandler']);
|
||||
} else {
|
||||
throw new Exception('Unknown server type(' . $config['type'] . ').');
|
||||
}
|
||||
|
||||
$this->debug(sprintf('Check listen %s::%d -> ok', $config['host'], $config['port']));
|
||||
|
||||
@@ -413,26 +416,6 @@ class Server extends HttpService
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param string $type
|
||||
* @throws Exception
|
||||
*/
|
||||
private function bindServerEvent($server, $type = self::TCP)
|
||||
{
|
||||
if (self::PACKAGE == $type) {
|
||||
$this->onBindCallback($server, 'packet', [make(OnPacket::class), 'onHandler']);
|
||||
} else if ($type == self::TCP) {
|
||||
$this->onBindCallback($server, 'connect', [make(OnConnect::class), 'onHandler']);
|
||||
$this->onBindCallback($server, 'close', [make(OnClose::class), 'onHandler']);
|
||||
$this->onBindCallback($server, 'receive', [make(OnReceive::class), 'onHandler']);
|
||||
} else if ($type === self::HTTP) {
|
||||
$this->onBindCallback($server, 'request', [make(OnRequest::class), 'onHandler']);
|
||||
} else {
|
||||
throw new Exception('Unknown server type(' . $type . ').');
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $name
|
||||
* @param $callback
|
||||
|
||||
@@ -10,85 +10,113 @@ use Snowflake\Application;
|
||||
use Snowflake\Exception\NotFindClassException;
|
||||
use Snowflake\Snowflake;
|
||||
|
||||
|
||||
/**
|
||||
* Trait Server
|
||||
* @package HttpServer\Service\Abstracts
|
||||
*/
|
||||
trait Server
|
||||
{
|
||||
|
||||
public ?Application $application = null;
|
||||
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);
|
||||
}
|
||||
/**
|
||||
* 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();
|
||||
}
|
||||
/**
|
||||
* @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('WorkerStop', $this->createHandler('workerStop'));
|
||||
$this->on('WorkerExit', $this->createHandler('workerExit'));
|
||||
$this->on('WorkerStart', $this->createHandler('workerStart'));
|
||||
$this->on('WorkerError', $this->createHandler('workerError'));
|
||||
$this->on('ManagerStart', $this->createHandler('managerStart'));
|
||||
$this->on('ManagerStop', $this->createHandler('managerStop'));
|
||||
$this->on('PipeMessage', $this->createHandler('pipeMessage'));
|
||||
$this->on('Shutdown', $this->createHandler('shutdown'));
|
||||
$this->on('Start', $this->createHandler('start'));
|
||||
$this->addTask();
|
||||
}
|
||||
/**
|
||||
* @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 NotFindClassException
|
||||
* @throws ReflectionException
|
||||
*/
|
||||
protected function addTask()
|
||||
{
|
||||
$settings = $this->setting;
|
||||
if (($taskNumber = $settings['task_worker_num'] ?? 0) > 0) {
|
||||
$this->on('Finish', $this->createHandler('finish'));
|
||||
$this->on('Task', $this->createHandler('task'));
|
||||
}
|
||||
}
|
||||
/**
|
||||
* @throws \ReflectionException
|
||||
* @throws \Snowflake\Exception\NotFindClassException
|
||||
*/
|
||||
private function onManager()
|
||||
{
|
||||
$this->on('ManagerStart', $this->createHandler('managerStart'));
|
||||
$this->on('ManagerStop', $this->createHandler('managerStop'));
|
||||
|
||||
$this->onWorker();
|
||||
$this->onOther();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @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 = Snowflake::createObject($classPrefix, [Snowflake::app()]);
|
||||
return [$class, 'onHandler'];
|
||||
}
|
||||
/**
|
||||
* @throws \ReflectionException
|
||||
* @throws \Snowflake\Exception\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 \Snowflake\Exception\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 = Snowflake::createObject($classPrefix, [Snowflake::app()]);
|
||||
return [$class, 'onHandler'];
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
@@ -33,9 +33,7 @@ class Packet extends Tcp
|
||||
*/
|
||||
public function onBaseListener()
|
||||
{
|
||||
$this->on('connect', $this->createHandler('connect'));
|
||||
$this->on('packet', $this->createHandler('packet'));
|
||||
$this->on('close', $this->createHandler('close'));
|
||||
}
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user