2020-08-31 01:27:08 +08:00
|
|
|
<?php
|
2020-10-29 18:17:25 +08:00
|
|
|
declare(strict_types=1);
|
2020-08-31 01:27:08 +08:00
|
|
|
|
|
|
|
|
namespace HttpServer\Events;
|
|
|
|
|
|
|
|
|
|
|
2021-04-07 14:10:21 +08:00
|
|
|
use Exception;
|
2020-09-04 01:05:33 +08:00
|
|
|
use HttpServer\Abstracts\Callback;
|
2021-03-08 16:01:07 +08:00
|
|
|
use HttpServer\Http\Request;
|
2021-04-07 14:07:18 +08:00
|
|
|
use HttpServer\Route\Router;
|
2021-04-07 14:10:21 +08:00
|
|
|
use ReflectionException;
|
2021-03-08 16:01:07 +08:00
|
|
|
use Snowflake\Core\Json;
|
2020-08-31 01:27:08 +08:00
|
|
|
use Snowflake\Event;
|
2021-04-07 14:10:21 +08:00
|
|
|
use Snowflake\Exception\NotFindClassException;
|
2020-08-31 01:27:08 +08:00
|
|
|
use Snowflake\Snowflake;
|
|
|
|
|
use Swoole\Server;
|
|
|
|
|
|
|
|
|
|
/**
|
2020-09-02 11:38:47 +08:00
|
|
|
* Class OnReceive
|
2020-08-31 01:27:08 +08:00
|
|
|
* @package HttpServer\Events
|
|
|
|
|
*/
|
2020-09-02 11:38:47 +08:00
|
|
|
class OnReceive extends Callback
|
2020-08-31 01:27:08 +08:00
|
|
|
{
|
|
|
|
|
|
2021-04-24 17:40:34 +08:00
|
|
|
public int $port = 0;
|
2020-09-02 11:38:47 +08:00
|
|
|
|
|
|
|
|
|
2021-04-24 17:40:34 +08:00
|
|
|
public string $host = '';
|
2020-09-02 11:38:47 +08:00
|
|
|
|
|
|
|
|
|
2021-04-24 17:40:34 +08:00
|
|
|
private Router $router;
|
2021-04-07 14:07:18 +08:00
|
|
|
|
|
|
|
|
|
2021-04-24 17:40:34 +08:00
|
|
|
public function init()
|
|
|
|
|
{
|
|
|
|
|
$this->router = Snowflake::app()->getRouter();
|
|
|
|
|
}
|
2021-04-07 14:07:18 +08:00
|
|
|
|
|
|
|
|
|
2021-04-24 17:40:34 +08:00
|
|
|
/**
|
|
|
|
|
* @param Server $server
|
|
|
|
|
* @param int $fd
|
|
|
|
|
* @param int $reID
|
|
|
|
|
* @param string $data
|
|
|
|
|
* @return mixed
|
|
|
|
|
* @throws Exception
|
|
|
|
|
*/
|
|
|
|
|
public function onHandler(Server $server, int $fd, int $reID, string $data): mixed
|
|
|
|
|
{
|
|
|
|
|
try {
|
2021-05-04 01:33:08 +08:00
|
|
|
defer(fn() => fire(Event::SYSTEM_RESOURCE_RELEASES));
|
|
|
|
|
|
2021-04-24 17:40:34 +08:00
|
|
|
$request = $this->_request($fd, $data, $reID);
|
|
|
|
|
if (($node = $this->router->find_path($request)) === null) {
|
|
|
|
|
return $server->send($fd, Json::encode(['state' => 404]));
|
|
|
|
|
}
|
|
|
|
|
$dispatch = $node->dispatch();
|
|
|
|
|
if (!is_string($dispatch)) $dispatch = Json::encode($dispatch);
|
|
|
|
|
if (empty($dispatch)) {
|
|
|
|
|
$dispatch = Json::encode(['state' => 0, 'message' => 'ok']);
|
|
|
|
|
}
|
|
|
|
|
if ($server->exist($fd)) {
|
|
|
|
|
return $server->send($fd, $dispatch);
|
|
|
|
|
}
|
|
|
|
|
return $dispatch;
|
|
|
|
|
} catch (\Throwable $exception) {
|
|
|
|
|
$this->addError($exception, 'receive');
|
|
|
|
|
$error = ['state' => 500, 'message' => $exception->getMessage()];
|
|
|
|
|
if ($server->exist($fd)) {
|
|
|
|
|
return $server->send($fd, Json::encode($error));
|
|
|
|
|
}
|
|
|
|
|
return Json::encode($error);
|
|
|
|
|
}
|
|
|
|
|
}
|
2020-08-31 01:27:08 +08:00
|
|
|
|
2021-03-08 16:01:07 +08:00
|
|
|
|
2020-08-31 01:27:08 +08:00
|
|
|
}
|