udp.h
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD-3 License)
3  *
4  * Copyright (c) 2018 Daniel Koch.
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice, this
11  * list of conditions and the following disclaimer.
12  *
13  * * Redistributions in binary form must reproduce the above copyright notice,
14  * this list of conditions and the following disclaimer in the documentation
15  * and/or other materials provided with the distribution.
16  *
17  * * Neither the name of the copyright holder nor the names of its
18  * contributors may be used to endorse or promote products derived from
19  * this software without specific prior written permission.
20  *
21  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
38 #ifndef ASYNC_COMM_UDP_H
39 #define ASYNC_COMM_UDP_H
40 
41 #include <string>
42 
43 #include <boost/asio.hpp>
44 #include <boost/function.hpp>
45 
47 #include <async_comm/comm.h>
48 
49 namespace async_comm
50 {
51 
56 class UDP : public Comm
57 {
58 public:
67  UDP(std::string bind_host = DEFAULT_BIND_HOST, uint16_t bind_port = DEFAULT_BIND_PORT,
68  std::string remote_host = DEFAULT_REMOTE_HOST, uint16_t remote_port = DEFAULT_REMOTE_PORT,
69  MessageHandler& message_handler = default_message_handler_);
70  ~UDP();
71 
72 private:
73  static constexpr auto DEFAULT_BIND_HOST = "localhost";
74  static constexpr uint16_t DEFAULT_BIND_PORT = 16140;
75  static constexpr auto DEFAULT_REMOTE_HOST = "localhost";
76  static constexpr uint16_t DEFAULT_REMOTE_PORT = 16145;
77 
78  bool is_open() override;
79  bool do_init() override;
80  void do_close() override;
81  void do_async_read(const boost::asio::mutable_buffers_1 &buffer,
82  boost::function<void(const boost::system::error_code&, size_t)> handler) override;
83  void do_async_write(const boost::asio::const_buffers_1 &buffer,
84  boost::function<void(const boost::system::error_code&, size_t)> handler) override;
85 
86  std::string bind_host_;
87  uint16_t bind_port_;
88 
89  std::string remote_host_;
90  uint16_t remote_port_;
91 
92  boost::asio::ip::udp::socket socket_;
93  boost::asio::ip::udp::endpoint bind_endpoint_;
94  boost::asio::ip::udp::endpoint remote_endpoint_;
95 };
96 
97 } // namespace async_comm
98 
99 #endif // ASYNC_COMM_UDP_H
boost::asio::ip::udp::endpoint bind_endpoint_
Definition: udp.h:93
bool do_init() override
Definition: udp.cpp:69
static constexpr uint16_t DEFAULT_REMOTE_PORT
Definition: udp.h:76
UDP(std::string bind_host=DEFAULT_BIND_HOST, uint16_t bind_port=DEFAULT_BIND_PORT, std::string remote_host=DEFAULT_REMOTE_HOST, uint16_t remote_port=DEFAULT_REMOTE_PORT, MessageHandler &message_handler=default_message_handler_)
Bind a UDP socket.
Definition: udp.cpp:48
boost::asio::ip::udp::socket socket_
Definition: udp.h:92
static constexpr auto DEFAULT_REMOTE_HOST
Definition: udp.h:75
static constexpr uint16_t DEFAULT_BIND_PORT
Definition: udp.h:74
Abstract base class for an asynchronous communication port.
Definition: comm.h:81
void do_close() override
Definition: udp.cpp:99
boost::asio::ip::udp::endpoint remote_endpoint_
Definition: udp.h:94
std::string remote_host_
Definition: udp.h:89
static DefaultMessageHandler default_message_handler_
Definition: comm.h:146
uint16_t bind_port_
Definition: udp.h:87
static constexpr auto DEFAULT_BIND_HOST
Definition: udp.h:73
Abstract base class for message handler.
bool is_open() override
Definition: udp.cpp:64
Asynchronous communication class for a UDP socket.
Definition: udp.h:56
std::string bind_host_
Definition: udp.h:86
uint16_t remote_port_
Definition: udp.h:90
void do_async_read(const boost::asio::mutable_buffers_1 &buffer, boost::function< void(const boost::system::error_code &, size_t)> handler) override
Definition: udp.cpp:104
void do_async_write(const boost::asio::const_buffers_1 &buffer, boost::function< void(const boost::system::error_code &, size_t)> handler) override
Definition: udp.cpp:110


async_comm
Author(s):
autogenerated on Fri May 14 2021 02:35:38