tcp_client.cpp
Go to the documentation of this file.
1 #include <array>
2 #include <chrono>
3 #include <iomanip>
4 #include <iostream>
5 #include <limits>
6 #include <string>
7 
8 #include <boost/asio.hpp>
9 
11 
12 using boost::asio::ip::tcp;
13 
14 int main(int argc, char** argv)
15 {
16  if(argc < 2)
17  {
18  std::cerr << "USAGE: tcp_client <host> <port>\n";
19  return 1;
20  }
21 
22  boost::asio::io_service io;
23 
24  tcp::resolver resolver(io);
25  tcp::resolver::query query(tcp::v4(), argv[1], argv[2]);
26  tcp::resolver::iterator iterator = resolver.resolve(query);
27 
28  std::cout << "Connecting to TCP server " << argv[1] << " on port " << argv[2] << '\n';
29 
30  tcp::socket socket{io};
31 
32  try
33  {
34  boost::asio::connect(socket, iterator);
35  }
36  catch(const boost::system::system_error& e)
37  {
38  std::cerr << "Failed to connect to the socket: " << e.what();
39  return 1;
40  }
41 
42  std::array<uint8_t, 9000> recvBuffer;
44  auto lastPrint = std::chrono::steady_clock::now();
45  try
46  {
47  while(true)
48  {
49  const auto bytesRead = socket.read_some(boost::asio::buffer(recvBuffer));
50 
51  decoder.addNewData(recvBuffer.data(), bytesRead);
52  while(decoder.parseNextFrame())
53  {
54  if((std::chrono::steady_clock::now() - lastPrint) >
55  std::chrono::seconds{1})
56  {
57  const auto nav = decoder.getLastNavData();
58  if(nav.position.is_initialized())
59  {
60  std::cout << "Position: \n"
61  << std::fixed << std::setprecision(9)
62  << " lat: " << nav.position->latitude_deg << " deg\n"
63  << " lon: " << nav.position->longitude_deg << " deg\n"
64  << std::setprecision(2)
65  << " alt: " << nav.position->altitude_m << " m\n";
66  lastPrint = std::chrono::steady_clock::now();
67  }
68  else
69  {
70  std::cout
71  << "Last nav frame received does not contain position\n";
72  }
73  }
74  }
75  }
76  }
77  catch(const std::runtime_error& e)
78  {
79  std::cerr << "Parser error: " << e.what() << '\n';
80  return 1;
81  }
82 }
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...
boost::optional< Position > position
Definition: stdbin.h:77
Data::BinaryNav getLastNavData(void) const
bool parseNextFrame()
Try to parse a frame from the parser internal buffer. Some binary data must have been added with the ...
Parser of a STDBIN IXblue message. This is the entry point of the library. Usage of this class is as ...
int main(int argc, char **argv)
Definition: tcp_client.cpp:14


ixblue_stdbin_decoder
Author(s): Adrien BARRAL , Laure LEBROTON
autogenerated on Sat Jan 9 2021 03:13:21