Files
kiri-core/HttpServer/Server.php
T

460 lines
10 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;
2020-09-04 01:22:26 +08:00
use HttpServer\Service\Websocket;
2020-08-31 01:27:08 +08:00
use Exception;
2020-09-02 11:38:47 +08:00
use ReflectionException;
2020-09-04 01:05:33 +08:00
use Snowflake\Abstracts\Config;
2020-09-02 12:19:20 +08:00
use Snowflake\Event;
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 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
{
2020-09-07 15:19:13 +08:00
use Action;
2020-08-31 01:27:08 +08:00
const HTTP = 'HTTP';
const TCP = 'TCP';
const PACKAGE = 'PACKAGE';
const WEBSOCKET = 'WEBSOCKET';
2020-10-29 18:17:25 +08:00
private array $listening = [];
private array $server = [
2020-08-31 01:27:08 +08:00
'HTTP' => [SWOOLE_TCP, Http::class],
'TCP' => [SWOOLE_TCP, Receive::class],
'PACKAGE' => [SWOOLE_UDP, Packet::class],
2020-09-04 01:22:26 +08:00
'WEBSOCKET' => [SWOOLE_SOCK_TCP, Websocket::class],
2020-08-31 01:27:08 +08:00
];
2020-09-03 00:37:02 +08:00
2020-09-04 01:22:26 +08:00
/** @var Http|Websocket|Packet|Receive */
2020-10-30 01:04:06 +08:00
private $baseServer;
2020-09-02 11:38:47 +08:00
2020-10-29 18:17:25 +08:00
public int $daemon = 0;
2020-09-07 16:46:09 +08:00
2020-09-10 19:26:42 +08:00
2020-10-29 18:17:25 +08:00
private array $process = [];
2020-09-10 19:26:42 +08:00
/**
* @param $name
* @param $process
*/
public function addProcess($name, $process)
{
$this->process[$name] = $process;
}
2020-08-31 01:27:08 +08:00
/**
* @param array $configs
2020-09-04 01:22:26 +08:00
* @return Http|Packet|Receive|Websocket
2020-08-31 01:27:08 +08:00
* @throws Exception
*/
public function initCore(array $configs)
{
2020-09-03 11:39:20 +08:00
$annotation = Snowflake::app()->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-04 13:56:33 +08:00
$this->enableCoroutine((bool)Config::get('settings.enable_coroutine'));
2020-11-01 04:20:07 +08:00
$this->orders($configs);
$this->onProcessListener();
return $this->getServer();
}
private function orders($configs)
{
$servers = $this->sortServers($configs);
foreach ($servers as $server) {
2020-09-02 11:38:47 +08:00
$this->create($server);
2020-09-07 18:31:43 +08:00
if (!$this->baseServer) {
return null;
}
2020-08-31 01:27:08 +08:00
}
2020-09-02 11:38:47 +08:00
}
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);
2020-09-07 16:28:31 +08:00
Snowflake::clearWorkerId();
2020-09-07 15:24:47 +08:00
$baseServer = $this->initCore($configs);
2020-09-07 18:31:43 +08:00
if (!$baseServer) {
return;
}
2020-09-03 00:35:03 +08:00
$baseServer->start();
2020-09-03 00:15:57 +08:00
}
2020-09-07 15:26:22 +08:00
/**
* @param $host
* @param $Port
* @throws Exception
*/
public function error_stop($host, $Port)
{
$this->error(sprintf('Port %s::%d is already.', $host, $Port));
2020-09-07 18:29:30 +08:00
if ($this->baseServer) {
$this->baseServer->shutdown();
}
2020-09-07 15:26:22 +08:00
}
2020-09-07 15:19:13 +08:00
/**
* @return bool
2020-09-11 14:42:03 +08:00
* @throws ConfigException
2020-09-07 15:19:13 +08:00
*/
public function isRunner()
{
2020-09-11 14:42:03 +08:00
$port = $this->sortServers(Config::get('servers'));
if (empty($port)) {
2020-09-07 15:19:13 +08:00
return false;
}
if (Snowflake::isLinux()) {
2020-09-11 14:42:03 +08:00
exec('netstat -tunlp | grep ' . $port[0]['port'], $output);
2020-09-07 15:19:13 +08:00
} else {
2020-09-11 14:42:03 +08:00
exec('lsof -i :' . $port[0]['port'] . ' | grep -i "LISTEN"', $output);
2020-09-07 15:19:13 +08:00
}
2020-09-11 14:42:03 +08:00
return !empty($output);
2020-09-07 15:19:13 +08:00
}
/**
* @return void
*
* start server
* @throws Exception
*/
public function shutdown()
{
$this->stop($this);
}
2020-09-02 18:53:55 +08:00
/**
* @param bool $isEnable
*/
private function enableCoroutine($isEnable = true)
{
2020-09-04 13:56:33 +08:00
if (!$isEnable) {
2020-09-02 18:53:55 +08:00
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 |
2020-09-07 11:54:57 +08:00
SWOOLE_HOOK_STREAM_FUNCTION |
2020-09-02 18:53:55 +08:00
SWOOLE_HOOK_PROC
);
}
2020-09-02 11:38:47 +08:00
/**
* @throws ConfigException
* @throws Exception
*/
public function onProcessListener()
{
$processes = Config::get('processes');
2020-09-10 19:26:42 +08:00
if (!empty($processes) && is_array($processes)) {
return $this->deliveryProcess(merge($processes, $this->process));
2020-09-02 11:38:47 +08:00
}
2020-09-10 19:26:42 +08:00
return $this->deliveryProcess($this->process);
}
2020-09-02 11:38:47 +08:00
2020-09-10 19:26:42 +08:00
/**
* @param $processes
* @throws Exception
*/
private function deliveryProcess($processes)
{
2020-09-03 11:39:20 +08:00
$application = Snowflake::app();
2020-09-10 19:26:42 +08:00
if (empty($processes) || !is_array($processes)) {
return;
}
2020-09-02 11:38:47 +08:00
foreach ($processes as $name => $process) {
2020-10-09 16:56:00 +08:00
$is_enable_coroutine = true;
if (is_array($process)) {
[$process, $is_enable_coroutine] = $process;
}
2020-10-09 17:03:54 +08:00
$this->debug(sprintf('Process %s', $process));
if (!is_string($process)) {
continue;
}
2020-10-09 16:56:00 +08:00
$system = new $process(Snowflake::app(), $name, $is_enable_coroutine);
2020-09-02 11:38:47 +08:00
$this->baseServer->addProcess($system);
2020-09-11 01:12:32 +08:00
$application->set($process, $system);
2020-09-02 11:38:47 +08:00
}
}
2020-09-07 16:46:09 +08:00
/**
* @param $daemon
* @return Server
*/
public function setDaemon($daemon)
{
if (!in_array($daemon, [0, 1])) {
return $this;
}
$this->daemon = $daemon;
return $this;
}
2020-09-02 11:38:47 +08:00
/**
2020-09-04 01:22:26 +08:00
* @return Http|Websocket|Packet|Receive
2020-09-02 11:38:47 +08:00
*/
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'] . ').');
}
$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;
}
2020-09-03 11:39:20 +08:00
$event = Snowflake::app()->event;
2020-08-31 01:27:08 +08:00
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-18 21:09:36 +08:00
$this->parseServer($config, $settings);
2020-09-02 11:38:47 +08:00
} else {
2020-11-01 04:21:19 +08:00
if ($this->isUse($config['port'])) {
return $this->error_stop($config['host'], $config['port']);
}
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-18 21:09:36 +08:00
/**
* @param $config
* @param $settings
* @return void
2020-09-18 21:39:56 +08:00
* @throws Exception
2020-09-18 21:09:36 +08:00
*/
private function parseServer($config, $settings)
{
$class = $this->dispatch($config['type']);
if ($this->isUse($config['port'])) {
return $this->error_stop($config['host'], $config['port']);
}
$this->baseServer = new $class($config['host'], $config['port'], SWOOLE_PROCESS, $config['mode']);
$settings['daemonize'] = $this->daemon;
if (!isset($settings['pid_file'])) {
$settings['pid_file'] = APP_PATH . 'storage/server.pid';
}
if ($this->baseServer instanceof Websocket) {
$this->onLoadWebsocketHandler();
} else if ($this->baseServer instanceof Http) {
$this->onLoadHttpHandler();
}
$this->baseServer->set($settings);
}
2020-09-02 11:38:47 +08:00
/**
* @param $config
* @param $newListener
2020-09-18 20:01:50 +08:00
* @return void
2020-09-02 12:19:20 +08:00
* @throws ReflectionException
2020-09-02 11:38:47 +08:00
* @throws Exception
2020-09-18 20:01:50 +08:00
* @throws NotFindClassException
2020-09-02 11:38:47 +08:00
*/
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']);
2020-11-01 04:27:01 +08:00
$callback = Snowflake::createObject([
'class' => $config['type'] == self::TCP ? OnReceive::class : OnPacket::class,
2020-11-01 04:27:55 +08:00
'pack' => $config['resolve']['pack'] ?? null,
'unpack' => $config['resolve']['unpack'] ?? null
2020-11-01 04:27:01 +08:00
]);
$this->onBind($newListener, 'packet', [$callback, 'onHandler']);
$this->onBind($newListener, 'receive', [$callback, 'onHandler']);
2020-09-02 11:38:47 +08:00
} else if ($config['type'] == self::WEBSOCKET) {
2020-09-04 01:22:26 +08:00
throw new Exception('Base server must instanceof \Swoole\Websocket\Server::class.');
2020-09-02 11:38:47 +08:00
} 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-03 11:39:20 +08:00
$event = Snowflake::app()->getEvent();
2020-09-18 21:43:28 +08:00
$event->on(Event::SERVER_WORKER_START, function () {
$router = Snowflake::app()->getRouter();
$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()
{
2020-09-18 21:09:36 +08:00
$event = Snowflake::app()->getEvent();
2020-09-18 21:39:56 +08:00
$event->on(Event::SERVER_WORKER_START, function () {
/** @var AWebsocket $websocket */
$websocket = Snowflake::app()->annotation->get('websocket');
2020-09-18 21:43:28 +08:00
$websocket->registration_notes(APP_PATH . 'app/Websocket', 'App\\Websocket');
2020-09-18 21:39:56 +08:00
});
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,
2020-09-04 01:22:26 +08:00
self::WEBSOCKET => Websocket::class,
2020-09-02 11:48:17 +08:00
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
}
}