Tacopie  3.0.0
Tacopie is a TCP Client & Server C++11 library.
All Classes Functions Variables Typedefs Enumerations
tcp_client.hpp
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 
31 #include <tacopie/network/io_service.hpp>
32 #include <tacopie/network/tcp_socket.hpp>
33 #include <tacopie/utils/typedefs.hpp>
34 
35 namespace tacopie {
36 
41 class tcp_client {
42 public:
44  tcp_client(void);
45  ~tcp_client(void);
46 
53  explicit tcp_client(tcp_socket&& socket);
54 
56  tcp_client(const tcp_client&) = delete;
58  tcp_client& operator=(const tcp_client&) = delete;
59 
60 public:
66  bool operator==(const tcp_client& rhs) const;
67 
73  bool operator!=(const tcp_client& rhs) const;
74 
75 public:
79  const std::string& get_host(void) const;
80 
84  std::uint32_t get_port(void) const;
85 
86 public:
94  void connect(const std::string& host, std::uint32_t port, std::uint32_t timeout_msecs = 0);
95 
101  void disconnect(bool wait_for_removal = false);
102 
106  bool is_connected(void) const;
107 
108 private:
112  void call_disconnection_handler(void);
113 
114 public:
120  struct read_result {
124  bool success;
128  std::vector<char> buffer;
129  };
130 
136  struct write_result {
140  bool success;
144  std::size_t size;
145  };
146 
147 public:
152  typedef std::function<void(read_result&)> async_read_callback_t;
153 
158  typedef std::function<void(write_result&)> async_write_callback_t;
159 
160 public:
166  struct read_request {
170  std::size_t size;
174  async_read_callback_t async_read_callback;
175  };
176 
182  struct write_request {
186  std::vector<char> buffer;
190  async_write_callback_t async_write_callback;
191  };
192 
193 public:
199  void async_read(const read_request& request);
200 
206  void async_write(const write_request& request);
207 
208 public:
213 
217  const tacopie::tcp_socket& get_socket(void) const;
218 
219 public:
223  const std::shared_ptr<tacopie::io_service>& get_io_service(void) const;
224 
225 public:
231  typedef std::function<void()> disconnection_handler_t;
232 
238  void set_on_disconnection_handler(const disconnection_handler_t& disconnection_handler);
239 
240 private:
247  void on_read_available(fd_t fd);
248 
255  void on_write_available(fd_t fd);
256 
257 private:
261  void clear_read_requests(void);
262 
266  void clear_write_requests(void);
267 
268 private:
277  async_read_callback_t process_read(read_result& result);
278 
287  async_write_callback_t process_write(write_result& result);
288 
289 private:
294  std::shared_ptr<io_service> m_io_service;
295 
299  tacopie::tcp_socket m_socket;
300 
304  std::atomic<bool> m_is_connected = ATOMIC_VAR_INIT(false);
305 
309  std::queue<read_request> m_read_requests;
313  std::queue<write_request> m_write_requests;
314 
318  std::mutex m_read_requests_mtx;
322  std::mutex m_write_requests_mtx;
323 
327  disconnection_handler_t m_disconnection_handler;
328 };
329 
330 } // namespace tacopie
void connect(const std::string &host, std::uint32_t port, std::uint32_t timeout_msecs=0)
bool success
Definition: tcp_client.hpp:124
std::function< void(write_result &)> async_write_callback_t
Definition: tcp_client.hpp:158
void disconnect(bool wait_for_removal=false)
void async_read(const read_request &request)
std::size_t size
Definition: tcp_client.hpp:170
Definition: tcp_socket.hpp:38
bool is_connected(void) const
const std::shared_ptr< tacopie::io_service > & get_io_service(void) const
void set_on_disconnection_handler(const disconnection_handler_t &disconnection_handler)
Definition: tcp_client.hpp:120
std::uint32_t get_port(void) const
Definition: tcp_client.hpp:136
bool operator==(const tcp_client &rhs) const
Definition: io_service.hpp:48
bool operator!=(const tcp_client &rhs) const
bool success
Definition: tcp_client.hpp:140
std::vector< char > buffer
Definition: tcp_client.hpp:186
std::function< void()> disconnection_handler_t
Definition: tcp_client.hpp:231
std::function< void(read_result &)> async_read_callback_t
Definition: tcp_client.hpp:152
Definition: tcp_client.hpp:166
Definition: tcp_client.hpp:41
async_read_callback_t async_read_callback
Definition: tcp_client.hpp:174
tacopie::tcp_socket & get_socket(void)
std::vector< char > buffer
Definition: tcp_client.hpp:128
void async_write(const write_request &request)
tcp_client(void)
ctor & dtor
const std::string & get_host(void) const
tcp_client & operator=(const tcp_client &)=delete
assignment operator
std::size_t size
Definition: tcp_client.hpp:144
Definition: tcp_client.hpp:182
async_write_callback_t async_write_callback
Definition: tcp_client.hpp:190