AsyncBerkeley
Asynchronous Berkeley sockets. Simple.
Loading...
Searching...
No Matches
socket_message.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_MESSAGE_HPP
22#define IO_SOCKET_MESSAGE_HPP
23#include "detail/buffer_iterator.hpp"
24#include "detail/socket.hpp"
25#include "socket_address.hpp"
26
27#include <memory>
28#include <optional>
29#include <vector>
30namespace io::socket {
39 std::span<std::byte> msg_name;
41 std::span<native_buffer_type> msg_iov;
43 std::span<std::byte> msg_control;
45 int flags{};
46
48 [[nodiscard]] explicit operator socket_message_type() noexcept;
49};
50
57template <AllocatorLike Allocator = std::allocator<native_buffer_type>>
59public:
61 using buffer_type = std::vector<native_buffer_type, Allocator>;
63 using iterator = buffer_iterator<typename buffer_type::iterator>;
66 using const_iterator = buffer_iterator<typename buffer_type::const_iterator>;
68 using size_type = typename buffer_type::size_type;
69
74 message_buffer(const Allocator &alloc = Allocator()) noexcept(
75 noexcept(Allocator()));
76
82 template <ScatterGatherLike... Bufs>
83 constexpr message_buffer(const Bufs &...bufs) noexcept;
84
91 template <ScatterGatherLike Buf>
92 constexpr auto push_back(const Buf &buf) -> void;
93
98 constexpr auto push_back(native_buffer_type buf) -> void;
99
107 template <typename... Args>
108 constexpr auto emplace_back(Args &&...args) -> decltype(auto);
109
111 [[nodiscard]] constexpr auto begin() noexcept -> iterator;
112
115 [[nodiscard]] constexpr auto begin() const noexcept -> const_iterator;
116
118 [[nodiscard]] constexpr auto end() noexcept -> iterator;
119
121 [[nodiscard]] constexpr auto end() const noexcept -> const_iterator;
122
124 [[nodiscard]] constexpr auto size() const noexcept -> size_type;
125
127 [[nodiscard]] constexpr auto empty() const noexcept -> bool;
128
133 [[nodiscard]] explicit constexpr operator bool() const noexcept;
134
144 auto operator+=(std::size_t len) noexcept -> message_buffer &;
145
147 constexpr auto native() -> buffer_type & { return buffer_; }
148
149private:
150 buffer_type buffer_;
151};
152
159template <SocketAddress Addr = sockaddr_storage_type,
160 AllocatorLike Allocator = std::allocator<char>>
163 using message_allocator = std::allocator_traits<
164 Allocator>::template rebind_alloc<native_buffer_type>;
167 std::allocator_traits<Allocator>::template rebind_alloc<std::byte>;
169 using address_type = std::optional<socket_address<Addr>>;
173 using control_type = std::vector<std::byte, control_allocator>;
174
177
180
183
185 int flags{};
186
188 [[nodiscard]] explicit operator message_header() noexcept;
190 [[nodiscard]] explicit operator socket_message_type() noexcept;
191};
192
193} // namespace io::socket
194
195#include "impl/socket_message_impl.hpp" // IWYU pragma: export
196
197#endif // IO_SOCKET_MESSAGE_HPP
A container for managing buffers for scatter-gather I/O operations.
Definition socket_message.hpp:58
std::vector< native_buffer_type, Allocator > buffer_type
The underlying buffer type.
Definition socket_message.hpp:61
message_buffer(const Allocator &alloc=Allocator()) noexcept(noexcept(Allocator()))
Construct message buffer with a custom allocator.
typename buffer_type::size_type size_type
Size type for the buffer.
Definition socket_message.hpp:68
buffer_iterator< typename buffer_type::const_iterator > const_iterator
Constant iterator for the buffer that returns spans when dereferenced.
Definition socket_message.hpp:66
buffer_iterator< typename buffer_type::iterator > iterator
Iterator for the buffer that returns spans when dereferenced.
Definition socket_message.hpp:63
A concept that checks if a type is an allocator.
Definition concepts.hpp:51
A concept that describes a scatter/gather like buffer object.
Definition concepts.hpp:66
The io::socket namespace provides a cross-platform abstraction for socket-level I/O operations.
Definition poll_multiplexer.hpp:36
Defines the socket_address class for platform-independent socket address management.
Represents the header of a socket message.
Definition socket_message.hpp:37
std::span< std::byte > msg_name
Optional address of the sender/receiver.
Definition socket_message.hpp:39
std::span< native_buffer_type > msg_iov
I/O vectors for scatter/gather operations.
Definition socket_message.hpp:41
std::span< std::byte > msg_control
Ancillary data (control information).
Definition socket_message.hpp:43
int flags
Flags on the received message.
Definition socket_message.hpp:45
Represents a complete socket message.
Definition socket_message.hpp:161
std::vector< std::byte, control_allocator > control_type
The control buffer type.
Definition socket_message.hpp:173
std::optional< socket_address< Addr > > address_type
The socket address type.
Definition socket_message.hpp:169
address_type address
Optional address of the sender/receiver.
Definition socket_message.hpp:176
std::allocator_traits< Allocator >::template rebind_alloc< std::byte > control_allocator
The allocator type for control data.
Definition socket_message.hpp:167
message_type buffers
Buffers for scatter/gather I/O.
Definition socket_message.hpp:179
control_type control
Ancillary data (control information).
Definition socket_message.hpp:182
std::allocator_traits< Allocator >::template rebind_alloc< native_buffer_type > message_allocator
The allocator type for message buffers.
Definition socket_message.hpp:164