TraveSim Adapters  0.1
Protobuf adapters for TraveSim project
travesim::udp::Receiver Class Referenceabstract

Receiver class using UDP. More...

#include "receiver.hpp"

Inheritance diagram for travesim::udp::Receiver:
Collaboration diagram for travesim::udp::Receiver:

Public Member Functions

 Receiver (const std::string receiver_address, const short receiver_port)
 Construct a new Receiver object. More...
 
virtual ~Receiver ()
 Destroy the Receiver object. More...
 
size_t receive (char *buffer, const size_t buffer_size)
 Receive data using UDP. More...
 
size_t receive_latest (char *buffer, const size_t buffer_size)
 Receive the latest data using UDP. More...
 
void force_specific_source (bool specific_source)
 Set wheter to enable any source or source specific. True for specific source, false for any source, default is false. More...
 
void set_receiver_endpoint (const std::string receiver_address, const short receiver_port)
 Set the receiver endpoint. More...
 
void reset (void)
 Reset the receiver. More...
 

Protected Member Functions

virtual void open_socket ()=0
 Open the socket with the desired options. More...
 
virtual void close_socket ()
 Close the socket. More...
 

Protected Attributes

boost::asio::ip::udp::socket * socket
 
boost::asio::ip::udp::endpoint receiver_endpoint
 

Private Member Functions

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 source. More...
 

Private Attributes

boost::asio::io_context io_context
 
boost::asio::ip::udp::endpoint sender_endpoint
 
bool specific_source
 

Detailed Description

Receiver class using UDP.

Definition at line 25 of file receiver.hpp.

Constructor & Destructor Documentation

◆ Receiver()

travesim::udp::Receiver::Receiver ( const std::string  receiver_address,
const short  receiver_port 
)

Construct a new Receiver object.

Parameters
receiver_addressAddress where to send data
receiver_portPort where to send data
Note
open_socket() must be called in the child's constructor

Definition at line 32 of file receiver.cpp.

32  {
33  this->set_receiver_endpoint(receiver_address, receiver_port);
34 
35  this->force_specific_source(false);
36 
37  this->socket = new boost::asio::ip::udp::socket(io_context);
38 };
boost::asio::ip::udp::socket * socket
Definition: receiver.hpp:88
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
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

References force_specific_source(), io_context, set_receiver_endpoint(), and socket.

Here is the call graph for this function:

◆ ~Receiver()

travesim::udp::Receiver::~Receiver ( )
virtual

Destroy the Receiver object.

Note
close_socket() must be called in the child's destructor

Definition at line 40 of file receiver.cpp.

40  {
41  delete this->socket;
42 };
boost::asio::ip::udp::socket * socket
Definition: receiver.hpp:88

References socket.

Member Function Documentation

◆ close_socket()

void travesim::udp::Receiver::close_socket ( )
protectedvirtual

Close the socket.

Reimplemented in travesim::udp::MulticastReceiver.

Definition at line 128 of file receiver.cpp.

128  {
129  if (this->socket->is_open()) {
130  this->socket->close();
131  }
132 };
boost::asio::ip::udp::socket * socket
Definition: receiver.hpp:88

References socket.

Here is the caller graph for this function:

◆ force_specific_source()

void travesim::udp::Receiver::force_specific_source ( bool  specific_source)

Set wheter to enable any source or source specific. True for specific source, false for any source, default is false.

Parameters
specific_sourceWhether to enable source specific or not.

Definition at line 108 of file receiver.cpp.

108  {
110 };

References specific_source.

Here is the caller graph for this function:

◆ open_socket()

virtual void travesim::udp::Receiver::open_socket ( )
protectedpure virtual

Open the socket with the desired options.

Example:
// Create the socket so that multiple may be bound to the same address.
this->socket->open(this->receiver_endpoint.protocol());
// Set socket options
// Bind to endpoint
this->socket->bind(this->receiver_endpoint);
// Set blocking mode
this->socket->non_blocking(true);
Note
When opening a socket, if the socket is already bound to an endpoint, a boost::wrapexcept<boost::system::system_error> exception is thrown.

Implemented in travesim::udp::MulticastReceiver, and travesim::udp::UnicastReceiver.

Here is the caller graph for this function:

◆ receive()

size_t travesim::udp::Receiver::receive ( char *  buffer,
const size_t  buffer_size 
)

Receive data using UDP.

Parameters
bufferBuffet to store data
buffer_sizeSize of the buffer where to store data
Returns
size_t Number of bytes received
Todo:
sender_endpoint loggin?

Definition at line 44 of file receiver.cpp.

44  {
45  size_t bytes_received;
46  boost::system::error_code ec;
47  boost::asio::ip::udp::endpoint current_endpoint;
48 
49  bytes_received =
50  this->socket->receive_from(boost::asio::buffer(buffer, buffer_size), current_endpoint, NO_FLAGS, ec);
51 
52  this->validate_sender_endpoint(current_endpoint);
53 
54  switch (ec.value()) {
55  case boost::system::errc::success: {
56  /**
57  * @todo sender_endpoint loggin?
58  */
59  break;
60  }
61 
62  case boost::asio::error::would_block: {
63  bytes_received = 0;
64  break;
65  }
66 
67  default: {
68  throw boost::system::system_error(ec);
69  }
70  }
71 
72  return bytes_received;
73 };
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
#define NO_FLAGS
Definition: receiver.cpp:22

References NO_FLAGS, socket, and validate_sender_endpoint().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ receive_latest()

size_t travesim::udp::Receiver::receive_latest ( char *  buffer,
const size_t  buffer_size 
)

Receive the latest data using UDP.

Parameters
bufferBuffet to store data
buffer_sizeSize of the buffer where to store data
Returns
size_t Number of bytes received
Todo:
sender_endpoint loggin?

Definition at line 75 of file receiver.cpp.

75  {
76  size_t bytes_received;
77  boost::system::error_code ec;
78  boost::asio::ip::udp::endpoint current_endpoint;
79 
80  do {
81  bytes_received =
82  this->socket->receive_from(boost::asio::buffer(buffer, buffer_size), current_endpoint, NO_FLAGS, ec);
83  } while (this->socket->available() > 0);
84 
85  this->validate_sender_endpoint(current_endpoint);
86 
87  switch (ec.value()) {
88  case boost::system::errc::success: {
89  /**
90  * @todo sender_endpoint loggin?
91  */
92  break;
93  }
94 
95  case boost::asio::error::would_block: {
96  bytes_received = 0;
97  break;
98  }
99 
100  default: {
101  throw boost::system::system_error(ec);
102  }
103  }
104 
105  return bytes_received;
106 };
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
#define NO_FLAGS
Definition: receiver.cpp:22

References NO_FLAGS, socket, and validate_sender_endpoint().

Here is the call graph for this function:

◆ reset()

void travesim::udp::Receiver::reset ( void  )

Reset the receiver.

Definition at line 117 of file receiver.cpp.

117  {
119 
120  this->close_socket();
121  this->open_socket();
122 };
virtual void close_socket()
Close the socket.
Definition: receiver.cpp:128
virtual void open_socket()=0
Open the socket with the desired options.
#define INVALID_ENDPOINT
Definition: receiver.cpp:24
boost::asio::ip::udp::endpoint sender_endpoint
Definition: receiver.hpp:121

References close_socket(), INVALID_ENDPOINT, open_socket(), and sender_endpoint.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ set_receiver_endpoint()

void travesim::udp::Receiver::set_receiver_endpoint ( const std::string  receiver_address,
const short  receiver_port 
)

Set the receiver endpoint.

Parameters
receiver_addressAddress where to send data
receiver_portPort where to send data
Warning
reset() must be called after changing the endpoint

Definition at line 112 of file receiver.cpp.

112  {
113  const boost::asio::ip::address receiver_boost_addr = boost::asio::ip::address::from_string(receiver_address);
114  this->receiver_endpoint = boost::asio::ip::udp::endpoint(receiver_boost_addr, receiver_port);
115 };
boost::asio::ip::udp::endpoint receiver_endpoint
Definition: receiver.hpp:89

References receiver_endpoint.

Here is the caller graph for this function:

◆ validate_sender_endpoint()

void travesim::udp::Receiver::validate_sender_endpoint ( boost::asio::ip::udp::endpoint  current_sender_endpoint)
inlineprivate

Validate whether the current sender endpoint matches the first sender endpoint when using specific source.

Parameters
current_sender_endpointThe endpoint from where the last data came from
Exceptions
std::runtime_errorWhen messages from multiple senders are received and any-source is not enabled.

Definition at line 138 of file receiver.cpp.

138  {
139  if (this->specific_source) {
140  if (this->sender_endpoint == INVALID_ENDPOINT) {
141  this->sender_endpoint = current_sender_endpoint;
142  } else if (this->sender_endpoint != current_sender_endpoint) {
143  if (current_sender_endpoint != INVALID_ENDPOINT) {
144  std::string error_msg = "Error in receiver. Any-source not enabled.";
145  throw std::runtime_error(error_msg);
146  }
147  }
148  }
149 };
#define INVALID_ENDPOINT
Definition: receiver.cpp:24
boost::asio::ip::udp::endpoint sender_endpoint
Definition: receiver.hpp:121

References INVALID_ENDPOINT, sender_endpoint, and specific_source.

Here is the caller graph for this function:

Member Data Documentation

◆ io_context

boost::asio::io_context travesim::udp::Receiver::io_context
private

boost/asio I/O execution context

Definition at line 119 of file receiver.hpp.

◆ receiver_endpoint

boost::asio::ip::udp::endpoint travesim::udp::Receiver::receiver_endpoint
protected

Receiver address and port pair

Definition at line 89 of file receiver.hpp.

◆ sender_endpoint

boost::asio::ip::udp::endpoint travesim::udp::Receiver::sender_endpoint
private

Sender address and port pair

Definition at line 121 of file receiver.hpp.

◆ socket

boost::asio::ip::udp::socket* travesim::udp::Receiver::socket
protected

Network socket

Definition at line 88 of file receiver.hpp.

◆ specific_source

bool travesim::udp::Receiver::specific_source
private

True for specific source, false for any source, default is false

Definition at line 123 of file receiver.hpp.


The documentation for this class was generated from the following files: