extend event compatibility

This commit is contained in:
whale
2020-03-09 00:33:04 +08:00
parent efce1767e6
commit 66a50f0a87
8 changed files with 54 additions and 79 deletions

View File

@@ -5,7 +5,6 @@ namespace ZM\Annotation;
use Doctrine\Common\Annotations\AnnotationException; use Doctrine\Common\Annotations\AnnotationException;
use Doctrine\Common\Annotations\AnnotationReader; use Doctrine\Common\Annotations\AnnotationReader;
use Framework\Console;
use Framework\ZMBuf; use Framework\ZMBuf;
use ReflectionClass; use ReflectionClass;
use ReflectionException; use ReflectionException;

View File

@@ -5,13 +5,15 @@ namespace ZM\Event\CQ;
use Co; use Co;
use Framework\Console;
use Framework\ZMBuf; use Framework\ZMBuf;
use ZM\Annotation\CQ\CQAfter; use ZM\Annotation\CQ\CQAfter;
use ZM\Annotation\CQ\CQBefore; use ZM\Annotation\CQ\CQBefore;
use ZM\Annotation\CQ\CQCommand; use ZM\Annotation\CQ\CQCommand;
use ZM\Annotation\CQ\CQMessage; use ZM\Annotation\CQ\CQMessage;
use ZM\Connection\ConnectionManager; use ZM\Connection\WSConnection;
use ZM\Exception\WaitTimeoutException; use ZM\Exception\WaitTimeoutException;
use ZM\Http\Response;
use ZM\ModBase; use ZM\ModBase;
use ZM\ModHandleType; use ZM\ModHandleType;
@@ -20,25 +22,21 @@ class MessageEvent
private $function_call = false; private $function_call = false;
private $data; private $data;
private $circle; private $circle;
/** /** @var WSConnection|Response */
* @var \ZM\Event\Swoole\MessageEvent private $connection;
*/
private $swoole_event;
public function __construct($data, \ZM\Event\Swoole\MessageEvent $event, $circle = 0) { public function __construct($data, $conn_or_response, $circle = 0) {
$this->data = $data; $this->data = $data;
$this->swoole_event = $event; $this->connection = $conn_or_response;
$this->circle = $circle; $this->circle = $circle;
} }
public function onBefore() { public function onBefore() {
foreach (ZMBuf::$events[CQBefore::class][CQMessage::class] ?? [] as $v) { foreach (ZMBuf::$events[CQBefore::class]["message"] ?? [] as $v) {
$c = $v->class; $c = $v->class;
$class = new $c([ $class = new $c([
"data" => $this->data, "data" => $this->data,
"frame" => $this->swoole_event->frame, "connection" => $this->connection
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
], ModHandleType::CQ_MESSAGE); ], ModHandleType::CQ_MESSAGE);
$r = call_user_func_array([$class, $v->method], []); $r = call_user_func_array([$class, $v->method], []);
if (!$r || $class->block_continue) return false; if (!$r || $class->block_continue) return false;
@@ -80,9 +78,7 @@ class MessageEvent
if (!isset($obj[$c])) if (!isset($obj[$c]))
$obj[$c] = new $c([ $obj[$c] = new $c([
"data" => $this->data, "data" => $this->data,
"frame" => $this->swoole_event->frame, "connection" => $this->connection
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
], ModHandleType::CQ_MESSAGE); ], ModHandleType::CQ_MESSAGE);
if ($word[0] != "" && $v->match == $word[0]) { if ($word[0] != "" && $v->match == $word[0]) {
$r = call_user_func([$obj[$c], $v->method], $word); $r = call_user_func([$obj[$c], $v->method], $word);
@@ -110,9 +106,7 @@ class MessageEvent
if (!isset($obj[$c])) if (!isset($obj[$c]))
$obj[$c] = new $c([ $obj[$c] = new $c([
"data" => $this->data, "data" => $this->data,
"frame" => $this->swoole_event->frame, "connection" => $this->connection
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
], ModHandleType::CQ_MESSAGE); ], ModHandleType::CQ_MESSAGE);
$r = call_user_func([$obj[$c], $v->method], $this->data["message"]); $r = call_user_func([$obj[$c], $v->method], $this->data["message"]);
if (is_string($r)) $obj[$c]->reply($r); if (is_string($r)) $obj[$c]->reply($r);
@@ -129,13 +123,11 @@ class MessageEvent
* 在调用完事件后执行的 * 在调用完事件后执行的
*/ */
public function onAfter() { public function onAfter() {
foreach (ZMBuf::$events[CQAfter::class][CQMessage::class] ?? [] as $v) { foreach (ZMBuf::$events[CQAfter::class]["message"] ?? [] as $v) {
$c = $v->class; $c = $v->class;
$class = new $c([ $class = new $c([
"data" => $this->data, "data" => $this->data,
"frame" => $this->swoole_event->frame, "connection" => $this->connection
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
], ModHandleType::CQ_MESSAGE); ], ModHandleType::CQ_MESSAGE);
$r = call_user_func_array([$class, $v->method], []); $r = call_user_func_array([$class, $v->method], []);
if (!$r || $class->block_continue) return false; if (!$r || $class->block_continue) return false;

View File

@@ -8,6 +8,7 @@ use Framework\ZMBuf;
use ZM\Annotation\CQ\CQBefore; use ZM\Annotation\CQ\CQBefore;
use ZM\Annotation\CQ\CQMetaEvent; use ZM\Annotation\CQ\CQMetaEvent;
use ZM\Connection\ConnectionManager; use ZM\Connection\ConnectionManager;
use ZM\Connection\CQConnection;
use ZM\Exception\WaitTimeoutException; use ZM\Exception\WaitTimeoutException;
use ZM\ModBase; use ZM\ModBase;
use ZM\ModHandleType; use ZM\ModHandleType;
@@ -15,25 +16,23 @@ use ZM\ModHandleType;
class MetaEvent class MetaEvent
{ {
private $data; private $data;
/** @var \ZM\Event\Swoole\MessageEvent */ /** @var CQConnection */
private $swoole_event; private $connection;
private $circle; private $circle;
public function __construct($data, \ZM\Event\Swoole\MessageEvent $event, $circle = 0) { public function __construct($data, $connection, $circle = 0) {
$this->data = $data; $this->data = $data;
$this->swoole_event = $event; $this->connection = $connection;
$this->circle = $circle; $this->circle = $circle;
} }
public function onBefore() { public function onBefore() {
foreach (ZMBuf::$events[CQBefore::class][CQMetaEvent::class] ?? [] as $v) { foreach (ZMBuf::$events[CQBefore::class]["meta_event"] ?? [] as $v) {
$c = $v->class; $c = $v->class;
/** @var CQMetaEvent $v */ /** @var CQMetaEvent $v */
$class = new $c([ $class = new $c([
"data" => $this->data, "data" => $this->data,
"frame" => $this->swoole_event->frame, "connection" => $this->connection
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
], ModHandleType::CQ_META_EVENT); ], ModHandleType::CQ_META_EVENT);
$r = call_user_func_array([$class, $v->method], []); $r = call_user_func_array([$class, $v->method], []);
if (!$r || $class->block_continue) return false; if (!$r || $class->block_continue) return false;
@@ -55,9 +54,7 @@ class MetaEvent
if (!isset($obj[$c])) if (!isset($obj[$c]))
$obj[$c] = new $c([ $obj[$c] = new $c([
"data" => $this->data, "data" => $this->data,
"frame" => $this->swoole_event->frame, "connection" => $this->connection
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
], ModHandleType::CQ_META_EVENT); ], ModHandleType::CQ_META_EVENT);
$r = call_user_func([$obj[$c], $v->method]); $r = call_user_func([$obj[$c], $v->method]);
if (is_string($r)) $obj[$c]->reply($r); if (is_string($r)) $obj[$c]->reply($r);

View File

@@ -8,7 +8,7 @@ use Framework\ZMBuf;
use ZM\Annotation\CQ\CQAfter; use ZM\Annotation\CQ\CQAfter;
use ZM\Annotation\CQ\CQBefore; use ZM\Annotation\CQ\CQBefore;
use ZM\Annotation\CQ\CQNotice; use ZM\Annotation\CQ\CQNotice;
use ZM\Connection\ConnectionManager; use ZM\Connection\CQConnection;
use ZM\Exception\WaitTimeoutException; use ZM\Exception\WaitTimeoutException;
use ZM\ModBase; use ZM\ModBase;
use ZM\ModHandleType; use ZM\ModHandleType;
@@ -16,25 +16,23 @@ use ZM\ModHandleType;
class NoticeEvent class NoticeEvent
{ {
private $data; private $data;
/** @var \ZM\Event\Swoole\MessageEvent */ /** @var CQConnection */
private $swoole_event; private $connection;
private $circle; private $circle;
public function __construct($data, \ZM\Event\Swoole\MessageEvent $event, $circle = 0) { public function __construct($data, $connection, $circle = 0) {
$this->data = $data; $this->data = $data;
$this->swoole_event = $event; $this->connection = $connection;
$this->circle = $circle; $this->circle = $circle;
} }
public function onBefore() { public function onBefore() {
foreach (ZMBuf::$events[CQBefore::class][CQNotice::class] ?? [] as $v) { foreach (ZMBuf::$events[CQBefore::class]["notice"] ?? [] as $v) {
$c = $v->class; $c = $v->class;
/** @var CQNotice $v */ /** @var CQNotice $v */
$class = new $c([ $class = new $c([
"data" => $this->data, "data" => $this->data,
"frame" => $this->swoole_event->frame, "connection" => $this->connection
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
], ModHandleType::CQ_NOTICE); ], ModHandleType::CQ_NOTICE);
$r = call_user_func_array([$class, $v->method], []); $r = call_user_func_array([$class, $v->method], []);
if (!$r || $class->block_continue) return false; if (!$r || $class->block_continue) return false;
@@ -57,9 +55,7 @@ class NoticeEvent
if (!isset($obj[$c])) if (!isset($obj[$c]))
$obj[$c] = new $c([ $obj[$c] = new $c([
"data" => $this->data, "data" => $this->data,
"frame" => $this->swoole_event->frame, "connection" => $this->connection
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
], ModHandleType::CQ_NOTICE); ], ModHandleType::CQ_NOTICE);
$r = call_user_func([$obj[$c], $v->method]); $r = call_user_func([$obj[$c], $v->method]);
if (is_string($r)) $obj[$c]->reply($r); if (is_string($r)) $obj[$c]->reply($r);
@@ -72,13 +68,11 @@ class NoticeEvent
} }
public function onAfter() { public function onAfter() {
foreach (ZMBuf::$events[CQAfter::class][CQNotice::class] ?? [] as $v) { foreach (ZMBuf::$events[CQAfter::class]["notice"] ?? [] as $v) {
$c = $v->class; $c = $v->class;
$class = new $c([ $class = new $c([
"data" => $this->data, "data" => $this->data,
"frame" => $this->swoole_event->frame, "connection" => $this->connection
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
], ModHandleType::CQ_NOTICE); ], ModHandleType::CQ_NOTICE);
$r = call_user_func_array([$class, $v->method], []); $r = call_user_func_array([$class, $v->method], []);
if (!$r || $class->block_continue) return false; if (!$r || $class->block_continue) return false;

View File

@@ -8,7 +8,7 @@ use Framework\ZMBuf;
use ZM\Annotation\CQ\CQAfter; use ZM\Annotation\CQ\CQAfter;
use ZM\Annotation\CQ\CQBefore; use ZM\Annotation\CQ\CQBefore;
use ZM\Annotation\CQ\CQRequest; use ZM\Annotation\CQ\CQRequest;
use ZM\Connection\ConnectionManager; use ZM\Connection\CQConnection;
use ZM\Exception\WaitTimeoutException; use ZM\Exception\WaitTimeoutException;
use ZM\ModBase; use ZM\ModBase;
use ZM\ModHandleType; use ZM\ModHandleType;
@@ -16,25 +16,23 @@ use ZM\ModHandleType;
class RequestEvent class RequestEvent
{ {
private $data; private $data;
/** @var \ZM\Event\Swoole\MessageEvent */ /** @var CQConnection */
private $swoole_event; private $connection;
private $circle; private $circle;
public function __construct($data, \ZM\Event\Swoole\MessageEvent $event, $circle = 0) { public function __construct($data, $connection, $circle = 0) {
$this->data = $data; $this->data = $data;
$this->swoole_event = $event; $this->connection = $connection;
$this->circle = $circle; $this->circle = $circle;
} }
public function onBefore() { public function onBefore() {
foreach (ZMBuf::$events[CQBefore::class][CQRequest::class] ?? [] as $v) { foreach (ZMBuf::$events[CQBefore::class]["request"] ?? [] as $v) {
$c = $v->class; $c = $v->class;
/** @var CQRequest $v */ /** @var CQRequest $v */
$class = new $c([ $class = new $c([
"data" => $this->data, "data" => $this->data,
"frame" => $this->swoole_event->frame, "connection" => $this->connection
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
], ModHandleType::CQ_REQUEST); ], ModHandleType::CQ_REQUEST);
$r = call_user_func_array([$class, $v->method], []); $r = call_user_func_array([$class, $v->method], []);
if (!$r || $class->block_continue) return false; if (!$r || $class->block_continue) return false;
@@ -58,9 +56,7 @@ class RequestEvent
if (!isset($obj[$c])) if (!isset($obj[$c]))
$obj[$c] = new $c([ $obj[$c] = new $c([
"data" => $this->data, "data" => $this->data,
"frame" => $this->swoole_event->frame, "connection" => $this->connection
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
], ModHandleType::CQ_REQUEST); ], ModHandleType::CQ_REQUEST);
$r = call_user_func([$obj[$c], $v->method]); $r = call_user_func([$obj[$c], $v->method]);
if (is_string($r)) $obj[$c]->reply($r); if (is_string($r)) $obj[$c]->reply($r);
@@ -73,13 +69,11 @@ class RequestEvent
} }
public function onAfter() { public function onAfter() {
foreach (ZMBuf::$events[CQAfter::class][CQRequest::class] ?? [] as $v) { foreach (ZMBuf::$events[CQAfter::class]["request"] ?? [] as $v) {
$c = $v->class; $c = $v->class;
$class = new $c([ $class = new $c([
"data" => $this->data, "data" => $this->data,
"frame" => $this->swoole_event->frame, "connection" => $this->connection
"server" => $this->swoole_event->server,
"connection" => ConnectionManager::get($this->swoole_event->frame->fd)
], ModHandleType::CQ_REQUEST); ], ModHandleType::CQ_REQUEST);
$r = call_user_func_array([$class, $v->method], []); $r = call_user_func_array([$class, $v->method], []);
if (!$r || $class->block_continue) return false; if (!$r || $class->block_continue) return false;

View File

@@ -15,7 +15,6 @@ use ZM\Utils\ZMUtil;
class EventHandler class EventHandler
{ {
public static function callSwooleEvent($event_name, $param0, $param1 = null) { public static function callSwooleEvent($event_name, $param0, $param1 = null) {
$starttime = microtime(true);
$event_name = strtolower($event_name); $event_name = strtolower($event_name);
switch ($event_name) { switch ($event_name) {
case "workerstart": case "workerstart":
@@ -54,7 +53,7 @@ class EventHandler
//Console::info(Console::setColor("Event: " . $event_name . " 运行了 " . round(microtime(true) - $starttime, 5) . " 秒", "gold")); //Console::info(Console::setColor("Event: " . $event_name . " 运行了 " . round(microtime(true) - $starttime, 5) . " 秒", "gold"));
} }
public static function callCQEvent($event_data, MessageEvent $event, $level = 0) { public static function callCQEvent($event_data, $conn_or_response, int $level = 0) {
if ($level >= 5) { if ($level >= 5) {
Console::warning("Recursive call reached " . $level . " times"); Console::warning("Recursive call reached " . $level . " times");
Console::stackTrace(); Console::stackTrace();
@@ -63,24 +62,24 @@ class EventHandler
$starttime = microtime(true); $starttime = microtime(true);
switch ($event_data["post_type"]) { switch ($event_data["post_type"]) {
case "message": case "message":
$event = new CQ\MessageEvent($event_data, $event, $level); $event = new CQ\MessageEvent($event_data, $conn_or_response, $level);
if ($event->onBefore()) $event->onActivate(); if ($event->onBefore()) $event->onActivate();
$event->onAfter(); $event->onAfter();
return $event->hasReply(); return $event->hasReply();
break; break;
case "notice": case "notice":
$event = new CQ\NoticeEvent($event_data, $event, $level); $event = new CQ\NoticeEvent($event_data, $conn_or_response, $level);
if($event->onBefore()) $event->onActivate(); if ($event->onBefore()) $event->onActivate();
$event->onAfter(); $event->onAfter();
return true; return true;
case "request": case "request":
$event = new CQ\RequestEvent($event_data, $event, $level); $event = new CQ\RequestEvent($event_data, $conn_or_response, $level);
if($event->onBefore()) $event->onActivate(); if ($event->onBefore()) $event->onActivate();
$event->onAfter(); $event->onAfter();
return true; return true;
case "meta_event": case "meta_event":
$event = new CQ\MetaEvent($event_data, $event, $level); $event = new CQ\MetaEvent($event_data, $conn_or_response, $level);
if($event->onBefore()) $event->onActivate(); if ($event->onBefore()) $event->onActivate();
return true; return true;
} }
unset($starttime); unset($starttime);
@@ -89,7 +88,7 @@ class EventHandler
public static function callCQResponse($req) { public static function callCQResponse($req) {
//Console::info("收到来自API连接的回复".json_encode($req, 128|256)); //Console::info("收到来自API连接的回复".json_encode($req, 128|256));
if(isset($req["echo"]) && ZMBuf::array_key_exists("sent_api", $req["echo"])) { if (isset($req["echo"]) && ZMBuf::array_key_exists("sent_api", $req["echo"])) {
$status = $req["status"]; $status = $req["status"];
$retcode = $req["retcode"]; $retcode = $req["retcode"];
$data = $req["data"]; $data = $req["data"];

View File

@@ -42,9 +42,9 @@ class MessageEvent implements SwooleEvent
try { try {
if (ConnectionManager::get($this->frame->fd)->getType() == "qq") { if (ConnectionManager::get($this->frame->fd)->getType() == "qq") {
$data = json_decode($this->frame->data, true); $data = json_decode($this->frame->data, true);
if (isset($data["post_type"])) if (isset($data["post_type"])) {
EventHandler::callCQEvent($data, $this, 0); EventHandler::callCQEvent($data, ConnectionManager::get($this->frame->fd), 0);
else } else
EventHandler::callCQResponse($data); EventHandler::callCQResponse($data);
} }
foreach (ZMBuf::$events[SwooleEventAt::class] ?? [] as $v) { foreach (ZMBuf::$events[SwooleEventAt::class] ?? [] as $v) {