Skip to content

Commit

Permalink
Rename create_client to create_client_id.
Browse files Browse the repository at this point in the history
  • Loading branch information
levlam committed Nov 14, 2020
1 parent 21f6ddc commit 5eea5b7
Show file tree
Hide file tree
Showing 16 changed files with 48 additions and 42 deletions.
2 changes: 1 addition & 1 deletion benchmark/check_proxy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion example/cpp/td_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class TdExample {
TdExample() {
td::ClientManager::execute(td_api::make_object<td_api::setLogVerbosityLevel>(1));
client_manager_ = std::make_unique<td::ClientManager>();
client_id_ = client_manager_->create_client();
client_id_ = client_manager_->create_client_id();
send_query(td_api::make_object<td_api::getOption>("version"), {});
}

Expand Down
2 changes: 1 addition & 1 deletion example/cpp/tdjson_example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion example/java/td_jni.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ static td::ClientManager *get_manager() {
}

static jint Client_createNativeClient(JNIEnv *env, jclass clazz) {
return static_cast<jint>(get_manager()->create_client());
return static_cast<jint>(get_manager()->create_client_id());
}

static void Client_nativeClientSend(JNIEnv *env, jclass clazz, jint client_id, jlong id, jobject function) {
Expand Down
8 changes: 4 additions & 4 deletions example/python/tdjson_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@
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
Expand Down Expand Up @@ -60,7 +60,7 @@ def td_execute(query):


# create client
client_id = _td_create_client()
client_id = _td_create_client_id()

# simple wrappers for client usage
def td_send(query):
Expand Down
2 changes: 1 addition & 1 deletion example/swift/src/main.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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<Int64, (Dictionary<String,Any>)->()>()
Expand Down
6 changes: 3 additions & 3 deletions example/web/tdweb/src/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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();
}

Expand Down
10 changes: 5 additions & 5 deletions td/telegram/Client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class TdReceiver {

class ClientManager::Impl final {
public:
ClientId create_client() {
ClientId create_client_id() {
CHECK(client_id_ != std::numeric_limits<ClientId>::max());
auto client_id = ++client_id_;
pending_clients_.insert(client_id);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -639,8 +639,8 @@ Client &Client::operator=(Client &&other) = default;
ClientManager::ClientManager() : impl_(std::make_unique<Impl>()) {
}

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<td_api::Function> &&request) {
Expand Down
23 changes: 14 additions & 9 deletions td/telegram/Client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions td/telegram/ClientJson.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,8 +125,8 @@ static std::mutex extra_mutex;
static std::unordered_map<int64, string> extra;
static std::atomic<uint64> extra_id{1};

int json_create_client() {
return static_cast<int>(get_manager()->create_client());
int json_create_client_id() {
return static_cast<int>(get_manager()->create_client_id());
}

void json_send(int client_id, Slice request) {
Expand Down
2 changes: 1 addition & 1 deletion td/telegram/ClientJson.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class ClientJson final {
std::atomic<std::uint64_t> extra_id_{1};
};

int json_create_client();
int json_create_client_id();

void json_send(int client_id, Slice request);

Expand Down
4 changes: 2 additions & 2 deletions td/telegram/td_emscripten.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
4 changes: 2 additions & 2 deletions td/telegram/td_json_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
11 changes: 6 additions & 5 deletions td/telegram/td_json_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion tdclientjson_export_list
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 3 additions & 3 deletions test/tdclient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<td::td_api::testSquareInt>(3));
}
Expand Down Expand Up @@ -1036,7 +1036,7 @@ TEST(Client, ManagerClose) {
std::atomic<td::int64> send_count{1};
std::atomic<td::int64> 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<td::uint64> request_ids;
Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 5eea5b7

Please sign in to comment.