update to version 1.4

This commit is contained in:
whale
2020-05-23 17:23:29 +08:00
parent 76ee308b91
commit 802f975825
30 changed files with 457 additions and 87 deletions

View File

@@ -48,8 +48,10 @@ class MessageEvent implements SwooleEvent
ctx()->setCache("level", 0);
Console::debug("Calling CQ Event from fd=" . $conn->fd);
EventHandler::callCQEvent($data, ConnectionManager::get(context()->getFrame()->fd), 0);
} else
} else{
set_coroutine_params(["connection" => $conn]);
EventHandler::callCQResponse($data);
}
}
foreach (ZMBuf::$events[SwooleEventAt::class] ?? [] as $v) {
if (strtolower($v->type) == "message" && $this->parseSwooleRule($v)) {

View File

@@ -6,6 +6,7 @@ namespace ZM\Event\Swoole;
use Closure;
use Doctrine\Common\Annotations\AnnotationException;
use Framework\Console;
use Framework\ZMBuf;
use Swoole\Http\Request;
use Swoole\WebSocket\Server;
@@ -51,9 +52,16 @@ class WSOpenEvent implements SwooleEvent
if ($type_conn == CQConnection::class) {
$qq = $this->request->get["qq"] ?? $this->request->header["x-self-id"] ?? "";
$self_token = ZMBuf::globals("access_token") ?? "";
$remote_token = $this->request->get["token"] ?? (isset($header["authorization"]) ? explode(" ", $this->request->header["authorization"])[1] : "");
if(isset($this->request->header["authorization"])) {
Console::debug($this->request->header["authorization"]);
}
$remote_token = $this->request->get["token"] ?? (isset($this->request->header["authorization"]) ? explode(" ", $this->request->header["authorization"])[1] : "");
if ($qq != "" && ($self_token == $remote_token)) $this->conn = new CQConnection($this->server, $this->request->fd, $qq);
else $this->conn = new UnknownConnection($this->server, $this->request->fd);
else {
$this->conn = new UnknownConnection($this->server, $this->request->fd);
Console::warning("connection of CQ has invalid QQ or token!");
Console::debug("Remote token: ".$remote_token);
}
} else {
$this->conn = new $type_conn($this->server, $this->request->fd);
}

View File

@@ -9,6 +9,8 @@ use Doctrine\Common\Annotations\AnnotationException;
use Exception;
use ReflectionException;
use Swoole\Coroutine;
use Swoole\Database\PDOConfig;
use Swoole\Database\PDOPool;
use Swoole\Process;
use Swoole\Timer;
use ZM\Annotation\AnnotationBase;
@@ -25,7 +27,6 @@ use Swoole\Server;
use ZM\Event\EventHandler;
use ZM\Exception\DbException;
use Framework\DataProvider;
use ZM\Utils\SQLPool;
use ZM\Utils\ZMUtil;
class WorkerStartEvent implements SwooleEvent
@@ -49,6 +50,9 @@ class WorkerStartEvent implements SwooleEvent
*/
public function onActivate(): WorkerStartEvent {
Console::info("Worker启动中");
ZMBuf::$server = $this->server;
Console::listenConsole(); //这个方法只能在这里调用且如果worker_num不为1的话此功能不可用
Process::signal(SIGINT, function () {
Console::warning("Server interrupted by keyboard.");
ZMUtil::stop(true);
@@ -76,14 +80,35 @@ class WorkerStartEvent implements SwooleEvent
}
if (ZMBuf::globals("sql_config")["sql_host"] != "") {
Console::info("新建SQL连接池中");
ZMBuf::$sql_pool = new SQLPool();
ob_start();
phpinfo();
$str = ob_get_clean();
$str = explode("\n", $str);
foreach($str as $k => $v) {
$v = trim($v);
if($v == "") continue;
if(mb_strpos($v, "API Extensions") === false) continue;
if(mb_strpos($v, "pdo_mysql") === false) {
throw new DbException("未安装 mysqlnd php-mysql扩展。");
}
}
$sql = ZMBuf::globals("sql_config");
ZMBuf::$sql_pool = new PDOPool((new PDOConfig())
->withHost($sql["sql_host"])
->withPort($sql["sql_port"])
// ->withUnixSocket('/tmp/mysql.sock')
->withDbName($sql["sql_database"])
->withCharset('utf8mb4')
->withUsername($sql["sql_username"])
->withPassword($sql["sql_password"])
);
DB::initTableList();
}
ZMBuf::$server = $this->server;
ZMBuf::$atomics['reload_time']->add(1);
Console::info("监听console输入");
Console::listenConsole(); //这个方法只能在这里调用且如果worker_num不为1的话此功能不可用
$this->setAutosaveTimer(ZMBuf::globals("auto_save_interval"));
$this->loadAllClass(); //加载composer资源、phar外置包、注解解析注册等
return $this;
@@ -99,18 +124,22 @@ class WorkerStartEvent implements SwooleEvent
}
ZMBuf::unsetCache("wait_start");
set_coroutine_params(["server" => $this->server, "worker_id" => $this->worker_id]);
foreach (ZMBuf::$events[OnStart::class] ?? [] as $v) {
$class_name = $v->class;
Console::debug("正在调用启动时函数: " . $class_name . " -> " . $v->method);
EventHandler::callWithMiddleware($class_name, $v->method, ["server" => $this->server, "worker_id" => $this->worker_id], []);
}
foreach (ZMBuf::$events[SwooleEventAfter::class] ?? [] as $v) {
/** @var AnnotationBase $v */
if (strtolower($v->type) == "workerstart") {
$class_name = $v->class;
Console::debug("正在调用启动时函数after: " . $class_name . " -> " . $v->method);
EventHandler::callWithMiddleware($class_name, $v->method, ["server" => $this->server, "worker_id" => $this->worker_id], []);
if (context()->getCache("block_continue") === true) break;
}
}
Console::debug("调用完毕!");
return $this;
}
@@ -118,8 +147,8 @@ class WorkerStartEvent implements SwooleEvent
foreach ($this->server->connections as $v) {
$this->server->close($v);
}
if (ZMBuf::$sql_pool instanceof SqlPool) {
ZMBuf::$sql_pool->destruct();
if (ZMBuf::$sql_pool !== null) {
ZMBuf::$sql_pool->close();
ZMBuf::$sql_pool = null;
}
}
@@ -147,12 +176,11 @@ class WorkerStartEvent implements SwooleEvent
}
//加载composer类
Console::info("加载composer资源中");
if (file_exists(DataProvider::getWorkingDir() . "/vendor/autoload.php")) {
Console::info("加载composer资源中");
require_once DataProvider::getWorkingDir() . "/vendor/autoload.php";
} else {
if (isPharMode()) require_once WORKING_DIR . "/vendor/autoload.php";
}
if (isPharMode()) require_once WORKING_DIR . "/vendor/autoload.php";
//加载各个模块的注解类,以及反射
Console::info("检索Module中");
@@ -162,6 +190,7 @@ class WorkerStartEvent implements SwooleEvent
ConnectionManager::registerCustomClass();
//加载自定义的全局函数
Console::debug("加载自定义的全局函数中");
if (file_exists(DataProvider::getWorkingDir() . "/src/Custom/global_function.php"))
require_once DataProvider::getWorkingDir() . "/src/Custom/global_function.php";
$this->afterCheck();
@@ -169,7 +198,7 @@ class WorkerStartEvent implements SwooleEvent
private function setAutosaveTimer($globals) {
DataProvider::$buffer_list = [];
Timer::tick($globals * 1000, function () {
zm_timer_tick($globals * 1000, function () {
DataProvider::saveBuffer();
});
}