-
Notifications
You must be signed in to change notification settings - Fork 18
/
PacketDriver.cpp
95 lines (79 loc) · 3.25 KB
/
PacketDriver.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
83
84
85
86
87
88
89
90
91
92
93
94
// Velodyne HDL Packet Driver
// Nick Rypkema ([email protected]), MIT 2017
// shared library to read a Velodyne HDL packet streaming over UDP
#include <iostream>
#include "PacketDriver.h"
#include <boost/bind.hpp>
using boost::asio::ip::udp;
PacketDriver::PacketDriver()
{
}
PacketDriver::PacketDriver(unsigned int port) : _port(port)
{
boost::asio::ip::udp::endpoint destination_endpoint(boost::asio::ip::address_v4::any(), _port);
try {
_socket = boost::shared_ptr<boost::asio::ip::udp::socket>(new boost::asio::ip::udp::socket(_io_service));
_socket->open(destination_endpoint.protocol());
_socket->bind(destination_endpoint);
} catch(std::exception & e) {
std::cout << "PacketDriver: Error binding to socket - " << e.what() << ". Trying once more..." << std::endl;
try {
destination_endpoint = boost::asio::ip::udp::endpoint(boost::asio::ip::address_v4::any(), _port);
_socket = boost::shared_ptr<boost::asio::ip::udp::socket>(new boost::asio::ip::udp::socket(_io_service));
_socket->open(destination_endpoint.protocol());
_socket->bind(destination_endpoint);
} catch(std::exception & e) {
std::cout << "PacketDriver: Error binding to socket - " << e.what() << ". Failed!" << std::endl;
return;
}
}
std::cout << "PacketDriver: Success binding to Velodyne socket!" << std::endl;
return;
}
PacketDriver::~PacketDriver()
{
_socket->close();
_io_service.stop();
}
void PacketDriver::InitPacketDriver(unsigned int port)
{
_port = port;
boost::asio::ip::udp::endpoint destination_endpoint(boost::asio::ip::address_v4::any(), _port);
try {
_socket = boost::shared_ptr<boost::asio::ip::udp::socket>(new boost::asio::ip::udp::socket(_io_service));
_socket->open(destination_endpoint.protocol());
_socket->bind(destination_endpoint);
} catch(std::exception & e) {
std::cout << "PacketDriver: Error binding to socket - " << e.what() << ". Trying once more..." << std::endl;
try {
destination_endpoint = boost::asio::ip::udp::endpoint(boost::asio::ip::address_v4::any(), _port);
_socket = boost::shared_ptr<boost::asio::ip::udp::socket>(new boost::asio::ip::udp::socket(_io_service));
_socket->open(destination_endpoint.protocol());
_socket->bind(destination_endpoint);
} catch(std::exception & e) {
std::cout << "PacketDriver: Error binding to socket - " << e.what() << ". Failed!" << std::endl;
return;
}
}
std::cout << "PacketDriver: Success binding to Velodyne socket!" << std::endl;
return;
}
bool PacketDriver::GetPacket(std::string* data, unsigned int* data_length)
{
try {
_socket->async_receive(boost::asio::buffer(_rx_buffer, 1500),
boost::bind(&PacketDriver::GetPacketCallback, this, boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred, data, data_length));
_io_service.reset();
_io_service.run();
return (true);
} catch(std::exception & e) {
std::cout << "PacketDriver: Error receiving packet - " << e.what() << "." << std::endl;
return(false);
}
}
void PacketDriver::GetPacketCallback(const boost::system::error_code& error, std::size_t num_bytes, std::string* data, unsigned int* data_length)
{
(*data).assign(_rx_buffer, num_bytes);
*data_length = (unsigned int) num_bytes;
return;
}