tcp_client.cpp
Go to the documentation of this file.
1 /*
2  * Software License Agreement (BSD-3 License)
3  *
4  * Copyright (c) 2019 Rein Appeldoorn.
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/tcp_client.h>
39 
40 #include <iostream>
41 
42 using boost::asio::ip::tcp;
43 
44 namespace async_comm
45 {
46 
47 TCPClient::TCPClient(std::string host, uint16_t port, MessageHandler& message_handler) :
48  Comm(message_handler),
49  host_(host),
50  port_(port),
51  socket_(io_service_)
52 {
53 }
54 
56 {
57  do_close();
58 }
59 
61 {
62  return socket_.is_open();
63 }
64 
66 {
67  try
68  {
69  tcp::resolver resolver(io_service_);
70 
71  endpoint_ = *resolver.resolve({tcp::v4(), host_, "", boost::asio::ip::resolver_query_base::numeric_service});
72  endpoint_.port(port_);
73  socket_.open(tcp::v4());
74 
75  socket_.connect(endpoint_);
76 
77  socket_.set_option(tcp::socket::reuse_address(true));
78  socket_.set_option(tcp::socket::send_buffer_size(WRITE_BUFFER_SIZE*1024));
79  socket_.set_option(tcp::socket::receive_buffer_size(READ_BUFFER_SIZE*1024));
80  }
81  catch (boost::system::system_error e)
82  {
83  message_handler_.error(e.what());
84  return false;
85  }
86 
87  return true;
88 }
89 
91 {
92  socket_.close();
93 }
94 
95 void TCPClient::do_async_read(const boost::asio::mutable_buffers_1 &buffer,
96  boost::function<void(const boost::system::error_code&, size_t)> handler)
97 {
98  socket_.async_receive(buffer, handler);
99 }
100 
101 void TCPClient::do_async_write(const boost::asio::const_buffers_1 &buffer,
102  boost::function<void(const boost::system::error_code&, size_t)> handler)
103 {
104  socket_.async_send(buffer, handler);
105 }
106 
107 } // namespace async_comm
std::string host_
Definition: tcp_client.h:81
void do_async_read(const boost::asio::mutable_buffers_1 &buffer, boost::function< void(const boost::system::error_code &, size_t)> handler) override
Definition: tcp_client.cpp:95
boost::asio::ip::tcp::socket socket_
Definition: tcp_client.h:84
static constexpr size_t WRITE_BUFFER_SIZE
Definition: comm.h:144
boost::asio::io_service io_service_
Definition: comm.h:157
void do_close() override
Definition: tcp_client.cpp:90
Abstract base class for an asynchronous communication port.
Definition: comm.h:81
MessageHandler & message_handler_
Definition: comm.h:156
virtual void error(const std::string &message)=0
void do_async_write(const boost::asio::const_buffers_1 &buffer, boost::function< void(const boost::system::error_code &, size_t)> handler) override
Definition: tcp_client.cpp:101
TCPClient(std::string host=DEFAULT_HOST, uint16_t port=DEFAULT_PORT, MessageHandler &message_handler=default_message_handler_)
Connect to a TCP socket as a client.
Definition: tcp_client.cpp:47
bool is_open() override
Definition: tcp_client.cpp:60
static constexpr size_t READ_BUFFER_SIZE
Definition: comm.h:143
Abstract base class for message handler.
bool do_init() override
Definition: tcp_client.cpp:65
boost::asio::ip::tcp::endpoint endpoint_
Definition: tcp_client.h:85


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