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

88 lines
2.7 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-11-03 21:02:24 +08:00
use ZM\Store\LightCacheInside;
use ZM\Store\Lock\SpinLock;
use ZM\Store\ZMAtomic;
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);
}
2020-10-03 23:00:18 +08:00
public 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-11-03 21:02:24 +08:00
SpinLock::lock("wait_api");
$r = LightCacheInside::get("wait_api", "wait_api");
$r[$api_id] = [
"data" => $reply,
"time" => microtime(true),
"self_id" => $connection->getOption("connect_id")
];
if ($function === true) $r[$api_id]["coroutine"] = Co::getuid();
LightCacheInside::set("wait_api", "wait_api", $r);
SpinLock::unlock("wait_api");
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();
2020-11-03 21:02:24 +08:00
SpinLock::lock("wait_api");
$r = LightCacheInside::get("wait_api", "wait_api");
$data = $r[$api_id];
unset($r[$api_id]);
LightCacheInside::set("wait_api", "wait_api", $r);
SpinLock::unlock("wait_api");
2020-03-02 16:14:20 +08:00
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
];
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
*/
2020-09-29 15:07:43 +08:00
public function processHttpAPI($connection, $reply, $function = null) {
return false;
}
public function __call($name, $arguments) {
return false;
}
}