forked from ars3niy/tdlib-purple
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathtransceiver.h
65 lines (56 loc) · 2.47 KB
/
transceiver.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#ifndef _TRANSCEIVER_H
#define _TRANSCEIVER_H
#include <td/telegram/Client.h>
#include <td/telegram/td_api.hpp>
#include <thread>
#include <mutex>
#include <map>
#include <atomic>
#include <purple.h>
#include <functional>
class PurpleTdClient;
class TdTransceiverImpl;
class TdTransceiver;
class ITransceiverBackend {
public:
virtual ~ITransceiverBackend() {}
void setOwner(TdTransceiver *owner) { m_owner = owner; }
virtual void send(td::Client::Request &&request) = 0;
virtual guint addTimeout(guint interval, GSourceFunc function, gpointer data) = 0;
virtual void cancelTimer(guint id) = 0;
void receive(td::Client::Response response);
private:
TdTransceiver *m_owner = nullptr;
};
// A wrapper around td::Client which processes incoming events (updates and responses to requests)
// in glib main thread using idle function, and also provides request-id-to-callback mapping
class TdTransceiver {
friend class ITransceiverBackend;
private:
using TdObjectPtr = td::td_api::object_ptr<td::td_api::Object>;
public:
using ResponseCb = void (PurpleTdClient::*)(uint64_t requestId, TdObjectPtr object);
using ResponseCb2 = std::function<void(uint64_t, TdObjectPtr)>;
using UpdateCb = void (PurpleTdClient::*)(td::td_api::Object &object);
TdTransceiver(PurpleTdClient *owner, PurpleAccount *account, UpdateCb updateCb,
ITransceiverBackend *testBackend);
~TdTransceiver();
uint64_t sendQuery(td::td_api::object_ptr<td::td_api::Function> f, ResponseCb handler);
uint64_t sendQuery(td::td_api::object_ptr<td::td_api::Function> f, ResponseCb2 handler);
uint64_t sendQueryWithTimeout(td::td_api::object_ptr<td::td_api::Function> f,
ResponseCb2 handler, unsigned timeoutSeconds);
void setQueryTimer(uint64_t queryId, ResponseCb handler, unsigned timeoutSeconds,
bool cancelNormalResponse);
void setQueryTimer(uint64_t queryId, ResponseCb2 handler, unsigned timeoutSeconds,
bool cancelNormalResponse);
private:
void pollThreadLoop();
void *queueResponse(td::Client::Response &&response);
static gboolean timerCallback(gpointer userdata);
std::shared_ptr<TdTransceiverImpl> m_impl;
PurpleAccount *m_account;
std::thread m_pollThread;
std::atomic_bool m_stopThread;
ITransceiverBackend *m_testBackend;
};
#endif