2018-12-31 20:04:05 +01:00
|
|
|
//
|
2022-12-31 22:28:08 +01:00
|
|
|
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2023
|
2018-12-31 20:04:05 +01:00
|
|
|
//
|
|
|
|
// Distributed under the Boost Software License, Version 1.0. (See accompanying
|
|
|
|
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
|
|
|
//
|
|
|
|
#include "td/telegram/ClientJson.h"
|
|
|
|
|
|
|
|
#include "td/telegram/td_api.h"
|
|
|
|
#include "td/telegram/td_api_json.h"
|
|
|
|
|
2019-02-12 22:26:36 +01:00
|
|
|
#include "td/utils/common.h"
|
2022-02-07 22:04:34 +01:00
|
|
|
#include "td/utils/FlatHashMap.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
#include "td/utils/JsonBuilder.h"
|
2018-10-31 15:51:07 +01:00
|
|
|
#include "td/utils/port/thread_local.h"
|
2021-05-17 14:21:11 +02:00
|
|
|
#include "td/utils/SliceBuilder.h"
|
2020-10-29 23:02:44 +01:00
|
|
|
#include "td/utils/StackAllocator.h"
|
2020-10-05 15:08:07 +02:00
|
|
|
#include "td/utils/StringBuilder.h"
|
2018-12-31 20:04:05 +01:00
|
|
|
|
2018-10-31 15:51:07 +01:00
|
|
|
#include <utility>
|
|
|
|
|
2018-12-31 20:04:05 +01:00
|
|
|
namespace td {
|
|
|
|
|
2019-08-06 02:29:16 +02:00
|
|
|
static td_api::object_ptr<td_api::Function> get_return_error_function(Slice error_message) {
|
|
|
|
auto error = td_api::make_object<td_api::error>(400, error_message.str());
|
|
|
|
return td_api::make_object<td_api::testReturnError>(std::move(error));
|
|
|
|
}
|
|
|
|
|
|
|
|
static std::pair<td_api::object_ptr<td_api::Function>, string> to_request(Slice request) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto request_str = request.str();
|
2019-08-06 02:29:16 +02:00
|
|
|
auto r_json_value = json_decode(request_str);
|
|
|
|
if (r_json_value.is_error()) {
|
|
|
|
return {get_return_error_function(PSLICE()
|
|
|
|
<< "Failed to parse request as JSON object: " << r_json_value.error().message()),
|
|
|
|
string()};
|
|
|
|
}
|
|
|
|
auto json_value = r_json_value.move_as_ok();
|
2018-12-31 20:04:05 +01:00
|
|
|
if (json_value.type() != JsonValue::Type::Object) {
|
2019-08-06 02:29:16 +02:00
|
|
|
return {get_return_error_function("Expected a JSON object"), string()};
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-10-31 15:51:07 +01:00
|
|
|
|
|
|
|
string extra;
|
2023-07-31 16:17:52 +02:00
|
|
|
if (json_value.get_object().has_field("@extra")) {
|
|
|
|
extra = json_encode<string>(json_value.get_object().extract_field("@extra"));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
td_api::object_ptr<td_api::Function> func;
|
2019-12-08 08:44:41 +01:00
|
|
|
auto status = from_json(func, std::move(json_value));
|
2019-08-06 02:29:16 +02:00
|
|
|
if (status.is_error()) {
|
2022-10-09 13:15:54 +02:00
|
|
|
return {get_return_error_function(PSLICE() << "Failed to parse JSON object as TDLib request: " << status.message()),
|
2019-08-06 02:29:16 +02:00
|
|
|
std::move(extra)};
|
|
|
|
}
|
|
|
|
return std::make_pair(std::move(func), std::move(extra));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-10-05 15:08:07 +02:00
|
|
|
static string from_response(const td_api::Object &object, const string &extra, int client_id) {
|
2020-10-29 23:02:44 +01:00
|
|
|
auto buf = StackAllocator::alloc(1 << 18);
|
|
|
|
JsonBuilder jb(StringBuilder(buf.as_slice(), true), -1);
|
|
|
|
jb.enter_value() << ToJson(object);
|
2021-07-27 08:41:11 +02:00
|
|
|
auto &sb = jb.string_builder();
|
|
|
|
auto slice = sb.as_cslice();
|
2020-10-29 23:02:44 +01:00
|
|
|
CHECK(!slice.empty() && slice.back() == '}');
|
2021-07-27 08:41:11 +02:00
|
|
|
sb.pop_back();
|
2018-12-31 20:04:05 +01:00
|
|
|
if (!extra.empty()) {
|
2021-07-27 08:41:11 +02:00
|
|
|
sb << ",\"@extra\":" << extra;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2020-10-05 15:08:07 +02:00
|
|
|
if (client_id != 0) {
|
2021-07-27 08:41:11 +02:00
|
|
|
sb << ",\"@client_id\":" << client_id;
|
2020-10-05 15:08:07 +02:00
|
|
|
}
|
2021-07-27 08:41:11 +02:00
|
|
|
sb << '}';
|
|
|
|
return sb.as_cslice().str();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-10-05 15:08:07 +02:00
|
|
|
static TD_THREAD_LOCAL string *current_output;
|
2018-10-31 15:51:07 +01:00
|
|
|
|
2020-10-05 15:08:07 +02:00
|
|
|
static const char *store_string(string str) {
|
|
|
|
init_thread_local<string>(current_output);
|
2018-10-31 15:51:07 +01:00
|
|
|
*current_output = std::move(str);
|
2020-10-05 14:48:37 +02:00
|
|
|
return current_output->c_str();
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void ClientJson::send(Slice request) {
|
2019-08-06 02:29:16 +02:00
|
|
|
auto parsed_request = to_request(request);
|
2018-10-31 15:51:07 +01:00
|
|
|
std::uint64_t extra_id = extra_id_.fetch_add(1, std::memory_order_relaxed);
|
2019-08-06 02:29:16 +02:00
|
|
|
if (!parsed_request.second.empty()) {
|
2018-10-31 15:51:07 +01:00
|
|
|
std::lock_guard<std::mutex> guard(mutex_);
|
2019-08-06 02:29:16 +02:00
|
|
|
extra_[extra_id] = std::move(parsed_request.second);
|
2018-10-31 15:51:07 +01:00
|
|
|
}
|
2019-08-06 02:29:16 +02:00
|
|
|
client_.send(Client::Request{extra_id, std::move(parsed_request.first)});
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-10-05 14:48:37 +02:00
|
|
|
const char *ClientJson::receive(double timeout) {
|
2018-12-31 20:04:05 +01:00
|
|
|
auto response = client_.receive(timeout);
|
2020-10-05 15:08:07 +02:00
|
|
|
if (response.object == nullptr) {
|
2020-10-05 14:48:37 +02:00
|
|
|
return nullptr;
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
2018-10-31 15:51:07 +01:00
|
|
|
|
2020-10-05 15:08:07 +02:00
|
|
|
string extra;
|
2018-10-31 15:51:07 +01:00
|
|
|
if (response.id != 0) {
|
|
|
|
std::lock_guard<std::mutex> guard(mutex_);
|
|
|
|
auto it = extra_.find(response.id);
|
|
|
|
if (it != extra_.end()) {
|
|
|
|
extra = std::move(it->second);
|
|
|
|
extra_.erase(it);
|
|
|
|
}
|
|
|
|
}
|
2020-10-05 15:08:07 +02:00
|
|
|
return store_string(from_response(*response.object, extra, 0));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
2020-10-05 14:48:37 +02:00
|
|
|
const char *ClientJson::execute(Slice request) {
|
2019-08-06 02:29:16 +02:00
|
|
|
auto parsed_request = to_request(request);
|
|
|
|
return store_string(from_response(*Client::execute(Client::Request{0, std::move(parsed_request.first)}).object,
|
2020-10-05 15:08:07 +02:00
|
|
|
parsed_request.second, 0));
|
|
|
|
}
|
|
|
|
|
|
|
|
static ClientManager *get_manager() {
|
2020-10-11 10:08:56 +02:00
|
|
|
return ClientManager::get_manager_singleton();
|
2020-10-05 15:08:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static std::mutex extra_mutex;
|
2022-02-07 20:41:07 +01:00
|
|
|
static FlatHashMap<int64, string> extra;
|
2020-10-05 15:08:07 +02:00
|
|
|
static std::atomic<uint64> extra_id{1};
|
|
|
|
|
2020-11-14 23:13:11 +01:00
|
|
|
int json_create_client_id() {
|
|
|
|
return static_cast<int>(get_manager()->create_client_id());
|
2020-10-05 15:08:07 +02:00
|
|
|
}
|
|
|
|
|
2020-11-12 12:45:18 +01:00
|
|
|
void json_send(int client_id, Slice request) {
|
2020-10-05 15:08:07 +02:00
|
|
|
auto parsed_request = to_request(request);
|
|
|
|
auto request_id = extra_id.fetch_add(1, std::memory_order_relaxed);
|
|
|
|
if (!parsed_request.second.empty()) {
|
|
|
|
std::lock_guard<std::mutex> guard(extra_mutex);
|
|
|
|
extra[request_id] = std::move(parsed_request.second);
|
|
|
|
}
|
|
|
|
get_manager()->send(client_id, request_id, std::move(parsed_request.first));
|
|
|
|
}
|
|
|
|
|
2020-11-12 12:45:18 +01:00
|
|
|
const char *json_receive(double timeout) {
|
2020-10-05 15:08:07 +02:00
|
|
|
auto response = get_manager()->receive(timeout);
|
|
|
|
if (!response.object) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
string extra_str;
|
|
|
|
if (response.request_id != 0) {
|
|
|
|
std::lock_guard<std::mutex> guard(extra_mutex);
|
|
|
|
auto it = extra.find(response.request_id);
|
|
|
|
if (it != extra.end()) {
|
|
|
|
extra_str = std::move(it->second);
|
|
|
|
extra.erase(it);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return store_string(from_response(*response.object, extra_str, response.client_id));
|
|
|
|
}
|
|
|
|
|
2020-11-12 12:45:18 +01:00
|
|
|
const char *json_execute(Slice request) {
|
2020-10-05 15:08:07 +02:00
|
|
|
auto parsed_request = to_request(request);
|
|
|
|
return store_string(
|
|
|
|
from_response(*ClientManager::execute(std::move(parsed_request.first)), parsed_request.second, 0));
|
2018-12-31 20:04:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace td
|