#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "common.h" namespace td_api = td::td_api; using TgObject = td_api::object_ptr; class TelegramClient { public: TelegramClient(uv_loop_t *eventLoop); ~TelegramClient(); void add_update_handler(std::function handler, void *context); void send_query(td_api::object_ptr f, std::function callback); bool start(); private: static void uv_callback(uv_async_t *h); static void uv_close_handle_callback(uv_handle_t *h); void run_thread(); void run_telegram_main_loop(); bool process_response(td::ClientManager::Response response); void dispatch_response(std::uint64_t queryId, TgObject response); void dispatch_update(TgObject &update); bool m_running = false; bool m_authorized = false; std::uint64_t m_nextQueryId = 0; std::map> m_handlers; std::vector, void*>> m_updateHandlers; std::thread m_thread; std::unique_ptr m_clientManager; std::int32_t m_clientId; uv_loop_t *m_eventLoop; uv_async_t *m_uvHandle = nullptr; std::string m_phoneNumber; std::string m_password; std::deque m_responseQueue; std::mutex m_responseQueueMutex; };