udp_listener.cpp
Go to the documentation of this file.
1 #include <array>
2 #include <chrono>
3 #include <iomanip>
4 #include <iostream>
5 #include <limits>
6 
7 #include <boost/asio.hpp>
8 
10 
11 using boost::asio::ip::udp;
12 
13 int main(int argc, char** argv)
14 {
15  if(argc < 2)
16  {
17  std::cerr << "USAGE: udp_listener <port>\n";
18  return 1;
19  }
20 
21  const int port = std::atoi(argv[1]);
22  if(port <= 0 || port > std::numeric_limits<uint16_t>::max())
23  {
24  std::cerr << "Port (=" << port << ") must be between 1 and "
25  << std::numeric_limits<uint16_t>::max() << '\n';
26  return 1;
27  }
28 
29  boost::asio::io_service io;
30  udp::socket socket{io, udp::endpoint(udp::v4(), port)};
31 
32  std::array<uint8_t, 9000> recvBuffer;
33  udp::endpoint senderEndpoint;
35  auto lastPrint = std::chrono::steady_clock::now();
36 
37  std::cout << "Listening on UDP port " << port << "...\n";
38 
39  try
40  {
41  while(true)
42  {
43  const auto bytesRead =
44  socket.receive_from(boost::asio::buffer(recvBuffer), senderEndpoint);
45  decoder.addNewData(recvBuffer.data(), bytesRead);
46  while(decoder.parseNextFrame())
47  {
48  if((std::chrono::steady_clock::now() - lastPrint) >
49  std::chrono::seconds{1})
50  {
51  const auto nav = decoder.getLastNavData();
52  if(nav.position.is_initialized())
53  {
54  std::cout << "Position: \n"
55  << std::fixed << std::setprecision(9)
56  << " lat: " << nav.position->latitude_deg << " deg\n"
57  << " lon: " << nav.position->longitude_deg << " deg\n"
58  << std::setprecision(2)
59  << " alt: " << nav.position->altitude_m << " m\n";
60  lastPrint = std::chrono::steady_clock::now();
61  }
62  }
63  }
64  }
65  }
66  catch(const std::runtime_error& e)
67  {
68  std::cerr << "Parser error: " << e.what() << '\n';
69  return 1;
70  }
71 }
ixblue_stdbin_decoder::StdBinDecoder::addNewData
void addNewData(const uint8_t *data, std::size_t length)
Add new binary data to the parser internal buffer The new data can only be a part of a frame,...
Definition: stdbin_decoder.cpp:158
ixblue_stdbin_decoder::Data::BinaryNav::position
boost::optional< Position > position
Definition: stdbin.h:77
ixblue_stdbin_decoder::StdBinDecoder::getLastNavData
Data::BinaryNav getLastNavData(void) const
Definition: stdbin_decoder.h:89
stdbin_decoder.h
ixblue_stdbin_decoder::StdBinDecoder::parseNextFrame
bool parseNextFrame()
Try to parse a frame from the parser internal buffer. Some binary data must have been added with the ...
Definition: stdbin_decoder.cpp:163
ixblue_stdbin_decoder::StdBinDecoder
Parser of a STDBIN IXblue message. This is the entry point of the library. Usage of this class is as ...
Definition: stdbin_decoder.h:33
main
int main(int argc, char **argv)
Definition: udp_listener.cpp:13


ixblue_stdbin_decoder
Author(s): Adrien BARRAL , Laure LEBROTON
autogenerated on Wed Apr 6 2022 02:55:48