udp.cpp
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 #include <async_comm/udp.h>
39 
40 #include <iostream>
41 
42 using boost::asio::ip::udp;
43 
44 namespace async_comm
45 {
46 
47 
48 UDP::UDP(std::string bind_host, uint16_t bind_port, std::string remote_host, uint16_t remote_port,
49  MessageHandler& message_handler) :
50  Comm(message_handler),
51  bind_host_(bind_host),
52  bind_port_(bind_port),
53  remote_host_(remote_host),
54  remote_port_(remote_port),
55  socket_(io_service_)
56 {
57 }
58 
60 {
61  do_close();
62 }
63 
65 {
66  return socket_.is_open();
67 }
68 
70 {
71  try
72  {
73  udp::resolver resolver(io_service_);
74 
75  bind_endpoint_ = *resolver.resolve({udp::v4(), bind_host_, "",
76  boost::asio::ip::resolver_query_base::numeric_service});
78 
79  remote_endpoint_ = *resolver.resolve({udp::v4(), remote_host_, "",
80  boost::asio::ip::resolver_query_base::numeric_service});
82 
83  socket_.open(udp::v4());
84  socket_.bind(bind_endpoint_);
85 
86  socket_.set_option(udp::socket::reuse_address(true));
87  socket_.set_option(udp::socket::send_buffer_size(WRITE_BUFFER_SIZE*1024));
88  socket_.set_option(udp::socket::receive_buffer_size(READ_BUFFER_SIZE*1024));
89  }
90  catch (boost::system::system_error e)
91  {
92  message_handler_.error(e.what());
93  return false;
94  }
95 
96  return true;
97 }
98 
100 {
101  socket_.close();
102 }
103 
104 void UDP::do_async_read(const boost::asio::mutable_buffers_1 &buffer,
105  boost::function<void(const boost::system::error_code&, size_t)> handler)
106 {
107  socket_.async_receive_from(buffer, remote_endpoint_, handler);
108 }
109 
110 void UDP::do_async_write(const boost::asio::const_buffers_1 &buffer,
111  boost::function<void(const boost::system::error_code&, size_t)> handler)
112 {
113  socket_.async_send_to(buffer, remote_endpoint_, handler);
114 }
115 
116 } // namespace async_comm
boost::asio::ip::udp::endpoint bind_endpoint_
Definition: udp.h:93
bool do_init() override
Definition: udp.cpp:69
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
static constexpr size_t WRITE_BUFFER_SIZE
Definition: comm.h:144
boost::asio::ip::udp::socket socket_
Definition: udp.h:92
boost::asio::io_service io_service_
Definition: comm.h:157
Abstract base class for an asynchronous communication port.
Definition: comm.h:81
void do_close() override
Definition: udp.cpp:99
MessageHandler & message_handler_
Definition: comm.h:156
virtual void error(const std::string &message)=0
boost::asio::ip::udp::endpoint remote_endpoint_
Definition: udp.h:94
std::string remote_host_
Definition: udp.h:89
uint16_t bind_port_
Definition: udp.h:87
static constexpr size_t READ_BUFFER_SIZE
Definition: comm.h:143
Abstract base class for message handler.
bool is_open() override
Definition: udp.cpp:64
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