Tacopie  3.0.0
Tacopie is a TCP Client & Server C++11 library.
tcp_client.hpp
Go to the documentation of this file.
1 // MIT License
2 //
3 // Copyright (c) 2016-2017 Simon Ninon <simon.ninon@gmail.com>
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to deal
7 // in the Software without restriction, including without limitation the rights
8 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 // copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // The above copyright notice and this permission notice shall be included in all
13 // copies or substantial portions of the Software.
14 //
15 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 // SOFTWARE.
22 
23 #pragma once
24 
25 #include <atomic>
26 #include <cstdint>
27 #include <mutex>
28 #include <queue>
29 #include <string>
30 
34 
35 namespace tacopie {
36 
37 class tcp_client {
38 public:
40  tcp_client(std::uint32_t num_io_workers = 1);
41  ~tcp_client(void);
42 
49  explicit tcp_client(tcp_socket&& socket);
50 
52  tcp_client(const tcp_client&) = delete;
54  tcp_client& operator=(const tcp_client&) = delete;
55 
56 public:
62  bool operator==(const tcp_client& rhs) const;
63 
69  bool operator!=(const tcp_client& rhs) const;
70 
71 public:
75  const std::string& get_host(void) const;
76 
80  std::uint32_t get_port(void) const;
81 
82 public:
90  void connect(const std::string& host, std::uint32_t port, std::uint32_t timeout_msecs = 0);
91 
97  void disconnect(bool wait_for_removal = false);
98 
102  bool is_connected(void) const;
103 
104 private:
108  void call_disconnection_handler(void);
109 
110 public:
116  struct read_result {
117  bool success;
118  std::vector<char> buffer;
119  };
120 
126  struct write_result {
127  bool success;
128  std::size_t size;
129  };
130 
131 public:
136  typedef std::function<void(read_result&)> async_read_callback_t;
137 
142  typedef std::function<void(write_result&)> async_write_callback_t;
143 
144 public:
150  struct read_request {
151  std::size_t size;
152  async_read_callback_t async_read_callback;
153  };
154 
160  struct write_request {
161  std::vector<char> buffer;
162  async_write_callback_t async_write_callback;
163  };
164 
165 public:
171  void async_read(const read_request& request);
172 
178  void async_write(const write_request& request);
179 
180 public:
185 
189  const tacopie::tcp_socket& get_socket(void) const;
190 
191 public:
195  const std::shared_ptr<tacopie::io_service>& get_io_service(void) const;
196 
197 public:
203  typedef std::function<void()> disconnection_handler_t;
204 
210  void set_on_disconnection_handler(const disconnection_handler_t& disconnection_handler);
211 
212 private:
219  void on_read_available(fd_t fd);
220 
227  void on_write_available(fd_t fd);
228 
229 private:
233  void clear_read_requests(void);
234 
238  void clear_write_requests(void);
239 
240 private:
249  async_read_callback_t process_read(read_result& result);
250 
259  async_write_callback_t process_write(write_result& result);
260 
261 private:
266  std::shared_ptr<io_service> m_io_service;
267 
272 
276  std::atomic<bool> m_is_connected = ATOMIC_VAR_INIT(false);
277 
281  std::queue<read_request> m_read_requests;
285  std::queue<write_request> m_write_requests;
286 
295 
299  disconnection_handler_t m_disconnection_handler;
300 };
301 
302 } // namespace tacopie
void connect(const std::string &host, std::uint32_t port, std::uint32_t timeout_msecs=0)
bool success
Definition: tcp_client.hpp:117
async_write_callback_t process_write(write_result &result)
void clear_write_requests(void)
std::function< void(write_result &)> async_write_callback_t
Definition: tcp_client.hpp:142
void disconnect(bool wait_for_removal=false)
void async_read(const read_request &request)
std::size_t size
Definition: tcp_client.hpp:151
Definition: tcp_socket.hpp:38
bool is_connected(void) const
const std::shared_ptr< tacopie::io_service > & get_io_service(void) const
tacopie::tcp_socket m_socket
Definition: tcp_client.hpp:271
void on_write_available(fd_t fd)
void set_on_disconnection_handler(const disconnection_handler_t &disconnection_handler)
void clear_read_requests(void)
void call_disconnection_handler(void)
Definition: tcp_client.hpp:116
std::uint32_t get_port(void) const
Definition: tcp_client.hpp:126
bool operator==(const tcp_client &rhs) const
tcp_client(std::uint32_t num_io_workers=1)
ctor & dtor
Definition: io_service.hpp:48
bool operator!=(const tcp_client &rhs) const
bool success
Definition: tcp_client.hpp:127
std::mutex m_write_requests_mtx
Definition: tcp_client.hpp:294
std::vector< char > buffer
Definition: tcp_client.hpp:161
std::function< void()> disconnection_handler_t
Definition: tcp_client.hpp:203
std::function< void(read_result &)> async_read_callback_t
Definition: tcp_client.hpp:136
std::atomic< bool > m_is_connected
Definition: tcp_client.hpp:276
Definition: tcp_client.hpp:150
Definition: tcp_client.hpp:37
async_read_callback_t async_read_callback
Definition: tcp_client.hpp:152
async_read_callback_t process_read(read_result &result)
std::mutex m_read_requests_mtx
Definition: tcp_client.hpp:290
tacopie::tcp_socket & get_socket(void)
std::vector< char > buffer
Definition: tcp_client.hpp:118
void async_write(const write_request &request)
disconnection_handler_t m_disconnection_handler
Definition: tcp_client.hpp:299
void on_read_available(fd_t fd)
const std::string & get_host(void) const
tcp_client & operator=(const tcp_client &)=delete
assignment operator
std::size_t size
Definition: tcp_client.hpp:128
Definition: tcp_client.hpp:160
std::queue< write_request > m_write_requests
Definition: tcp_client.hpp:285
int fd_t
file descriptor platform type
Definition: typedefs.hpp:36
async_write_callback_t async_write_callback
Definition: tcp_client.hpp:162
std::shared_ptr< io_service > m_io_service
Definition: tcp_client.hpp:266
std::queue< read_request > m_read_requests
Definition: tcp_client.hpp:281