TraveSim Adapters  0.1
Protobuf adapters for TraveSim project
unicast_receiver_example.cpp
Go to the documentation of this file.
1 /**
2  * @file unicast_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 UnicastReceiver
8  *
9  * @date 04/2021
10  *
11  * @copyright MIT License - Copyright (c) 2021 ThundeRatz
12  */
13 
14 #include <iostream>
15 
17 
18 /*****************************************
19  * Private Constants
20  *****************************************/
21 
22 #define BUFFER_SIZE 1024U
23 
24 /*****************************************
25  * Main Function
26  *****************************************/
27 
28 int main(int argc, char* argv[]) {
29  const std::string unicast_address_str = "127.0.0.1";
30  const short unicast_port = 30001;
31 
32  char data_buff[BUFFER_SIZE];
33 
34  try {
35  boost::asio::io_context io_context;
36  boost::asio::steady_timer my_timer(io_context);
37  travesim::udp::UnicastReceiver my_receiver(unicast_address_str, unicast_port);
38 
39  my_receiver.force_specific_source(true);
40 
41  size_t data_size = 0;
42 
43  for (long int i = 0;; i++) {
44  data_size = my_receiver.receive(data_buff, BUFFER_SIZE);
45 
46  if (data_size > 0) {
47  std::string received_msg(data_buff, data_size);
48  std::cout << received_msg << std::endl;
49  }
50 
51  my_timer.expires_after(std::chrono::milliseconds(200));
52  my_timer.wait();
53  }
54  } catch (std::exception& e) {
55  std::cerr << "Exception: " << e.what() << "\n";
56  }
57 
58  return 0;
59 }
Receiver data using UDP in unicast mode.
Receiver class using UDP in unicast mode.
size_t receive(char *buffer, const size_t buffer_size)
Receive data using UDP.
Definition: receiver.cpp:44
#define BUFFER_SIZE
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
int main(int argc, char *argv[])