From b9ab9a869d26ad8d5eafd42cc6a24b20b6e41fb0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Thu, 12 Aug 2021 15:39:33 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HttpServer/Abstracts/Callback.php | 108 --------------- HttpServer/Abstracts/ServerBase.php | 40 ------ HttpServer/Events/OnAfterReload.php | 31 ----- HttpServer/Events/OnBeforeReload.php | 33 ----- HttpServer/Events/OnClose.php | 38 ----- HttpServer/Events/OnConnect.php | 43 ------ HttpServer/Events/OnFinish.php | 38 ----- HttpServer/Events/OnHandshake.php | 135 ------------------ HttpServer/Events/OnManagerStart.php | 37 ----- HttpServer/Events/OnManagerStop.php | 31 ----- HttpServer/Events/OnMessage.php | 44 ------ HttpServer/Events/OnPacket.php | 48 ------- HttpServer/Events/OnPipeMessage.php | 85 ------------ HttpServer/Events/OnReceive.php | 50 ------- HttpServer/Events/OnRequest.php | 112 --------------- HttpServer/Events/OnShutdown.php | 34 ----- HttpServer/Events/OnStart.php | 33 ----- HttpServer/Events/OnTask.php | 154 --------------------- HttpServer/Events/OnWorkerError.php | 42 ------ HttpServer/Events/OnWorkerExit.php | 34 ----- HttpServer/Events/OnWorkerStart.php | 115 --------------- HttpServer/Events/OnWorkerStop.php | 34 ----- HttpServer/Server.php | 63 +-------- HttpServer/Service/Abstracts/Http.php | 17 --- HttpServer/Service/Abstracts/Server.php | 122 ---------------- HttpServer/Service/Abstracts/Tcp.php | 16 --- HttpServer/Service/Abstracts/Websocket.php | 16 --- HttpServer/Service/Http.php | 35 ----- HttpServer/Service/Packet.php | 40 ------ HttpServer/Service/Receive.php | 42 ------ HttpServer/Service/Websocket.php | 42 ------ Server/ServerManager.php | 6 + 32 files changed, 8 insertions(+), 1710 deletions(-) delete mode 100644 HttpServer/Abstracts/Callback.php delete mode 100644 HttpServer/Abstracts/ServerBase.php delete mode 100644 HttpServer/Events/OnAfterReload.php delete mode 100644 HttpServer/Events/OnBeforeReload.php delete mode 100644 HttpServer/Events/OnClose.php delete mode 100644 HttpServer/Events/OnConnect.php delete mode 100644 HttpServer/Events/OnFinish.php delete mode 100644 HttpServer/Events/OnHandshake.php delete mode 100644 HttpServer/Events/OnManagerStart.php delete mode 100644 HttpServer/Events/OnManagerStop.php delete mode 100644 HttpServer/Events/OnMessage.php delete mode 100644 HttpServer/Events/OnPacket.php delete mode 100644 HttpServer/Events/OnPipeMessage.php delete mode 100644 HttpServer/Events/OnReceive.php delete mode 100644 HttpServer/Events/OnRequest.php delete mode 100644 HttpServer/Events/OnShutdown.php delete mode 100644 HttpServer/Events/OnStart.php delete mode 100644 HttpServer/Events/OnTask.php delete mode 100644 HttpServer/Events/OnWorkerError.php delete mode 100644 HttpServer/Events/OnWorkerExit.php delete mode 100644 HttpServer/Events/OnWorkerStart.php delete mode 100644 HttpServer/Events/OnWorkerStop.php delete mode 100644 HttpServer/Service/Abstracts/Http.php delete mode 100644 HttpServer/Service/Abstracts/Server.php delete mode 100644 HttpServer/Service/Abstracts/Tcp.php delete mode 100644 HttpServer/Service/Abstracts/Websocket.php delete mode 100644 HttpServer/Service/Http.php delete mode 100644 HttpServer/Service/Packet.php delete mode 100644 HttpServer/Service/Receive.php delete mode 100644 HttpServer/Service/Websocket.php diff --git a/HttpServer/Abstracts/Callback.php b/HttpServer/Abstracts/Callback.php deleted file mode 100644 index 6e246bd5..00000000 --- a/HttpServer/Abstracts/Callback.php +++ /dev/null @@ -1,108 +0,0 @@ - '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(); - } - -} diff --git a/HttpServer/Abstracts/ServerBase.php b/HttpServer/Abstracts/ServerBase.php deleted file mode 100644 index fc710770..00000000 --- a/HttpServer/Abstracts/ServerBase.php +++ /dev/null @@ -1,40 +0,0 @@ -server; - } - - /** - * @param $server - */ - public function setServer($server) - { - $this->server = $server; - } - -} diff --git a/HttpServer/Events/OnAfterReload.php b/HttpServer/Events/OnAfterReload.php deleted file mode 100644 index 587ac283..00000000 --- a/HttpServer/Events/OnAfterReload.php +++ /dev/null @@ -1,31 +0,0 @@ - fire(Event::SYSTEM_RESOURCE_RELEASES)); - - Event::trigger(Event::SERVER_ON_CLOSE, [$server, $fd]); - } catch (\Throwable $exception) { - $this->addError($exception, 'throwable'); - } - } - -} diff --git a/HttpServer/Events/OnConnect.php b/HttpServer/Events/OnConnect.php deleted file mode 100644 index 63c9755f..00000000 --- a/HttpServer/Events/OnConnect.php +++ /dev/null @@ -1,43 +0,0 @@ - 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'); - } - } - - -} diff --git a/HttpServer/Events/OnFinish.php b/HttpServer/Events/OnFinish.php deleted file mode 100644 index 7b7c17a4..00000000 --- a/HttpServer/Events/OnFinish.php +++ /dev/null @@ -1,38 +0,0 @@ - fire(Event::SYSTEM_RESOURCE_RELEASES)); - - fire(Event::TASK_FINISH, [$task_id, $data]); - } catch (\Throwable $exception) { - $this->addError($exception, 'task'); - } - } - -} diff --git a/HttpServer/Events/OnHandshake.php b/HttpServer/Events/OnHandshake.php deleted file mode 100644 index dcbea0ed..00000000 --- a/HttpServer/Events/OnHandshake.php +++ /dev/null @@ -1,135 +0,0 @@ -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)]; - } - - -} diff --git a/HttpServer/Events/OnManagerStart.php b/HttpServer/Events/OnManagerStart.php deleted file mode 100644 index dc6593a2..00000000 --- a/HttpServer/Events/OnManagerStart.php +++ /dev/null @@ -1,37 +0,0 @@ -manager_pid); - - fire(Event::SERVER_MANAGER_START, [$server]); - - name($server->manager_pid, 'manager'); - } - - -} diff --git a/HttpServer/Events/OnManagerStop.php b/HttpServer/Events/OnManagerStop.php deleted file mode 100644 index 8c5e991f..00000000 --- a/HttpServer/Events/OnManagerStop.php +++ /dev/null @@ -1,31 +0,0 @@ -warning('manager stop.'); - - fire(Event::SERVER_MANAGER_STOP, [$server]); - } - -} diff --git a/HttpServer/Events/OnMessage.php b/HttpServer/Events/OnMessage.php deleted file mode 100644 index 70b7b3de..00000000 --- a/HttpServer/Events/OnMessage.php +++ /dev/null @@ -1,44 +0,0 @@ - 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()); - } - } - -} diff --git a/HttpServer/Events/OnPacket.php b/HttpServer/Events/OnPacket.php deleted file mode 100644 index b7c72bf4..00000000 --- a/HttpServer/Events/OnPacket.php +++ /dev/null @@ -1,48 +0,0 @@ - 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); - } - } - - -} diff --git a/HttpServer/Events/OnPipeMessage.php b/HttpServer/Events/OnPipeMessage.php deleted file mode 100644 index 910869b8..00000000 --- a/HttpServer/Events/OnPipeMessage.php +++ /dev/null @@ -1,85 +0,0 @@ - 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'; - } - - -} diff --git a/HttpServer/Events/OnReceive.php b/HttpServer/Events/OnReceive.php deleted file mode 100644 index b9624aef..00000000 --- a/HttpServer/Events/OnReceive.php +++ /dev/null @@ -1,50 +0,0 @@ - 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); - } - } - - -} diff --git a/HttpServer/Events/OnRequest.php b/HttpServer/Events/OnRequest.php deleted file mode 100644 index ebdd9f09..00000000 --- a/HttpServer/Events/OnRequest.php +++ /dev/null @@ -1,112 +0,0 @@ -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); - } - } - -} diff --git a/HttpServer/Events/OnShutdown.php b/HttpServer/Events/OnShutdown.php deleted file mode 100644 index abb8c455..00000000 --- a/HttpServer/Events/OnShutdown.php +++ /dev/null @@ -1,34 +0,0 @@ -debug('server shutdown~'); - - $this->system_mail('server shutdown~'); - - fire(Event::SERVER_SHUTDOWN); - } - -} diff --git a/HttpServer/Events/OnStart.php b/HttpServer/Events/OnStart.php deleted file mode 100644 index 7c833d18..00000000 --- a/HttpServer/Events/OnStart.php +++ /dev/null @@ -1,33 +0,0 @@ -isLinux()) { - name($server->master_pid, 'master'); - } - fire(Event::SERVER_EVENT_START, [$server]); - } - -} diff --git a/HttpServer/Events/OnTask.php b/HttpServer/Events/OnTask.php deleted file mode 100644 index 5f26c522..00000000 --- a/HttpServer/Events/OnTask.php +++ /dev/null @@ -1,154 +0,0 @@ -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(); - } - -} diff --git a/HttpServer/Events/OnWorkerError.php b/HttpServer/Events/OnWorkerError.php deleted file mode 100644 index fe06defd..00000000 --- a/HttpServer/Events/OnWorkerError.php +++ /dev/null @@ -1,42 +0,0 @@ -system_mail($message); - } - -} diff --git a/HttpServer/Events/OnWorkerExit.php b/HttpServer/Events/OnWorkerExit.php deleted file mode 100644 index 2156a6da..00000000 --- a/HttpServer/Events/OnWorkerExit.php +++ /dev/null @@ -1,34 +0,0 @@ -insert(); - } - -} diff --git a/HttpServer/Events/OnWorkerStart.php b/HttpServer/Events/OnWorkerStart.php deleted file mode 100644 index bbeaeaba..00000000 --- a/HttpServer/Events/OnWorkerStart.php +++ /dev/null @@ -1,115 +0,0 @@ -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')]); - } - -} diff --git a/HttpServer/Events/OnWorkerStop.php b/HttpServer/Events/OnWorkerStop.php deleted file mode 100644 index abf3ac68..00000000 --- a/HttpServer/Events/OnWorkerStop.php +++ /dev/null @@ -1,34 +0,0 @@ -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); } diff --git a/HttpServer/Service/Abstracts/Http.php b/HttpServer/Service/Abstracts/Http.php deleted file mode 100644 index 5b9cf5ac..00000000 --- a/HttpServer/Service/Abstracts/Http.php +++ /dev/null @@ -1,17 +0,0 @@ -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']; - } - - -} diff --git a/HttpServer/Service/Abstracts/Tcp.php b/HttpServer/Service/Abstracts/Tcp.php deleted file mode 100644 index 2ed5ef30..00000000 --- a/HttpServer/Service/Abstracts/Tcp.php +++ /dev/null @@ -1,16 +0,0 @@ -onHandlerListener(); - $this->onBaseListener(); - } - - - /** - * @throws NotFindClassException - * @throws ReflectionException - */ - public function onBaseListener() - { - $this->on('request', $this->createHandler('request')); - } - - -} diff --git a/HttpServer/Service/Packet.php b/HttpServer/Service/Packet.php deleted file mode 100644 index 6e3db55e..00000000 --- a/HttpServer/Service/Packet.php +++ /dev/null @@ -1,40 +0,0 @@ -onHandlerListener(); - $this->onBaseListener(); - } - - - /** - * @throws NotFindClassException - * @throws ReflectionException - */ - public function onBaseListener() - { - $this->on('packet', $this->createHandler('packet')); - } - - -} diff --git a/HttpServer/Service/Receive.php b/HttpServer/Service/Receive.php deleted file mode 100644 index 71e7a761..00000000 --- a/HttpServer/Service/Receive.php +++ /dev/null @@ -1,42 +0,0 @@ -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')); - } - - -} diff --git a/HttpServer/Service/Websocket.php b/HttpServer/Service/Websocket.php deleted file mode 100644 index ea95faa7..00000000 --- a/HttpServer/Service/Websocket.php +++ /dev/null @@ -1,42 +0,0 @@ -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')); - } - -} diff --git a/Server/ServerManager.php b/Server/ServerManager.php index 378fce90..20f30c63 100644 --- a/Server/ServerManager.php +++ b/Server/ServerManager.php @@ -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;