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 #ifndef NOMINMAX
8 # define NOMINMAX
9 #endif
10 
11 #include <dbus/dbus.h>
12 #include <event2/event.h>
13 
14 #include <functional>
15 #include <memory>
16 #include <string>
17 #include <unordered_map>
18 #include <vector>
19 
20 #include "dbuscallbacks.h"
21 #include "dbusdispatcher.h"
22 #include "dbuserror.h"
23 #include "dbusmessage.h"
24 
25 namespace XSwiftBus
26 {
27  class CDBusObject;
28 
30  class CDBusServer : public IDispatchable
31  {
32  public:
34  using NewConnectionFunc = std::function<void(std::shared_ptr<CDBusConnection>)>;
35 
37  CDBusServer();
38 
40  virtual ~CDBusServer() override;
41 
43  void setDispatcher(CDBusDispatcher *dispatcher);
44 
46  bool listen(const std::string &address);
47 
49  bool isConnected() const;
50 
51  void dispatch() override {}
52 
54  void close();
55 
57  CDBusError lastError() const { return m_lastError; }
58 
60  void setNewConnectionFunc(const NewConnectionFunc &func) { m_newConnectionFunc = func; }
61 
62  private:
63  void onNewConnection(DBusServer *server, DBusConnection *conn);
64  static void onNewConnection(DBusServer *server, DBusConnection *conn, void *data);
65 
66  struct DBusServerDeleter
67  {
68  void operator()(DBusServer *obj) const { dbus_server_unref(obj); }
69  };
70 
71  CDBusDispatcher *m_dispatcher = nullptr;
72  std::unique_ptr<DBusServer, DBusServerDeleter> m_server;
73  CDBusError m_lastError;
74  NewConnectionFunc m_newConnectionFunc;
75  };
76 } // namespace XSwiftBus
77 
78 #endif // SWIFT_SIM_XSWIFTBUS_DBUSSERVER_H
DBus connection.
Definition: dbusserver.h:31
CDBusError lastError() const
Get the last error.
Definition: dbusserver.h:57
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:34
void dispatch()
Dispatch execution method.
Definition: dbusserver.h:51
void setNewConnectionFunc(const NewConnectionFunc &func)
Set the function to be used for handling new connections.
Definition: dbusserver.h:60
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