This commit is contained in:
2021-08-12 14:00:44 +08:00
parent a4056c8341
commit 3bd3a39642
12 changed files with 11 additions and 736 deletions
+1 -5
View File
@@ -5,23 +5,19 @@ namespace Server\Abstracts;
use Annotation\Inject;
use HttpServer\Route\Router;
use Kiri\Abstracts\Config;
use Kiri\Events\EventDispatch;
use Kiri\Exception\ConfigException;
use Kiri\Exception\NotFindClassException;
use Kiri\Kiri;
use ReflectionException;
use Server\Constrict\Response as CResponse;
use Server\Constrict\ResponseEmitter;
use Server\ExceptionHandlerDispatcher;
use Server\ExceptionHandlerInterface;
use Server\ListenerHelper;
use Server\SInterface\OnRequest;
/**
*
*/
abstract class Http implements OnRequest
abstract class Http extends Server implements OnRequest
{
+1 -82
View File
@@ -21,93 +21,12 @@ abstract class Server
{
protected array $_events = [];
protected Event $_event;
/**
* @param $prefix
* @throws ConfigException
*/
protected function setProcessName($prefix)
{
if (Kiri::getPlatform()->isMac()) {
return;
}
$name = Config::get('id', 'system-service');
if (!empty($prefix)) {
$name .= '.' . $prefix;
}
swoole_set_process_name($name);
}
/**
* Server constructor.
* @throws Exception
*/
public function __construct()
public function __construct(protected \Swoole\Server|Port $server)
{
$this->_event = Kiri::getApp('event');
}
/**
* @param string $name
* @param array|null $events
* @throws Exception
*/
public function setEvents(string $name, ?array $events): void
{
if (is_array($events) && is_string($events[0])) {
$events[0] = Kiri::getDi()->get($events[0]);
}
if (!is_callable($events)) {
return;
}
$this->_events[$name] = $events;
}
/**
* @return array
*/
public function getEvents(): array
{
return $this->_events;
}
/**
* @param string $name
* @return mixed
*/
public function getEvent(string $name): mixed
{
return $this->_events[$name] ?? null;
}
/**
* @param $name
* @param Closure|null $closure
* @param array $params
* @return mixed
*/
public function runEvent($name, ?Closure $closure, array $params): void
{
$event = $this->getEvent($name);
if (empty($event)) {
if (!is_callable($closure)) {
return;
}
call_user_func($closure, ...$params);
} else {
call_user_func($event, ...$params);
}
}
}
+1 -1
View File
@@ -11,7 +11,7 @@ use Server\ExceptionHandlerDispatcher;
use Server\ExceptionHandlerInterface;
use Server\SInterface\OnReceive;
abstract class Tcp implements OnReceive
abstract class Tcp extends Server implements OnReceive
{
use EventDispatchHelper;
+5 -1
View File
@@ -12,7 +12,11 @@ use Server\ExceptionHandlerInterface;
use Server\SInterface\OnPacket;
use Server\SInterface\OnReceive;
abstract class Udp implements OnPacket
/**
*
*/
abstract class Udp extends Server implements OnPacket
{
use EventDispatchHelper;
+1 -3
View File
@@ -15,13 +15,12 @@ use Server\SInterface\OnHandshake;
use Server\SInterface\OnMessage;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Server;
/**
*
*/
abstract class Websocket implements OnHandshake, OnMessage, OnClose
abstract class Websocket extends Server implements OnHandshake, OnMessage, OnClose
{
use EventDispatchHelper;
@@ -58,7 +57,6 @@ abstract class Websocket implements OnHandshake, OnMessage, OnClose
public function onHandshake(Request $request, Response $response): void
{
// TODO: Implement OnHandshake() method.
/** @var \Swoole\WebSocket\Server $server */
$secWebSocketKey = $request->header['sec-websocket-key'];
$patten = '#^[+/0-9A-Za-z]{21}[AQgw]==$#';
if (0 === preg_match($patten, $secWebSocketKey) || 16 !== strlen(base64_decode($secWebSocketKey))) {