TDLib
Loading...
Searching...
No Matches
td::Client Class Referencefinal

Classes

struct  Request
 
struct  Response
 

Public Member Functions

 Client ()
 
void send (Request &&request)
 
Response receive (double timeout)
 
 ~Client ()
 
 Client (Client &&other) noexcept
 
Clientoperator= (Client &&other) noexcept
 

Static Public Member Functions

static Response execute (Request &&request)
 

Detailed Description

Old native C++ interface for interaction with TDLib to be removed in TDLib 2.0.0.

The TDLib instance is created for the lifetime of the Client object. Requests to TDLib can be sent using the Client::send method from any thread. New updates and responses to requests can be received using the Client::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. Given this information, it's advisable to call this function from a dedicated thread. Some service TDLib requests can be executed synchronously from any thread using the Client::execute method.

General pattern of usage:

std::shared_ptr<td::Client> client = std::make_shared<td::Client>();
// somehow share the client with other threads, which will be able to send requests via client->send
const double WAIT_TIMEOUT = 10.0; // seconds
bool is_closed = false; // should be set to true, when updateAuthorizationState with
// authorizationStateClosed is received
while (!is_closed) {
auto response = client->receive(WAIT_TIMEOUT);
if (response.object == nullptr) {
continue;
}
if (response.id == 0) {
// process response.object as an incoming update of type td_api::Update
} else {
// process response.object as an answer to a sent request with identifier response.id
}
}

Constructor & Destructor Documentation

◆ Client() [1/2]

td::Client::Client ( )

Creates a new TDLib client.

◆ ~Client()

td::Client::~Client ( )

Destroys the client and TDLib instance.

◆ Client() [2/2]

td::Client::Client ( Client &&  other)
noexcept

Move constructor.

Member Function Documentation

◆ execute()

static Response td::Client::execute ( Request &&  request)
static

Synchronously executes TDLib requests. Only a few requests can be executed synchronously. May be called from any thread.

Parameters
[in]requestRequest to the TDLib.
Returns
The request response.

◆ operator=()

Client & td::Client::operator= ( Client &&  other)
noexcept

Move assignment operator.

◆ receive()

Response td::Client::receive ( double  timeout)

Receives incoming updates and request responses from TDLib. May be called from any thread, but shouldn't be called simultaneously from two different threads.

Parameters
[in]timeoutThe maximum number of seconds allowed for this function to wait for new data.
Returns
An incoming update or request response. The object returned in the response may be a nullptr if the timeout expires.

◆ send()

void td::Client::send ( Request &&  request)

Sends request to TDLib. May be called from any thread.

Parameters
[in]requestRequest to TDLib.

The documentation for this class was generated from the following file: