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
21#pragma once
22#ifndef IO_SOCKET_POSIX_HPP
23#define IO_SOCKET_POSIX_HPP
24#include <ios>
25#include <span>
26
27#include <fcntl.h>
28#include <sys/socket.h>
29#include <unistd.h>
30
31namespace io::socket {
39
46inline static constexpr native_socket_type INVALID_SOCKET = -1;
47
51inline static constexpr int SOCKET_ERROR = -1;
52
56using socket_buffer_type = std::span<char, std::dynamic_extent>;
57
64using socket_message_type = struct msghdr;
65
74inline auto close(native_socket_type socket) noexcept -> int {
75 return ::close(socket);
76}
77
91template <typename... Args>
92inline auto fcntl(native_socket_type socket, int cmd,
93 Args &&...args) noexcept -> int {
94 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-vararg)
95 return ::fcntl(socket, cmd, std::forward<Args>(args)...);
96}
97
109inline auto sendmsg(native_socket_type socket, const socket_message_type *msg,
110 int flags) noexcept -> std::streamsize {
111 return ::sendmsg(socket, msg, flags);
112}
113
126 int flags) noexcept -> std::streamsize {
127 return ::recvmsg(socket, msg, flags);
128}
129
137using sockaddr_type = struct sockaddr;
138
146using sockaddr_storage_type = struct sockaddr_storage;
147
155using socklen_type = socklen_t;
156} // namespace io::socket
157
158#endif // IO_SOCKET_POSIX_HPP
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
auto close(native_socket_type socket) noexcept -> int
Closes a socket descriptor on POSIX systems.
Definition socket.hpp:74
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
socklen_t socklen_type
The type used to represent socket-related sizes on POSIX systems.
Definition socket.hpp:155
sockaddr_storage sockaddr_storage_type
The socket address storage structure for POSIX systems.
Definition socket.hpp:146
std::span< char, std::dynamic_extent > socket_buffer_type
The socket buffer type for POSIX systems.
Definition socket.hpp:56
constexpr detail::cpo< socket::recvmsg_t > recvmsg
A customization point object that receives a message from a socket.
Definition io.hpp:49
constexpr detail::cpo< socket::fcntl_t > fcntl
A customization point object that performs a file control operation on a socket.
Definition io.hpp:64
constexpr detail::cpo< socket::sendmsg_t > sendmsg
A customization point object that sends a message on a socket.
Definition io.hpp:47