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_WINDOWS_HPP
23#define IO_SOCKET_WINDOWS_HPP
24#include <cassert>
25#include <ios>
26#include <span>
27#include <tuple>
28
29#include <winsock2.h>
30#include <ws2tcpip.h>
31
32namespace io::socket {
38using native_socket_type = ::SOCKET;
39
46inline static constexpr native_socket_type INVALID_SOCKET = INVALID_SOCKET;
47
51inline static constexpr int SOCKET_ERROR = ::SOCKET_ERROR;
52
56using socket_buffer_type = std::span<char, std::dynamic_extent>;
57
63using socket_message_type = ::WSAMSG;
64
73inline auto close(native_socket_type socket) noexcept -> int {
74 return ::closesocket(socket);
75}
76
77// POSIX fcntl constants
78static constexpr int F_SETFL = 4;
79static constexpr int O_NONBLOCK = 2048;
80
100template <typename... Args>
101inline auto fcntl(native_socket_type socket, int cmd,
102 Args &&...args) noexcept -> int {
103 static_assert(sizeof...(args) == 1,
104 "fcntl for windows is only implemented for F_SETFL and a "
105 "single argument O_NONBLOCK.");
106 assert(cmd == F_SETFL && "fcntl for windows is only implemented for F_SETFL");
107
108 auto arg = std::get<0>(std::forward_as_tuple(std::forward<Args>(args)...));
109 auto mode = (arg & O_NONBLOCK) ? 1 : 0;
110 switch (cmd) {
111 case F_SETFL:
112 return ioctlsocket(socket, FIONBIO, &mode);
113 default:
114 return SOCKET_ERROR;
115 }
116}
117
130inline auto sendmsg(native_socket_type socket, const socket_message_type *msg,
131 int flags) noexcept -> std::streamsize {
132 std::streamsize len = 0;
133 int error = ::WSASendMsg(socket, msg, &len, nullptr, nullptr);
134 return (error == 0) ? len : error;
135}
136
148inline auto recvmsg(native_socket_type socket, socket_message_type *msg,
149 int flags) noexcept -> std::streamsize {
150 std::streamsize len = 0;
151 int error = ::WSARecvMsg(socket, msg, &len, nullptr, nullptr);
152 return (error == 0) ? len : error;
153}
154
162using sockaddr_type = ::SOCKADDR;
163
171using sockaddr_storage_type = ::SOCKADDR_STORAGE;
172
179using socklen_type = int;
180} // namespace io::socket
181
182#endif // IO_SOCKET_WINDOWS_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