This commit is contained in:
2021-04-27 15:57:50 +08:00
parent 4fbb525059
commit 0b2523ba9f
22 changed files with 73 additions and 287 deletions
+5 -6
View File
@@ -7,6 +7,7 @@ namespace Annotation;
use Exception;
use Snowflake\Exception\ComponentException;
use Snowflake\Snowflake;
use Snowflake\Event as SEvent;
/**
@@ -29,16 +30,14 @@ use Snowflake\Snowflake;
/**
* @param array $handler
* @return \Snowflake\Event
* @throws ComponentException
* @return bool
* @throws Exception
*/
public function execute(array $handler): \Snowflake\Event
public function execute(array $handler): bool
{
// TODO: Implement execute() method.
$event = Snowflake::app()->getEvent();
$event->on($this->name, $handler, $this->params);
return $event;
SEvent::on($this->name, $handler, $this->params);
return true;
}
}
+1 -2
View File
@@ -28,8 +28,7 @@ use Snowflake\Snowflake;
if ($this->async_reload !== true) {
return;
}
$event = Snowflake::app()->getEvent();
$event->on(Event::SERVER_WORKER_EXIT, function () {
Event::on(Event::SERVER_WORKER_EXIT, function () {
Snowflake::app()->remove($this->service);
});
}
+4 -6
View File
@@ -67,9 +67,8 @@ class Connection extends Component
*/
public function init()
{
$event = Snowflake::app()->getEvent();
$event->on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'disconnect']);
$event->on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'clear_connection']);
Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'disconnect']);
Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'clear_connection']);
}
@@ -83,9 +82,8 @@ class Connection extends Component
}
$this->beginTransaction();
$event = Snowflake::app()->getEvent();
$event->on(Connection::TRANSACTION_COMMIT, [$this, 'commit'], false, true);
$event->on(Connection::TRANSACTION_ROLLBACK, [$this, 'rollback'], false, true);
Event::on(Connection::TRANSACTION_COMMIT, [$this, 'commit'], false, true);
Event::on(Connection::TRANSACTION_ROLLBACK, [$this, 'rollback'], false, true);
}
/**
+1 -2
View File
@@ -33,8 +33,7 @@ class DatabasesProviders extends Providers
{
$application->set('db', $this);
$event = Snowflake::app()->getEvent();
$event->on(Event::SERVER_WORKER_START, [$this, 'createPool']);
Event::on(Event::SERVER_WORKER_START, [$this, 'createPool']);
}
+5 -6
View File
@@ -12,6 +12,7 @@ namespace Database;
use Database\Traits\QueryTrait;
use Exception;
use HttpServer\Http\Context;
use Snowflake\Event;
use Snowflake\Snowflake;
/**
@@ -49,9 +50,8 @@ class Db
if (!static::transactionsActive()) {
return;
}
$event = Snowflake::app()->getEvent();
$event->trigger(Connection::TRANSACTION_COMMIT);
$event->offName(Connection::TRANSACTION_COMMIT);
Event::trigger(Connection::TRANSACTION_COMMIT);
Event::offName(Connection::TRANSACTION_COMMIT);
static::$_inTransaction = false;
}
@@ -63,9 +63,8 @@ class Db
if (!static::transactionsActive()) {
return;
}
$event = Snowflake::app()->getEvent();
$event->trigger(Connection::TRANSACTION_ROLLBACK);
$event->offName(Connection::TRANSACTION_ROLLBACK);
Event::trigger(Connection::TRANSACTION_ROLLBACK);
Event::offName(Connection::TRANSACTION_ROLLBACK);
static::$_inTransaction = false;
}
-66
View File
@@ -1,66 +0,0 @@
<?php
defined('CONNECT_HOST') or define('CONNECT_HOST', '');
defined('CONNECT_USER') or define('CONNECT_USER', '');
defined('CONNECT_PASS') or define('CONNECT_PASS', '');
return [
'cache' => [
'file' => [
'path' => strpos(null, 'data')
],
'redis' => [
'host' => '127.0.0.1',
'port' => '6379',
'prefix' => 'cache_',
'auth' => '',
'databases' => '0',
'timeout' => -1,
'read_timeout' => -1,
],
],
'databases' => [
'db' => [
'id' => 'db',
'cds' => 'mysql:dbname=aircraftwar;host=' . CONNECT_HOST,
'username' => CONNECT_USER,
'password' => CONNECT_PASS,
'tablePrefix' => 'aircraftwar_',
'maxNumber' => 100,
'slaveConfig' => [
'cds' => 'mysql:dbname=aircraftwar;host=' . CONNECT_HOST,
'username' => CONNECT_USER,
'password' => CONNECT_PASS
],
],
'server' => [
'id' => 'server',
'cds' => 'mysql:dbname=server;host=' . CONNECT_HOST,
'username' => CONNECT_USER,
'password' => CONNECT_PASS,
'tablePrefix' => 'master_',
'maxNumber' => 100,
'slaveConfig' => [
'cds' => 'mysql:dbname=server;host=' . CONNECT_HOST,
'username' => CONNECT_USER,
'password' => CONNECT_PASS
],
],
'game' => [
'id' => 'game',
'cds' => 'mysql:dbname=game;host=' . CONNECT_HOST,
'username' => CONNECT_USER,
'password' => CONNECT_PASS,
'maxNumber' => 100,
'tablePrefix' => 'game_',
'slaveConfig' => [
'cds' => 'mysql:dbname=game;host=' . CONNECT_HOST,
'username' => CONNECT_USER,
'password' => CONNECT_PASS
],
],
]
];
+1 -3
View File
@@ -21,13 +21,11 @@ class OnAfterReload extends Callback
/**
* @param Server $server
* @throws ComponentException
* @throws Exception
*/
public function onHandler(Server $server)
{
$event = Snowflake::app()->getEvent();
$event->trigger(Event::SERVER_AFTER_RELOAD, [$server]);
Event::trigger(Event::SERVER_AFTER_RELOAD, [$server]);
}
}
+1 -2
View File
@@ -24,8 +24,7 @@ class OnBeforeReload extends Callback
*/
public function onHandler(Server $server)
{
$event = Snowflake::app()->getEvent();
$event->trigger(Event::SERVER_BEFORE_RELOAD, [$server]);
Event::trigger(Event::SERVER_BEFORE_RELOAD, [$server]);
Snowflake::clearWorkerPid();
Snowflake::clearTaskPid();
+2 -4
View File
@@ -31,12 +31,10 @@ class OnClose extends Callback
fire(Event::SYSTEM_RESOURCE_RELEASES);
});
$clientInfo = $server->getClientInfo($fd);
$event = Snowflake::app()->getEvent();
if (!$event->exists(($name = $this->getName($clientInfo)))) {
if (!Event::exists(($name = $this->getName($clientInfo)))) {
return;
}
$event->trigger($name, [$server, $fd]);
Event::trigger($name, [$server, $fd]);
} catch (\Throwable $exception) {
$this->addError($exception, 'throwable');
}
+2 -4
View File
@@ -45,12 +45,10 @@ class OnMessage extends Callback
return;
}
$clientInfo = $server->getClientInfo($frame->fd);
$event = Snowflake::app()->getEvent();
if (!$event->exists(($name = $this->getName($clientInfo)))) {
if (!Event::exists(($name = $this->getName($clientInfo)))) {
return;
}
$event->trigger($name, [$frame, $server]);
Event::trigger($name, [$frame, $server]);
} catch (\Throwable $exception) {
$this->addError($exception, 'websocket');
if (!swoole()->exist($frame->fd)) {
-3
View File
@@ -38,7 +38,6 @@ class OnRequest extends Callback
*/
public function init()
{
$this->event = Snowflake::app()->getEvent();
$this->router = Snowflake::app()->getRouter();
}
@@ -93,8 +92,6 @@ class OnRequest extends Callback
[$sRequest, $sResponse] = [HRequest::create($sRequest), HResponse::create($sResponse)];
}
$this->event->dispatch(Event::EVENT_AFTER_REQUEST, [$sRequest, $exception]);
$headers = $sRequest->headers->get('access-control-request-headers');
$methods = $sRequest->headers->get('access-control-request-method');
+1 -2
View File
@@ -28,8 +28,7 @@ class OnWorkerError extends Callback
*/
public function onHandler(Server $server, int $worker_id, int $worker_pid, int $exit_code, int $signal)
{
$event = Snowflake::app()->getEvent();
$event->trigger(Event::SERVER_WORKER_ERROR);
Event::trigger(Event::SERVER_WORKER_ERROR);
$message = sprintf('Worker#%d::%d error stop. signal %d, exit_code %d',
$worker_id, $signal, $worker_pid, $exit_code
+1 -2
View File
@@ -25,11 +25,10 @@ class OnWorkerExit extends Callback
public function onHandler($server, $worker_id)
{
putenv('state=exit');
$event = Snowflake::app()->getEvent();
$channel = Snowflake::app()->getChannel();
$channel->cleanAll();
$event->trigger(Event::SERVER_WORKER_EXIT);
Event::trigger(Event::SERVER_WORKER_EXIT);
logger()->insert();
}
+1 -2
View File
@@ -26,8 +26,7 @@ class OnWorkerStop extends Callback
*/
public function onHandler($server, $worker_id)
{
$event = Snowflake::app()->getEvent();
$event->trigger(Event::SERVER_WORKER_STOP);
Event::trigger(Event::SERVER_WORKER_STOP);
$this->clearMysqlClient();
$this->clearRedisClient();
+2 -2
View File
@@ -17,6 +17,7 @@ use Exception;
use ReflectionException;
use Snowflake\Abstracts\Config;
use Snowflake\Error\LoggerProcess;
use Snowflake\Event;
use Snowflake\Exception\ConfigException;
use Snowflake\Exception\NotFindClassException;
use Snowflake\Process\Biomonitoring;
@@ -300,9 +301,8 @@ class Server extends HttpService
if (!is_array($config['events'])) {
return;
}
$event = Snowflake::app()->getEvent();
foreach ($config['events'] as $name => $_event) {
$event->on('listen ' . $config['port'] . ' ' . $name, $_event);
Event::on('listen ' . $config['port'] . ' ' . $name, $_event);
}
}
-111
View File
@@ -1,111 +0,0 @@
<?php
use HttpServer\Server;
use Snowflake\Snowflake;
use Snowflake\Event;
use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\WebSocket\Frame;
return [
'servers' => [
[
'id' => '',
'type' => Server::HTTP,
'host' => '127.0.0.1',
'port' => 9527,
'settings' => [
'worker_num' => 10,
'enable_coroutine' => 1
],
'events' => [
Event::SERVER_WORKER_START => function () {
$router = Snowflake::app()->getRouter();
$router->loadRouterSetting();
},
]
],
[
'id' => '',
'type' => Server::PACKAGE,
'host' => '127.0.0.1',
'port' => 9628,
'settings' => [
'worker_num' => 10,
'enable_coroutine' => 1
],
'message' => [
'pack' => function ($data) {
return \Snowflake\Core\Json::encode($data);
},
'unpack' => function ($data) {
return \Snowflake\Core\Json::decode($data);
},
],
'events' => [
]
],
[
'id' => '',
'type' => Server::TCP,
'host' => '127.0.0.1',
'port' => 9629,
'settings' => [
'worker_num' => 10,
'enable_coroutine' => 1
],
'message' => [
'pack' => function ($data) {
return \Snowflake\Core\Json::encode($data);
},
'unpack' => function ($data) {
return \Snowflake\Core\Json::decode($data);
},
],
'events' => [
Event::RECEIVE_CONNECTION => function ($data) {
return 'hello word~';
}
]
],
[
'id' => '',
'type' => Server::WEBSOCKET,
'host' => '127.0.0.1',
'port' => 9530,
'settings' => [
'worker_num' => 10,
'enable_coroutine' => 1
],
'events' => [
Event::SERVER_WORKER_START => function () {
$path = APP_PATH . 'app/Websocket';
$websocket = Snowflake::app()->annotation->websocket;
$websocket->registration_notes($path, 'App\\Sockets\\');
},
Event::SERVER_HANDSHAKE => function (Request $request, Response $response) {
$this->error($request->fd . ' connect.');
$response->status(101);
$response->end();
},
Event::SERVER_MESSAGE => function (\Swoole\WebSocket\Server $server, Frame $frame) {
$this->error('websocket SERVER_MESSAGE.');
if (is_null($json = json_decode($frame->data, true))) {
return $server->push($frame->fd, 'format error~');
}
$websocket = Snowflake::app()->annotation->websocket;
if ($websocket->has($json['path'])) {
return $websocket->runWith($json['path'], [$frame->fd, $json]);
} else {
return $server->push($frame->fd, 'hello word~');
}
},
Event::SERVER_CLOSE => function (int $fd) {
$this->error($fd . ' disconnect.');
return 'hello word~';
}
]
],
]
];
+15 -27
View File
@@ -180,43 +180,42 @@ abstract class BaseApplication extends Service
if (!isset($config['events']) || !is_array($config['events'])) {
return;
}
$event = Snowflake::app()->getEvent();
foreach ($config['events'] as $key => $value) {
if (is_string($value)) {
$value = Snowflake::createObject($value);
}
$this->addEvent($event, $key, $value);
$this->addEvent($key, $value);
}
}
/**
* @param $event
* @param $key
* @param $value
* @throws InitException
* @throws NotFindClassException
* @throws ReflectionException
*/
private function addEvent($event, $key, $value): void
/**
* @param $key
* @param $value
* @throws InitException
* @throws NotFindClassException
* @throws ReflectionException
* @throws Exception
*/
private function addEvent($key, $value): void
{
if ($value instanceof \Closure) {
$event->on($key, $value, [], true);
Event::on($key, $value, [], true);
return;
}
if (is_object($value)) {
$event->on($key, $value, [], true);
Event::on($key, $value, [], true);
return;
}
if (is_array($value)) {
if (is_object($value[0]) && !($value[0] instanceof \Closure)) {
$event->on($key, $value, [], true);
Event::on($key, $value, [], true);
return;
}
if (is_string($value[0])) {
$value[0] = Snowflake::createObject($value[0]);
$event->on($key, $value, [], true);
Event::on($key, $value, [], true);
return;
}
@@ -224,7 +223,7 @@ abstract class BaseApplication extends Service
if (!is_callable($item, true)) {
throw new InitException("Class does not hav callback.");
}
$event->on($key, $item, [], true);
Event::on($key, $item, [], true);
}
}
@@ -367,16 +366,6 @@ abstract class BaseApplication extends Service
}
/**
* @return Event
* @throws Exception
*/
public function getEvent(): Event
{
return $this->get('event');
}
/**
* @return Jwt
* @throws Exception
@@ -454,7 +443,6 @@ abstract class BaseApplication extends Service
{
$this->setComponents([
'error' => ['class' => ErrorHandler::class],
'event' => ['class' => Event::class],
'connections' => ['class' => Connection::class],
'redis_connections' => ['class' => SRedis::class],
'pool' => ['class' => SPool::class],
+3 -4
View File
@@ -35,10 +35,9 @@ class Redis extends Component
*/
public function init()
{
$event = Snowflake::app()->getEvent();
$event->on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'destroy']);
$event->on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'release']);
$event->on(Event::SERVER_WORKER_START, [$this, 'createPool']);
Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'destroy']);
Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'release']);
Event::on(Event::SERVER_WORKER_START, [$this, 'createPool']);
}
+2 -4
View File
@@ -74,8 +74,7 @@ class ErrorHandler extends Component implements ErrorInterface
{
$this->category = 'exception';
$event = Snowflake::app()->getEvent();
$event->trigger(Event::SYSTEM_RESOURCE_CLEAN);
Event::trigger(Event::SYSTEM_RESOURCE_CLEAN);
$this->sendError($exception->getMessage(), $exception->getFile(), $exception->getLine());
}
@@ -103,8 +102,7 @@ class ErrorHandler extends Component implements ErrorInterface
Snowflake::app()->error($data, 'error');
$event = Snowflake::app()->getEvent();
$event->trigger(Event::SYSTEM_RESOURCE_CLEAN);
Event::trigger(Event::SYSTEM_RESOURCE_CLEAN);
throw new \ErrorException($error[1], $error[0], 1, $error[2], $error[3]);
}
+2 -3
View File
@@ -33,9 +33,8 @@ class Logger extends Component
public function init()
{
$event = Snowflake::app()->getEvent();
$event->on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'insert']);
$event->on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'insert']);
Event::on(Event::SYSTEM_RESOURCE_CLEAN, [$this, 'insert']);
Event::on(Event::SYSTEM_RESOURCE_RELEASES, [$this, 'insert']);
}
+20 -20
View File
@@ -71,7 +71,7 @@ class Event extends BaseObject
* @param bool $isAppend
* @throws Exception
*/
public function on($name, $callback, $parameter = [], $isAppend = false)
public static function on($name, $callback, $parameter = [], $isAppend = false)
{
if (!isset(static::$_events[$name])) {
static::$_events[$name] = [];
@@ -84,7 +84,7 @@ class Event extends BaseObject
}
$callback[0] = Snowflake::createObject($callback[0]);
}
if ($this->exists($name, $callback)) {
if (static::exists($name, $callback)) {
return;
}
if (!empty(static::$_events[$name]) && $isAppend === true) {
@@ -99,7 +99,7 @@ class Event extends BaseObject
* @param $name
* @param $callback
*/
public function of($name, $callback): void
public static function of($name, $callback): void
{
if (!isset(static::$_events[$name])) {
return;
@@ -118,13 +118,13 @@ class Event extends BaseObject
* @param $name
* @return bool
*/
public function offName($name): bool
public static function offName($name): bool
{
if (!$this->exists($name)) {
if (!static::exists($name)) {
return true;
}
unset(static::$_events[$name]);
return $this->exists($name);
return static::exists($name);
}
@@ -133,7 +133,7 @@ class Event extends BaseObject
* @param null $callback
* @return bool
*/
public function exists($name, $callback = null): bool
public static function exists($name, $callback = null): bool
{
if (!isset(static::$_events[$name])) {
return false;
@@ -156,9 +156,9 @@ class Event extends BaseObject
* @param $handler
* @return mixed
*/
public function get($name, $handler): mixed
public static function get($name, $handler): mixed
{
if (!$this->exists($name, $handler)) {
if (!static::exists($name, $handler)) {
return null;
}
@@ -175,7 +175,7 @@ class Event extends BaseObject
}
public function clean()
public static function clean()
{
static::$_events = [];
}
@@ -188,9 +188,9 @@ class Event extends BaseObject
* @return bool
* @throws Exception
*/
public function dispatch($name, $params = [], $scope = null): bool
public static function dispatch($name, $params = [], $scope = null): bool
{
return $this->trigger($name, $params, $scope);
return static::trigger($name, $params, $scope);
}
@@ -202,17 +202,17 @@ class Event extends BaseObject
* @return bool
* @throws Exception
*/
public function trigger($name, $parameter = null, $handler = null, $is_remove = false): bool
public static function trigger($name, $parameter = null, $handler = null, $is_remove = false): bool
{
$events = $this->get($name, $handler);
$events = static::get($name, $handler);
if (empty($events)) {
return true;
}
foreach ($events as $event) {
$this->execute($event, $parameter);
static::execute($event, $parameter);
}
if ($is_remove) {
$this->offName($name);
static::offName($name);
}
return true;
}
@@ -224,16 +224,16 @@ class Event extends BaseObject
* @return bool
* @throws Exception
*/
private function execute($event, $parameter): bool
private static function execute($event, $parameter): bool
{
try {
$meta = $this->mergeParams($event[1], $parameter);
$meta = static::mergeParams($event[1], $parameter);
if (call_user_func($event[0], ...$meta) === false) {
return false;
}
return true;
} catch (\Throwable $throwable) {
return $this->addError($throwable,'throwable');
return static::addError($throwable,'throwable');
}
}
@@ -243,7 +243,7 @@ class Event extends BaseObject
* @param $parameter
* @return array
*/
private function mergeParams($defaultParameter, $parameter = []): array
private static function mergeParams($defaultParameter, $parameter = []): array
{
if (empty($defaultParameter)) {
$defaultParameter = $parameter;
+4 -6
View File
@@ -19,6 +19,7 @@ use Snowflake\Abstracts\Config;
use Snowflake\Application;
use Snowflake\Core\ArrayAccess;
use Snowflake\Error\Logger;
use Snowflake\Event;
use Snowflake\Exception\ConfigException;
use Snowflake\Snowflake;
use Swoole\WebSocket\Server;
@@ -350,8 +351,7 @@ if (!function_exists('fire')) {
*/
function fire(string $event, array $params = [])
{
$logger = Snowflake::app()->getEvent();
$logger->trigger($event, $params);
Event::trigger($event, $params);
}
}
@@ -586,8 +586,7 @@ if (!function_exists('listen')) {
*/
function listen($name, $callback, $params = [], $isAppend = true)
{
$event = Snowflake::app()->getEvent();
$event->on($name, $callback, $params, $isAppend);
Event::on($name, $callback, $params, $isAppend);
}
}
@@ -606,8 +605,7 @@ if (!function_exists('event')) {
*/
function event($name, $callback, $params = [], $isAppend = true)
{
$event = Snowflake::app()->getEvent();
$event->on($name, $callback, $params, $isAppend);
Event::on($name, $callback, $params, $isAppend);
}
}