TraveSim Adapters  0.1
Protobuf adapters for TraveSim project
multicast_receiver_example.cpp
Go to the documentation of this file.
1 /**
2  * @file multicast_receiver_example.cpp
3  *
4  * @author Lucas Haug <lucas.haug@thuneratz.org>
5  * @author Lucas Schneider <lucas.schneider@thuneratz.org>
6  *
7  * @brief Example on how to use the MulticastReceiver
8  *
9  * @date 04/2021
10  *
11  * @copyright MIT License - Copyright (c) 2021 ThundeRatz
12  */
13 
14 #include <iostream>
15 #include <string>
16 #include <boost/asio.hpp>
17 #include "boost/bind.hpp"
18 
20 
21 /*****************************************
22  * Private Constants
23  *****************************************/
24 
25 #define BUFFER_SIZE 1024U
26 
27 /*****************************************
28  * Main Function
29  *****************************************/
30 
31 int main(int argc, char* argv[]) {
32  const std::string listen_address_str = "0.0.0.0";
33  const std::string multicast_address_str = "224.0.0.1";
34  const short multicast_port = 10002;
35 
36  char data_buff[BUFFER_SIZE];
37 
38  try {
39  boost::asio::io_context io_context;
40  boost::asio::steady_timer my_timer(io_context);
41  travesim::udp::MulticastReceiver my_receiver(multicast_address_str, multicast_port);
42 
43  size_t data_size = 0;
44 
45  for (long int i = 0;; i++) {
46  data_size = my_receiver.receive(data_buff, BUFFER_SIZE);
47 
48  if (data_size > 0) {
49  std::string received_msg(data_buff, data_size);
50  std::cout << received_msg << std::endl;
51  }
52 
53  if (i % 100000 == 0) {
54  std::cout << "Loop count: " << i << std::endl;
55  }
56 
57  my_timer.expires_after(std::chrono::milliseconds(200));
58  my_timer.wait();
59  }
60  } catch (std::exception& e) {
61  std::cerr << "Exception: " << e.what() << "\n";
62  }
63 
64  return 0;
65 }
int main(int argc, char *argv[])
Receiver class using UDP in multicast mode.
#define BUFFER_SIZE
size_t receive(char *buffer, const size_t buffer_size)
Receive data using UDP.
Definition: receiver.cpp:44
Receiver data using UDP in multicast mode.