Files
kiri-core/http-server/Server.php
T

357 lines
8.4 KiB
PHP
Raw Normal View History

2020-08-31 01:27:08 +08:00
<?php
namespace HttpServer;
2020-09-02 11:38:47 +08:00
use HttpServer\Events\OnClose;
use HttpServer\Events\OnConnect;
use HttpServer\Events\OnPacket;
use HttpServer\Events\OnReceive;
use HttpServer\Events\OnRequest;
2020-09-02 12:04:43 +08:00
use HttpServer\Route\Annotation\Annotation;
use HttpServer\Route\Annotation\Tcp;
2020-09-02 11:38:47 +08:00
use HttpServer\Service\Http;
use HttpServer\Service\Receive;
use HttpServer\Service\Packet;
use HttpServer\Service\WebSocket;
2020-08-31 01:27:08 +08:00
use Exception;
2020-09-02 11:38:47 +08:00
use ReflectionException;
use Snowflake\Config;
2020-09-02 17:55:20 +08:00
use Snowflake\Core\ArrayAccess;
2020-09-02 12:19:20 +08:00
use Snowflake\Event;
2020-09-02 17:46:30 +08:00
use Snowflake\Exception\ComponentException;
2020-09-02 11:38:47 +08:00
use Snowflake\Exception\ConfigException;
use Snowflake\Exception\NotFindClassException;
2020-08-31 01:27:08 +08:00
use Snowflake\Snowflake;
2020-09-02 11:38:47 +08:00
use Swoole\Process;
2020-09-02 12:04:43 +08:00
use HttpServer\Route\Annotation\Websocket as AWebsocket;
2020-09-02 18:53:55 +08:00
use Swoole\Runtime;
2020-08-31 01:27:08 +08:00
/**
* Class Server
* @package HttpServer
*
*
* @example [
* ['host'=> '127.0.0.1', 'port'=> 5775, 'mode'=> SWOOLE_TCP],
* ['host'=> '127.0.0.1', 'port'=> 5775, 'mode'=> SWOOLE_TCP],
* ['host'=> '127.0.0.1', 'port'=> 5775, 'mode'=> SWOOLE_TCP],
* ['host'=> '127.0.0.1', 'port'=> 5775, 'mode'=> SWOOLE_TCP],
* ['host'=> '127.0.0.1', 'port'=> 5775, 'mode'=> SWOOLE_UDP],
* ['host'=> '127.0.0.1', 'port'=> 5775, 'mode'=> SWOOLE_TCP]
* ]
*/
class Server extends Application
{
const HTTP = 'HTTP';
const TCP = 'TCP';
const PACKAGE = 'PACKAGE';
const WEBSOCKET = 'WEBSOCKET';
2020-09-02 12:20:19 +08:00
private $listening = [];
2020-08-31 01:27:08 +08:00
private $server = [
'HTTP' => [SWOOLE_TCP, Http::class],
'TCP' => [SWOOLE_TCP, Receive::class],
'PACKAGE' => [SWOOLE_UDP, Packet::class],
'WEBSOCKET' => [SWOOLE_SOCK_TCP, WebSocket::class],
];
2020-09-02 11:38:47 +08:00
/** @var Http|WebSocket|Packet|Receive */
private $baseServer;
2020-08-31 01:27:08 +08:00
/**
* @param array $configs
2020-09-02 11:38:47 +08:00
* @return Http|Packet|Receive|WebSocket
2020-08-31 01:27:08 +08:00
* @throws Exception
*/
public function initCore(array $configs)
{
2020-09-02 11:55:29 +08:00
if ($this->baseServer) {
return $this->getServer();
}
2020-09-02 12:04:43 +08:00
$annotation = Snowflake::get()->annotation;
2020-09-02 12:19:20 +08:00
$annotation->register('tcp', Tcp::class);
$annotation->register('http', Annotation::class);
$annotation->register('websocket', AWebsocket::class);
2020-09-02 12:04:43 +08:00
2020-09-02 18:54:25 +08:00
$this->enableCoroutine((bool)Config::get('enable_coroutine'));
2020-09-02 18:53:55 +08:00
foreach ($this->sortServers($configs) as $server) {
2020-09-02 11:38:47 +08:00
$this->create($server);
2020-08-31 01:27:08 +08:00
}
2020-09-02 18:53:55 +08:00
2020-09-02 11:38:47 +08:00
$this->onProcessListener();
return $this->getServer();
}
2020-09-03 00:15:57 +08:00
/**
2020-09-03 00:35:03 +08:00
* @return void
2020-09-03 00:15:57 +08:00
*
* start server
2020-09-03 00:35:03 +08:00
* @throws ConfigException
* @throws Exception
2020-09-03 00:15:57 +08:00
*/
public function start()
{
2020-09-03 00:35:03 +08:00
$configs = Config::get('servers', true);
$baseServer = $this->initCore($configs);
$baseServer->start();
2020-09-03 00:15:57 +08:00
}
2020-09-02 18:53:55 +08:00
/**
* @param bool $isEnable
*/
private function enableCoroutine($isEnable = true)
{
if ($isEnable !== true) {
return;
}
Runtime::enableCoroutine(true, SWOOLE_HOOK_TCP |
SWOOLE_HOOK_UNIX |
SWOOLE_HOOK_UDP |
SWOOLE_HOOK_UDG |
SWOOLE_HOOK_SSL |
SWOOLE_HOOK_TLS |
SWOOLE_HOOK_SLEEP |
SWOOLE_HOOK_STREAM_FUNCTION |
SWOOLE_HOOK_PROC
);
}
2020-09-02 11:38:47 +08:00
/**
* @throws ReflectionException
* @throws ConfigException
* @throws NotFindClassException
* @throws Exception
*/
public function onProcessListener()
{
$processes = Config::get('processes');
if (empty($processes) || !is_array($processes)) {
return;
}
$application = Snowflake::get();
foreach ($processes as $name => $process) {
$class = Snowflake::createObject($process);
if (!method_exists($class, 'onHandler')) {
continue;
}
2020-09-02 12:23:33 +08:00
$this->debug(sprintf('Process %s', $process));
2020-09-02 11:38:47 +08:00
$system = new Process([$class, 'onHandler'], false, null, true);
if (Snowflake::isLinux()) {
$system->name($name);
}
$this->baseServer->addProcess($system);
$application->set($name, $process);
}
}
/**
* @return Http|WebSocket|Packet|Receive
*/
public function getServer()
{
return $this->baseServer;
2020-08-31 01:27:08 +08:00
}
/**
* @param $config
* @return mixed
* @throws Exception
*/
private function create($config)
{
2020-09-02 15:49:26 +08:00
$settings = Config::get('settings', false, []);
2020-08-31 01:27:08 +08:00
if (!isset($this->server[$config['type']])) {
throw new Exception('Unknown server type(' . $config['type'] . ').');
}
2020-09-02 17:55:20 +08:00
if (isset($config['settings'])) {
$settings = ArrayAccess::merge($settings, $config['settings']);
}
2020-08-31 01:27:08 +08:00
$server = $this->dispatchCreate($config, $settings);
if (isset($config['events'])) {
$this->createEventListen($config);
}
return $server;
}
/**
* @param $config
2020-09-02 11:38:47 +08:00
* @throws Exception
2020-08-31 01:27:08 +08:00
*/
protected function createEventListen($config)
{
if (!is_array($config['events'])) {
return;
}
$event = Snowflake::get()->event;
foreach ($config['events'] as $name => $_event) {
$event->on($name, $_event);
}
}
/**
* @param $config
* @param $settings
* @return mixed
* @throws Exception
*/
private function dispatchCreate($config, $settings)
{
2020-09-02 11:59:04 +08:00
if (!($this->baseServer instanceof \Swoole\Server)) {
2020-09-02 11:41:05 +08:00
$class = $this->dispatch($config['type']);
$this->baseServer = new $class($config['host'], $config['port'], SWOOLE_PROCESS, $config['mode']);
2020-09-02 11:38:47 +08:00
$this->baseServer->set($settings);
2020-09-02 17:46:30 +08:00
$this->bindAnnotation();
2020-09-02 11:38:47 +08:00
} else {
2020-09-02 11:41:05 +08:00
$newListener = $this->baseServer->addlistener($config['host'], $config['port'], $config['mode']);
2020-09-02 17:33:48 +08:00
if (isset($config['settings']) && is_array($config['settings'])) {
$newListener->set($config['settings']);
2020-09-02 11:38:47 +08:00
}
2020-09-02 17:53:52 +08:00
$this->onListenerBind($config, $this->baseServer);
2020-09-02 11:38:47 +08:00
}
return $this->baseServer;
}
2020-09-02 17:46:30 +08:00
/**
* @throws Exception
*/
private function bindAnnotation()
{
if ($this->baseServer instanceof WebSocket) {
2020-09-02 17:53:52 +08:00
$this->onLoadWebsocketHandler();
2020-09-02 17:46:30 +08:00
}
if ($this->baseServer instanceof Http) {
2020-09-02 17:53:52 +08:00
$this->onLoadHttpHandler();
2020-09-02 17:46:30 +08:00
}
}
2020-09-02 11:38:47 +08:00
/**
* @param $config
* @param $newListener
2020-09-02 12:19:20 +08:00
* @throws NotFindClassException
* @throws ReflectionException
2020-09-02 11:38:47 +08:00
* @throws Exception
*/
2020-09-02 17:53:52 +08:00
private function onListenerBind($config, $newListener)
2020-09-02 11:38:47 +08:00
{
2020-09-02 12:22:36 +08:00
$this->debug(sprintf('Listener %s::%d', $config['host'], $config['port']));
2020-09-02 11:38:47 +08:00
if ($config['type'] == self::HTTP) {
2020-09-02 17:33:48 +08:00
$this->onBind($newListener, 'request', [Snowflake::createObject(OnRequest::class), 'onHandler']);
2020-09-02 11:38:47 +08:00
} else if ($config['type'] == self::TCP || $config['type'] == self::PACKAGE) {
2020-09-02 17:33:48 +08:00
$this->onBind($newListener, 'connect', [Snowflake::createObject(OnConnect::class), 'onHandler']);
$this->onBind($newListener, 'close', [Snowflake::createObject(OnClose::class), 'onHandler']);
$this->onBind($newListener, 'packet', [Snowflake::createObject(OnPacket::class), 'onHandler']);
$this->onBind($newListener, 'receive', [Snowflake::createObject(OnReceive::class), 'onHandler']);
2020-09-02 11:38:47 +08:00
} else if ($config['type'] == self::WEBSOCKET) {
throw new Exception('Base server must instanceof \Swoole\WebSocket\Server::class.');
} else {
throw new Exception('Unknown server type(' . $config['type'] . ').');
}
}
2020-09-02 17:33:48 +08:00
/**
* @param $server
* @param $name
* @param $callback
2020-09-02 17:46:30 +08:00
* @throws Exception
2020-09-02 17:33:48 +08:00
*/
private function onBind($server, $name, $callback)
{
if (in_array($name, $this->listening)) {
return;
}
2020-09-02 17:46:30 +08:00
if ($name === 'request') {
2020-09-02 17:53:52 +08:00
$this->onLoadHttpHandler();
2020-09-02 17:46:30 +08:00
}
2020-09-02 17:53:52 +08:00
array_push($this->listening, $name);
$server->on($name, $callback);
2020-09-02 17:33:48 +08:00
}
2020-09-02 12:19:20 +08:00
/**
* Load router handler
2020-09-02 17:53:52 +08:00
* @throws Exception
2020-09-02 12:19:20 +08:00
*/
public function onLoadHttpHandler()
{
2020-09-02 17:53:52 +08:00
$event = Snowflake::get()->getEvent();
$router = Snowflake::get()->getRouter();
if ($event->exists(Event::SERVER_WORKER_START, [$router, 'loadRouterSetting'])) {
return;
}
$event->on(Event::SERVER_WORKER_START, [$router, 'loadRouterSetting']);
2020-09-02 12:19:20 +08:00
}
/**
2020-09-02 17:53:52 +08:00
* @throws Exception
2020-09-02 12:19:20 +08:00
*/
public function onLoadWebsocketHandler()
{
/** @var AWebsocket $websocket */
$websocket = Snowflake::get()->annotation->register('websocket', AWebsocket::class);
2020-09-02 17:53:52 +08:00
$websocket->namespace = 'App\\Websocket';
$websocket->path = APP_PATH . 'app/Websocket';
$event = Snowflake::get()->event;
if ($event->exists(Event::SERVER_WORKER_START, [$websocket, 'registration_notes'])) {
return;
}
$event->on(Event::SERVER_WORKER_START, [$websocket, 'registration_notes']);
2020-09-02 12:19:20 +08:00
}
2020-09-02 11:38:47 +08:00
/**
* @param $type
* @return string
*/
private function dispatch($type)
{
$default = [
2020-09-02 11:48:17 +08:00
self::HTTP => Http::class,
self::WEBSOCKET => WebSocket::class,
self::TCP => Receive::class,
self::PACKAGE => Packet::class
2020-09-02 11:38:47 +08:00
];
2020-09-02 11:48:17 +08:00
return $default[$type] ?? Receive::class;
2020-09-02 11:38:47 +08:00
}
/**
* @param $servers
* @return array
*/
private function sortServers($servers)
{
$array = [];
foreach ($servers as $server) {
switch ($server['type']) {
case self::WEBSOCKET:
array_unshift($array, $server);
break;
case self::HTTP:
case self::PACKAGE | self::TCP:
$array[] = $server;
break;
default:
$array[] = $server;
}
2020-08-31 01:27:08 +08:00
}
2020-09-02 11:38:47 +08:00
return $array;
2020-08-31 01:27:08 +08:00
}
}