TraveSim Adapters  0.1
Protobuf adapters for TraveSim project
receiver.hpp
Go to the documentation of this file.
1 /**
2  * @file receiver.hpp
3  *
4  * @author Lucas Haug <lucas.haug@thuneratz.org>
5  * @author Lucas Schneider <lucas.schneider@thuneratz.org>
6  *
7  * @brief Receiver data using UDP
8  *
9  * @date 04/2021
10  *
11  * @copyright MIT License - Copyright (c) 2021 ThundeRatz
12  */
13 
14 #include <boost/asio.hpp>
15 #include <string>
16 
17 #ifndef __RECEIVER_H__
18 #define __RECEIVER_H__
19 
20 namespace travesim {
21 namespace udp {
22 /**
23  * @brief Receiver class using UDP
24  */
25 class Receiver {
26  public:
27  /**
28  * @brief Construct a new Receiver object
29  *
30  * @param receiver_address Address where to send data
31  * @param receiver_port Port where to send data
32  *
33  * @note open_socket() must be called in the child's constructor
34  */
35  Receiver(const std::string receiver_address, const short receiver_port);
36 
37  /**
38  * @brief Destroy the Receiver object
39  *
40  * @note close_socket() must be called in the child's destructor
41  */
42  virtual ~Receiver();
43 
44  /**
45  * @brief Receive data using UDP
46  *
47  * @param buffer Buffet to store data
48  * @param buffer_size Size of the buffer where to store data
49  *
50  * @return size_t Number of bytes received
51  */
52  size_t receive(char* buffer, const size_t buffer_size);
53 
54  /**
55  * @brief Receive the latest data using UDP
56  *
57  * @param buffer Buffet to store data
58  * @param buffer_size Size of the buffer where to store data
59  *
60  * @return size_t Number of bytes received
61  */
62  size_t receive_latest(char* buffer, const size_t buffer_size);
63 
64  /**
65  * @brief Set wheter to enable any source or source specific.
66  * True for specific source, false for any source, default is false.
67  *
68  * @param specific_source Whether to enable source specific or not.
69  */
71 
72  /**
73  * @brief Set the receiver endpoint
74  *
75  * @param receiver_address Address where to send data
76  * @param receiver_port Port where to send data
77  *
78  * @warning reset() must be called after changing the endpoint
79  */
80  void set_receiver_endpoint(const std::string receiver_address, const short receiver_port);
81 
82  /**
83  * @brief Reset the receiver
84  */
85  void reset(void);
86 
87  protected:
88  boost::asio::ip::udp::socket* socket; /**< Network socket*/
89  boost::asio::ip::udp::endpoint receiver_endpoint; /**< Receiver address and port pair */
90 
91  /**
92  * @brief Open the socket with the desired options
93  *
94  * @par Example:
95  * @code{.cpp}
96  * // Create the socket so that multiple may be bound to the same address.
97  * this->socket->open(this->receiver_endpoint.protocol());
98  *
99  * // Set socket options
100  *
101  * // Bind to endpoint
102  * this->socket->bind(this->receiver_endpoint);
103  *
104  * // Set blocking mode
105  * this->socket->non_blocking(true);
106  * @endcode
107  *
108  * @note When opening a socket, if the socket is already bound to an endpoint,
109  * a boost::wrapexcept<boost::system::system_error> exception is thrown.
110  */
111  virtual void open_socket() = 0;
112 
113  /**
114  * @brief Close the socket
115  */
116  virtual void close_socket();
117 
118  private:
119  boost::asio::io_context io_context; /**< boost/asio I/O execution context */
120 
121  boost::asio::ip::udp::endpoint sender_endpoint; /**< Sender address and port pair */
122 
123  bool specific_source; /**< True for specific source, false for any source, default is false */
124 
125  /**
126  * @brief Validate whether the current sender endpoint matches the first sender
127  * endpoint when using specific source.
128  *
129  * @param current_sender_endpoint The endpoint from where the last data came from
130  *
131  * @throw std::runtime_error When messages from multiple senders are received and
132  * any-source is not enabled.
133  */
134  void validate_sender_endpoint(boost::asio::ip::udp::endpoint current_sender_endpoint);
135 };
136 } // namespace udp
137 } // namespace travesim
138 
139 #endif // __RECEIVER_H__
virtual void close_socket()
Close the socket.
Definition: receiver.cpp:128
boost::asio::ip::udp::socket * socket
Definition: receiver.hpp:88
void validate_sender_endpoint(boost::asio::ip::udp::endpoint current_sender_endpoint)
Validate whether the current sender endpoint matches the first sender endpoint when using specific so...
Definition: receiver.cpp:138
virtual void open_socket()=0
Open the socket with the desired options.
void reset(void)
Reset the receiver.
Definition: receiver.cpp:117
boost::asio::ip::udp::endpoint receiver_endpoint
Definition: receiver.hpp:89
size_t receive_latest(char *buffer, const size_t buffer_size)
Receive the latest data using UDP.
Definition: receiver.cpp:75
size_t receive(char *buffer, const size_t buffer_size)
Receive data using UDP.
Definition: receiver.cpp:44
boost::asio::io_context io_context
Definition: receiver.hpp:119
void set_receiver_endpoint(const std::string receiver_address, const short receiver_port)
Set the receiver endpoint.
Definition: receiver.cpp:112
Receiver class using UDP.
Definition: receiver.hpp:25
Receiver(const std::string receiver_address, const short receiver_port)
Construct a new Receiver object.
Definition: receiver.cpp:32
void force_specific_source(bool specific_source)
Set wheter to enable any source or source specific. True for specific source, false for any source,...
Definition: receiver.cpp:108
virtual ~Receiver()
Destroy the Receiver object.
Definition: receiver.cpp:40
boost::asio::ip::udp::endpoint sender_endpoint
Definition: receiver.hpp:121