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\AnnotationReader;
use Framework\Console;
use Framework\ZMBuf;
use ReflectionClass;
use ReflectionException;

View File

@ -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;

View File

@ -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);

View File

@ -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;

View File

@ -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;

View File

@ -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"];

View File

@ -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) {

View File

@ -141,4 +141,4 @@ class RequestEvent implements SwooleEvent
}
return true;
}
}
}