Place ClientManager before deprecated Client interface.

This commit is contained in:
levlam 2021-12-22 14:07:44 +03:00
parent 5280790347
commit 5fd2f22b7c
2 changed files with 117 additions and 117 deletions

View File

@ -17,122 +17,7 @@
namespace td {
/**
* Native C++ interface for interaction with TDLib.
*
* 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:
* \code
* 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 id response.id
* }
* }
* \endcode
*/
class Client final {
public:
/**
* Creates a new TDLib client.
*/
Client();
/**
* A request to the TDLib.
*/
struct Request {
/**
* Request identifier.
* Responses to TDLib requests will have the same id as the corresponding request.
* Updates from TDLib will have id == 0, incoming requests are thus disallowed to have id == 0.
*/
std::uint64_t id;
/**
* TDLib API function representing a request to TDLib.
*/
td_api::object_ptr<td_api::Function> function;
};
/**
* Sends request to TDLib. May be called from any thread.
* \param[in] request Request to TDLib.
*/
void send(Request &&request);
/**
* A response to a request, or an incoming update from TDLib.
*/
struct Response {
/**
* TDLib request identifier, which corresponds to the response, or 0 for incoming updates from TDLib.
*/
std::uint64_t id;
/**
* TDLib API object representing a response to a TDLib request or an incoming update.
*/
td_api::object_ptr<td_api::Object> object;
};
/**
* Receives incoming updates and request responses from TDLib. May be called from any thread, but shouldn't be
* called simultaneously from two different threads.
* \param[in] timeout The maximum number of seconds allowed for this function to wait for new data.
* \return An incoming update or request response. The object returned in the response may be a nullptr
* if the timeout expires.
*/
Response receive(double timeout);
/**
* Synchronously executes TDLib requests. Only a few requests can be executed synchronously.
* May be called from any thread.
* \param[in] request Request to the TDLib.
* \return The request response.
*/
static Response execute(Request &&request);
/**
* Destroys the client and TDLib instance.
*/
~Client();
/**
* Move constructor.
*/
Client(Client &&other) noexcept;
/**
* Move assignment operator.
*/
Client &operator=(Client &&other) noexcept;
private:
class Impl;
std::unique_ptr<Impl> impl_;
};
/**
* The future native C++ interface for interaction with TDLib.
* The native C++ interface for interaction with TDLib.
*
* A TDLib client instance can be created through the method ClientManager::create_client_id.
* Requests can be sent using the method ClientManager::send from any thread.
@ -286,4 +171,119 @@ class ClientManager final {
std::unique_ptr<Impl> impl_;
};
/**
* 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:
* \code
* 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 id response.id
* }
* }
* \endcode
*/
class Client final {
public:
/**
* Creates a new TDLib client.
*/
Client();
/**
* A request to the TDLib.
*/
struct Request {
/**
* Request identifier.
* Responses to TDLib requests will have the same id as the corresponding request.
* Updates from TDLib will have id == 0, incoming requests are thus disallowed to have id == 0.
*/
std::uint64_t id;
/**
* TDLib API function representing a request to TDLib.
*/
td_api::object_ptr<td_api::Function> function;
};
/**
* Sends request to TDLib. May be called from any thread.
* \param[in] request Request to TDLib.
*/
void send(Request &&request);
/**
* A response to a request, or an incoming update from TDLib.
*/
struct Response {
/**
* TDLib request identifier, which corresponds to the response, or 0 for incoming updates from TDLib.
*/
std::uint64_t id;
/**
* TDLib API object representing a response to a TDLib request or an incoming update.
*/
td_api::object_ptr<td_api::Object> object;
};
/**
* Receives incoming updates and request responses from TDLib. May be called from any thread, but shouldn't be
* called simultaneously from two different threads.
* \param[in] timeout The maximum number of seconds allowed for this function to wait for new data.
* \return An incoming update or request response. The object returned in the response may be a nullptr
* if the timeout expires.
*/
Response receive(double timeout);
/**
* Synchronously executes TDLib requests. Only a few requests can be executed synchronously.
* May be called from any thread.
* \param[in] request Request to the TDLib.
* \return The request response.
*/
static Response execute(Request &&request);
/**
* Destroys the client and TDLib instance.
*/
~Client();
/**
* Move constructor.
*/
Client(Client &&other) noexcept;
/**
* Move assignment operator.
*/
Client &operator=(Client &&other) noexcept;
private:
class Impl;
std::unique_ptr<Impl> impl_;
};
} // namespace td

View File

@ -110,7 +110,7 @@ TDJSON_EXPORT void td_set_log_message_callback(int max_verbosity_level, td_log_m
/**
* \file
* Alternatively, you can use old TDLib JSON interface, which will be removed at TDLib 2.0.0.
* Alternatively, you can use old TDLib JSON interface, which will be removed in TDLib 2.0.0.
*
* Objects and functions serialization to JSON is the same for both JSON interfaces.
*