61 lines
1.3 KiB
PHP
61 lines
1.3 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace HttpServer\Events;
|
|
|
|
|
|
use Closure;
|
|
use HttpServer\Abstracts\Callback;
|
|
use Snowflake\Core\JSON;
|
|
use Snowflake\Event;
|
|
use Snowflake\Snowflake;
|
|
use Swoole\Server;
|
|
use Exception;
|
|
use HttpServer\Events\Utility\DataResolve;
|
|
|
|
/**
|
|
* Class OnPacket
|
|
* @package HttpServer\Events
|
|
*/
|
|
class OnPacket extends Callback
|
|
{
|
|
|
|
|
|
/** @var ?Closure */
|
|
public ?Closure $unpack = null;
|
|
|
|
|
|
/** @var ?Closure */
|
|
public ?Closure $pack = null;
|
|
|
|
|
|
/**
|
|
* @param Server $server
|
|
* @param $data
|
|
* @param $clientInfo
|
|
* @return mixed
|
|
* @throws Exception
|
|
*/
|
|
public function onHandler(Server $server, string $data, array $clientInfo)
|
|
{
|
|
try {
|
|
$client = [$clientInfo['address'], $clientInfo['port']];
|
|
$data = DataResolve::unpack($this->unpack, $clientInfo['address'], $clientInfo['port'], $data);
|
|
if (empty($data)) {
|
|
throw new Exception('Format error.');
|
|
}
|
|
$client[] = DataResolve::pack($this->pack, $data);
|
|
return $server->sendto(...$client);
|
|
} catch (\Throwable $exception) {
|
|
$client[] = DataResolve::pack($this->pack, JSON::encode(['message' => $exception->getMessage()]));
|
|
var_dump($client);
|
|
return $server->sendto(...$client);
|
|
} finally {
|
|
$event = Snowflake::app()->event;
|
|
$event->trigger(Event::SERVER_WORKER_STOP);
|
|
}
|
|
}
|
|
|
|
|
|
}
|