Agora  1.2.0
Agora project
udp_comm.h
Go to the documentation of this file.
1 
5 #ifndef UDP_COMM_H_
6 #define UDP_COMM_H_
7 
8 #include <netdb.h>
9 
10 #include <cstddef>
11 #include <map>
12 #include <mutex>
13 #include <string>
14 #include <vector>
15 
16 // Basic UDP client class based on OS sockets that supports sending messages
17 // and caches remote addrinfo mappings
18 class UDPComm {
19  public:
20  static constexpr bool kDebugPrintUdpInit = false;
21  static constexpr bool kDebugPrintUdpSend = false;
22  static constexpr bool kDebugPrintUdpRecv = false;
23  explicit UDPComm(std::string local_addr, uint16_t local_port,
24  size_t rx_buffer_size, size_t tx_buffer_size);
25 
26  explicit UDPComm(std::string local_addr, const std::string& local_port,
27  size_t rx_buffer_size, size_t tx_buffer_size);
28 
29  UDPComm& operator=(const UDPComm&) = delete;
30  UDPComm(const UDPComm&) = delete;
31  ~UDPComm();
32 
40  ssize_t Connect(const std::string& remote_address,
41  uint16_t remote_port) const;
42  ssize_t Connect(const std::string& remote_address,
43  const std::string& remote_port) const;
44 
56  void Send(const std::string& rem_hostname, uint16_t rem_port,
57  const std::byte* msg, size_t len);
58 
65  void Send(const std::byte* msg, size_t len);
66 
74  ssize_t Recv(std::byte* buf, size_t len) const;
75  ssize_t Recv(const std::string& src_address, uint16_t src_port,
76  std::byte* buf, size_t len);
77 
78  // Enable recording of all packets sent by this UDPComm object
79  inline void EnableRecording() { enable_recording_flag_ = true; }
80 
85  void MakeBlocking(size_t rx_timeout_sec = 0) const;
86 
87  private:
91  int sock_fd_ = -1;
92 
96  std::map<std::string, addrinfo*> addrinfo_map_;
101  std::mutex map_insert_access_;
102 
106  std::vector<std::vector<uint8_t>> sent_vec_;
107 
112 };
113 
114 #endif // UDP_CLIENT_H_
UDPComm::map_insert_access_
std::mutex map_insert_access_
Variable to control write access to the non-thread safe data structures.
Definition: udp_comm.h:101
fmt::v8::detail::byte
byte
Definition: core.h:388
udp_comm.h
Declaration file for the UDPComm class. This class is used to send messages and receive messages from...
UDPComm::UDPComm
UDPComm(std::string local_addr, uint16_t local_port, size_t rx_buffer_size, size_t tx_buffer_size)
Definition: udp_comm.cc:23
UDPComm::MakeBlocking
void MakeBlocking(size_t rx_timeout_sec=0) const
Configures the socket in blocking mode. Any calls to recv / send will now block.
Definition: udp_comm.cc:449
UDPComm
Definition: udp_comm.h:18
src_port
uint16_t src_port
Definition: eth_common.h:60
UDPComm::~UDPComm
~UDPComm()
Definition: udp_comm.cc:185
agora_comm::GetAddressInfo
::addrinfo * GetAddressInfo(const std::string &address, const std::string &port)
Definition: network_utils.cc:181
network_utils.h
Helper definations for networking.
AGORA_LOG_ERROR
#define AGORA_LOG_ERROR(...)
Definition: logger.h:39
UDPComm::kDebugPrintUdpSend
static constexpr bool kDebugPrintUdpSend
Definition: udp_comm.h:21
AGORA_LOG_TRACE
#define AGORA_LOG_TRACE(...)
Definition: logger.h:92
UDPComm::kDebugPrintUdpInit
static constexpr bool kDebugPrintUdpInit
Definition: udp_comm.h:20
matplotlibcpp::close
void close()
Definition: matplotlibcpp.h:2567
UDPComm::sock_fd_
int sock_fd_
The raw socket file descriptor.
Definition: udp_comm.h:91
len
uint16_t len
Definition: eth_common.h:62
u
Plot Rx waveform for u
Definition: inspect_single_frame.m:108
UDPComm::Recv
ssize_t Recv(std::byte *buf, size_t len) const
Try to receive up to len bytes in buf by default this will not block.
Definition: udp_comm.cc:372
UDPComm::Connect
ssize_t Connect(const std::string &remote_address, uint16_t remote_port) const
The remote_address | remote_port is the address to which datagrams are sent. 1:1 association me<->rem...
Definition: udp_comm.cc:250
check
uint16_t check
Definition: eth_common.h:69
UDPComm::addrinfo_map_
std::map< std::string, addrinfo * > addrinfo_map_
A cache mapping hostname:udp_port to addrinfo.
Definition: udp_comm.h:96
UDPComm::EnableRecording
void EnableRecording()
Definition: udp_comm.h:79
AGORA_LOG_INFO
#define AGORA_LOG_INFO(...)
Definition: logger.h:62
std
Definition: json.hpp:5213
AGORA_LOG_WARN
#define AGORA_LOG_WARN(...)
Definition: logger.h:53
UDPComm::enable_recording_flag_
bool enable_recording_flag_
If set to ture, we record all sent packets, otherwise we dont.
Definition: udp_comm.h:111
to_string
std::string to_string() const
Definition: eth_common.h:64
kDefaultAddress
static const std::string kDefaultAddress
Default to ipv4 for historic reasons, use ::1 if you want ipv6.
Definition: udp_comm.cc:21
UDPComm::kDebugPrintUdpRecv
static constexpr bool kDebugPrintUdpRecv
Definition: udp_comm.h:22
agora_comm::PrintAddressInfo
void PrintAddressInfo(const ::addrinfo *print_info)
Definition: network_utils.cc:229
UDPComm::operator=
UDPComm & operator=(const UDPComm &)=delete
UDPComm::Send
void Send(const std::string &rem_hostname, uint16_t rem_port, const std::byte *msg, size_t len)
Send one UDP packet to a remote server. The client caches the the remote server's addrinfo after reso...
Definition: udp_comm.cc:270
UDPComm::sent_vec_
std::vector< std::vector< uint8_t > > sent_vec_
All packets sent, maintained if recording is enabled.
Definition: udp_comm.h:106