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

89 lines
2.8 KiB
PHP
Raw Normal View History

2020-03-02 16:14:20 +08:00
<?php
namespace ZM\API;
use Co;
2020-08-31 10:11:06 +08:00
use ZM\ConnectionManager\ConnectionObject;
use ZM\Console\Console;
2020-05-08 16:37:38 +08:00
use ZM\Event\EventHandler;
2020-08-31 10:11:06 +08:00
use ZM\Store\ZMBuf;
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
{
/**
2020-08-31 10:11:06 +08:00
* @param ConnectionObject $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) {
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);
}
public function processWebsocketAPI($connection, $reply, $function = null) {
$api_id = ZMBuf::atomic("wait_msg_id")->get();
2020-03-02 16:14:20 +08:00
$reply["echo"] = $api_id;
2020-09-29 15:07:43 +08:00
ZMBuf::atomic("wait_msg_id")->add(1);
2020-05-08 16:37:38 +08:00
EventHandler::callCQAPISend($reply, $connection);
2020-03-02 16:14:20 +08:00
if (is_callable($function)) {
ZMBuf::appendKey("sent_api", $api_id, [
"data" => $reply,
"time" => microtime(true),
"func" => $function,
2020-09-29 15:07:43 +08:00
"self_id" => $connection->getOption("connect_id")
2020-03-02 16:14:20 +08:00
]);
} elseif ($function === true) {
ZMBuf::appendKey("sent_api", $api_id, [
"data" => $reply,
"time" => microtime(true),
"coroutine" => Co::getuid(),
2020-09-29 15:07:43 +08:00
"self_id" => $connection->getOption("connect_id")
2020-03-02 16:14:20 +08:00
]);
} else {
ZMBuf::appendKey("sent_api", $api_id, [
"data" => $reply,
"time" => microtime(true),
2020-09-29 15:07:43 +08:00
"self_id" => $connection->getOption("connect_id")
2020-03-02 16:14:20 +08:00
]);
}
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) {
Co::suspend();
$data = ZMBuf::get("sent_api")[$api_id];
ZMBuf::unsetByValue("sent_api", $reply["echo"]);
return isset($data['result']) ? $data['result'] : null;
}
return true;
} else {
Console::warning("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
];
$s = ZMBuf::get("sent_api")[$reply["echo"]];
if (($s["func"] ?? null) !== null)
call_user_func($s["func"], $response, $reply);
ZMBuf::unsetByValue("sent_api", $reply["echo"]);
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
public function processHttpAPI($connection, $reply, $function = null) {
return false;
}
public function __call($name, $arguments) {
return false;
}
}