-
Notifications
You must be signed in to change notification settings - Fork 10
/
tcp_client.cpp
82 lines (71 loc) · 2.4 KB
/
tcp_client.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <array>
#include <chrono>
#include <iomanip>
#include <iostream>
#include <limits>
#include <string>
#include <boost/asio.hpp>
#include <ixblue_stdbin_decoder/stdbin_decoder.h>
using boost::asio::ip::tcp;
int main(int argc, char** argv)
{
if(argc < 2)
{
std::cerr << "USAGE: tcp_client <host> <port>\n";
return 1;
}
boost::asio::io_service io;
tcp::resolver resolver(io);
tcp::resolver::query query(tcp::v4(), argv[1], argv[2]);
tcp::resolver::iterator iterator = resolver.resolve(query);
std::cout << "Connecting to TCP server " << argv[1] << " on port " << argv[2] << '\n';
tcp::socket socket{io};
try
{
boost::asio::connect(socket, iterator);
}
catch(const boost::system::system_error& e)
{
std::cerr << "Failed to connect to the socket: " << e.what();
return 1;
}
std::array<uint8_t, 9000> recvBuffer;
ixblue_stdbin_decoder::StdBinDecoder decoder;
auto lastPrint = std::chrono::steady_clock::now();
try
{
while(true)
{
const auto bytesRead = socket.read_some(boost::asio::buffer(recvBuffer));
decoder.addNewData(recvBuffer.data(), bytesRead);
while(decoder.parseNextFrame())
{
if((std::chrono::steady_clock::now() - lastPrint) >
std::chrono::seconds{1})
{
const auto nav = decoder.getLastNavData();
if(nav.position.is_initialized())
{
std::cout << "Position: \n"
<< std::fixed << std::setprecision(9)
<< " lat: " << nav.position->latitude_deg << " deg\n"
<< " lon: " << nav.position->longitude_deg << " deg\n"
<< std::setprecision(2)
<< " alt: " << nav.position->altitude_m << " m\n";
lastPrint = std::chrono::steady_clock::now();
}
else
{
std::cout
<< "Last nav frame received does not contain position\n";
}
}
}
}
}
catch(const std::runtime_error& e)
{
std::cerr << "Parser error: " << e.what() << '\n';
return 1;
}
}