swift
dbusserver.h
1 // SPDX-FileCopyrightText: Copyright (C) 2018 swift Project Community / Contributors
2 // SPDX-License-Identifier: GPL-3.0-or-later OR LicenseRef-swift-pilot-client-1
3 
4 #ifndef SWIFT_SIM_XSWIFTBUS_DBUSSERVER_H
5 #define SWIFT_SIM_XSWIFTBUS_DBUSSERVER_H
6 
7 #include <dbus/dbus.h>
8 #include <event2/event.h>
9 
10 #include <functional>
11 #include <memory>
12 #include <string>
13 #include <unordered_map>
14 #include <vector>
15 
16 #include "dbuscallbacks.h"
17 #include "dbusdispatcher.h"
18 #include "dbuserror.h"
19 #include "dbusmessage.h"
20 
21 namespace XSwiftBus
22 {
23  class CDBusObject;
24 
26  class CDBusServer : public IDispatchable
27  {
28  public:
30  using NewConnectionFunc = std::function<void(std::shared_ptr<CDBusConnection>)>;
31 
33  CDBusServer();
34 
36  virtual ~CDBusServer() override;
37 
39  void setDispatcher(CDBusDispatcher *dispatcher);
40 
42  bool listen(const std::string &address);
43 
45  bool isConnected() const;
46 
47  void dispatch() override {}
48 
50  void close();
51 
53  CDBusError lastError() const { return m_lastError; }
54 
56  void setNewConnectionFunc(const NewConnectionFunc &func) { m_newConnectionFunc = func; }
57 
58  private:
59  void onNewConnection(DBusServer *server, DBusConnection *conn);
60  static void onNewConnection(DBusServer *server, DBusConnection *conn, void *data);
61 
62  struct DBusServerDeleter
63  {
64  void operator()(DBusServer *obj) const { dbus_server_unref(obj); }
65  };
66 
67  CDBusDispatcher *m_dispatcher = nullptr;
68  std::unique_ptr<DBusServer, DBusServerDeleter> m_server;
69  CDBusError m_lastError;
70  NewConnectionFunc m_newConnectionFunc;
71  };
72 } // namespace XSwiftBus
73 
74 #endif // SWIFT_SIM_XSWIFTBUS_DBUSSERVER_H
DBus connection.
Definition: dbusserver.h:27
CDBusError lastError() const
Get the last error.
Definition: dbusserver.h:53
void close()
Close connection.
Definition: dbusserver.cpp:37
virtual ~CDBusServer()
Destructor.
Definition: dbusserver.cpp:18
bool listen(const std::string &address)
Connect to bus.
Definition: dbusserver.cpp:20
std::function< void(std::shared_ptr< CDBusConnection >)> NewConnectionFunc
New connection handler function.
Definition: dbusserver.h:30
void dispatch()
Dispatch execution method.
Definition: dbusserver.h:47
void setNewConnectionFunc(const NewConnectionFunc &func)
Set the function to be used for handling new connections.
Definition: dbusserver.h:56
void setDispatcher(CDBusDispatcher *dispatcher)
Set the dispatcher.
Definition: dbusserver.cpp:42
CDBusServer()
Constructor.
Definition: dbusserver.cpp:16
bool isConnected() const
Is connected?
Definition: dbusserver.cpp:35
Dispatchable Interface.
Plugin loaded by X-Plane which publishes a DBus service.
Definition: command.h:14