| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
#pragma once |
| 8 |
|
| 9 |
#include "dbuscallbacks.h" |
| 10 |
|
| 11 |
#include <dbus/dbus.h> |
| 12 |
#include <event2/event.h> |
| 13 |
|
| 14 |
#include <memory> |
| 15 |
#include <unordered_map> |
| 16 |
#include <vector> |
| 17 |
|
| 18 |
namespace flightgear::swift { |
| 19 |
|
| 20 |
class WatchHandler; |
| 21 |
class TimeoutHandler; |
| 22 |
class CDBusConnection; |
| 23 |
class CDBusDispatcher; |
| 24 |
|
| 25 |
|
| 26 |
class IDispatchable |
| 27 |
{ |
| 28 |
public: |
| 29 |
|
| 30 |
IDispatchable() = default; |
| 31 |
|
| 32 |
|
| 33 |
virtual ~IDispatchable() = default; |
| 34 |
|
| 35 |
|
| 36 |
virtual void dispatch() = 0; |
| 37 |
|
| 38 |
private: |
| 39 |
friend CDBusDispatcher; |
| 40 |
}; |
| 41 |
|
| 42 |
|
| 43 |
class CDBusDispatcher |
| 44 |
{ |
| 45 |
public: |
| 46 |
|
| 47 |
CDBusDispatcher(); |
| 48 |
|
| 49 |
|
| 50 |
virtual ~CDBusDispatcher(); |
| 51 |
|
| 52 |
|
| 53 |
void add(IDispatchable* dispatchable); |
| 54 |
|
| 55 |
|
| 56 |
void remove(IDispatchable* dispatchable); |
| 57 |
|
| 58 |
|
| 59 |
void waitAndRun(); |
| 60 |
|
| 61 |
|
| 62 |
void runOnce(); |
| 63 |
|
| 64 |
private: |
| 65 |
friend class WatchHandler; |
| 66 |
friend class TimeoutHandler; |
| 67 |
friend class Timer; |
| 68 |
friend class CDBusConnection; |
| 69 |
friend class CDBusServer; |
| 70 |
|
| 71 |
struct EventBaseDeleter { |
| 72 |
void operator()(event_base* obj) const { event_base_free(obj); } |
| 73 |
}; |
| 74 |
|
| 75 |
using WatchCallbacks = DBusAsyncCallbacks<DBusWatch>; |
| 76 |
using TimeoutCallbacks = DBusAsyncCallbacks<DBusTimeout>; |
| 77 |
|
| 78 |
void dispatch(); |
| 79 |
|
| 80 |
dbus_bool_t dbusAddWatch(DBusWatch* watch); |
| 81 |
void dbusRemoveWatch(DBusWatch* watch); |
| 82 |
void dbusWatchToggled(DBusWatch* watch); |
| 83 |
|
| 84 |
dbus_bool_t dbusAddTimeout(DBusTimeout* timeout); |
| 85 |
void dbusRemoveTimeout(DBusTimeout* timeout); |
| 86 |
void dbusTimeoutToggled(DBusTimeout* timeout); |
| 87 |
|
| 88 |
WatchCallbacks m_watchCallbacks; |
| 89 |
TimeoutCallbacks m_timeoutCallbacks; |
| 90 |
std::unordered_multimap<evutil_socket_t, std::unique_ptr<WatchHandler>> m_watchers; |
| 91 |
std::vector<std::unique_ptr<TimeoutHandler>> m_timeouts; |
| 92 |
std::unique_ptr<event_base, EventBaseDeleter> m_eventBase; |
| 93 |
|
| 94 |
std::vector<IDispatchable*> m_dispatchList; |
| 95 |
}; |
| 96 |
} |