io
C++ I/O scheduling library with asynchronous socket operations
Loading...
Searching...
No Matches
socket.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
25#pragma once
26#ifndef IO_SOCKET_HPP
27#define IO_SOCKET_HPP
28
29#include "socket_address.hpp"
30#include "socket_handle.hpp"
31#include "socket_message.hpp"
32#include "socket_ops.hpp"
33
42namespace io::socket {
57auto tag_invoke(bind_t, const socket_handle &socket, const sockaddr_type *addr,
58 socklen_type len) -> int;
59
72auto tag_invoke(bind_t, const socket_handle &socket,
73 const socket_address &addr) -> int;
74
87auto tag_invoke(listen_t, const socket_handle &socket, int backlog) -> int;
88
102auto tag_invoke(connect_t, const socket_handle &socket,
103 const sockaddr_type *addr, socklen_type len) -> int;
104
116auto tag_invoke(connect_t, const socket_handle &socket,
117 const socket_address &addr) -> int;
118
134auto tag_invoke(accept_t, const socket_handle &socket, sockaddr_type *addr,
136
151auto tag_invoke(accept_t, const socket_handle &socket, socket_address addr = {})
152 -> std::tuple<socket_handle, socket_address>;
153
164auto tag_invoke(sendmsg_t, const socket_handle &socket,
165 const socket_message_type *msg, int flags) -> std::streamsize;
166
180auto tag_invoke(sendmsg_t, const socket_handle &socket,
181 const socket_message &msg) -> std::streamsize;
182
193auto tag_invoke(recvmsg_t, const socket_handle &socket,
194 socket_message_type *msg, int flags) -> std::streamsize;
195
208auto tag_invoke(getsockopt_t, const socket_handle &socket, int level,
209 int optname, void *optval, socklen_type *optlen) -> int;
210
223auto tag_invoke(setsockopt_t, const socket_handle &socket, int level,
224 int optname, const void *optval, socklen_type optlen) -> int;
225
236auto tag_invoke(getsockname_t, const socket_handle &socket, sockaddr_type *addr,
237 socklen_type *len) -> int;
238
249auto tag_invoke(getpeername_t, const socket_handle &socket, sockaddr_type *addr,
250 socklen_type *len) -> int;
251
264auto tag_invoke(shutdown_t, const socket_handle &socket, int how) -> int;
265
276template <typename... Args>
277auto tag_invoke([[maybe_unused]] fcntl_t tag, const socket_handle &socket,
278 int cmd, Args &&...args) -> int {
279 return ::io::socket::fcntl(static_cast<native_socket_type>(socket), cmd,
280 std::forward<Args>(args)...);
281}
282
283// TODO: Implement free-standing functions in the berkeley sockets APIs
284// - send
285// - sendto
286// - recv
287// - recvfrom
288} // namespace io::socket
289
290#endif // IOSCHED_SOCKET_HPP
A thread-safe, move-only RAII wrapper for a native socket handle.
The io::socket namespace provides a cross-platform abstraction for socket-level I/O operations.
Definition socket.hpp:31
msghdr socket_message_type
The socket message type for POSIX systems.
Definition socket.hpp:64
sockaddr sockaddr_type
The generic socket address structure for POSIX systems.
Definition socket.hpp:137
int native_socket_type
The native socket handle type for POSIX systems.
Definition socket.hpp:38
auto tag_invoke(bind_t tag, const socket_handle &socket, const sockaddr_type *addr, socklen_type len) -> int
Binds a socket to a local address.
Definition socket.cpp:20
socklen_t socklen_type
The type used to represent socket-related sizes on POSIX systems.
Definition socket.hpp:155
This file defines the socket_address class, a platform-independent abstraction for socket addresses.
This file defines the socket_handle class, a cross-platform, thread-safe RAII wrapper for native sock...
This file defines the socket_message class, a thread-safe container for advanced socket I/O operation...
This file defines the socket customization point operations.
A tag type for the io::fcntl customization point object (CPO).