8 #ifndef NT_NETWORKCONNECTION_H_
9 #define NT_NETWORKCONNECTION_H_
16 #include "support/ConcurrentQueue.h"
18 #include "ntcore_cpp.h"
30 enum State { kCreated, kInit, kHandshake, kSynchronized, kActive, kDead };
32 typedef std::function<bool(
34 std::function<std::shared_ptr<Message>()> get_msg,
35 std::function<
void(llvm::ArrayRef<std::shared_ptr<Message>>)> send_msgs)>
37 typedef std::function<void(std::shared_ptr<Message> msg,
40 typedef std::vector<std::shared_ptr<Message>> Outgoing;
41 typedef wpi::ConcurrentQueue<Outgoing> OutgoingQueue;
44 Notifier& notifier, HandshakeFunc handshake,
45 Message::GetEntryTypeFunc get_entry_type);
49 void set_process_incoming(ProcessIncomingFunc func) {
50 m_process_incoming = func;
58 bool active()
const {
return m_active; }
59 wpi::NetworkStream& stream() {
return *m_stream; }
61 void QueueOutgoing(std::shared_ptr<Message> msg);
62 void PostOutgoing(
bool keep_alive);
63 void NotifyIfActive(ConnectionListenerCallback callback)
const;
65 unsigned int uid()
const {
return m_uid; }
67 unsigned int proto_rev()
const {
return m_proto_rev; }
68 void set_proto_rev(
unsigned int proto_rev) { m_proto_rev = proto_rev; }
71 void set_state(State state);
73 std::string remote_id()
const;
74 void set_remote_id(StringRef remote_id);
76 unsigned long long last_update()
const {
return m_last_update; }
82 void ReadThreadMain();
83 void WriteThreadMain();
85 static std::atomic_uint s_uid;
88 std::unique_ptr<wpi::NetworkStream> m_stream;
90 OutgoingQueue m_outgoing;
91 HandshakeFunc m_handshake;
92 Message::GetEntryTypeFunc m_get_entry_type;
93 ProcessIncomingFunc m_process_incoming;
94 std::thread m_read_thread;
95 std::thread m_write_thread;
96 std::atomic_bool m_active;
97 std::atomic_uint m_proto_rev;
98 mutable std::mutex m_state_mutex;
100 mutable std::mutex m_remote_id_mutex;
101 std::string m_remote_id;
102 std::atomic_ullong m_last_update;
103 std::chrono::steady_clock::time_point m_last_post;
105 std::mutex m_pending_mutex;
106 Outgoing m_pending_outgoing;
107 std::vector<std::pair<std::size_t, std::size_t>> m_pending_update;
110 std::mutex m_shutdown_mutex;
111 std::condition_variable m_read_shutdown_cv;
112 std::condition_variable m_write_shutdown_cv;
113 bool m_read_shutdown =
false;
114 bool m_write_shutdown =
false;
119 #endif // NT_NETWORKCONNECTION_H_
NetworkTables Connection Information.
Definition: ntcore_cpp.h:43
Definition: NetworkConnection.h:28
Definition: Notifier.h:19