TraveSim Adapters  0.1
Protobuf adapters for TraveSim project
travesim::udp::Sender Class Reference

Sender class using UDP. More...

#include "sender.hpp"

Inheritance diagram for travesim::udp::Sender:
Collaboration diagram for travesim::udp::Sender:

Public Member Functions

 Sender (const std::string receiver_address, const short receiver_port)
 Construct a new Sender object. More...
 
virtual ~Sender ()
 Destroy the Sender object. More...
 
size_t send (const char *buffer, const size_t buffer_size)
 Send data using UDP. More...
 
void set_receiver_endpoint (const std::string receiver_address, const short receiver_port)
 Set the receiver endpoint object. More...
 

Protected Attributes

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

Private Attributes

boost::asio::io_context io_context
 

Detailed Description

Sender class using UDP.

Definition at line 25 of file sender.hpp.

Constructor & Destructor Documentation

◆ Sender()

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

Construct a new Sender object.

Parameters
receiver_addressAddress where to send data
receiver_portPort where to send data

Definition at line 33 of file sender.cpp.

33  {
34  this->set_receiver_endpoint(receiver_address, receiver_port);
35 
36  this->socket = new boost::asio::ip::udp::socket(this->io_context);
37  this->socket->open(this->endpoint.protocol());
38 };
boost::asio::io_context io_context
Definition: sender.hpp:63
void set_receiver_endpoint(const std::string receiver_address, const short receiver_port)
Set the receiver endpoint object.
Definition: sender.cpp:68
boost::asio::ip::udp::socket * socket
Definition: sender.hpp:59
boost::asio::ip::udp::endpoint endpoint
Definition: sender.hpp:60

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

Here is the call graph for this function:

◆ ~Sender()

travesim::udp::Sender::~Sender ( )
virtual

Destroy the Sender object.

Definition at line 40 of file sender.cpp.

40  {
41  this->socket->close();
42 
43  delete this->socket;
44 };
boost::asio::ip::udp::socket * socket
Definition: sender.hpp:59

References socket.

Member Function Documentation

◆ send()

size_t travesim::udp::Sender::send ( const char *  buffer,
const size_t  buffer_size 
)

Send data using UDP.

Parameters
bufferBuffer to be sent
buffer_sizeSize of the buffer to be sent
Returns
size_t Number of bytes sent
Todo:
Message logging?

Definition at line 46 of file sender.cpp.

46  {
47  size_t bytes_sent;
48  boost::system::error_code ec;
49 
50  bytes_sent = this->socket->send_to(boost::asio::buffer(buffer, buffer_size), this->endpoint, NO_FLAGS, ec);
51 
52  switch (ec.value()) {
53  case boost::system::errc::success: {
54  /**
55  * @todo Message logging?
56  */
57  break;
58  }
59 
60  default: {
61  throw boost::system::system_error(ec);
62  }
63  }
64 
65  return bytes_sent;
66 };
boost::asio::ip::udp::socket * socket
Definition: sender.hpp:59
#define NO_FLAGS
Definition: sender.cpp:25
boost::asio::ip::udp::endpoint endpoint
Definition: sender.hpp:60

References endpoint, NO_FLAGS, and socket.

Here is the caller graph for this function:

◆ set_receiver_endpoint()

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

Set the receiver endpoint object.

Parameters
receiver_addressAddress where to send data
receiver_portPort where to send data

Definition at line 68 of file sender.cpp.

68  {
69  const boost::asio::ip::address receiver_boost_addr = boost::asio::ip::address::from_string(receiver_address);
70 
71  this->endpoint = boost::asio::ip::udp::endpoint(receiver_boost_addr, receiver_port);
72 };
boost::asio::ip::udp::endpoint endpoint
Definition: sender.hpp:60

References endpoint.

Here is the caller graph for this function:

Member Data Documentation

◆ endpoint

boost::asio::ip::udp::endpoint travesim::udp::Sender::endpoint
protected

Multicast address and port pair

Definition at line 60 of file sender.hpp.

◆ io_context

boost::asio::io_context travesim::udp::Sender::io_context
private

boost/asio I/O execution context

Definition at line 63 of file sender.hpp.

◆ socket

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

Network socket

Definition at line 59 of file sender.hpp.


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