WPILibC++  unspecified
NetworkConnection.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) FIRST 2015. All Rights Reserved. */
3 /* Open Source Software - may be modified and shared by FRC teams. The code */
4 /* must be accompanied by the FIRST BSD license file in the root directory of */
5 /* the project. */
6 /*----------------------------------------------------------------------------*/
7 
8 #ifndef NT_NETWORKCONNECTION_H_
9 #define NT_NETWORKCONNECTION_H_
10 
11 #include <atomic>
12 #include <chrono>
13 #include <memory>
14 #include <thread>
15 
16 #include "support/ConcurrentQueue.h"
17 #include "INetworkConnection.h"
18 #include "Message.h"
19 #include "ntcore_cpp.h"
20 
21 namespace wpi {
22 class Logger;
23 class NetworkStream;
24 }
25 
26 namespace nt {
27 
28 class IConnectionNotifier;
29 
31  public:
32  typedef std::function<bool(
33  NetworkConnection& conn,
34  std::function<std::shared_ptr<Message>()> get_msg,
35  std::function<void(llvm::ArrayRef<std::shared_ptr<Message>>)> send_msgs)>
36  HandshakeFunc;
37  typedef std::function<void(std::shared_ptr<Message> msg,
38  NetworkConnection* conn)>
39  ProcessIncomingFunc;
40  typedef std::vector<std::shared_ptr<Message>> Outgoing;
42 
43  NetworkConnection(unsigned int uid,
44  std::unique_ptr<wpi::NetworkStream> stream,
45  IConnectionNotifier& notifier, wpi::Logger& logger,
46  HandshakeFunc handshake,
47  Message::GetEntryTypeFunc get_entry_type);
49 
50  // Set the input processor function. This must be called before Start().
51  void set_process_incoming(ProcessIncomingFunc func) {
52  m_process_incoming = func;
53  }
54 
55  void Start();
56  void Stop();
57 
58  ConnectionInfo info() const override;
59 
60  bool active() const { return m_active; }
61  wpi::NetworkStream& stream() { return *m_stream; }
62 
63  void QueueOutgoing(std::shared_ptr<Message> msg) override;
64  void PostOutgoing(bool keep_alive) override;
65 
66  unsigned int uid() const { return m_uid; }
67 
68  unsigned int proto_rev() const override;
69  void set_proto_rev(unsigned int proto_rev) override;
70 
71  State state() const override;
72  void set_state(State state) override;
73 
74  std::string remote_id() const;
75  void set_remote_id(StringRef remote_id);
76 
77  unsigned long long last_update() const { return m_last_update; }
78 
79  NetworkConnection(const NetworkConnection&) = delete;
80  NetworkConnection& operator=(const NetworkConnection&) = delete;
81 
82  private:
83  void ReadThreadMain();
84  void WriteThreadMain();
85 
86  unsigned int m_uid;
87  std::unique_ptr<wpi::NetworkStream> m_stream;
88  IConnectionNotifier& m_notifier;
89  wpi::Logger& m_logger;
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;
99  State m_state;
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;
104 
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;
108 
109  // Condition variables for shutdown
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;
115 };
116 
117 } // namespace nt
118 
119 #endif // NT_NETWORKCONNECTION_H_
NetworkTables Connection Information.
Definition: ntcore_cpp.h:57
Definition: NetworkStream.h:17
Definition: IConnectionNotifier.h:17
Definition: SocketError.cpp:18
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:32
Definition: NetworkConnection.h:30
Definition: IEntryNotifier.h:15
Definition: Logger.h:30
Definition: INetworkConnection.h:18
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:42