Agora  1.2.0
Agora project
udp_server.h
Go to the documentation of this file.
1 
5 #ifndef UDP_SERVER_H_
6 #define UDP_SERVER_H_
7 
8 #include <utility>
9 
10 #include "udp_comm.h"
11 
13 class UDPServer {
14  public:
15  // Initialize a UDP server listening on this UDP port with socket buffer
16  // size = rx_buffer_size
17  explicit UDPServer(std::string local_address, uint16_t local_port,
18  size_t rx_buffer_size = 0)
19  : address_(std::move(local_address)),
20  port_(std::to_string(local_port)),
21  comm_object_(address_, port_, rx_buffer_size, 0) {}
22  explicit UDPServer(std::string local_address, std::string local_port,
23  size_t rx_buffer_size = 0)
24  : address_(std::move(local_address)),
25  port_(std::move(local_port)),
26  comm_object_(address_, port_, rx_buffer_size, 0) {}
27 
28  UDPServer& operator=(const UDPServer&) = delete;
29  UDPServer(const UDPServer&) = delete;
30  ~UDPServer() = default;
31 
39  inline ssize_t Connect(const std::string& remote_address,
40  const std::string& remote_port) {
41  return comm_object_.Connect(remote_address, remote_port);
42  }
43  inline ssize_t Connect(const std::string& remote_address,
44  uint16_t remote_port) {
45  return comm_object_.Connect(remote_address, remote_port);
46  }
47 
55  inline ssize_t Recv(std::byte* buf, size_t len) const {
56  return comm_object_.Recv(buf, len);
57  }
58  inline ssize_t Recv(const std::string& src_address, uint16_t src_port,
59  std::byte* buf, size_t len) {
60  return comm_object_.Recv(src_address, src_port, buf, len);
61  }
62 
67  inline void MakeBlocking(size_t timeout_sec = 0) const {
68  return comm_object_.MakeBlocking(timeout_sec);
69  }
70 
71  inline const std::string& Address() const { return address_; }
72  inline const std::string& Port() const { return port_; }
73 
74  private:
78  const std::string address_;
79  const std::string port_;
81 };
82 
83 #endif // UDP_SERVER_IPV6_H_
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::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
UDPServer::comm_object_
UDPComm comm_object_
Definition: udp_server.h:80
UDPServer::Port
const std::string & Port() const
Definition: udp_server.h:72
UDPServer::Connect
ssize_t Connect(const std::string &remote_address, const std::string &remote_port)
The remote_address | remote_port is the only address to which datagrams are received....
Definition: udp_server.h:39
UDPServer::UDPServer
UDPServer(std::string local_address, std::string local_port, size_t rx_buffer_size=0)
Definition: udp_server.h:22
UDPServer
Basic UDP server class based on OS sockets that supports receiving messages.
Definition: udp_server.h:13
UDPServer::Recv
ssize_t Recv(const std::string &src_address, uint16_t src_port, std::byte *buf, size_t len)
Definition: udp_server.h:58
UDPServer::Address
const std::string & Address() const
Definition: udp_server.h:71
len
uint16_t len
Definition: eth_common.h:62
UDPServer::UDPServer
UDPServer(std::string local_address, uint16_t local_port, size_t rx_buffer_size=0)
Definition: udp_server.h:17
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
std
Definition: json.hpp:5213
UDPServer::MakeBlocking
void MakeBlocking(size_t timeout_sec=0) const
Configures the socket in blocking mode. Any calls to recv / send will now block.
Definition: udp_server.h:67
UDPServer::~UDPServer
~UDPServer()=default
to_string
std::string to_string() const
Definition: eth_common.h:64
UDPServer::operator=
UDPServer & operator=(const UDPServer &)=delete
UDPServer::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_server.h:55
UDPServer::Connect
ssize_t Connect(const std::string &remote_address, uint16_t remote_port)
Definition: udp_server.h:43
UDPServer::address_
const std::string address_
The UDP port to server is listening on.
Definition: udp_server.h:78
UDPServer::port_
const std::string port_
Definition: udp_server.h:79