-
Notifications
You must be signed in to change notification settings - Fork 3
/
connection.h
222 lines (184 loc) · 7.63 KB
/
connection.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#ifndef __DBUS2VDR_CONNECTION_H
#define __DBUS2VDR_CONNECTION_H
#include <gio/gio.h>
#include <vdr/tools.h>
class cDBusObject;
class cDBusTcpAddress;
class cDBusConnection;
typedef void (*cDBusNameAcquiredFunc)(cDBusConnection *Connection, gpointer UserData);
typedef void (*cDBusNameLostFunc)(cDBusConnection *Connection, gpointer UserData);
typedef void (*cDBusOnConnectFunc)(cDBusConnection *Connection, gpointer UserData);
typedef void (*cDBusOnDisconnectFunc)(cDBusConnection *Connection, gpointer UserData);
typedef void (*cDBusMethodReplyFunc)(GVariant *Reply, gpointer UserData);
typedef void (*cDBusOnSignalFunc)(const gchar *SenderName, const gchar *ObjectPath, const gchar *Interface, const gchar *Signal, GVariant *Parameters, gpointer UserData);
class cDBusSignal : public cListObject
{
friend class cDBusConnection;
private:
cDBusConnection *_connection;
guint _subscription_id;
cDBusOnSignalFunc _on_signal;
gpointer _on_signal_user_data;
gchar *_busname;
gchar *_object_path;
gchar *_interface;
gchar *_signal;
GVariant *_parameters;
public:
cDBusSignal(const char *Busname, const char *ObjectPath, const char *Interface, const char *Signal, GVariant *Parameters, cDBusOnSignalFunc OnSignal, gpointer UserData);
virtual ~cDBusSignal(void);
const char *Busname(void) const { return _busname; }
const char *ObjectPath(void) const { return _object_path; }
const char *Interface(void) const { return _interface; }
const char *Signal(void) const { return _signal; }
GVariant *Parameters(void) const { return _parameters; }
};
class cDBusMethodCall : public cListObject
{
friend class cDBusConnection;
private:
cDBusConnection *_connection;
gchar *_busname;
gchar *_object_path;
gchar *_interface;
gchar *_method;
GVariant *_parameters;
cDBusMethodReplyFunc _on_reply;
gpointer _on_reply_user_data;
public:
cDBusMethodCall(const char *Busname, const char *ObjectPath, const char *Interface, const char *Method, GVariant *Parameters, cDBusMethodReplyFunc OnReply, gpointer UserData);
virtual ~cDBusMethodCall(void);
};
class cDBusWatcher : public cListObject
{
friend class cDBusConnection;
private:
static void on_busname_appeared(GDBusConnection *connection,
const gchar *name,
const gchar *name_owner,
gpointer user_data);
static void on_busname_vanished(GDBusConnection *connection,
const gchar *name,
gpointer user_data);
cDBusConnection *_connection;
gchar *_caller;
gchar *_name;
guint _id;
guint _watch_id;
guint Watch(cDBusConnection *Connection);
void Unwatch(bool CallVanished);
public:
cDBusWatcher(const char *Caller, const char *Name);
virtual ~cDBusWatcher(void);
const char *Caller(void) const { return _caller; }
const char *Name(void) const { return _name; }
guint Id(void) const { return _id; };
guint RealId(void) const { return _watch_id; };
};
class cDBusConnection
{
private:
// wrapper functions for GMainLoop calls
static void on_name_acquired(GDBusConnection *connection,
const gchar *name,
gpointer user_data);
static void on_name_lost(GDBusConnection *connection,
const gchar *name,
gpointer user_data);
static void on_name_lost_close(GObject *source_object,
GAsyncResult *res,
gpointer user_data);
static void on_bus_get(GObject *source_object,
GAsyncResult *res,
gpointer user_data);
static gboolean do_reconnect(gpointer user_data);
static gboolean do_connect(gpointer user_data);
static gboolean do_disconnect(gpointer user_data);
static void on_disconnect_close(GObject *source_object,
GAsyncResult *res,
gpointer user_data);
static gboolean do_flush(gpointer user_data);
static void on_flush(GObject *source_object,
GAsyncResult *res,
gpointer user_data);
static gboolean do_emit_signal(gpointer user_data);
static gboolean do_call_method(gpointer user_data);
static void do_call_reply(GObject *source_object,
GAsyncResult *res,
gpointer user_data);
static void on_signal(GDBusConnection *connection,
const gchar *sender_name,
const gchar *object_path,
const gchar *interface_name,
const gchar *signal_name,
GVariant *parameters,
gpointer user_data);
static void do_work(gpointer data, gpointer user_data);
gchar *_busname;
GBusType _bus_type;
gchar *_bus_address;
gchar *_name;
cDBusNameAcquiredFunc _on_name_acquired;
cDBusNameLostFunc _on_name_lost;
gpointer _on_name_user_data;
cDBusOnConnectFunc _on_connect;
cDBusOnDisconnectFunc _on_disconnect;
gpointer _on_connect_user_data;
GMainContext *_context;
GDBusConnection *_connection;
guint _owner_id;
gboolean _auto_reconnect;
gboolean _reconnect;
guint _connect_status;
GMutex _disconnect_mutex;
GCond _disconnect_cond;
guint _disconnect_status;
GMutex _flush_mutex;
GCond _flush_cond;
cList<cDBusObject> _objects;
cList<cDBusSignal> _signals;
cList<cDBusMethodCall> _method_calls;
cList<cDBusSignal> _subscriptions;
cList<cDBusWatcher> _watchers;
void RegisterObjects(void);
void UnregisterObjects(void);
void RegisterWatchers(void);
void UnregisterWatchers(void);
public:
static void FreeThreadPool(void);
cDBusConnection(const char *Busname, GBusType Type, GMainContext *Context);
cDBusConnection(const char *Busname, const char *Name, const char *Address, GMainContext *Context);
virtual ~cDBusConnection(void);
GDBusConnection *GetConnection(void) const { return _connection; };
guint GetConnectStatus(void) { return _connect_status; };
const char *Name(void) const;
// must be called before "Connect"
void AddObject(cDBusObject *Object);
void AddWatcher(cDBusWatcher *Watcher);
void SetNameCallbacks(cDBusNameAcquiredFunc OnNameAcquired, cDBusNameLostFunc OnNameLost, gpointer OnNameUserData)
{
_on_name_acquired = OnNameAcquired;
_on_name_lost = OnNameLost;
_on_name_user_data = OnNameUserData;
};
void SetConnectCallbacks(cDBusOnConnectFunc OnConnect, cDBusOnDisconnectFunc OnDisconnect, gpointer OnConnectUserData)
{
_on_connect = OnConnect;
_on_disconnect = OnDisconnect;
_on_connect_user_data = OnConnectUserData;
};
// "Connect" is async
void Connect(gboolean AutoReconnect);
// "Disconnect" blocks
void Disconnect(void);
// "Signal" and "Call" objects will be deleted by cDBusConnection
void EmitSignal(cDBusSignal *Signal);
void CallMethod(cDBusMethodCall *Call);
void Subscribe(cDBusSignal *Signal);
void Unsubscribe(cDBusSignal *Signal);
guint Watch(cDBusWatcher *Watcher);
void Unwatch(guint Id);
// "Flush" blocks
bool Flush(void);
};
#endif