Files
zhamao-framework/src/ZM/API/CQAPI.php

87 lines
2.6 KiB
PHP
Raw Normal View History

2020-03-02 16:14:20 +08:00
<?php
namespace ZM\API;
2021-11-16 15:41:01 +08:00
use Closure;
2020-08-31 10:11:06 +08:00
use ZM\Console\Console;
2020-11-03 21:02:24 +08:00
use ZM\Store\LightCacheInside;
use ZM\Store\Lock\SpinLock;
use ZM\Store\ZMAtomic;
use ZM\Utils\CoMessage;
2020-03-02 16:14:20 +08:00
2020-08-31 10:11:06 +08:00
trait CQAPI
2020-03-02 16:14:20 +08:00
{
2021-11-16 15:41:01 +08:00
/** @var null|Closure */
private static $filter = null;
public static function registerFilter(callable $callable) {
self::$filter = $callable;
}
/**
* @param $connection
2020-03-02 16:14:20 +08:00
* @param $reply
* @param |null $function
2020-05-23 17:23:29 +08:00
* @return bool|array
2020-03-02 16:14:20 +08:00
*/
2020-08-31 10:11:06 +08:00
private function processAPI($connection, $reply, $function = null) {
2021-11-16 15:41:01 +08:00
if (is_callable(self::$filter)) {
$reply2 = call_user_func(self::$filter, $reply);
if (is_bool($reply2)) return $reply2;
else $reply = $reply2;
}
2020-09-29 15:07:43 +08:00
if ($connection->getOption("type") === CONN_WEBSOCKET)
return $this->processWebsocketAPI($connection, $reply, $function);
else
return $this->processHttpAPI($connection, $reply, $function);
}
2021-09-01 14:14:00 +08:00
private function processWebsocketAPI($connection, $reply, $function = false) {
2020-11-03 21:02:24 +08:00
$api_id = ZMAtomic::get("wait_msg_id")->add(1);
2020-03-02 16:14:20 +08:00
$reply["echo"] = $api_id;
2020-09-29 15:07:43 +08:00
if (server()->push($connection->getFd(), json_encode($reply))) {
2020-03-02 16:14:20 +08:00
if ($function === true) {
2021-03-22 07:44:11 +08:00
$obj = [
"data" => $reply,
"time" => microtime(true),
"self_id" => $connection->getOption("connect_id"),
"echo" => $api_id
];
2021-09-01 14:14:00 +08:00
return CoMessage::yieldByWS($obj, ["echo"], 30);
2020-03-02 16:14:20 +08:00
}
return true;
} else {
2021-06-16 00:17:30 +08:00
Console::warning(zm_internal_errcode("E00036") . "CQAPI send failed, websocket push error.");
2020-03-02 16:14:20 +08:00
$response = [
"status" => "failed",
2020-05-23 17:23:29 +08:00
"retcode" => -1000,
2020-03-02 16:14:20 +08:00
"data" => null,
2020-09-29 15:07:43 +08:00
"self_id" => $connection->getOption("connect_id")
2020-03-02 16:14:20 +08:00
];
2020-11-03 21:02:24 +08:00
SpinLock::lock("wait_api");
$r = LightCacheInside::get("wait_api", "wait_api");
unset($r[$reply["echo"]]);
LightCacheInside::set("wait_api", "wait_api", $r);
SpinLock::unlock("wait_api");
2020-05-23 17:23:29 +08:00
if ($function === true) return $response;
2020-03-02 16:14:20 +08:00
return false;
}
}
2020-09-29 15:07:43 +08:00
2020-11-03 21:02:24 +08:00
/**
* @param $connection
* @param $reply
* @param null $function
* @return bool
* @noinspection PhpUnusedParameterInspection
*/
2021-09-01 14:14:00 +08:00
private function processHttpAPI($connection, $reply, $function = null): bool {
2020-09-29 15:07:43 +08:00
return false;
}
public function __call($name, $arguments) {
return false;
}
}