8 #ifndef NT_DISPATCHER_H_ 9 #define NT_DISPATCHER_H_ 13 #include <condition_variable> 21 #include "llvm/StringRef.h" 23 #include "IDispatcher.h" 24 #include "INetworkConnection.h" 28 class NetworkAcceptor;
34 class IConnectionNotifier;
36 class NetworkConnection;
39 friend class DispatcherTest;
42 typedef std::function<std::unique_ptr<wpi::NetworkStream>()> Connector;
46 virtual ~DispatcherBase();
50 std::unique_ptr<wpi::NetworkAcceptor> acceptor);
59 unsigned int AddListener(
61 bool immediate_notify)
const;
62 unsigned int AddPolledListener(
unsigned int poller_uid,
63 bool immediate_notify)
const;
65 void SetConnector(Connector connector);
66 void SetConnectorOverride(Connector connector);
67 void ClearConnectorOverride();
69 bool active()
const {
return m_active; }
71 DispatcherBase(
const DispatcherBase&) =
delete;
72 DispatcherBase& operator=(
const DispatcherBase&) =
delete;
75 void DispatchThreadMain();
76 void ServerThreadMain();
77 void ClientThreadMain();
81 std::function<std::shared_ptr<Message>()> get_msg,
82 std::function<
void(
llvm::ArrayRef<std::shared_ptr<Message>>)> send_msgs);
85 std::function<std::shared_ptr<Message>()> get_msg,
86 std::function<
void(
llvm::ArrayRef<std::shared_ptr<Message>>)> send_msgs);
88 void ClientReconnect(
unsigned int proto_rev = 0x0300);
95 unsigned int m_networkMode = NT_NET_MODE_NONE;
96 std::string m_persist_filename;
97 std::thread m_dispatch_thread;
98 std::thread m_clientserver_thread;
100 std::unique_ptr<wpi::NetworkAcceptor> m_server_acceptor;
101 Connector m_client_connector_override;
102 Connector m_client_connector;
103 uint8_t m_connections_uid = 0;
106 mutable std::mutex m_user_mutex;
107 std::vector<std::shared_ptr<INetworkConnection>> m_connections;
108 std::string m_identity;
110 std::atomic_bool m_active;
111 std::atomic_uint m_update_rate;
114 std::mutex m_flush_mutex;
115 std::condition_variable m_flush_cv;
116 std::chrono::steady_clock::time_point m_last_flush;
117 bool m_do_flush =
false;
120 std::condition_variable m_reconnect_cv;
121 unsigned int m_reconnect_proto_rev = 0x0300;
122 bool m_do_reconnect =
true;
129 friend class DispatcherTest;
139 void SetServer(
const char* server_name,
unsigned int port);
143 void SetServerOverride(
const char* server_name,
unsigned int port);
144 void ClearServerOverride();
149 #endif // NT_DISPATCHER_H_ Definition: Dispatcher.h:38
std::vector< ConnectionInfo > GetConnections()
Get information on the currently established network connections.
Definition: ntcore_cpp.cpp:891
void SetUpdateRate(double interval)
Set the periodic update rate.
Definition: ntcore_cpp.cpp:871
void SetServer(const char *server_name, unsigned int port)
Sets server address and port for client (without restarting client).
Definition: ntcore_cpp.cpp:821
Definition: Dispatcher.h:128
void Flush()
Flush Entries.
Definition: ntcore_cpp.cpp:882
Definition: IConnectionNotifier.h:17
Definition: SocketError.cpp:18
Definition: IStorage.h:26
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:32
Definition: NetworkConnection.h:30
bool IsConnected(NT_Inst inst)
Return whether or not the instance is connected to another node.
Definition: ntcore_cpp.cpp:902
Definition: IEntryNotifier.h:15
NetworkTables Connection Notification.
Definition: ntcore_cpp.h:206
unsigned int GetNetworkMode()
Get the current network mode.
Definition: ntcore_cpp.cpp:732
void SetServerTeam(NT_Inst inst, unsigned int team, unsigned int port)
Sets server addresses and port for client (without restarting client).
Definition: ntcore_cpp.cpp:844
void StartServer(StringRef persist_filename, const char *listen_address, unsigned int port)
Starts a server using the specified filename, listening address, and port.
Definition: ntcore_cpp.cpp:743
Definition: IDispatcher.h:21
Definition: INetworkConnection.h:18
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:42
void StartClient()
Starts a client.
Definition: ntcore_cpp.cpp:766