diff --git a/HttpServer/Events/OnPacket.php b/HttpServer/Events/OnPacket.php index e6c1e90d..517479dc 100644 --- a/HttpServer/Events/OnPacket.php +++ b/HttpServer/Events/OnPacket.php @@ -6,11 +6,11 @@ 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 @@ -28,35 +28,6 @@ class OnPacket extends Callback public ?Closure $pack = null; - /** - * @param $data - * @return mixed - * @throws Exception - */ - public function pack($data) - { - $callback = $this->pack; - if (is_callable($callback, true)) { - return $callback($data); - } - return JSON::encode($data); - } - - - /** - * @param $data - * @return mixed - */ - public function unpack($data) - { - $callback = $this->unpack; - if (is_callable($callback, true)) { - return $callback($data); - } - return JSON::decode($data); - } - - /** * @param Server $server * @param $data @@ -64,17 +35,18 @@ class OnPacket extends Callback * @return mixed * @throws Exception */ - public function onHandler(Server $server,string $data,array $clientInfo) + public function onHandler(Server $server, string $data, array $clientInfo) { try { $client = [$clientInfo['address'], $clientInfo['port']]; - if (empty($data = $this->unpack($data))) { + $data = DataResolve::unpack($this->unpack, $clientInfo['address'], $clientInfo['port'], $data); + if (empty($data)) { throw new Exception('Format error.'); } - $client[] = $this->pack($data); + $client[] = DataResolve::pack($this->pack, $data); return $server->sendto(...$client); } catch (\Throwable $exception) { - $client[] = $this->pack(['message' => $exception->getMessage()]); + $client[] = DataResolve::pack($this->pack,['message' => $exception->getMessage()]); return $server->sendto(...$client); } finally { $event = Snowflake::app()->event; diff --git a/HttpServer/Events/OnReceive.php b/HttpServer/Events/OnReceive.php index 277bb9e1..a348afd7 100644 --- a/HttpServer/Events/OnReceive.php +++ b/HttpServer/Events/OnReceive.php @@ -5,12 +5,12 @@ namespace HttpServer\Events; use HttpServer\Abstracts\Callback; -use Snowflake\Core\JSON; use Snowflake\Event; use Snowflake\Snowflake; use Swoole\Server; use Exception; use Closure; +use HttpServer\Events\Utility\DataResolve; /** * Class OnReceive @@ -28,35 +28,6 @@ class OnReceive extends Callback public ?Closure $pack = null; - /** - * @param $data - * @return mixed - * @throws Exception - */ - public function pack($data) - { - $callback = $this->pack; - if (is_callable($callback, true)) { - return $callback($data); - } - return JSON::encode($data); - } - - - /** - * @param $data - * @return mixed - */ - public function unpack($data) - { - $callback = $this->unpack; - if (is_callable($callback, true)) { - return $callback($data); - } - return JSON::decode($data); - } - - /** * @param Server $server * @param int $fd @@ -65,17 +36,18 @@ class OnReceive extends Callback * @return mixed * @throws Exception */ - public function onHandler(\Swoole\Server $server, int $fd,int $reID,string $data) + public function onHandler(\Swoole\Server $server, int $fd, int $reID, string $data) { try { $client = [$fd]; - if (empty($data = $this->unpack($data))) { + $data = DataResolve::unpack($this->unpack, null, null, $data); + if (empty($data)) { throw new Exception('Format error.'); } - $client[] = $this->pack($data); + $client[] = DataResolve::pack($this->pack, $data); return $server->send(...$client); } catch (\Throwable $exception) { - $client[] = $this->pack(['message' => $exception->getMessage()]); + $client[] = DataResolve::pack($this->pack, ['message' => $exception->getMessage()]); return $server->send(...$client); } finally { $event = Snowflake::app()->event; diff --git a/HttpServer/Events/Utility/DataResolve.php b/HttpServer/Events/Utility/DataResolve.php new file mode 100644 index 00000000..6d1f1d7c --- /dev/null +++ b/HttpServer/Events/Utility/DataResolve.php @@ -0,0 +1,79 @@ +get($callback[0]); + } + if (!empty($address) && !empty($port)) { + return call_user_func($callback, $address, $port, $data); + } + return call_user_func($callback, $data); + } + +} diff --git a/function.php b/function.php index a303a5b2..4d8a9b83 100644 --- a/function.php +++ b/function.php @@ -201,7 +201,7 @@ if (!function_exists('fire')) { * @throws ComponentException * @throws Exception */ - function fire(string $event,array $params = []) + function fire(string $event, array $params = []) { $logger = Snowflake::app()->getEvent(); $logger->trigger($event, $params); @@ -584,3 +584,35 @@ if (!function_exists('jTraceEx')) { } + + +if (!function_exists('swoole_substr_json_decode')) { + + + /** + * @param $packet + * @param int $length + * @return mixed + */ + function swoole_substr_json_decode($packet, $length = 0) + { + return json_decode($packet, true); + } + +} + + +if (!function_exists('swoole_substr_unserialize')) { + + + /** + * @param $packet + * @param int $length + * @return mixed + */ + function swoole_substr_unserialize($packet, $length = 0) + { + return unserialize($packet); + } + +}