TraveSim Adapters  0.1
Protobuf adapters for TraveSim project
multicast_receiver.cpp
Go to the documentation of this file.
1 /**
2  * @file multicast_receiver.cpp
3  *
4  * @author Lucas Haug <lucas.haug@thuneratz.org>
5  * @author Lucas Schneider <lucas.schneider@thuneratz.org>
6  *
7  * @brief Receiver data using UDP in multicast mode
8  *
9  * @date 04/2021
10  *
11  * @copyright MIT License - Copyright (c) 2021 ThundeRatz
12  */
13 
15 
16 namespace travesim {
17 namespace udp {
18 /*****************************************
19  * Public Methods Bodies Definitions
20  *****************************************/
21 
22 MulticastReceiver::MulticastReceiver(std::string multicast_address, short multicast_port,
23  std::string receiver_address) : Receiver(receiver_address, multicast_port) {
24  this->set_multicast_address(multicast_address);
25 
26  this->open_socket();
27 };
28 
29 MulticastReceiver::MulticastReceiver(std::string multicast_address, short multicast_port) :
30  MulticastReceiver(multicast_address, multicast_port, multicast_address) {
31 };
32 
34  this->close_socket();
35 };
36 
37 void MulticastReceiver::set_multicast_address(const std::string multicast_address) {
38  const boost::asio::ip::address multicast_boost_addr = boost::asio::ip::address::from_string(multicast_address);
39  this->multicast_address = multicast_boost_addr;
40 };
41 
42 /*****************************************
43  * Protected Methods Bodies Definitions
44  *****************************************/
45 
47  // Create the socket so that multiple may be bound to the same address.
48  this->socket->open(this->receiver_endpoint.protocol());
49 
50  this->socket->set_option(boost::asio::ip::udp::socket::reuse_address(true));
51  this->socket->set_option(boost::asio::ip::multicast::hops(1));
52 
53  // Join the multicast group.
54  this->socket->set_option(boost::asio::ip::multicast::join_group(this->multicast_address));
55 
56  this->socket->bind(this->receiver_endpoint);
57 
58  // Use non blocking for syncronous reading
59  this->socket->non_blocking(true);
60 };
61 
63  if (this->socket->is_open()) {
64  this->socket->set_option(boost::asio::ip::multicast::leave_group(this->multicast_address));
65  this->socket->close();
66  }
67 };
68 } // namespace udp
69 } // namespace travesim
boost::asio::ip::udp::socket * socket
Definition: receiver.hpp:88
boost::asio::ip::udp::endpoint receiver_endpoint
Definition: receiver.hpp:89
~MulticastReceiver()
Destroy the Multicast Receiver object.
void set_multicast_address(const std::string multicast_address)
Set the multicast address.
Receiver class using UDP in multicast mode.
MulticastReceiver(const std::string multicast_address, const short multicast_port, const std::string receiver_address)
Construct a new Multicast Receiver object.
boost::asio::ip::address multicast_address
Receiver class using UDP.
Definition: receiver.hpp:25
void close_socket()
Close the socket.
Receiver data using UDP in multicast mode.
void open_socket()
Open the socket with the desired options.