Tacopie  3.0.0
Tacopie is a TCP Client & Server C++11 library.
tcp_socket.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 <cstdint>
26 #include <string>
27 #include <vector>
28 
29 #include <tacopie/utils/typedefs.hpp>
30 
31 namespace tacopie {
32 
38 class tcp_socket {
39 public:
46  enum class type {
47  CLIENT,
48  SERVER,
49  UNKNOWN
50  };
51 
52 public:
54  tcp_socket(void);
56  ~tcp_socket(void) = default;
57 
66  tcp_socket(fd_t fd, const std::string& host, std::uint32_t port, type t);
67 
70 
72  tcp_socket(const tcp_socket&) = delete;
74  tcp_socket& operator=(const tcp_socket&) = delete;
75 
76 public:
82  bool operator==(const tcp_socket& rhs) const;
83 
89  bool operator!=(const tcp_socket& rhs) const;
90 
91 public:
99  std::vector<char> recv(std::size_t size_to_read);
100 
109  std::size_t send(const std::vector<char>& data, std::size_t size_to_write);
110 
119  void connect(const std::string& host, std::uint32_t port, std::uint32_t timeout_msecs = 0);
120 
128  void bind(const std::string& host, std::uint32_t port);
129 
136  void listen(std::size_t max_connection_queue);
137 
144  tcp_socket accept(void);
145 
149  void close(void);
150 
151 public:
155  const std::string& get_host(void) const;
156 
160  std::uint32_t get_port(void) const;
161 
165  type get_type(void) const;
166 
173  void set_type(type t);
174 
180  fd_t get_fd(void) const;
181 
182 public:
186  bool is_ipv6(void) const;
187 
188 private:
192  void create_socket_if_necessary(void);
193 
200  void check_or_set_type(type t);
201 
202 private:
206  fd_t m_fd;
207 
211  std::string m_host;
215  std::uint32_t m_port;
216 
220  type m_type;
221 };
222 
223 } // namespace tacopie
type get_type(void) const
bool is_ipv6(void) const
void connect(const std::string &host, std::uint32_t port, std::uint32_t timeout_msecs=0)
void set_type(type t)
tcp_socket accept(void)
Definition: tcp_socket.hpp:38
std::uint32_t get_port(void) const
~tcp_socket(void)=default
dtor
type
Definition: tcp_socket.hpp:46
tcp_socket & operator=(const tcp_socket &)=delete
assignment operator
Definition: io_service.hpp:48
tcp_socket(void)
ctor
fd_t get_fd(void) const
bool operator==(const tcp_socket &rhs) const
void listen(std::size_t max_connection_queue)
const std::string & get_host(void) const
std::size_t send(const std::vector< char > &data, std::size_t size_to_write)
bool operator!=(const tcp_socket &rhs) const
void bind(const std::string &host, std::uint32_t port)
std::vector< char > recv(std::size_t size_to_read)