Rename create_client to create_client_id.
This commit is contained in:
parent
21f6ddc6fb
commit
5eea5b7501
@ -161,7 +161,7 @@ int main(int argc, char **argv) {
|
|||||||
SET_VERBOSITY_LEVEL(new_verbosity_level);
|
SET_VERBOSITY_LEVEL(new_verbosity_level);
|
||||||
|
|
||||||
td::ClientManager client_manager;
|
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++) {
|
for (size_t i = 0; i < requests.size(); i++) {
|
||||||
auto &request = requests[i].second;
|
auto &request = requests[i].second;
|
||||||
request->dc_id_ = dc_id;
|
request->dc_id_ = dc_id;
|
||||||
|
@ -55,7 +55,7 @@ class TdExample {
|
|||||||
TdExample() {
|
TdExample() {
|
||||||
td::ClientManager::execute(td_api::make_object<td_api::setLogVerbosityLevel>(1));
|
td::ClientManager::execute(td_api::make_object<td_api::setLogVerbosityLevel>(1));
|
||||||
client_manager_ = std::make_unique<td::ClientManager>();
|
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"), {});
|
send_query(td_api::make_object<td_api::getOption>("version"), {});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -15,7 +15,7 @@ int main() {
|
|||||||
// disable TDLib logging
|
// disable TDLib logging
|
||||||
td_execute("{\"@type\":\"setLogVerbosityLevel\", \"new_verbosity_level\":0}");
|
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
|
// 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
|
// start the client by sending request to it
|
||||||
|
@ -31,7 +31,7 @@ static td::ClientManager *get_manager() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
static jint Client_createNativeClient(JNIEnv *env, jclass clazz) {
|
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) {
|
static void Client_nativeClientSend(JNIEnv *env, jclass clazz, jint client_id, jlong id, jobject function) {
|
||||||
|
@ -18,9 +18,9 @@ if tdjson_path is None:
|
|||||||
tdjson = CDLL(tdjson_path)
|
tdjson = CDLL(tdjson_path)
|
||||||
|
|
||||||
# load TDLib functions from shared library
|
# load TDLib functions from shared library
|
||||||
_td_create_client = tdjson.td_create_client
|
_td_create_client_id = tdjson.td_create_client_id
|
||||||
_td_create_client.restype = c_int
|
_td_create_client_id.restype = c_int
|
||||||
_td_create_client.argtypes = []
|
_td_create_client_id.argtypes = []
|
||||||
|
|
||||||
_td_receive = tdjson.td_receive
|
_td_receive = tdjson.td_receive
|
||||||
_td_receive.restype = c_char_p
|
_td_receive.restype = c_char_p
|
||||||
@ -60,7 +60,7 @@ print(str(td_execute({'@type': 'setLogVerbosityLevel', 'new_verbosity_level': 1,
|
|||||||
|
|
||||||
|
|
||||||
# create client
|
# create client
|
||||||
client_id = _td_create_client()
|
client_id = _td_create_client_id()
|
||||||
|
|
||||||
# simple wrappers for client usage
|
# simple wrappers for client usage
|
||||||
def td_send(query):
|
def td_send(query):
|
||||||
|
@ -9,7 +9,7 @@ import Foundation
|
|||||||
|
|
||||||
// TDLib Client Swift binding
|
// TDLib Client Swift binding
|
||||||
class TdClient {
|
class TdClient {
|
||||||
var client_id = td_create_client()!
|
var client_id = td_create_client_id()!
|
||||||
let tdlibMainLoop = DispatchQueue(label: "TDLib")
|
let tdlibMainLoop = DispatchQueue(label: "TDLib")
|
||||||
let tdlibQueryQueue = DispatchQueue(label: "TDLibQuery")
|
let tdlibQueryQueue = DispatchQueue(label: "TDLibQuery")
|
||||||
var queryF = Dictionary<Int64, (Dictionary<String,Any>)->()>()
|
var queryF = Dictionary<Int64, (Dictionary<String,Any>)->()>()
|
||||||
|
@ -614,7 +614,7 @@ class TdClient {
|
|||||||
this.TdModule = await loadTdlib(mode, this.onFS, options.wasmUrl);
|
this.TdModule = await loadTdlib(mode, this.onFS, options.wasmUrl);
|
||||||
log.info('got TdModule');
|
log.info('got TdModule');
|
||||||
this.td_functions = {
|
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, [
|
td_send: this.TdModule.cwrap('td_emscripten_send', null, [
|
||||||
'number',
|
'number',
|
||||||
'string'
|
'string'
|
||||||
@ -679,7 +679,7 @@ class TdClient {
|
|||||||
options.logVerbosityLevel = 2;
|
options.logVerbosityLevel = 2;
|
||||||
}
|
}
|
||||||
this.td_functions.td_set_verbosity(options.logVerbosityLevel);
|
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.savingFiles = new Map();
|
||||||
this.send({
|
this.send({
|
||||||
@ -842,7 +842,7 @@ class TdClient {
|
|||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
query = this.prepareQuery(query);
|
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();
|
this.scheduleReceiveSoon();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -77,7 +77,7 @@ class TdReceiver {
|
|||||||
|
|
||||||
class ClientManager::Impl final {
|
class ClientManager::Impl final {
|
||||||
public:
|
public:
|
||||||
ClientId create_client() {
|
ClientId create_client_id() {
|
||||||
CHECK(client_id_ != std::numeric_limits<ClientId>::max());
|
CHECK(client_id_ != std::numeric_limits<ClientId>::max());
|
||||||
auto client_id = ++client_id_;
|
auto client_id = ++client_id_;
|
||||||
pending_clients_.insert(client_id);
|
pending_clients_.insert(client_id);
|
||||||
@ -205,7 +205,7 @@ class ClientManager::Impl final {
|
|||||||
|
|
||||||
class Client::Impl final {
|
class Client::Impl final {
|
||||||
public:
|
public:
|
||||||
Impl() : client_id_(impl_.create_client()) {
|
Impl() : client_id_(impl_.create_client_id()) {
|
||||||
}
|
}
|
||||||
|
|
||||||
void send(Request request) {
|
void send(Request request) {
|
||||||
@ -455,7 +455,7 @@ class MultiImplPool {
|
|||||||
|
|
||||||
class ClientManager::Impl final {
|
class ClientManager::Impl final {
|
||||||
public:
|
public:
|
||||||
ClientId create_client() {
|
ClientId create_client_id() {
|
||||||
auto client_id = MultiImpl::create_id();
|
auto client_id = MultiImpl::create_id();
|
||||||
{
|
{
|
||||||
auto lock = impls_mutex_.lock_write().move_as_ok();
|
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<Impl>()) {
|
ClientManager::ClientManager() : impl_(std::make_unique<Impl>()) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ClientManager::ClientId ClientManager::create_client() {
|
ClientManager::ClientId ClientManager::create_client_id() {
|
||||||
return impl_->create_client();
|
return impl_->create_client_id();
|
||||||
}
|
}
|
||||||
|
|
||||||
void ClientManager::send(ClientId client_id, RequestId request_id, td_api::object_ptr<td_api::Function> &&request) {
|
void ClientManager::send(ClientId client_id, RequestId request_id, td_api::object_ptr<td_api::Function> &&request) {
|
||||||
|
@ -134,20 +134,24 @@ class Client final {
|
|||||||
/**
|
/**
|
||||||
* The future native C++ interface for interaction with TDLib.
|
* 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.
|
* The TDLib client instance is created using the ClientManager::create_client_id method, returning a client identifier.
|
||||||
* Requests to TDLib can be sent using the ClientManager::send method from any thread.
|
* 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,
|
* 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
|
* after a first request is sent to the client instance. ClientManager::receive must not be called simultaneously from
|
||||||
* responses to requests should be applied in the same order as they were received, to ensure consistency.
|
* 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.
|
* Some TDLib requests can be executed synchronously from any thread by using the ClientManager::execute method.
|
||||||
*
|
*
|
||||||
* General pattern of usage:
|
* General pattern of usage:
|
||||||
* \code
|
* \code
|
||||||
* td::ClientManager manager;
|
* 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,
|
* // somehow share the manager and the client_id with other threads,
|
||||||
* // which will be able to send requests via manager.send(client_id, ...)
|
* // 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
|
* const double WAIT_TIMEOUT = 10.0; // seconds
|
||||||
* while (true) {
|
* while (true) {
|
||||||
* auto response = manager.receive(WAIT_TIMEOUT);
|
* auto response = manager.receive(WAIT_TIMEOUT);
|
||||||
@ -183,10 +187,11 @@ class ClientManager final {
|
|||||||
using RequestId = std::uint64_t;
|
using RequestId = std::uint64_t;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a new TDLib client and returns its opaque identifier.
|
* Returns an opaque identifier of a new TDLib instance.
|
||||||
* The client will not send updates until the first request is sent to it.
|
* 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.
|
* Sends request to TDLib. May be called from any thread.
|
||||||
|
@ -125,8 +125,8 @@ static std::mutex extra_mutex;
|
|||||||
static std::unordered_map<int64, string> extra;
|
static std::unordered_map<int64, string> extra;
|
||||||
static std::atomic<uint64> extra_id{1};
|
static std::atomic<uint64> extra_id{1};
|
||||||
|
|
||||||
int json_create_client() {
|
int json_create_client_id() {
|
||||||
return static_cast<int>(get_manager()->create_client());
|
return static_cast<int>(get_manager()->create_client_id());
|
||||||
}
|
}
|
||||||
|
|
||||||
void json_send(int client_id, Slice request) {
|
void json_send(int client_id, Slice request) {
|
||||||
|
@ -33,7 +33,7 @@ class ClientJson final {
|
|||||||
std::atomic<std::uint64_t> extra_id_{1};
|
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);
|
void json_send(int client_id, Slice request);
|
||||||
|
|
||||||
|
@ -13,8 +13,8 @@
|
|||||||
|
|
||||||
extern "C" {
|
extern "C" {
|
||||||
|
|
||||||
EMSCRIPTEN_KEEPALIVE double td_emscripten_create() {
|
EMSCRIPTEN_KEEPALIVE double td_emscripten_create_client_id() {
|
||||||
return td_create_client();
|
return td_create_client_id();
|
||||||
}
|
}
|
||||||
|
|
||||||
EMSCRIPTEN_KEEPALIVE void td_emscripten_send(double client_id, const char *query) {
|
EMSCRIPTEN_KEEPALIVE void td_emscripten_send(double client_id, const char *query) {
|
||||||
|
@ -30,8 +30,8 @@ const char *td_json_client_execute(void *client, const char *request) {
|
|||||||
return td::ClientJson::execute(td::Slice(request == nullptr ? "" : request));
|
return td::ClientJson::execute(td::Slice(request == nullptr ? "" : request));
|
||||||
}
|
}
|
||||||
|
|
||||||
int td_create_client() {
|
int td_create_client_id() {
|
||||||
return td::json_create_client();
|
return td::json_create_client_id();
|
||||||
}
|
}
|
||||||
|
|
||||||
void td_send(int client_id, const char *request) {
|
void td_send(int client_id, const char *request) {
|
||||||
|
@ -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
|
* 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 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.
|
* 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
|
* 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
|
* 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:
|
* General pattern of usage:
|
||||||
* \code
|
* \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
|
* // share the client_id with other threads, which will be able to send requests via td_send
|
||||||
*
|
*
|
||||||
* const double WAIT_TIMEOUT = 10.0; // seconds
|
* 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.
|
* Returns an opaque identifier of a new TDLib instance.
|
||||||
* \return Opaque indentifier of the created 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.
|
* Sends request to the TDLib client. May be called from any thread.
|
||||||
|
@ -7,7 +7,7 @@ _td_set_log_file_path
|
|||||||
_td_set_log_max_file_size
|
_td_set_log_max_file_size
|
||||||
_td_set_log_verbosity_level
|
_td_set_log_verbosity_level
|
||||||
_td_set_log_fatal_error_callback
|
_td_set_log_fatal_error_callback
|
||||||
_td_create_client
|
_td_create_client_id
|
||||||
_td_send
|
_td_send
|
||||||
_td_receive
|
_td_receive
|
||||||
_td_execute
|
_td_execute
|
||||||
|
@ -942,7 +942,7 @@ TEST(Client, Manager) {
|
|||||||
for (int i = 0; i < threads_n; i++) {
|
for (int i = 0; i < threads_n; i++) {
|
||||||
threads.emplace_back([&] {
|
threads.emplace_back([&] {
|
||||||
for (int i = 0; i <= clients_n; i++) {
|
for (int i = 0; i <= clients_n; i++) {
|
||||||
auto id = client.create_client();
|
auto id = client.create_client_id();
|
||||||
if (i != 0) {
|
if (i != 0) {
|
||||||
client.send(id, 3, td::make_tl_object<td::td_api::testSquareInt>(3));
|
client.send(id, 3, td::make_tl_object<td::td_api::testSquareInt>(3));
|
||||||
}
|
}
|
||||||
@ -1036,7 +1036,7 @@ TEST(Client, ManagerClose) {
|
|||||||
std::atomic<td::int64> send_count{1};
|
std::atomic<td::int64> send_count{1};
|
||||||
std::atomic<td::int64> receive_count{0};
|
std::atomic<td::int64> receive_count{0};
|
||||||
td::ClientManager client_manager;
|
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::mutex request_ids_mutex;
|
||||||
std::set<td::uint64> request_ids;
|
std::set<td::uint64> request_ids;
|
||||||
@ -1141,7 +1141,7 @@ TEST(Client, ManagerCloseOneThread) {
|
|||||||
|
|
||||||
receive();
|
receive();
|
||||||
|
|
||||||
auto client_id = client_manager.create_client();
|
auto client_id = client_manager.create_client_id();
|
||||||
|
|
||||||
for (td::int32 i = -5; i < 5; i++) {
|
for (td::int32 i = -5; i < 5; i++) {
|
||||||
send_request(i, i == client_id ? 0 : (i > 0 && i < client_id ? 500 : 400));
|
send_request(i, i == client_id ? 0 : (i > 0 && i < client_id ? 500 : 400));
|
||||||
|
Loading…
Reference in New Issue
Block a user