FlightGear next
dbusserver.h
Go to the documentation of this file.
1/*
2 * SPDX-FileCopyrightText: (C) 2019-2022 swift Project Community / Contributors (https://swift-project.org/)
3 * SPDX-FileCopyrightText: (C) 2019-2022 Lars Toenning <dev@ltoenning.de>
4 * SPDX-License-Identifier: GPL-2.0-or-later
5 */
6
7#pragma once
8
9#include "dbuscallbacks.h"
10#include "dbusdispatcher.h"
11#include "dbuserror.h"
12#include "dbusmessage.h"
13
14#include <dbus/dbus.h>
15#include <event2/event.h>
16#include <functional>
17#include <memory>
18#include <string>
19#include <unordered_map>
20#include <vector>
21
22namespace flightgear::swift {
23
24class CDBusObject;
25
28{
29public:
31 using NewConnectionFunc = std::function<void(std::shared_ptr<CDBusConnection>)>;
32
35
37 ~CDBusServer() override;
38
40 void setDispatcher(CDBusDispatcher* dispatcher);
41
43 bool listen(const std::string& address);
44
46 bool isConnected() const;
47
48 void dispatch() override {}
49
51 void close();
52
54 CDBusError lastError() const { return m_lastError; }
55
58 {
59 m_newConnectionFunc = func;
60 }
61
62private:
63 void onNewConnection(DBusServer* server, DBusConnection* conn);
64 static void onNewConnection(DBusServer* server, DBusConnection* conn, void* data);
65
66 struct DBusServerDeleter {
67 void operator()(DBusServer* obj) const { dbus_server_unref(obj); }
68 };
69
70 CDBusDispatcher* m_dispatcher = nullptr;
71 std::unique_ptr<DBusServer, DBusServerDeleter> m_server;
72 CDBusError m_lastError;
73 NewConnectionFunc m_newConnectionFunc;
74};
75
76} // namespace flightgear::swift
std::function< void(std::shared_ptr< CDBusConnection >)> NewConnectionFunc
New connection handler function.
Definition dbusserver.h:31
~CDBusServer() override
Destructor.
void setDispatcher(CDBusDispatcher *dispatcher)
Set the dispatcher.
bool isConnected() const
Is connected?
bool listen(const std::string &address)
Connect to bus.
void setNewConnectionFunc(const NewConnectionFunc &func)
Set the function to be used for handling new connections.
Definition dbusserver.h:57
void close()
Close connection.
CDBusError lastError() const
Get the last error.
Definition dbusserver.h:54
void dispatch() override
Dispatch execution method.
Definition dbusserver.h:48
IDispatchable()=default
Default constructor.
naCFunction func