WPILibC++  unspecified
NetworkConnection.h
1 /*----------------------------------------------------------------------------*/
2 /* Copyright (c) 2015-2018 FIRST. 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 NTCORE_NETWORKCONNECTION_H_
9 #define NTCORE_NETWORKCONNECTION_H_
10 
11 #include <stdint.h>
12 
13 #include <atomic>
14 #include <chrono>
15 #include <memory>
16 #include <string>
17 #include <thread>
18 #include <utility>
19 #include <vector>
20 
21 #include <wpi/ConcurrentQueue.h>
22 #include <wpi/condition_variable.h>
23 #include <wpi/mutex.h>
24 
25 #include "INetworkConnection.h"
26 #include "Message.h"
27 #include "ntcore_cpp.h"
28 
29 namespace wpi {
30 class Logger;
31 class NetworkStream;
32 } // namespace wpi
33 
34 namespace nt {
35 
36 class IConnectionNotifier;
37 
39  public:
40  typedef std::function<bool(
41  NetworkConnection& conn,
42  std::function<std::shared_ptr<Message>()> get_msg,
43  std::function<void(wpi::ArrayRef<std::shared_ptr<Message>>)> send_msgs)>
44  HandshakeFunc;
45  typedef std::function<void(std::shared_ptr<Message> msg,
46  NetworkConnection* conn)>
47  ProcessIncomingFunc;
48  typedef std::vector<std::shared_ptr<Message>> Outgoing;
50 
51  NetworkConnection(unsigned int uid,
52  std::unique_ptr<wpi::NetworkStream> stream,
53  IConnectionNotifier& notifier, wpi::Logger& logger,
54  HandshakeFunc handshake,
55  Message::GetEntryTypeFunc get_entry_type);
57 
58  // Set the input processor function. This must be called before Start().
59  void set_process_incoming(ProcessIncomingFunc func) {
60  m_process_incoming = func;
61  }
62 
63  void Start();
64  void Stop();
65 
66  ConnectionInfo info() const override;
67 
68  bool active() const { return m_active; }
69  wpi::NetworkStream& stream() { return *m_stream; }
70 
71  void QueueOutgoing(std::shared_ptr<Message> msg) override;
72  void PostOutgoing(bool keep_alive) override;
73 
74  unsigned int uid() const { return m_uid; }
75 
76  unsigned int proto_rev() const override;
77  void set_proto_rev(unsigned int proto_rev) override;
78 
79  State state() const override;
80  void set_state(State state) override;
81 
82  std::string remote_id() const;
83  void set_remote_id(StringRef remote_id);
84 
85  uint64_t last_update() const { return m_last_update; }
86 
87  NetworkConnection(const NetworkConnection&) = delete;
88  NetworkConnection& operator=(const NetworkConnection&) = delete;
89 
90  private:
91  void ReadThreadMain();
92  void WriteThreadMain();
93 
94  unsigned int m_uid;
95  std::unique_ptr<wpi::NetworkStream> m_stream;
96  IConnectionNotifier& m_notifier;
97  wpi::Logger& m_logger;
98  OutgoingQueue m_outgoing;
99  HandshakeFunc m_handshake;
100  Message::GetEntryTypeFunc m_get_entry_type;
101  ProcessIncomingFunc m_process_incoming;
102  std::thread m_read_thread;
103  std::thread m_write_thread;
104  std::atomic_bool m_active;
105  std::atomic_uint m_proto_rev;
106  mutable wpi::mutex m_state_mutex;
107  State m_state;
108  mutable wpi::mutex m_remote_id_mutex;
109  std::string m_remote_id;
110  std::atomic_ullong m_last_update;
111  std::chrono::steady_clock::time_point m_last_post;
112 
113  wpi::mutex m_pending_mutex;
114  Outgoing m_pending_outgoing;
115  std::vector<std::pair<size_t, size_t>> m_pending_update;
116 
117  // Condition variables for shutdown
118  wpi::mutex m_shutdown_mutex;
119  wpi::condition_variable m_read_shutdown_cv;
120  wpi::condition_variable m_write_shutdown_cv;
121  bool m_read_shutdown = false;
122  bool m_write_shutdown = false;
123 };
124 
125 } // namespace nt
126 
127 #endif // NTCORE_NETWORKCONNECTION_H_
NetworkTables Connection Information.
Definition: ntcore_cpp.h:62
Definition: NetworkStream.h:17
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
Definition: ArrayRef.h:41
Definition: IConnectionNotifier.h:17
namespace to hold default to_json function
Definition: json_binary_writer.cpp:39
Definition: NetworkConnection.h:38
Definition: IStorage.h:21
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:49
Definition: Logger.h:30
Definition: INetworkConnection.h:18