From 5eea5b75010fe6649254faba1ca7a879f93eb8cc Mon Sep 17 00:00:00 2001 From: levlam Date: Sun, 15 Nov 2020 01:13:11 +0300 Subject: [PATCH] Rename create_client to create_client_id. --- benchmark/check_proxy.cpp | 2 +- example/cpp/td_example.cpp | 2 +- example/cpp/tdjson_example.cpp | 2 +- example/java/td_jni.cpp | 2 +- example/python/tdjson_example.py | 8 ++++---- example/swift/src/main.swift | 2 +- example/web/tdweb/src/worker.js | 6 +++--- td/telegram/Client.cpp | 10 +++++----- td/telegram/Client.h | 23 ++++++++++++++--------- td/telegram/ClientJson.cpp | 4 ++-- td/telegram/ClientJson.h | 2 +- td/telegram/td_emscripten.cpp | 4 ++-- td/telegram/td_json_client.cpp | 4 ++-- td/telegram/td_json_client.h | 11 ++++++----- tdclientjson_export_list | 2 +- test/tdclient.cpp | 6 +++--- 16 files changed, 48 insertions(+), 42 deletions(-) diff --git a/benchmark/check_proxy.cpp b/benchmark/check_proxy.cpp index e1ff5523e..7b9a1480d 100644 --- a/benchmark/check_proxy.cpp +++ b/benchmark/check_proxy.cpp @@ -161,7 +161,7 @@ int main(int argc, char **argv) { SET_VERBOSITY_LEVEL(new_verbosity_level); td::ClientManager client_manager; - auto client_id = client_manager.create_client(); + auto client_id = client_manager.create_client_id(); for (size_t i = 0; i < requests.size(); i++) { auto &request = requests[i].second; request->dc_id_ = dc_id; diff --git a/example/cpp/td_example.cpp b/example/cpp/td_example.cpp index 4bb493857..5715501fa 100644 --- a/example/cpp/td_example.cpp +++ b/example/cpp/td_example.cpp @@ -55,7 +55,7 @@ class TdExample { TdExample() { td::ClientManager::execute(td_api::make_object(1)); client_manager_ = std::make_unique(); - client_id_ = client_manager_->create_client(); + client_id_ = client_manager_->create_client_id(); send_query(td_api::make_object("version"), {}); } diff --git a/example/cpp/tdjson_example.cpp b/example/cpp/tdjson_example.cpp index 62978f178..080de9698 100644 --- a/example/cpp/tdjson_example.cpp +++ b/example/cpp/tdjson_example.cpp @@ -15,7 +15,7 @@ int main() { // disable TDLib logging td_execute("{\"@type\":\"setLogVerbosityLevel\", \"new_verbosity_level\":0}"); - int client_id = td_create_client(); + int client_id = td_create_client_id(); // somehow share the client_id with other threads, which will be able to send requests via td_send // start the client by sending request to it diff --git a/example/java/td_jni.cpp b/example/java/td_jni.cpp index e0106e258..61df8b090 100644 --- a/example/java/td_jni.cpp +++ b/example/java/td_jni.cpp @@ -31,7 +31,7 @@ static td::ClientManager *get_manager() { } static jint Client_createNativeClient(JNIEnv *env, jclass clazz) { - return static_cast(get_manager()->create_client()); + return static_cast(get_manager()->create_client_id()); } static void Client_nativeClientSend(JNIEnv *env, jclass clazz, jint client_id, jlong id, jobject function) { diff --git a/example/python/tdjson_example.py b/example/python/tdjson_example.py index ff1d2dd02..e0f46f82c 100644 --- a/example/python/tdjson_example.py +++ b/example/python/tdjson_example.py @@ -18,9 +18,9 @@ if tdjson_path is None: tdjson = CDLL(tdjson_path) # load TDLib functions from shared library -_td_create_client = tdjson.td_create_client -_td_create_client.restype = c_int -_td_create_client.argtypes = [] +_td_create_client_id = tdjson.td_create_client_id +_td_create_client_id.restype = c_int +_td_create_client_id.argtypes = [] _td_receive = tdjson.td_receive _td_receive.restype = c_char_p @@ -60,7 +60,7 @@ print(str(td_execute({'@type': 'setLogVerbosityLevel', 'new_verbosity_level': 1, # create client -client_id = _td_create_client() +client_id = _td_create_client_id() # simple wrappers for client usage def td_send(query): diff --git a/example/swift/src/main.swift b/example/swift/src/main.swift index ea29b8fec..333301762 100644 --- a/example/swift/src/main.swift +++ b/example/swift/src/main.swift @@ -9,7 +9,7 @@ import Foundation // TDLib Client Swift binding class TdClient { - var client_id = td_create_client()! + var client_id = td_create_client_id()! let tdlibMainLoop = DispatchQueue(label: "TDLib") let tdlibQueryQueue = DispatchQueue(label: "TDLibQuery") var queryF = Dictionary)->()>() diff --git a/example/web/tdweb/src/worker.js b/example/web/tdweb/src/worker.js index ad6c32e0a..363a96e15 100644 --- a/example/web/tdweb/src/worker.js +++ b/example/web/tdweb/src/worker.js @@ -614,7 +614,7 @@ class TdClient { this.TdModule = await loadTdlib(mode, this.onFS, options.wasmUrl); log.info('got TdModule'); this.td_functions = { - td_create: this.TdModule.cwrap('td_emscripten_create', 'number', []), + td_create: this.TdModule.cwrap('td_emscripten_create_client_id', 'number', []), td_send: this.TdModule.cwrap('td_emscripten_send', null, [ 'number', 'string' @@ -679,7 +679,7 @@ class TdClient { options.logVerbosityLevel = 2; } this.td_functions.td_set_verbosity(options.logVerbosityLevel); - this.client = this.td_functions.td_create(); + this.client_id = this.td_functions.td_create(); this.savingFiles = new Map(); this.send({ @@ -842,7 +842,7 @@ class TdClient { return; } query = this.prepareQuery(query); - this.td_functions.td_send(this.client, JSON.stringify(query)); + this.td_functions.td_send(this.client_id, JSON.stringify(query)); this.scheduleReceiveSoon(); } diff --git a/td/telegram/Client.cpp b/td/telegram/Client.cpp index c56fdd82a..3a2058a87 100644 --- a/td/telegram/Client.cpp +++ b/td/telegram/Client.cpp @@ -77,7 +77,7 @@ class TdReceiver { class ClientManager::Impl final { public: - ClientId create_client() { + ClientId create_client_id() { CHECK(client_id_ != std::numeric_limits::max()); auto client_id = ++client_id_; pending_clients_.insert(client_id); @@ -205,7 +205,7 @@ class ClientManager::Impl final { class Client::Impl final { public: - Impl() : client_id_(impl_.create_client()) { + Impl() : client_id_(impl_.create_client_id()) { } void send(Request request) { @@ -455,7 +455,7 @@ class MultiImplPool { class ClientManager::Impl final { public: - ClientId create_client() { + ClientId create_client_id() { auto client_id = MultiImpl::create_id(); { auto lock = impls_mutex_.lock_write().move_as_ok(); @@ -639,8 +639,8 @@ Client &Client::operator=(Client &&other) = default; ClientManager::ClientManager() : impl_(std::make_unique()) { } -ClientManager::ClientId ClientManager::create_client() { - return impl_->create_client(); +ClientManager::ClientId ClientManager::create_client_id() { + return impl_->create_client_id(); } void ClientManager::send(ClientId client_id, RequestId request_id, td_api::object_ptr &&request) { diff --git a/td/telegram/Client.h b/td/telegram/Client.h index ee1e2d755..cbe7f3d25 100644 --- a/td/telegram/Client.h +++ b/td/telegram/Client.h @@ -134,20 +134,24 @@ class Client final { /** * The future native C++ interface for interaction with TDLib. * - * The TDLib client instance is created using the ClientManager::create_client method, returning a client identifier. - * Requests to TDLib can be sent using the ClientManager::send method from any thread. - * New updates and responses to requests can be received using the ClientManager::receive method from any thread, - * this function must not be called simultaneously from two different threads. Also note that all updates and - * responses to requests should be applied in the same order as they were received, to ensure consistency. + * The TDLib client instance is created using the ClientManager::create_client_id method, returning a client identifier. + * Requests to a TDLib client instance can be sent using the ClientManager::send method from any thread. + * New updates and responses to requests can be received using the ClientManager::receive method from any thread + * after a first request is sent to the client instance. ClientManager::receive must not be called simultaneously from + * two different threads. Also note that all updates and responses to requests should be applied in the same order as + * they were received, to ensure consistency. * Some TDLib requests can be executed synchronously from any thread by using the ClientManager::execute method. * * General pattern of usage: * \code * td::ClientManager manager; - * auto client_id = manager.create_client(); + * auto client_id = manager.create_client_id(); * // somehow share the manager and the client_id with other threads, * // which will be able to send requests via manager.send(client_id, ...) * + * // send some dummy requests to the new instance to activate it + * manager.send(client_id, ...); + * * const double WAIT_TIMEOUT = 10.0; // seconds * while (true) { * auto response = manager.receive(WAIT_TIMEOUT); @@ -183,10 +187,11 @@ class ClientManager final { using RequestId = std::uint64_t; /** - * Creates a new TDLib client and returns its opaque identifier. - * The client will not send updates until the first request is sent to it. + * Returns an opaque identifier of a new TDLib instance. + * The TDLib instance will not send updates until the first request is sent to it. + * \return Opaque indentifier of a new TDLib instance. */ - ClientId create_client(); + ClientId create_client_id(); /** * Sends request to TDLib. May be called from any thread. diff --git a/td/telegram/ClientJson.cpp b/td/telegram/ClientJson.cpp index 960b55207..78d34c42a 100644 --- a/td/telegram/ClientJson.cpp +++ b/td/telegram/ClientJson.cpp @@ -125,8 +125,8 @@ static std::mutex extra_mutex; static std::unordered_map extra; static std::atomic extra_id{1}; -int json_create_client() { - return static_cast(get_manager()->create_client()); +int json_create_client_id() { + return static_cast(get_manager()->create_client_id()); } void json_send(int client_id, Slice request) { diff --git a/td/telegram/ClientJson.h b/td/telegram/ClientJson.h index d7bc85125..195fb7078 100644 --- a/td/telegram/ClientJson.h +++ b/td/telegram/ClientJson.h @@ -33,7 +33,7 @@ class ClientJson final { std::atomic extra_id_{1}; }; -int json_create_client(); +int json_create_client_id(); void json_send(int client_id, Slice request); diff --git a/td/telegram/td_emscripten.cpp b/td/telegram/td_emscripten.cpp index 77435bfec..0cedf79e7 100644 --- a/td/telegram/td_emscripten.cpp +++ b/td/telegram/td_emscripten.cpp @@ -13,8 +13,8 @@ extern "C" { -EMSCRIPTEN_KEEPALIVE double td_emscripten_create() { - return td_create_client(); +EMSCRIPTEN_KEEPALIVE double td_emscripten_create_client_id() { + return td_create_client_id(); } EMSCRIPTEN_KEEPALIVE void td_emscripten_send(double client_id, const char *query) { diff --git a/td/telegram/td_json_client.cpp b/td/telegram/td_json_client.cpp index c346a9048..dde62d7a6 100644 --- a/td/telegram/td_json_client.cpp +++ b/td/telegram/td_json_client.cpp @@ -30,8 +30,8 @@ const char *td_json_client_execute(void *client, const char *request) { return td::ClientJson::execute(td::Slice(request == nullptr ? "" : request)); } -int td_create_client() { - return td::json_create_client(); +int td_create_client_id() { + return td::json_create_client_id(); } void td_send(int client_id, const char *request) { diff --git a/td/telegram/td_json_client.h b/td/telegram/td_json_client.h index f98705210..417f92424 100644 --- a/td/telegram/td_json_client.h +++ b/td/telegram/td_json_client.h @@ -102,7 +102,7 @@ TDJSON_EXPORT void td_json_client_destroy(void *client); * Each returned object will have an "@client_id" field, containing and identifier of the client for which * a response or an update is received. * - * A TDLib client instance can be created through td_create_client. + * A TDLib client instance can be created through td_create_client_id. * Requests then can be sent using td_send from any thread and the received client identifier. * New updates and request responses can be received through td_receive from any thread. This function * must not be called simultaneously from two different threads. Also note that all updates and request responses @@ -112,7 +112,7 @@ TDJSON_EXPORT void td_json_client_destroy(void *client); * * General pattern of usage: * \code - * int client_id = td_create_client(); + * int client_id = td_create_client_id(); * // share the client_id with other threads, which will be able to send requests via td_send * * const double WAIT_TIMEOUT = 10.0; // seconds @@ -126,10 +126,11 @@ TDJSON_EXPORT void td_json_client_destroy(void *client); */ /** - * Creates a new instance of TDLib. The TDLib instance will not send updates until the first request is sent to it. - * \return Opaque indentifier of the created TDLib instance. + * Returns an opaque identifier of a new TDLib instance. + * The TDLib instance will not send updates until the first request is sent to it. + * \return Opaque indentifier of a new TDLib instance. */ -TDJSON_EXPORT int td_create_client(); +TDJSON_EXPORT int td_create_client_id(); /** * Sends request to the TDLib client. May be called from any thread. diff --git a/tdclientjson_export_list b/tdclientjson_export_list index 9a1891049..d70976f7d 100644 --- a/tdclientjson_export_list +++ b/tdclientjson_export_list @@ -7,7 +7,7 @@ _td_set_log_file_path _td_set_log_max_file_size _td_set_log_verbosity_level _td_set_log_fatal_error_callback -_td_create_client +_td_create_client_id _td_send _td_receive _td_execute diff --git a/test/tdclient.cpp b/test/tdclient.cpp index 800108b82..1937150ce 100644 --- a/test/tdclient.cpp +++ b/test/tdclient.cpp @@ -942,7 +942,7 @@ TEST(Client, Manager) { for (int i = 0; i < threads_n; i++) { threads.emplace_back([&] { for (int i = 0; i <= clients_n; i++) { - auto id = client.create_client(); + auto id = client.create_client_id(); if (i != 0) { client.send(id, 3, td::make_tl_object(3)); } @@ -1036,7 +1036,7 @@ TEST(Client, ManagerClose) { std::atomic send_count{1}; std::atomic receive_count{0}; td::ClientManager client_manager; - auto client_id = client_manager.create_client(); + auto client_id = client_manager.create_client_id(); std::mutex request_ids_mutex; std::set request_ids; @@ -1141,7 +1141,7 @@ TEST(Client, ManagerCloseOneThread) { receive(); - auto client_id = client_manager.create_client(); + auto client_id = client_manager.create_client_id(); for (td::int32 i = -5; i < 5; i++) { send_request(i, i == client_id ? 0 : (i > 0 && i < client_id ? 500 : 400));