This commit is contained in:
2020-09-04 00:41:33 +08:00
parent 622071256e
commit 46f9de1f6a
141 changed files with 105 additions and 73 deletions
+17
View File
@@ -0,0 +1,17 @@
<?php
namespace HttpServer\Service\Abstracts;
use HttpServer\IInterface\Service;
use Swoole\Http\Server;
abstract class Http extends Server implements Service
{
use \HttpServer\Service\Abstracts\Server;
}
+97
View File
@@ -0,0 +1,97 @@
<?php
namespace HttpServer\Service\Abstracts;
use Exception;
use ReflectionException;
use Snowflake\Application;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
trait Server
{
/** @var Application */
public $application;
/**
* Server constructor.
* @param $host
* @param null $port
* @param null $mode
* @param null $sock_type
*/
public function __construct($host, $port = null, $mode = null, $sock_type = null)
{
$this->application = Snowflake::app();
parent::__construct($host, $port, $mode, $sock_type);
}
/**
* @param array $settings
* @return mixed|void
*/
public function set(array $settings)
{
parent::set($settings); // TODO: Change the autogenerated stub
$this->onInit();
}
/**
* @return mixed|void
* @throws NotFindClassException
* @throws ReflectionException
*/
public function onHandlerListener()
{
$this->on('workerStop', $this->createHandler('workerStop'));
$this->on('workerExit', $this->createHandler('workerExit'));
$this->on('workerStart', $this->createHandler('workerStart'));
$this->on('workerError', $this->createHandler('workerError'));
$this->on('managerStart', $this->createHandler('managerStart'));
$this->on('managerStop', $this->createHandler('managerStop'));
$this->on('pipeMessage', $this->createHandler('pipeMessage'));
$this->on('shutdown', $this->createHandler('shutdown'));
$this->on('start', $this->createHandler('start'));
$this->addTask();
}
/**
* @throws NotFindClassException
* @throws ReflectionException
*/
protected function addTask()
{
$settings = $this->setting;
if (($taskNumber = $settings['task_worker_num'] ?? 0) > 0) {
$this->on('finish', $this->createHandler('finish'));
$this->on('task', $this->createHandler('task'));
}
}
/**
* @param $eventName
* @return array
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
protected function createHandler($eventName)
{
$classPrefix = 'HttpServer\Events\On' . ucfirst($eventName);
if (!class_exists($classPrefix)) {
throw new Exception('class not found.');
}
$class = Snowflake::createObject($classPrefix, [Snowflake::app()]);
return [$class, 'onHandler'];
}
}
+24
View File
@@ -0,0 +1,24 @@
<?php
namespace HttpServer\Service\Abstracts;
use Closure;
use HttpServer\IInterface\Service;
use Swoole\Server;
abstract class Tcp extends Server implements Service
{
use \HttpServer\Service\Abstracts\Server;
/** @var Closure|array */
public $unpack;
/** @var Closure|array */
public $pack;
}
@@ -0,0 +1,16 @@
<?php
namespace HttpServer\Service\Abstracts;
use HttpServer\IInterface\Service;
use Swoole\WebSocket\Server;
abstract class Websocket extends Server implements Service
{
use \HttpServer\Service\Abstracts\Server;
}
+45
View File
@@ -0,0 +1,45 @@
<?php
namespace HttpServer\Service;
use Exception;
use HttpServer\Http\Context;
use HttpServer\Http\Request as HRequest;
use HttpServer\Http\Response as HResponse;
use ReflectionException;
use Snowflake\Core\JSON;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
use Swoole\Error;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Http\Server;
use HttpServer\Service\Abstracts\Http as AHttp;
class Http extends AHttp
{
/**
* @throws ReflectionException
* @throws NotFindClassException
*/
public function onInit()
{
$this->onHandlerListener();
$this->onBaseListener();
}
/**
* @throws NotFindClassException
* @throws ReflectionException
*/
public function onBaseListener()
{
$this->on('request', $this->createHandler('request'));
}
}
+46
View File
@@ -0,0 +1,46 @@
<?php
namespace HttpServer\Service;
use Exception;
use HttpServer\Service\Abstracts\Tcp;
use ReflectionException;
use Snowflake\Event;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
use Swoole\Server;
/**
* Class OnPacket
* @package HttpServer\Events
*/
class Packet extends Tcp
{
/**
* @throws ReflectionException
* @throws NotFindClassException
*/
public function onInit()
{
$this->onHandlerListener();
$this->onBaseListener();
}
/**
* @throws NotFindClassException
* @throws ReflectionException
*/
public function onBaseListener()
{
$this->on('connect', $this->createHandler('connect'));
$this->on('packet', $this->createHandler('packet'));
$this->on('close', $this->createHandler('close'));
}
}
+42
View File
@@ -0,0 +1,42 @@
<?php
namespace HttpServer\Service;
use HttpServer\Service\Abstracts\Tcp;
use ReflectionException;
use Snowflake\Exception\NotFindClassException;
/**
* Class Receive
* @package HttpServer\Events
*/
class Receive extends Tcp
{
/**
* @throws ReflectionException
* @throws NotFindClassException
*/
public function onInit()
{
$this->onHandlerListener();
$this->onBaseListener();
}
/**
* @throws NotFindClassException
* @throws ReflectionException
*/
public function onBaseListener()
{
$this->on('connect', $this->createHandler('connect'));
$this->on('receive', $this->createHandler('receive'));
$this->on('close', $this->createHandler('close'));
}
}
+45
View File
@@ -0,0 +1,45 @@
<?php
namespace HttpServer\Service;
use Exception;
use ReflectionException;
use Snowflake\Error\Logger;
use Snowflake\Event;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Snowflake;
use Swoole\Http\Request as SRequest;
use Swoole\Http\Response as SResponse;
use Swoole\WebSocket\Frame;
use Swoole\WebSocket\Server;
use HttpServer\Service\Abstracts\Websocket as HAWebsocket;
use HttpServer\Route\Annotation\Websocket as AWebsocket;
class Websocket extends HAWebsocket
{
/**
* @throws ReflectionException
* @throws NotFindClassException
*/
public function onInit()
{
$this->onHandlerListener();
$this->onBaseListener();
}
/**
* @throws NotFindClassException
* @throws ReflectionException
*/
public function onBaseListener()
{
$this->on('handshake', $this->createHandler('handshake'));
$this->on('message', $this->createHandler('message'));
$this->on('close', $this->createHandler('close'));
}
}