This commit is contained in:
2020-11-04 19:28:40 +08:00
parent dc2216a8b9
commit d533102a77
4 changed files with 124 additions and 69 deletions
+6 -34
View File
@@ -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;
+6 -34
View File
@@ -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;
+79
View File
@@ -0,0 +1,79 @@
<?php
namespace HttpServer\Events\Utility;
use Closure;
use Exception;
use ReflectionException;
use Snowflake\Core\JSON;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
class DataResolve
{
/**
* @param $unpack
* @param $data
* @return mixed
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
public static function pack($unpack, $data)
{
if (empty($unpack)) {
return JSON::encode($data);
}
return self::callbackResolve($unpack, null, null, $data);
}
/**
* @param $unpack
* @param $address
* @param $port
* @param $data
* @return mixed
* @throws NotFindClassException
* @throws ReflectionException
*/
public static function unpack($unpack, $address, $port, $data)
{
if (empty($unpack)) {
return JSON::decode($data);
}
return self::callbackResolve($unpack, $address, $port, $data);
}
/**
* @param $callback
* @param $address
* @param $port
* @param $data
* @return array|mixed
* @throws NotFindClassException
* @throws ReflectionException
*/
private static function callbackResolve($callback, $address, $port, $data)
{
if ($callback instanceof Closure) {
return $callback($address, $port, $data);
}
if (is_string($callback)) {
$callback = [$callback, 'onHandler'];
}
if (isset($callback[0]) && is_string($callback[0])) {
$callback[0] = Snowflake::getDi()->get($callback[0]);
}
if (!empty($address) && !empty($port)) {
return call_user_func($callback, $address, $port, $data);
}
return call_user_func($callback, $data);
}
}