This commit is contained in:
as2252258@163.com
2021-07-11 03:57:25 +08:00
parent 27dc21e24e
commit 12d547c1a2
13 changed files with 1038 additions and 816 deletions
+94 -66
View File
@@ -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'];
}
}