From 2f5370f730debdd5c748220e49b8d8f0425fbf5b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mr=C2=B7x?= Date: Thu, 5 Nov 2020 18:27:24 +0800 Subject: [PATCH] =?UTF-8?q?=E6=94=B9=E5=90=8D?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HttpServer/Events/OnPacket.php | 10 +++------- HttpServer/Events/Utility/DataResolve.php | 18 ++++++++++++++---- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/HttpServer/Events/OnPacket.php b/HttpServer/Events/OnPacket.php index 9a583940..0e7981f7 100644 --- a/HttpServer/Events/OnPacket.php +++ b/HttpServer/Events/OnPacket.php @@ -43,14 +43,10 @@ class OnPacket extends Callback if (empty($data)) { throw new Exception('Format error.'); } - $client = DataResolve::pack($this->pack, $data); - return $server->sendto($clientInfo['address'], $clientInfo['port'], $client ?? 'Format error'); + return $server->sendto($clientInfo['address'], $clientInfo['port'], DataResolve::pack($this->pack, $data)); } catch (\Throwable $exception) { - $client = DataResolve::pack($this->pack, $exception->getMessage()); - - var_dump($exception->getMessage()); - - return $server->sendto($clientInfo['address'], $clientInfo['port'], $client ?? 'Format error'); + $pack = DataResolve::pack($this->pack, ['message' => $exception->getMessage()]); + return $server->sendto($clientInfo['address'], $clientInfo['port'], $pack); } finally { $event = Snowflake::app()->event; $event->trigger(Event::SERVER_WORKER_STOP); diff --git a/HttpServer/Events/Utility/DataResolve.php b/HttpServer/Events/Utility/DataResolve.php index 6d1f1d7c..12237f37 100644 --- a/HttpServer/Events/Utility/DataResolve.php +++ b/HttpServer/Events/Utility/DataResolve.php @@ -26,9 +26,14 @@ class DataResolve public static function pack($unpack, $data) { if (empty($unpack)) { - return JSON::encode($data); + $params = JSON::encode($data); + } else { + $params = self::callbackResolve($unpack, null, null, $data); } - return self::callbackResolve($unpack, null, null, $data); + if ($params === null) { + return 'Format error.'; + } + return $params; } @@ -44,9 +49,14 @@ class DataResolve public static function unpack($unpack, $address, $port, $data) { if (empty($unpack)) { - return JSON::decode($data); + $params = JSON::decode($data); + } else { + $params = self::callbackResolve($unpack, $address, $port, $data); } - return self::callbackResolve($unpack, $address, $port, $data); + if ($params === null) { + return 'Format error.'; + } + return $params; }