From 66a50f0a87b9840390e3070286133b53d01a1687 Mon Sep 17 00:00:00 2001 From: whale Date: Mon, 9 Mar 2020 00:33:04 +0800 Subject: [PATCH] extend event compatibility --- src/ZM/Annotation/AnnotationParser.php | 1 - src/ZM/Event/CQ/MessageEvent.php | 34 ++++++++++---------------- src/ZM/Event/CQ/MetaEvent.php | 19 ++++++-------- src/ZM/Event/CQ/NoticeEvent.php | 26 ++++++++------------ src/ZM/Event/CQ/RequestEvent.php | 26 ++++++++------------ src/ZM/Event/EventHandler.php | 19 +++++++------- src/ZM/Event/Swoole/MessageEvent.php | 6 ++--- src/ZM/Event/Swoole/RequestEvent.php | 2 +- 8 files changed, 54 insertions(+), 79 deletions(-) diff --git a/src/ZM/Annotation/AnnotationParser.php b/src/ZM/Annotation/AnnotationParser.php index e1221102..faeb1e8e 100644 --- a/src/ZM/Annotation/AnnotationParser.php +++ b/src/ZM/Annotation/AnnotationParser.php @@ -5,7 +5,6 @@ namespace ZM\Annotation; use Doctrine\Common\Annotations\AnnotationException; use Doctrine\Common\Annotations\AnnotationReader; -use Framework\Console; use Framework\ZMBuf; use ReflectionClass; use ReflectionException; diff --git a/src/ZM/Event/CQ/MessageEvent.php b/src/ZM/Event/CQ/MessageEvent.php index 9edf431c..068f264f 100644 --- a/src/ZM/Event/CQ/MessageEvent.php +++ b/src/ZM/Event/CQ/MessageEvent.php @@ -5,13 +5,15 @@ namespace ZM\Event\CQ; use Co; +use Framework\Console; use Framework\ZMBuf; use ZM\Annotation\CQ\CQAfter; use ZM\Annotation\CQ\CQBefore; use ZM\Annotation\CQ\CQCommand; use ZM\Annotation\CQ\CQMessage; -use ZM\Connection\ConnectionManager; +use ZM\Connection\WSConnection; use ZM\Exception\WaitTimeoutException; +use ZM\Http\Response; use ZM\ModBase; use ZM\ModHandleType; @@ -20,25 +22,21 @@ class MessageEvent private $function_call = false; private $data; private $circle; - /** - * @var \ZM\Event\Swoole\MessageEvent - */ - private $swoole_event; + /** @var WSConnection|Response */ + private $connection; - public function __construct($data, \ZM\Event\Swoole\MessageEvent $event, $circle = 0) { + public function __construct($data, $conn_or_response, $circle = 0) { $this->data = $data; - $this->swoole_event = $event; + $this->connection = $conn_or_response; $this->circle = $circle; } public function onBefore() { - foreach (ZMBuf::$events[CQBefore::class][CQMessage::class] ?? [] as $v) { + foreach (ZMBuf::$events[CQBefore::class]["message"] ?? [] as $v) { $c = $v->class; $class = new $c([ "data" => $this->data, - "frame" => $this->swoole_event->frame, - "server" => $this->swoole_event->server, - "connection" => ConnectionManager::get($this->swoole_event->frame->fd) + "connection" => $this->connection ], ModHandleType::CQ_MESSAGE); $r = call_user_func_array([$class, $v->method], []); if (!$r || $class->block_continue) return false; @@ -80,9 +78,7 @@ class MessageEvent if (!isset($obj[$c])) $obj[$c] = new $c([ "data" => $this->data, - "frame" => $this->swoole_event->frame, - "server" => $this->swoole_event->server, - "connection" => ConnectionManager::get($this->swoole_event->frame->fd) + "connection" => $this->connection ], ModHandleType::CQ_MESSAGE); if ($word[0] != "" && $v->match == $word[0]) { $r = call_user_func([$obj[$c], $v->method], $word); @@ -110,9 +106,7 @@ class MessageEvent if (!isset($obj[$c])) $obj[$c] = new $c([ "data" => $this->data, - "frame" => $this->swoole_event->frame, - "server" => $this->swoole_event->server, - "connection" => ConnectionManager::get($this->swoole_event->frame->fd) + "connection" => $this->connection ], ModHandleType::CQ_MESSAGE); $r = call_user_func([$obj[$c], $v->method], $this->data["message"]); if (is_string($r)) $obj[$c]->reply($r); @@ -129,13 +123,11 @@ class MessageEvent * 在调用完事件后执行的 */ public function onAfter() { - foreach (ZMBuf::$events[CQAfter::class][CQMessage::class] ?? [] as $v) { + foreach (ZMBuf::$events[CQAfter::class]["message"] ?? [] as $v) { $c = $v->class; $class = new $c([ "data" => $this->data, - "frame" => $this->swoole_event->frame, - "server" => $this->swoole_event->server, - "connection" => ConnectionManager::get($this->swoole_event->frame->fd) + "connection" => $this->connection ], ModHandleType::CQ_MESSAGE); $r = call_user_func_array([$class, $v->method], []); if (!$r || $class->block_continue) return false; diff --git a/src/ZM/Event/CQ/MetaEvent.php b/src/ZM/Event/CQ/MetaEvent.php index 1460464e..89fc0cd8 100644 --- a/src/ZM/Event/CQ/MetaEvent.php +++ b/src/ZM/Event/CQ/MetaEvent.php @@ -8,6 +8,7 @@ use Framework\ZMBuf; use ZM\Annotation\CQ\CQBefore; use ZM\Annotation\CQ\CQMetaEvent; use ZM\Connection\ConnectionManager; +use ZM\Connection\CQConnection; use ZM\Exception\WaitTimeoutException; use ZM\ModBase; use ZM\ModHandleType; @@ -15,25 +16,23 @@ use ZM\ModHandleType; class MetaEvent { private $data; - /** @var \ZM\Event\Swoole\MessageEvent */ - private $swoole_event; + /** @var CQConnection */ + private $connection; private $circle; - public function __construct($data, \ZM\Event\Swoole\MessageEvent $event, $circle = 0) { + public function __construct($data, $connection, $circle = 0) { $this->data = $data; - $this->swoole_event = $event; + $this->connection = $connection; $this->circle = $circle; } public function onBefore() { - foreach (ZMBuf::$events[CQBefore::class][CQMetaEvent::class] ?? [] as $v) { + foreach (ZMBuf::$events[CQBefore::class]["meta_event"] ?? [] as $v) { $c = $v->class; /** @var CQMetaEvent $v */ $class = new $c([ "data" => $this->data, - "frame" => $this->swoole_event->frame, - "server" => $this->swoole_event->server, - "connection" => ConnectionManager::get($this->swoole_event->frame->fd) + "connection" => $this->connection ], ModHandleType::CQ_META_EVENT); $r = call_user_func_array([$class, $v->method], []); if (!$r || $class->block_continue) return false; @@ -55,9 +54,7 @@ class MetaEvent if (!isset($obj[$c])) $obj[$c] = new $c([ "data" => $this->data, - "frame" => $this->swoole_event->frame, - "server" => $this->swoole_event->server, - "connection" => ConnectionManager::get($this->swoole_event->frame->fd) + "connection" => $this->connection ], ModHandleType::CQ_META_EVENT); $r = call_user_func([$obj[$c], $v->method]); if (is_string($r)) $obj[$c]->reply($r); diff --git a/src/ZM/Event/CQ/NoticeEvent.php b/src/ZM/Event/CQ/NoticeEvent.php index fe7c62fb..2ebdd481 100644 --- a/src/ZM/Event/CQ/NoticeEvent.php +++ b/src/ZM/Event/CQ/NoticeEvent.php @@ -8,7 +8,7 @@ use Framework\ZMBuf; use ZM\Annotation\CQ\CQAfter; use ZM\Annotation\CQ\CQBefore; use ZM\Annotation\CQ\CQNotice; -use ZM\Connection\ConnectionManager; +use ZM\Connection\CQConnection; use ZM\Exception\WaitTimeoutException; use ZM\ModBase; use ZM\ModHandleType; @@ -16,25 +16,23 @@ use ZM\ModHandleType; class NoticeEvent { private $data; - /** @var \ZM\Event\Swoole\MessageEvent */ - private $swoole_event; + /** @var CQConnection */ + private $connection; private $circle; - public function __construct($data, \ZM\Event\Swoole\MessageEvent $event, $circle = 0) { + public function __construct($data, $connection, $circle = 0) { $this->data = $data; - $this->swoole_event = $event; + $this->connection = $connection; $this->circle = $circle; } public function onBefore() { - foreach (ZMBuf::$events[CQBefore::class][CQNotice::class] ?? [] as $v) { + foreach (ZMBuf::$events[CQBefore::class]["notice"] ?? [] as $v) { $c = $v->class; /** @var CQNotice $v */ $class = new $c([ "data" => $this->data, - "frame" => $this->swoole_event->frame, - "server" => $this->swoole_event->server, - "connection" => ConnectionManager::get($this->swoole_event->frame->fd) + "connection" => $this->connection ], ModHandleType::CQ_NOTICE); $r = call_user_func_array([$class, $v->method], []); if (!$r || $class->block_continue) return false; @@ -57,9 +55,7 @@ class NoticeEvent if (!isset($obj[$c])) $obj[$c] = new $c([ "data" => $this->data, - "frame" => $this->swoole_event->frame, - "server" => $this->swoole_event->server, - "connection" => ConnectionManager::get($this->swoole_event->frame->fd) + "connection" => $this->connection ], ModHandleType::CQ_NOTICE); $r = call_user_func([$obj[$c], $v->method]); if (is_string($r)) $obj[$c]->reply($r); @@ -72,13 +68,11 @@ class NoticeEvent } public function onAfter() { - foreach (ZMBuf::$events[CQAfter::class][CQNotice::class] ?? [] as $v) { + foreach (ZMBuf::$events[CQAfter::class]["notice"] ?? [] as $v) { $c = $v->class; $class = new $c([ "data" => $this->data, - "frame" => $this->swoole_event->frame, - "server" => $this->swoole_event->server, - "connection" => ConnectionManager::get($this->swoole_event->frame->fd) + "connection" => $this->connection ], ModHandleType::CQ_NOTICE); $r = call_user_func_array([$class, $v->method], []); if (!$r || $class->block_continue) return false; diff --git a/src/ZM/Event/CQ/RequestEvent.php b/src/ZM/Event/CQ/RequestEvent.php index ad99b7c3..3123005e 100644 --- a/src/ZM/Event/CQ/RequestEvent.php +++ b/src/ZM/Event/CQ/RequestEvent.php @@ -8,7 +8,7 @@ use Framework\ZMBuf; use ZM\Annotation\CQ\CQAfter; use ZM\Annotation\CQ\CQBefore; use ZM\Annotation\CQ\CQRequest; -use ZM\Connection\ConnectionManager; +use ZM\Connection\CQConnection; use ZM\Exception\WaitTimeoutException; use ZM\ModBase; use ZM\ModHandleType; @@ -16,25 +16,23 @@ use ZM\ModHandleType; class RequestEvent { private $data; - /** @var \ZM\Event\Swoole\MessageEvent */ - private $swoole_event; + /** @var CQConnection */ + private $connection; private $circle; - public function __construct($data, \ZM\Event\Swoole\MessageEvent $event, $circle = 0) { + public function __construct($data, $connection, $circle = 0) { $this->data = $data; - $this->swoole_event = $event; + $this->connection = $connection; $this->circle = $circle; } public function onBefore() { - foreach (ZMBuf::$events[CQBefore::class][CQRequest::class] ?? [] as $v) { + foreach (ZMBuf::$events[CQBefore::class]["request"] ?? [] as $v) { $c = $v->class; /** @var CQRequest $v */ $class = new $c([ "data" => $this->data, - "frame" => $this->swoole_event->frame, - "server" => $this->swoole_event->server, - "connection" => ConnectionManager::get($this->swoole_event->frame->fd) + "connection" => $this->connection ], ModHandleType::CQ_REQUEST); $r = call_user_func_array([$class, $v->method], []); if (!$r || $class->block_continue) return false; @@ -58,9 +56,7 @@ class RequestEvent if (!isset($obj[$c])) $obj[$c] = new $c([ "data" => $this->data, - "frame" => $this->swoole_event->frame, - "server" => $this->swoole_event->server, - "connection" => ConnectionManager::get($this->swoole_event->frame->fd) + "connection" => $this->connection ], ModHandleType::CQ_REQUEST); $r = call_user_func([$obj[$c], $v->method]); if (is_string($r)) $obj[$c]->reply($r); @@ -73,13 +69,11 @@ class RequestEvent } public function onAfter() { - foreach (ZMBuf::$events[CQAfter::class][CQRequest::class] ?? [] as $v) { + foreach (ZMBuf::$events[CQAfter::class]["request"] ?? [] as $v) { $c = $v->class; $class = new $c([ "data" => $this->data, - "frame" => $this->swoole_event->frame, - "server" => $this->swoole_event->server, - "connection" => ConnectionManager::get($this->swoole_event->frame->fd) + "connection" => $this->connection ], ModHandleType::CQ_REQUEST); $r = call_user_func_array([$class, $v->method], []); if (!$r || $class->block_continue) return false; diff --git a/src/ZM/Event/EventHandler.php b/src/ZM/Event/EventHandler.php index 1a7023a1..191a6191 100644 --- a/src/ZM/Event/EventHandler.php +++ b/src/ZM/Event/EventHandler.php @@ -15,7 +15,6 @@ use ZM\Utils\ZMUtil; class EventHandler { public static function callSwooleEvent($event_name, $param0, $param1 = null) { - $starttime = microtime(true); $event_name = strtolower($event_name); switch ($event_name) { case "workerstart": @@ -54,7 +53,7 @@ class EventHandler //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) { Console::warning("Recursive call reached " . $level . " times"); Console::stackTrace(); @@ -63,24 +62,24 @@ class EventHandler $starttime = microtime(true); switch ($event_data["post_type"]) { 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(); $event->onAfter(); return $event->hasReply(); break; case "notice": - $event = new CQ\NoticeEvent($event_data, $event, $level); - if($event->onBefore()) $event->onActivate(); + $event = new CQ\NoticeEvent($event_data, $conn_or_response, $level); + if ($event->onBefore()) $event->onActivate(); $event->onAfter(); return true; case "request": - $event = new CQ\RequestEvent($event_data, $event, $level); - if($event->onBefore()) $event->onActivate(); + $event = new CQ\RequestEvent($event_data, $conn_or_response, $level); + if ($event->onBefore()) $event->onActivate(); $event->onAfter(); return true; case "meta_event": - $event = new CQ\MetaEvent($event_data, $event, $level); - if($event->onBefore()) $event->onActivate(); + $event = new CQ\MetaEvent($event_data, $conn_or_response, $level); + if ($event->onBefore()) $event->onActivate(); return true; } unset($starttime); @@ -89,7 +88,7 @@ class EventHandler public static function callCQResponse($req) { //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"]; $retcode = $req["retcode"]; $data = $req["data"]; diff --git a/src/ZM/Event/Swoole/MessageEvent.php b/src/ZM/Event/Swoole/MessageEvent.php index c30279af..bba48462 100644 --- a/src/ZM/Event/Swoole/MessageEvent.php +++ b/src/ZM/Event/Swoole/MessageEvent.php @@ -42,9 +42,9 @@ class MessageEvent implements SwooleEvent try { if (ConnectionManager::get($this->frame->fd)->getType() == "qq") { $data = json_decode($this->frame->data, true); - if (isset($data["post_type"])) - EventHandler::callCQEvent($data, $this, 0); - else + if (isset($data["post_type"])) { + EventHandler::callCQEvent($data, ConnectionManager::get($this->frame->fd), 0); + } else EventHandler::callCQResponse($data); } foreach (ZMBuf::$events[SwooleEventAt::class] ?? [] as $v) { diff --git a/src/ZM/Event/Swoole/RequestEvent.php b/src/ZM/Event/Swoole/RequestEvent.php index 58c0ec5d..d72a2a0b 100644 --- a/src/ZM/Event/Swoole/RequestEvent.php +++ b/src/ZM/Event/Swoole/RequestEvent.php @@ -141,4 +141,4 @@ class RequestEvent implements SwooleEvent } return true; } -} +} \ No newline at end of file