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
|
|
|
|
|
|
|
|
namespace HttpServer\Events;
|
|
|
|
|
|
|
|
|
|
|
2021-04-07 14:15:50 +08:00
|
|
|
use Exception;
|
2020-09-04 01:05:33 +08:00
|
|
|
use HttpServer\Abstracts\Callback;
|
2021-08-11 01:04:57 +08:00
|
|
|
use Kiri\Core\Json;
|
|
|
|
|
use Kiri\Event;
|
2020-08-31 01:27:08 +08:00
|
|
|
use Swoole\Server;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Class OnPacket
|
2020-09-02 11:38:47 +08:00
|
|
|
* @package HttpServer\Events
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
2020-09-02 11:38:47 +08:00
|
|
|
class OnPacket extends Callback
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param Server $server
|
2021-06-25 11:20:38 +08:00
|
|
|
* @param string $data
|
|
|
|
|
* @param array $client
|
2020-08-31 01:27:08 +08:00
|
|
|
* @return mixed
|
2020-11-01 04:47:05 +08:00
|
|
|
* @throws Exception
|
2020-08-31 01:27:08 +08:00
|
|
|
*/
|
2021-06-25 11:20:38 +08:00
|
|
|
public function onHandler(Server $server, string $data, array $client): mixed
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
try {
|
2021-06-25 11:20:38 +08:00
|
|
|
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
|
2021-05-04 01:33:08 +08:00
|
|
|
|
2021-06-25 11:20:38 +08:00
|
|
|
$client['server_port'] = $client['port'];
|
|
|
|
|
$name = $this->getName($client, Event::SERVER_RECEIVE);
|
2021-03-08 16:01:07 +08:00
|
|
|
|
2021-06-25 11:20:38 +08:00
|
|
|
$result = Event::trigger($name, [$server, $data, $client]);
|
2020-08-31 01:27:08 +08:00
|
|
|
} catch (\Throwable $exception) {
|
2021-06-25 11:20:38 +08:00
|
|
|
$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);
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-09-02 11:38:47 +08:00
|
|
|
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|