tcp_client.h
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 #ifndef ASYNC_COMM_TCP_CLIENT_H
39 #define ASYNC_COMM_TCP_CLIENT_H
40 
41 #include <string>
42 
43 #include <boost/asio.hpp>
44 #include <boost/function.hpp>
45 
46 #include <async_comm/comm.h>
47 
48 namespace async_comm
49 {
50 
55 class TCPClient : public Comm
56 {
57 public:
63  TCPClient(std::string host = DEFAULT_HOST, uint16_t port = DEFAULT_PORT);
64  ~TCPClient();
65 
66 private:
67  static constexpr auto DEFAULT_HOST = "localhost";
68  static constexpr uint16_t DEFAULT_PORT = 16140;
69 
70  bool is_open() override;
71  bool do_init() override;
72  void do_close() override;
73  void do_async_read(const boost::asio::mutable_buffers_1 &buffer,
74  boost::function<void(const boost::system::error_code&, size_t)> handler) override;
75  void do_async_write(const boost::asio::const_buffers_1 &buffer,
76  boost::function<void(const boost::system::error_code&, size_t)> handler) override;
77 
78  std::string host_;
79  uint16_t port_;
80 
81  boost::asio::ip::tcp::socket socket_;
82  boost::asio::ip::tcp::endpoint endpoint_;
83 };
84 
85 } // namespace async_comm
86 
87 #endif // ASYNC_COMM_TCP_CLIENT_H
std::string host_
Definition: tcp_client.h:78
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:81
void do_close() override
Definition: tcp_client.cpp:90
Abstract base class for an asynchronous communication port.
Definition: comm.h:59
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
Asynchronous communication class for a TCP client.
Definition: tcp_client.h:55
static constexpr uint16_t DEFAULT_PORT
Definition: tcp_client.h:68
bool is_open() override
Definition: tcp_client.cpp:60
TCPClient(std::string host=DEFAULT_HOST, uint16_t port=DEFAULT_PORT)
Connect to a TCP socket as a client.
Definition: tcp_client.cpp:47
bool do_init() override
Definition: tcp_client.cpp:65
boost::asio::ip::tcp::endpoint endpoint_
Definition: tcp_client.h:82
static constexpr auto DEFAULT_HOST
Definition: tcp_client.h:67


async_comm
Author(s):
autogenerated on Thu May 16 2019 03:03:47