2020-09-02 11:38:47 +08:00
|
|
|
<?php
|
2020-10-29 18:17:25 +08:00
|
|
|
declare(strict_types=1);
|
2020-09-02 11:38:47 +08:00
|
|
|
|
|
|
|
|
namespace HttpServer\Events;
|
|
|
|
|
|
|
|
|
|
|
2020-12-15 14:04:02 +08:00
|
|
|
use Annotation\Route\Socket;
|
2020-09-04 01:05:33 +08:00
|
|
|
use HttpServer\Abstracts\Callback;
|
2021-03-03 14:10:53 +08:00
|
|
|
use HttpServer\Http\Request;
|
2020-12-15 14:04:02 +08:00
|
|
|
use HttpServer\Route\Node;
|
2021-02-20 16:23:23 +08:00
|
|
|
use Snowflake\Abstracts\Config;
|
2020-09-02 11:38:47 +08:00
|
|
|
use Snowflake\Event;
|
2020-11-27 14:57:58 +08:00
|
|
|
use Snowflake\Exception\ComponentException;
|
2021-03-03 14:10:53 +08:00
|
|
|
use Snowflake\Exception\ConfigException;
|
|
|
|
|
use Snowflake\Exception\NotFindClassException;
|
2020-09-02 11:38:47 +08:00
|
|
|
use Snowflake\Snowflake;
|
2020-11-27 14:52:04 +08:00
|
|
|
use Swoole\Coroutine;
|
2020-09-02 11:38:47 +08:00
|
|
|
use Swoole\Server;
|
|
|
|
|
use Exception;
|
|
|
|
|
use Swoole\WebSocket\Server as WServer;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class OnClose
|
|
|
|
|
* @package HttpServer\Events
|
2020-11-06 16:48:57 +08:00
|
|
|
*
|
2020-09-02 11:38:47 +08:00
|
|
|
*/
|
|
|
|
|
class OnClose extends Callback
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Server $server
|
|
|
|
|
* @param int $fd
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function onHandler(Server $server, int $fd)
|
2020-11-27 14:57:58 +08:00
|
|
|
{
|
|
|
|
|
$this->execute($server, $fd);
|
2021-02-20 15:21:42 +08:00
|
|
|
fire(Event::SYSTEM_RESOURCE_RELEASES);
|
2020-11-27 14:57:58 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Server $server
|
|
|
|
|
* @param int $fd
|
|
|
|
|
* @throws ComponentException
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2020-12-15 14:04:02 +08:00
|
|
|
private function execute(Server $server, int $fd): void
|
2020-09-02 11:38:47 +08:00
|
|
|
{
|
|
|
|
|
try {
|
2021-03-03 14:10:53 +08:00
|
|
|
$this->loadNode($server, $fd);
|
2020-09-02 11:38:47 +08:00
|
|
|
} catch (\Throwable $exception) {
|
2020-11-02 14:45:11 +08:00
|
|
|
$this->addError($exception);
|
2020-09-02 11:38:47 +08:00
|
|
|
} finally {
|
2020-09-03 11:39:20 +08:00
|
|
|
$logger = Snowflake::app()->getLogger();
|
2020-09-02 11:38:47 +08:00
|
|
|
$logger->insert();
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-03-03 14:10:53 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param $server
|
|
|
|
|
* @param $fd
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws ComponentException
|
|
|
|
|
* @throws ConfigException
|
|
|
|
|
* @throws NotFindClassException
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
private function loadNode($server, $fd): mixed
|
|
|
|
|
{
|
|
|
|
|
$query = Request::socketQuery((object)['fd' => $fd], Socket::CLOSE);
|
|
|
|
|
if (($node = router()->find_path($query)) !== null) {
|
|
|
|
|
return $node->dispatch($server, $fd);
|
|
|
|
|
}
|
|
|
|
|
return null;
|
|
|
|
|
}
|
2020-09-02 11:38:47 +08:00
|
|
|
}
|