This commit is contained in:
2020-11-05 18:27:24 +08:00
parent 3e3ee5c276
commit 2f5370f730
2 changed files with 17 additions and 11 deletions
+3 -7
View File
@@ -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);
+14 -4
View File
@@ -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;
}