modify plugin name
This commit is contained in:
+2
-4
@@ -28,16 +28,14 @@
|
||||
"psr/http-server-middleware": "1.0.1",
|
||||
"game-worker/kiri-event": "~v2.0",
|
||||
"game-worker/kiri-container": "~v1.0",
|
||||
"ext-inotify": "*"
|
||||
"ext-inotify": "*",
|
||||
"ext-pcntl": "*"
|
||||
},
|
||||
"autoload": {
|
||||
"psr-4": {
|
||||
"Kiri\\": "kiri-engine/",
|
||||
"Kiri\\Gateway\\": "kiri-gateway/",
|
||||
"Kiri\\Websocket\\": "kiri-websocket-server/",
|
||||
"Gii\\": "kiri-gii/",
|
||||
"Kiri\\Annotation\\": "kiri-annotation/",
|
||||
"Kiri\\Server\\": "kiri-server/",
|
||||
"Kiri\\Task\\": "kiri-task/"
|
||||
},
|
||||
"files": [
|
||||
|
||||
@@ -1,14 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Gateway;
|
||||
|
||||
class Collector
|
||||
{
|
||||
|
||||
|
||||
public function get()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,23 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Gateway;
|
||||
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Http\Response;
|
||||
|
||||
class GatewayServer
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
*/
|
||||
public function onRequest(Request $request, Response $response)
|
||||
{
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
@@ -1,35 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Gateway;
|
||||
|
||||
class HashMap
|
||||
{
|
||||
|
||||
|
||||
const HTTP = 1;
|
||||
const TCP = 2;
|
||||
const UDP = 2;
|
||||
|
||||
|
||||
public string $domain;
|
||||
|
||||
|
||||
public string $path;
|
||||
|
||||
|
||||
public string $scheme;
|
||||
|
||||
|
||||
public string $method;
|
||||
|
||||
|
||||
public string $proxy_host;
|
||||
|
||||
|
||||
public string $proxy_port;
|
||||
|
||||
|
||||
public int $type = self::HTTP;
|
||||
|
||||
|
||||
}
|
||||
@@ -1,56 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Websocket;
|
||||
|
||||
use JetBrains\PhpStorm\Pure;
|
||||
use Kiri\Annotation\Inject;
|
||||
use Kiri\Core\HashMap;
|
||||
use Swoole\Http\Response;
|
||||
|
||||
class FdCollector
|
||||
{
|
||||
|
||||
|
||||
#[Inject(HashMap::class)]
|
||||
public HashMap $fds;
|
||||
|
||||
|
||||
/**
|
||||
* @param int $fd
|
||||
* @param Response $response
|
||||
* @return void
|
||||
*/
|
||||
public function set(int $fd, Response $response)
|
||||
{
|
||||
$this->fds->put('fd_' . $fd, $response);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $fd
|
||||
* @return bool
|
||||
*/
|
||||
#[Pure] public function has(int $fd): bool
|
||||
{
|
||||
return $this->fds->has('fd_' . $fd);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $fd
|
||||
* @return ?Response
|
||||
*/
|
||||
#[Pure] public function get(int $fd): ?Response
|
||||
{
|
||||
return $this->fds->get('fd_' . $fd);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $fd
|
||||
* @return void
|
||||
*/
|
||||
public function remove(int $fd)
|
||||
{
|
||||
$this->fds->del('fd_' . $fd);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,136 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Websocket;
|
||||
|
||||
use Kiri;
|
||||
use Swoole\{Coroutine\Http\Server as AliasServer, WebSocket\Server};
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
class Sender extends Kiri\Abstracts\Component implements WebSocketInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @var AliasServer|Server|null
|
||||
*/
|
||||
private AliasServer|Server|null $server = null;
|
||||
|
||||
|
||||
private FdCollector $collector;
|
||||
|
||||
|
||||
/**
|
||||
* @return void
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$this->collector = $this->getContainer()->get(FdCollector::class);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param AliasServer|Server $server
|
||||
*/
|
||||
public function setServer(mixed $server): void
|
||||
{
|
||||
$this->server = $server;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $fd
|
||||
* @param mixed $data
|
||||
* @param int $opcode
|
||||
* @param int $flags
|
||||
* @return bool
|
||||
*/
|
||||
public function push(int $fd, string $data, int $opcode = WEBSOCKET_OPCODE_TEXT, int $flags = SWOOLE_WEBSOCKET_FLAG_FIN): bool
|
||||
{
|
||||
if (!$this->isEstablished($fd)) {
|
||||
return false;
|
||||
}
|
||||
if ($this->server instanceof Server) {
|
||||
return $this->server->push($fd, $data, $opcode, $flags);
|
||||
}
|
||||
|
||||
$response = $this->collector->get($fd);
|
||||
if (!empty($response)) {
|
||||
return $response->push($data, $opcode, $flags);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param $fd
|
||||
* @param $reactor_id
|
||||
* @return array|null
|
||||
*/
|
||||
public function connection_info($fd, $reactor_id = null): ?array
|
||||
{
|
||||
if ($this->server instanceof Server) {
|
||||
return $this->server->getClientInfo($fd, $reactor_id);
|
||||
}
|
||||
if ($this->server->exist($fd)) {
|
||||
return ['websocket_status' => 1];
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $fd
|
||||
* @param int $code
|
||||
* @param string $reason
|
||||
* @return bool
|
||||
*/
|
||||
public function disconnect(int $fd, int $code = SWOOLE_WEBSOCKET_CLOSE_NORMAL, string $reason = ''): bool
|
||||
{
|
||||
if (!$this->isEstablished($fd)) {
|
||||
return false;
|
||||
}
|
||||
if ($this->server instanceof Server) {
|
||||
return $this->server->disconnect($fd, $code, $reason);
|
||||
}
|
||||
return $this->server->close($fd, $reason);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $fd
|
||||
* @return bool
|
||||
*/
|
||||
public function isEstablished(int $fd): bool
|
||||
{
|
||||
if (!$this->exist($fd)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if ($this->server instanceof Server) {
|
||||
return $this->server->isEstablished($fd);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $fd
|
||||
* @return bool
|
||||
*/
|
||||
public function exist(int $fd): bool
|
||||
{
|
||||
if ($this->server instanceof Server) {
|
||||
return $this->server->exist($fd);
|
||||
}
|
||||
$collector = Kiri::getContainer()->get(FdCollector::class);
|
||||
return $collector->has($fd);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,139 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Websocket;
|
||||
|
||||
use Exception;
|
||||
use Kiri\Abstracts\AbstractServer;
|
||||
use Kiri\Message\Handler\DataGrip;
|
||||
use Kiri\Message\Handler\RouterCollector;
|
||||
use Kiri\Server\Contract\OnCloseInterface;
|
||||
use Kiri\Server\Contract\OnHandshakeInterface;
|
||||
use Kiri\Server\Contract\OnMessageInterface;
|
||||
use Kiri\Server\Contract\OnOpenInterface;
|
||||
use Kiri\Server\SwooleServerInterface;
|
||||
use Psr\Container\ContainerExceptionInterface;
|
||||
use Psr\Container\NotFoundExceptionInterface;
|
||||
use Swoole\Http\Request;
|
||||
use Swoole\Http\Response;
|
||||
use Swoole\WebSocket\Frame;
|
||||
|
||||
|
||||
/**
|
||||
* websocket server
|
||||
*/
|
||||
class Server extends AbstractServer
|
||||
{
|
||||
|
||||
public RouterCollector $router;
|
||||
|
||||
|
||||
const SHA1_KEY = '258EAFA5-E914-47DA-95CA-C5AB0DC85B11';
|
||||
|
||||
|
||||
public mixed $callback = null;
|
||||
|
||||
|
||||
public Sender $sender;
|
||||
|
||||
|
||||
public FdCollector $collector;
|
||||
|
||||
|
||||
/**
|
||||
* @throws ContainerExceptionInterface
|
||||
* @throws NotFoundExceptionInterface
|
||||
*/
|
||||
public function init()
|
||||
{
|
||||
$container = $this->getContainer();
|
||||
|
||||
$this->router = $container->get(DataGrip::class)->get('ws');
|
||||
$handler = $this->router->find('/', 'GET');
|
||||
|
||||
$this->collector = $container->get(FdCollector::class);
|
||||
|
||||
$this->sender = $container->get(Sender::class);
|
||||
if ($container->has(SwooleServerInterface::class)) {
|
||||
$this->sender->setServer($container->get(SwooleServerInterface::class));
|
||||
}
|
||||
if (is_int($handler) || is_null($handler)) {
|
||||
return;
|
||||
}
|
||||
$this->callback = $handler->callback[0];
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param int $fd
|
||||
*/
|
||||
public function onClose(int $fd): void
|
||||
{
|
||||
$this->collector->remove($fd);
|
||||
if (!$this->sender->isEstablished($fd)) {
|
||||
return;
|
||||
}
|
||||
if ($this->callback instanceof OnCloseInterface) {
|
||||
$this->callback->onClose($fd);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Request $request
|
||||
* @param Response $response
|
||||
*/
|
||||
public function onHandshake(Request $request, Response $response): void
|
||||
{
|
||||
try {
|
||||
$secWebSocketKey = $request->header['sec-websocket-key'];
|
||||
$patten = '#^[+/0-9A-Za-z]{21}[AQgw]==$#';
|
||||
if (preg_match($patten, $secWebSocketKey) === 0 || strlen(base64_decode($secWebSocketKey)) !== 16) {
|
||||
throw new Exception('protocol error.', 500);
|
||||
}
|
||||
$key = base64_encode(sha1($request->header['sec-websocket-key'] . self::SHA1_KEY, true));
|
||||
$headers = [
|
||||
'Upgrade' => 'websocket',
|
||||
'Connection' => 'Upgrade',
|
||||
'Sec-Websocket-Accept' => $key,
|
||||
'Sec-Websocket-Version' => '13',
|
||||
];
|
||||
if (isset($request->header['sec-websocket-protocol'])) {
|
||||
$headers['Sec-Websocket-Protocol'] = $request->header['sec-websocket-protocol'];
|
||||
}
|
||||
foreach ($headers as $key => $val) {
|
||||
$response->header($key, $val);
|
||||
}
|
||||
|
||||
if ($this->callback instanceof OnHandshakeInterface) {
|
||||
$this->callback->onHandshake($request, $response);
|
||||
} else {
|
||||
$response->setStatusCode(101, 'connection success.');
|
||||
$response->end();
|
||||
|
||||
if ($this->callback instanceof OnOpenInterface) {
|
||||
$this->callback->onOpen($request);
|
||||
}
|
||||
|
||||
}
|
||||
} catch (\Throwable $throwable) {
|
||||
$response->status(4000 + $throwable->getCode(), $throwable->getMessage());
|
||||
$response->end();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Frame $frame
|
||||
*/
|
||||
public function onMessage(Frame $frame): void
|
||||
{
|
||||
if ($frame->opcode == 0x08) {
|
||||
$this->collector->remove($frame->fd);
|
||||
} else {
|
||||
if (!($this->callback instanceof OnMessageInterface)) {
|
||||
return;
|
||||
}
|
||||
$this->callback->onMessage($frame);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,47 +0,0 @@
|
||||
<?php
|
||||
|
||||
namespace Kiri\Websocket;
|
||||
|
||||
|
||||
/**
|
||||
* @mixin \Swoole\WebSocket\Server
|
||||
* @mixin \Swoole\Coroutine\Http\Server
|
||||
*/
|
||||
interface WebSocketInterface
|
||||
{
|
||||
|
||||
|
||||
/**
|
||||
* @param int $fd
|
||||
* @param mixed $data
|
||||
* @param int $opcode
|
||||
* @param int $flags
|
||||
* @return bool
|
||||
*/
|
||||
public function push(int $fd, string $data, int $opcode = WEBSOCKET_OPCODE_TEXT, int $flags = SWOOLE_WEBSOCKET_FLAG_FIN): bool;
|
||||
|
||||
|
||||
/**
|
||||
* @param int $fd
|
||||
* @param int $code
|
||||
* @param string $reason
|
||||
* @return mixed
|
||||
*/
|
||||
public function disconnect(int $fd, int $code = SWOOLE_WEBSOCKET_CLOSE_NORMAL, string $reason = ''): bool;
|
||||
|
||||
|
||||
/**
|
||||
* @param int $fd
|
||||
* @return bool
|
||||
*/
|
||||
public function isEstablished(int $fd): bool;
|
||||
|
||||
|
||||
/**
|
||||
* @param int $fd
|
||||
* @return bool
|
||||
*/
|
||||
public function exist(int $fd): bool;
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user