2020-08-31 01:27:08 +08:00
|
|
|
<?php
|
2020-10-29 18:17:25 +08:00
|
|
|
declare(strict_types=1);
|
2020-08-31 01:27:08 +08:00
|
|
|
|
2020-09-02 11:38:47 +08:00
|
|
|
namespace HttpServer\Events;
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
|
|
|
|
|
use Exception;
|
2020-09-04 01:05:33 +08:00
|
|
|
use HttpServer\Abstracts\Callback;
|
2020-08-31 01:27:08 +08:00
|
|
|
use Snowflake\Event;
|
|
|
|
|
use Snowflake\Snowflake;
|
|
|
|
|
use Swoole\Server;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class OnConnect
|
2020-09-02 11:38:47 +08:00
|
|
|
* @package HttpServer\Events
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
|
|
|
|
class OnConnect extends Callback
|
|
|
|
|
{
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Server $server
|
|
|
|
|
* @param int $fd
|
|
|
|
|
* @param int $reactorId
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
2021-03-08 18:23:46 +08:00
|
|
|
public function onHandler(Server $server, int $fd, int $reactorId)
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
2021-02-20 13:02:58 +08:00
|
|
|
$event = Snowflake::app()->getEvent();
|
2021-03-08 18:23:46 +08:00
|
|
|
|
|
|
|
|
$clientInfo = $server->getClientInfo($fd);
|
|
|
|
|
|
2021-03-08 18:40:42 +08:00
|
|
|
try {
|
|
|
|
|
$name = 'listen ' . $clientInfo['server_port'] . ' ' . Event::RECEIVE_CONNECTION;
|
2021-03-08 18:23:46 +08:00
|
|
|
|
2021-03-08 18:40:42 +08:00
|
|
|
$event->trigger($name, [$server, $fd, $reactorId]);
|
|
|
|
|
} catch (\Throwable $throwable) {
|
|
|
|
|
$this->addError($throwable, 'connect');
|
|
|
|
|
}
|
2021-03-05 14:55:50 +08:00
|
|
|
|
|
|
|
|
fire(Event::SYSTEM_RESOURCE_RELEASES);
|
|
|
|
|
logger()->insert();
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
}
|