8 udp::endpoint target_socket,
9 std::shared_ptr<std::function<
void(std::vector<uint8_t>)>> callback)
10 : target_socket_(target_socket), callback_(callback) {
26 boost::asio::io_service io_service;
27 boost::asio::io_service::work work(io_service);
28 std::thread io_thread([&io_service]() { io_service.run(); });
31 udp::endpoint remote_endpoint;
34 std::vector<unsigned char> vec(1500, 0);
35 std::future<std::size_t> recv_length;
38 socket.async_receive_from(boost::asio::buffer(vec), remote_endpoint,
39 0, boost::asio::use_future);
41 if (recv_length.wait_for(std::chrono::milliseconds(100)) ==
42 std::future_status::timeout) {
45 vec.resize(recv_length.get());
66 if (error.value() != boost::system::errc::success) {
67 ROS_WARN(
"fail to send data. %s", error.message().c_str());
75 boost::asio::placeholders::error,
76 boost::asio::placeholders::bytes_transferred));
80 boost::asio::io_service service;
81 udp::socket socket(service);
82 socket.open(udp::v4());
83 socket.bind(udp::endpoint(udp::v4(), 0));
84 return socket.local_endpoint();
udp::endpoint local_endpoint_
virtual void Stop() override
virtual void Start() override
std::unique_ptr< std::thread > recv_thread_
void send_handler(const boost::system::error_code &error, std::size_t len)
std::unique_ptr< udp::socket > send_socket_
udp::endpoint getFreePort()
udp::endpoint target_socket_
virtual unsigned short Port() override
std::unique_ptr< boost::asio::io_service > send_io_service_
Component< SocketFactory > getUdpSocketComponent()
virtual void SendData(std::vector< uint8_t > vec) override