io
C++ I/O scheduling library with asynchronous socket operations
Loading...
Searching...
No Matches
socket_handle.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_HANDLE_HPP
23#define IO_SOCKET_HANDLE_HPP
24#include <boost/predef.h>
25
26#if BOOST_OS_WINDOWS
28#else
30#endif
31
32#include <atomic>
33#include <mutex>
34
35namespace io::socket {
47
48public:
54 socket_handle() = default;
55
61 socket_handle(const socket_handle &other) = delete;
62
68 auto operator=(const socket_handle &other) -> socket_handle & = delete;
69
78 socket_handle(socket_handle &&other) noexcept;
79
91 auto operator=(socket_handle &&other) noexcept -> socket_handle &;
92
101 explicit socket_handle(native_socket_type handle);
102
111 socket_handle(int domain, int type, int protocol);
112
121 explicit operator native_socket_type() const noexcept;
122
131 friend auto swap(socket_handle &lhs, socket_handle &rhs) noexcept -> void;
132
141 [[nodiscard]] explicit operator bool() const noexcept;
142
152 auto operator<=>(const socket_handle &other) const noexcept
153 -> std::strong_ordering;
154
164 auto operator==(const socket_handle &other) const noexcept -> bool;
165
174 auto
175 operator<=>(native_socket_type other) const noexcept -> std::strong_ordering;
176
187 auto operator==(native_socket_type other) const noexcept -> bool;
188
194 ~socket_handle();
195
196private:
203 auto close() noexcept -> void;
204
212 std::atomic<native_socket_type> socket_{INVALID_SOCKET};
213
218 mutable std::mutex mtx_;
219};
220
221} // namespace io::socket
222#endif // IO_SOCKET_HANDLE_HPP
A thread-safe, move-only RAII wrapper for a native socket handle.
socket_handle(const socket_handle &other)=delete
Deleted copy constructor.
auto operator=(const socket_handle &other) -> socket_handle &=delete
Deleted copy assignment operator.
socket_handle()=default
Default constructor. Initializes an invalid socket handle.
friend auto swap(socket_handle &lhs, socket_handle &rhs) noexcept -> void
Swaps the contents of two socket_handle objects.
The io::socket namespace provides a cross-platform abstraction for socket-level I/O operations.
Definition socket.hpp:31
int native_socket_type
The native socket handle type for POSIX systems.
Definition socket.hpp:38
This file contains the POSIX-specific socket definitions and functions.
This file contains the Windows-specific socket definitions and functions.