LIBWIRE
Next-generation C++17 networking library.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Macros Pages
bsdsocket.hpp
Go to the documentation of this file.
1 /*
2  * Copyright © 2018 Max Mazurov (fox.cpp) <fox.cpp [at] disroot [dot] org>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a copy
5  * of this software and associated documentation files (the "Software"), to deal
6  * in the Software without restriction, including without limitation the rights
7  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8  * copies of the Software, and to permit persons to whom the Software is
9  * furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20  * SOFTWARE.
21  */
22 
23 #pragma once
24 
25 #include <cstdint>
26 #include <tuple>
27 #include <system_error>
28 #include <libwire/address.hpp>
29 #include <libwire/protocols.hpp>
31 #include <libwire/endpoint.hpp>
32 
33 namespace libwire::internal_ {
37  struct socket {
38 #if defined(LIBWIRE_POSIX)
39  using native_handle_t = int;
40 #endif
41 #if defined(LIBWIRE_WINDOWS)
42  using native_handle_t = unsigned long long;
43  // Actually SOCKET type, but defined here as ULL to avoid inclusion of system headers.
44 #endif
45 
46  static unsigned max_pending_connections;
47 
54 #if defined(LIBWIRE_POSIX)
55  static constexpr native_handle_t not_initialized = -1;
56 #endif
57 #if defined(LIBWIRE_WINDOWS)
58  static constexpr native_handle_t not_initialized = ~0;
59  // Actually INVALID_SOCKET, but defined here to avoid inclusion of system headers.
60 #endif
61 
65  socket() noexcept = default;
66 
67  explicit socket(native_handle_t fd) : handle(fd) {
68  }
69 
74  socket(ip ipver, transport transport, std::error_code& ec) noexcept;
75 
76  socket(const socket&) = delete;
77  socket(socket&&) noexcept;
78  socket& operator=(const socket&) = delete;
79  socket& operator=(socket&&) noexcept;
80 
86  ~socket();
87 
88  native_handle_t native_handle() const noexcept;
89 
94  void connect(endpoint target, std::error_code& ec) noexcept;
95 
99  void shutdown(bool read = true, bool write = true) noexcept;
100 
105  void bind(endpoint target, std::error_code& ec) noexcept;
106 
113  void listen(int backlog, std::error_code& ec) noexcept;
114 
119  socket accept(std::error_code& ec) noexcept;
120 
125  size_t write(const void* input, size_t length_bytes, std::error_code& ec) noexcept;
126 
131  size_t read(void* output, size_t length_bytes, std::error_code& ec) noexcept;
132 
137  void disassociate() noexcept;
138 
142  size_t sendto(const void* input, size_t length_bytes, endpoint dest, std::error_code& ec) noexcept;
143 
147  size_t recvfrom(void* output, size_t length_bytes, endpoint& source, std::error_code& ec) noexcept;
148 
152  operator bool() const noexcept;
153 
154  endpoint local_endpoint() const noexcept;
155 
156  endpoint remote_endpoint() const noexcept;
157 
158  native_handle_t handle = not_initialized;
159 
160  struct state {
161  // Set if user did set_option(non_blocking, ...);
162  bool user_non_blocking : 1;
163  } state{};
164  };
165 } // namespace libwire::internal_
This file defines universal address structure for both IPv4 and IPv6 network addresses.
Cannot send after transport endpoint shutdown.
Definition: error.hpp:210