AsyncBerkeley
Asynchronous Berkeley sockets. Simple.
Loading...
Searching...
No Matches
customization.hpp
Go to the documentation of this file.
1/* Copyright 2025 Kevin Exton
2 *
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
20#pragma once
21#ifndef IO_CUSTOMIZATION_HPP
22#define IO_CUSTOMIZATION_HPP
23#include <span>
24#include <utility>
33namespace io {
34
37struct accept_t {};
38struct bind_t {};
39struct connect_t {};
40struct fcntl_t {};
41struct getpeername_t {};
42struct getsockname_t {};
43struct getsockopt_t {};
44struct listen_t {};
45struct recvmsg_t {};
46struct sendmsg_t {};
47struct setsockopt_t {};
48struct shutdown_t {};
49
50// TODO: Implement free-standing functions in the berkeley sockets APIs
51// - send
52// - sendto
53// - recv
54// - recvfrom
55
60template <typename T> struct cpo {
68 template <typename... Args>
69 constexpr auto operator()(Args &&...args) const -> decltype(auto)
70 {
71 return tag_invoke(static_cast<T *>(nullptr), std::forward<Args>(args)...);
72 }
73};
75
93inline auto accept(auto &&socket,
94 std::span<std::byte> address = {}) -> decltype(auto)
95{
96 static constexpr cpo<accept_t> accept{};
97 return accept(std::forward<decltype(socket)>(socket), std::move(address));
98}
99
106inline auto bind(auto &&socket,
107 std::span<const std::byte> address) -> decltype(auto)
108{
109 static constexpr cpo<bind_t> bind{};
110 return bind(std::forward<decltype(socket)>(socket), std::move(address));
111}
112
120inline auto connect(auto &&socket,
121 std::span<const std::byte> address) -> decltype(auto)
122{
123 static constexpr cpo<connect_t> connect{};
124 return connect(std::forward<decltype(socket)>(socket), std::move(address));
125}
126
134inline auto fcntl(auto &&socket, int cmd, auto &&...args) -> decltype(auto)
135{
136 static constexpr cpo<fcntl_t> fcntl{};
137 return fcntl(std::forward<decltype(socket)>(socket), cmd,
138 std::forward<decltype(args)>(args)...);
139}
140
147inline auto getpeername(auto &&socket,
148 std::span<std::byte> address) -> decltype(auto)
149{
150 static constexpr cpo<getpeername_t> getpeername{};
151 return getpeername(std::forward<decltype(socket)>(socket),
152 std::move(address));
153}
154
164// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
165inline auto getsockopt(auto &&socket, int level, int optname,
166 std::span<std::byte> option) -> decltype(auto)
167{
168 static constexpr cpo<getsockopt_t> getsockopt{};
169 return getsockopt(std::forward<decltype(socket)>(socket), level, optname,
170 std::move(option));
171}
172
179inline auto getsockname(auto &&socket,
180 std::span<std::byte> address) -> decltype(auto)
181{
182 static constexpr cpo<getsockname_t> getsockname{};
183 return getsockname(std::forward<decltype(socket)>(socket),
184 std::move(address));
185}
186
193inline auto listen(auto &&socket, int backlog) -> decltype(auto)
194{
195 static constexpr cpo<listen_t> listen{};
196 return listen(std::forward<decltype(socket)>(socket), backlog);
197}
198
207inline auto recvmsg(auto &&socket, auto &&msg, int flags) -> decltype(auto)
208{
209 static constexpr cpo<recvmsg_t> recvmsg{};
210 return recvmsg(std::forward<decltype(socket)>(socket),
211 std::forward<decltype(msg)>(msg), flags);
212}
213
222inline auto sendmsg(auto &&socket, auto &&msg, int flags) -> decltype(auto)
223{
224 static constexpr cpo<sendmsg_t> sendmsg{};
225 return sendmsg(std::forward<decltype(socket)>(socket),
226 std::forward<decltype(msg)>(msg), flags);
227}
228
237// NOLINTNEXTLINE(bugprone-easily-swappable-parameters)
238inline auto setsockopt(auto &&socket, int level, int optname,
239 std::span<const std::byte> option) -> decltype(auto)
240{
241 static constexpr cpo<setsockopt_t> setsockopt{};
242 return setsockopt(std::forward<decltype(socket)>(socket), level, optname,
243 std::move(option));
244}
245
252inline auto shutdown(auto &&socket, int how) -> decltype(auto)
253{
254 static constexpr cpo<shutdown_t> shutdown{};
255 return shutdown(std::forward<decltype(socket)>(socket), how);
256}
259} // namespace io
260#endif // IO_CUSTOMIZATION_HPP
auto listen(auto &&socket, int backlog) -> decltype(auto)
Sets a socket to listen for incoming connections.
Definition customization.hpp:193
auto sendmsg(auto &&socket, auto &&msg, int flags) -> decltype(auto)
Sends a message on a socket.
Definition customization.hpp:222
auto shutdown(auto &&socket, int how) -> decltype(auto)
Shuts down all or part of a connection on a socket.
Definition customization.hpp:252
auto accept(auto &&socket, std::span< std::byte > address={}) -> decltype(auto)
Accepts an incoming connection on a listening socket.
Definition customization.hpp:93
auto getsockname(auto &&socket, std::span< std::byte > address) -> decltype(auto)
Gets the local address of a socket.
Definition customization.hpp:179
auto getpeername(auto &&socket, std::span< std::byte > address) -> decltype(auto)
Gets the peer address of a connected socket.
Definition customization.hpp:147
auto bind(auto &&socket, std::span< const std::byte > address) -> decltype(auto)
Binds a socket to a local address.
Definition customization.hpp:106
auto setsockopt(auto &&socket, int level, int optname, std::span< const std::byte > option) -> decltype(auto)
Sets a socket option.
Definition customization.hpp:238
auto fcntl(auto &&socket, int cmd, auto &&...args) -> decltype(auto)
Performs a file control operation on a socket.
Definition customization.hpp:134
auto connect(auto &&socket, std::span< const std::byte > address) -> decltype(auto)
Connects a socket to a remote address.
Definition customization.hpp:120
auto getsockopt(auto &&socket, int level, int optname, std::span< std::byte > option) -> decltype(auto)
Gets a socket option.
Definition customization.hpp:165
auto recvmsg(auto &&socket, auto &&msg, int flags) -> decltype(auto)
Receives a message from a socket.
Definition customization.hpp:207
The io namespace contains all the functions and classes for the I/O library.
Definition executor.hpp:33