Compare commits

..

5 Commits
1.5.5 ... 1.5.7

Author SHA1 Message Date
whale
e976a96d0b add auto-reconnect method to terminal_listener.php
add short name of ZM_BREAKPOINT (BP)
fix loop error of syntax happened
2020-06-20 14:51:24 +08:00
whale
dc7af105f4 deprecate ModBase, it will be removed in v1.6 2020-06-15 22:11:03 +08:00
whale
fd96543268 remove stupid access token 2020-06-15 19:51:32 +08:00
whale
cebc6fb310 update to 1.5.6 version
add CQCommand filter
add ZM_DEFAULT_FETCH_MODE
2020-06-15 19:50:07 +08:00
whale
c654cf40d5 update README.md 2020-06-13 17:45:29 +08:00
14 changed files with 53 additions and 19 deletions

View File

@@ -2,7 +2,7 @@
[![作者QQ](https://img.shields.io/badge/作者QQ-627577391-orange.svg)]()
[![zhamao License](https://img.shields.io/hexpm/l/plug.svg?maxAge=2592000)](https://github.com/zhamao-robot/zhamao-framework/blob/master/LICENSE)
[![版本](https://img.shields.io/badge/version-1.5-green.svg)]()
[![Latest Stable Version](http://img.shields.io/packagist/v/zhamao/framework.svg)](https://packagist.org/packages/zhamao/framework)
[![stupid counter](https://img.shields.io/github/search/zhamao-robot/zhamao-framework/stupid.svg)](https://github.com/zhamao-robot/zhamao-framework/search?q=stupid)
[![TODO counter](https://img.shields.io/github/search/zhamao-robot/zhamao-framework/TODO.svg)](https://github.com/zhamao-robot/zhamao-framework/search?q=TODO)

View File

@@ -3,7 +3,7 @@
"description": "High performance QQ robot and web server development framework",
"minimum-stability": "stable",
"license": "Apache-2.0",
"version": "1.5.5",
"version": "1.5.7",
"authors": [
{
"name": "whale",

View File

@@ -43,7 +43,8 @@ $config['sql_config'] = [
PDO::ATTR_STRINGIFY_FETCHES => false,
PDO::ATTR_EMULATE_PREPARES => false
],
'sql_no_exception' => false
'sql_no_exception' => false,
'sql_default_fetch_mode' => PDO::FETCH_BOTH // added in 1.5.6
];
/** CQHTTP连接约定的token */

View File

@@ -184,6 +184,7 @@ class Console
$vss->callback = function(?WSConnection $conn) use ($terminal_id){
$req = ctx()->getRequest();
if($conn->getType() != "terminal") return false;
Console::debug("Terminal fd: ".$conn->fd);
if(($req->header["x-terminal-id"] ?? "") != $terminal_id) {
$conn->close();
return false;
@@ -195,6 +196,7 @@ class Console
$vss2->type = "message";
$vss2->rule = "connectType:terminal";
$vss2->callback = function(?WSConnection $conn){
if ($conn === null) return false;
if($conn->getType() != "terminal") return false;
$cmd = ctx()->getFrame()->data;
self::executeCommand($cmd);

View File

@@ -151,6 +151,8 @@ class FrameworkLoader
define("ZM_MATCH_NUMBER", 2);
define("ZM_MATCH_SECOND", 3);
define("ZM_BREAKPOINT", 'if(in_array("--debug-mode", \Framework\FrameworkLoader::$argv)) extract(\Psy\debug(get_defined_vars(), isset($this) ? $this : @get_called_class()));');
define("BP", ZM_BREAKPOINT);
define("ZM_DEFAULT_FETCH_MODE", self::$settings->get("sql_config")["sql_default_fetch_mode"] ?? 4);
}
private function selfCheck() {

View File

@@ -3,6 +3,7 @@
use Swoole\Coroutine\Http\Client;
Co\run(function (){
hello:
global $terminal_id, $port;
$client = new Client("127.0.0.1", $port);
$client->set(['websocket_mask' => true]);
@@ -17,8 +18,9 @@ Co\run(function (){
break;
}
if($r === false) {
echo "Unable to connect framework terminal, connection closed.\n";
break;
echo "Unable to connect framework terminal, connection closed. Trying to reconnect after 5s.\n";
sleep(5);
goto hello;
}
} else {
break;

View File

@@ -110,6 +110,7 @@ class AnnotationParser
foreach ($method_annotations as $vss) {
if ($vss instanceof Rule) $vss = self::registerRuleEvent($vss, $vs, $reflection_class);
else $vss = self::registerMethod($vss, $vs, $reflection_class);
Console::debug("寻找 ".$vs->getName() ." -> ".get_class($vss));
if ($vss instanceof SwooleEventAt) ZMBuf::$events[SwooleEventAt::class][] = $vss;
elseif ($vss instanceof SwooleEventAfter) ZMBuf::$events[SwooleEventAfter::class][] = $vss;
@@ -326,6 +327,7 @@ class AnnotationParser
$class = getAllClasses(DataProvider::getWorkingDir() . "/src/Custom/Annotation/", "Custom\\Annotation");
foreach ($class as $v) {
$s = DataProvider::getWorkingDir() . '/src/' . str_replace("\\", "/", $v) . ".php";
Console::debug("Requiring custom annotation ".$s);
require_once $s;
}
}

View File

@@ -21,6 +21,14 @@ class CQCommand extends AnnotationBase implements Level
public $regexMatch = "";
/** @var string[] */
public $alias = [];
/** @var string */
public $message_type = "";
/** @var int */
public $user_id = 0;
/** @var int */
public $group_id = 0;
/** @var int */
public $discuss_id = 0;
/** @var int */
public $level = 20;

View File

@@ -90,10 +90,11 @@ class DB
/**
* @param string $line
* @param array $params
* @param int $fetch_mode
* @return mixed
* @throws DbException
*/
public static function rawQuery(string $line, $params = []) {
public static function rawQuery(string $line, $params = [], $fetch_mode = ZM_DEFAULT_FETCH_MODE) {
if (ZMBuf::get("sql_log") === true) {
$starttime = microtime(true);
}
@@ -131,7 +132,7 @@ class DB
"] " . $line . " " . json_encode($params, JSON_UNESCAPED_UNICODE) . "\n";
Coroutine::writeFile(CRASH_DIR . "sql.log", $log, FILE_APPEND);
}
return $ps->fetchAll();
return $ps->fetchAll($fetch_mode);
}
} catch (DbException $e) {
if (ZMBuf::get("sql_log") === true) {

View File

@@ -41,10 +41,11 @@ class SelectBody
}
/**
* @param int $fetch_mode
* @return null
* @throws DbException
*/
public function fetchAll() {
public function fetchAll($fetch_mode = ZM_DEFAULT_FETCH_MODE) {
if ($this->table->isCacheEnabled()) {
$rr = md5(implode(",", $this->select_thing) . serialize($this->where_thing));
if (array_key_exists($rr, $this->table->cache)) {
@@ -52,7 +53,7 @@ class SelectBody
return $this->table->cache[$rr]->getResult();
}
}
$this->execute();
$this->execute($fetch_mode);
if ($this->table->isCacheEnabled() && !in_array($rr, $this->table->cache)) {
$this->table->cache[$rr] = $this;
}
@@ -81,11 +82,12 @@ class SelectBody
}
/**
* @param int $fetch_mode
* @throws DbException
*/
public function execute() {
public function execute($fetch_mode = ZM_DEFAULT_FETCH_MODE) {
$str = $this->queryPrepare();
$this->result = DB::rawQuery($str[0], $str[1]);
$this->result = DB::rawQuery($str[0], $str[1], $fetch_mode);
}
public function getResult() { return $this->result; }

View File

@@ -101,7 +101,11 @@ class MessageEvent
foreach (ZMBuf::$events[CQCommand::class] ?? [] as $v) {
/** @var CQCommand $v */
if ($v->match == "" && $v->regexMatch == "") continue;
else {
elseif (($v->user_id == 0 || ($v->user_id != 0 && $v->user_id == context()->getData()["user_id"])) &&
($v->group_id == 0 || ($v->group_id != 0 && $v->group_id == (context()->getData()["group_id"] ?? 0))) &&
($v->discuss_id == 0 || ($v->discuss_id != 0 && $v->discuss_id == (context()->getData()["discuss_id"] ?? 0))) &&
($v->message_type == '' || ($v->message_type != '' && $v->message_type == context()->getData()["message_type"]))
) {
$c = $v->class;
$class_construct = [
"data" => context()->getData(),

View File

@@ -39,14 +39,17 @@ class EventHandler
switch ($event_name) {
case "workerstart":
try {
register_shutdown_function(function () {
register_shutdown_function(function () use ($param0) {
$error = error_get_last();
if ($error["type"] != 0) {
Console::error("Internal fatal error: " . $error["message"] . " at " . $error["file"] . "({$error["line"]})");
}
DataProvider::saveBuffer();
ZMBuf::$server->shutdown();
/** @var Server $param0 */
if (ZMBuf::$server === null) $param0->shutdown();
else ZMBuf::$server->shutdown();
});
ZMBuf::$server = $param0;
$r = (new WorkerStartEvent($param0, $param1))->onActivate();
Console::log("\n=== Worker #" . $param0->worker_id . " 已启动 ===\n", "gold");
$r->onAfter();
@@ -57,7 +60,9 @@ class EventHandler
ZMUtil::stop();
return;
} catch (Error $e) {
var_export($e);
Console::error("PHP Error: " . $e->getMessage() . " in " . $e->getFile() . " on line " . $e->getLine());
Console::error("Maybe it caused by your own code if in your own Module directory.");
Console::log($e->getTraceAsString(), 'gray');
ZMUtil::stop();
}
break;
@@ -85,7 +90,7 @@ class EventHandler
" [" . $param1->getStatusCode() . "] " . $param0->server["request_uri"]
);
if (!$param1->isEnd()) $param1->end("Internal server error: " . $e->getMessage());
Console::error("Internal server exception (500), caused by ".get_class($e));
Console::error("Internal server exception (500), caused by " . get_class($e));
Console::log($e->getTraceAsString(), "gray");
} catch (Error $e) {
/** @var Response $param1 */
@@ -134,7 +139,7 @@ class EventHandler
* @throws AnnotationException
*/
public static function callCQEvent($event_data, $conn_or_response, int $level = 0) {
ctx()->setCache("level",$level);
ctx()->setCache("level", $level);
if ($level >= 5) {
Console::warning("Recursive call reached " . $level . " times");
Console::stackTrace();
@@ -172,7 +177,7 @@ class EventHandler
* @throws AnnotationException
*/
public static function callCQResponse($req) {
Console::debug("收到来自API连接的回复".json_encode($req, 128|256));
Console::debug("收到来自API连接的回复" . json_encode($req, 128 | 256));
$status = $req["status"];
$retcode = $req["retcode"];
$data = $req["data"];

View File

@@ -15,6 +15,11 @@ use ZM\Http\Response;
use Swoole\WebSocket\Frame;
use Swoole\WebSocket\Server;
/**
* Class ModBase
* @package ZM
* @deprecated
*/
abstract class ModBase
{
/** @var Server */

View File

@@ -24,7 +24,7 @@ class ZMUtil
public static function stop($without_shutdown = false) {
Console::info(Console::setColor("Stopping server...", "red"));
foreach (ZMBuf::$server->connections as $v) {
foreach ((ZMBuf::$server->connections ?? []) as $v) {
ZMBuf::$server->close($v);
}
DataProvider::saveBuffer();