改名
This commit is contained in:
@@ -6,11 +6,11 @@ namespace HttpServer\Events;
|
|||||||
|
|
||||||
use Closure;
|
use Closure;
|
||||||
use HttpServer\Abstracts\Callback;
|
use HttpServer\Abstracts\Callback;
|
||||||
use Snowflake\Core\JSON;
|
|
||||||
use Snowflake\Event;
|
use Snowflake\Event;
|
||||||
use Snowflake\Snowflake;
|
use Snowflake\Snowflake;
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
use Exception;
|
use Exception;
|
||||||
|
use HttpServer\Events\Utility\DataResolve;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class OnPacket
|
* Class OnPacket
|
||||||
@@ -28,35 +28,6 @@ class OnPacket extends Callback
|
|||||||
public ?Closure $pack = null;
|
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 Server $server
|
||||||
* @param $data
|
* @param $data
|
||||||
@@ -64,17 +35,18 @@ class OnPacket extends Callback
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
public function onHandler(Server $server,string $data,array $clientInfo)
|
public function onHandler(Server $server, string $data, array $clientInfo)
|
||||||
{
|
{
|
||||||
try {
|
try {
|
||||||
$client = [$clientInfo['address'], $clientInfo['port']];
|
$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.');
|
throw new Exception('Format error.');
|
||||||
}
|
}
|
||||||
$client[] = $this->pack($data);
|
$client[] = DataResolve::pack($this->pack, $data);
|
||||||
return $server->sendto(...$client);
|
return $server->sendto(...$client);
|
||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
$client[] = $this->pack(['message' => $exception->getMessage()]);
|
$client[] = DataResolve::pack($this->pack,['message' => $exception->getMessage()]);
|
||||||
return $server->sendto(...$client);
|
return $server->sendto(...$client);
|
||||||
} finally {
|
} finally {
|
||||||
$event = Snowflake::app()->event;
|
$event = Snowflake::app()->event;
|
||||||
|
|||||||
@@ -5,12 +5,12 @@ namespace HttpServer\Events;
|
|||||||
|
|
||||||
|
|
||||||
use HttpServer\Abstracts\Callback;
|
use HttpServer\Abstracts\Callback;
|
||||||
use Snowflake\Core\JSON;
|
|
||||||
use Snowflake\Event;
|
use Snowflake\Event;
|
||||||
use Snowflake\Snowflake;
|
use Snowflake\Snowflake;
|
||||||
use Swoole\Server;
|
use Swoole\Server;
|
||||||
use Exception;
|
use Exception;
|
||||||
use Closure;
|
use Closure;
|
||||||
|
use HttpServer\Events\Utility\DataResolve;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class OnReceive
|
* Class OnReceive
|
||||||
@@ -28,35 +28,6 @@ class OnReceive extends Callback
|
|||||||
public ?Closure $pack = null;
|
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 Server $server
|
||||||
* @param int $fd
|
* @param int $fd
|
||||||
@@ -65,17 +36,18 @@ class OnReceive extends Callback
|
|||||||
* @return mixed
|
* @return mixed
|
||||||
* @throws Exception
|
* @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 {
|
try {
|
||||||
$client = [$fd];
|
$client = [$fd];
|
||||||
if (empty($data = $this->unpack($data))) {
|
$data = DataResolve::unpack($this->unpack, null, null, $data);
|
||||||
|
if (empty($data)) {
|
||||||
throw new Exception('Format error.');
|
throw new Exception('Format error.');
|
||||||
}
|
}
|
||||||
$client[] = $this->pack($data);
|
$client[] = DataResolve::pack($this->pack, $data);
|
||||||
return $server->send(...$client);
|
return $server->send(...$client);
|
||||||
} catch (\Throwable $exception) {
|
} catch (\Throwable $exception) {
|
||||||
$client[] = $this->pack(['message' => $exception->getMessage()]);
|
$client[] = DataResolve::pack($this->pack, ['message' => $exception->getMessage()]);
|
||||||
return $server->send(...$client);
|
return $server->send(...$client);
|
||||||
} finally {
|
} finally {
|
||||||
$event = Snowflake::app()->event;
|
$event = Snowflake::app()->event;
|
||||||
|
|||||||
@@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
+33
-1
@@ -201,7 +201,7 @@ if (!function_exists('fire')) {
|
|||||||
* @throws ComponentException
|
* @throws ComponentException
|
||||||
* @throws Exception
|
* @throws Exception
|
||||||
*/
|
*/
|
||||||
function fire(string $event,array $params = [])
|
function fire(string $event, array $params = [])
|
||||||
{
|
{
|
||||||
$logger = Snowflake::app()->getEvent();
|
$logger = Snowflake::app()->getEvent();
|
||||||
$logger->trigger($event, $params);
|
$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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user