AsyncBerkeley
Asynchronous Berkeley sockets. Simple.
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
20#pragma once
21#ifndef IO_SOCKET_HANDLE_HPP
22#define IO_SOCKET_HANDLE_HPP
23#include "detail/socket.hpp"
24
25#include <atomic>
26#include <mutex>
27// Forward Declarations for testing.
29#ifndef NDEBUG
30namespace io::socket {
31auto is_valid_socket(native_socket_type handle) -> bool;
32} // namespace io::socket
33#endif // NDEBUG
36namespace io::socket {
43
44public:
48 socket_handle() = default;
49
50 socket_handle(const socket_handle &other) = delete;
51 auto operator=(const socket_handle &other) -> socket_handle & = delete;
52
57 socket_handle(socket_handle &&other) noexcept;
58
64 auto operator=(socket_handle &&other) noexcept -> socket_handle &;
65
71 explicit socket_handle(native_socket_type handle);
72
80 explicit socket_handle(int domain, int type, int protocol);
81
85 explicit operator native_socket_type() const noexcept;
86
90 friend auto swap(socket_handle &lhs, socket_handle &rhs) noexcept -> void;
91
95 [[nodiscard]] explicit operator bool() const noexcept;
96
100 auto operator<=>(const socket_handle &other) const noexcept
101 -> std::strong_ordering;
102
106 auto operator==(const socket_handle &other) const noexcept -> bool;
107
111 auto
112 operator<=>(native_socket_type other) const noexcept -> std::strong_ordering;
113
117 auto operator==(native_socket_type other) const noexcept -> bool;
118
122 auto set_error(int error) noexcept -> void;
123
127 auto get_error() const noexcept -> std::error_code;
128
133
134private:
138 auto close() noexcept -> void;
139
143 std::atomic<native_socket_type> socket_{INVALID_SOCKET};
147 std::atomic<int> error_;
151 mutable std::mutex mtx_;
152};
153
154} // namespace io::socket
155
156#include "detail/sync_operations.hpp" // IWYU pragma: export
157#include "impl/socket_handle_impl.hpp" // IWYU pragma: export
158
159#endif // IO_SOCKET_HANDLE_HPP
A thread-safe, move-only RAII wrapper for a native socket handle.
Definition socket_handle.hpp:42
socket_handle(socket_handle &&other) noexcept
Move constructor.
auto operator=(socket_handle &&other) noexcept -> socket_handle &
Move assignment.
socket_handle(native_socket_type handle)
Constructs from a native socket handle.
socket_handle()=default
Initializes an invalid socket handle.
socket_handle(int domain, int type, int protocol)
Constructs a new socket.
The io::socket namespace provides a cross-platform abstraction for socket-level I/O operations.
Definition poll_multiplexer.hpp:36