udp_server.h
Go to the documentation of this file.
1 /*
2 * Unpublished Copyright (c) 2009-2021 AutonomouStuff, LLC, All Rights Reserved.
3 *
4 * This file is part of the network_interface ROS 1.0 driver which is released under the MIT license.
5 * See file LICENSE included with this software or go to https://opensource.org/licenses/MIT for full license details.
6 */
7 
8 // Define a class that supports a basic network interface that's independent of the hardware and driver library used
9 // Different libraries can be created to define all these functions for a specific driver library
10 
11 #ifndef NETWORK_INTERFACE_UDP_SERVER_H
12 #define NETWORK_INTERFACE_UDP_SERVER_H
13 
15 
16 // C++ Includes
17 #include <cstdio>
18 #include <iostream>
19 #include <string>
20 #include <vector>
21 #include <boost/asio.hpp>
22 #include <boost/bind.hpp>
23 
24 // OS Includes
25 #include <unistd.h>
26 
27 namespace AS
28 {
29 namespace Network
30 {
31 
32 class UDPServer
33 {
34 public:
35  UDPServer();
36 
37  // Called to pass in parameters and open ethernet link
38  ReturnStatuses open(const std::string& ip_address, uint32_t port);
39 
40  // Close the bound socket connection
42 
43  // Check on the status of the link
44  bool is_open();
45 
46  // Register a callback for receiving data and sending a response
47  void registerReceiveHandler(std::function<std::vector<uint8_t>(const std::vector<uint8_t>&)> callback);
48 
49  // Run the io_service, will block until shutdown
50  void run();
51 
52  // Stop the io_service, thread safe
53  void stop();
54 
55 private:
56  // Queue an io_service task for receiving on the socket
57  void startReceive();
58 
59  // Callback for when received data is ready
60  void handleReceive(const boost::system::error_code& error, std::size_t bytes_transferred);
61 
62  // Callback for when server replies are sent
63  void handleSend(std::vector<uint8_t> sent_payload,
64  const boost::system::error_code& ec, std::size_t bytes_transferred);
65 
66  boost::asio::io_service io_service_;
67  boost::asio::ip::udp::socket socket_;
68  boost::asio::ip::udp::endpoint server_endpoint_;
69  boost::asio::ip::udp::endpoint client_endpoint_;
70  std::function<std::vector<uint8_t>(const std::vector<uint8_t>&)> receive_callback_;
71 };
72 
73 std::string return_status_desc(const ReturnStatuses &ret);
74 
75 } // namespace Network
76 } // namespace AS
77 #endif // NETWORK_INTERFACE_UDP_SERVER_H
AS::Network::UDPServer::run
void run()
Definition: udp_server.cpp:137
AS::Network::UDPServer::handleReceive
void handleReceive(const boost::system::error_code &error, std::size_t bytes_transferred)
Definition: udp_server.cpp:93
AS::Network::UDPServer::UDPServer
UDPServer()
Definition: udp_server.cpp:16
AS::Network::UDPServer::stop
void stop()
Definition: udp_server.cpp:142
AS::Network::UDPServer::receive_callback_
std::function< std::vector< uint8_t >const std::vector< uint8_t > &)> receive_callback_
Definition: udp_server.h:70
AS::Network::UDPServer::client_endpoint_
boost::asio::ip::udp::endpoint client_endpoint_
Definition: udp_server.h:69
AS::Network::UDPServer
Definition: udp_server.h:32
AS::Network::UDPServer::registerReceiveHandler
void registerReceiveHandler(std::function< std::vector< uint8_t >(const std::vector< uint8_t > &)> callback)
Definition: udp_server.cpp:78
AS::Network::UDPServer::handleSend
void handleSend(std::vector< uint8_t > sent_payload, const boost::system::error_code &ec, std::size_t bytes_transferred)
Definition: udp_server.cpp:126
AS::Network::UDPServer::is_open
bool is_open()
Definition: udp_server.cpp:73
AS::Network::UDPServer::server_endpoint_
boost::asio::ip::udp::endpoint server_endpoint_
Definition: udp_server.h:68
AS
Definition: common.h:11
AS::Network::UDPServer::open
ReturnStatuses open(const std::string &ip_address, uint32_t port)
Definition: udp_server.cpp:21
common.h
AS::Network::return_status_desc
std::string return_status_desc(const ReturnStatuses &ret)
Definition: utils.cpp:11
AS::Network::UDPServer::close
ReturnStatuses close()
Definition: udp_server.cpp:53
AS::Network::UDPServer::startReceive
void startReceive()
Definition: udp_server.cpp:84
AS::Network::UDPServer::socket_
boost::asio::ip::udp::socket socket_
Definition: udp_server.h:67
AS::Network::UDPServer::io_service_
boost::asio::io_service io_service_
Definition: udp_server.h:66
AS::Network::ReturnStatuses
ReturnStatuses
Definition: common.h:16


network_interface
Author(s): Joshua Whitley , Daniel Stanek , Joe Kale
autogenerated on Wed Mar 2 2022 00:35:33