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/socket.hpp"
24#include "socket_address.hpp"
25
26#include <memory>
27#include <optional>
28#include <vector>
29namespace io::socket {
38 std::span<std::byte> msg_name;
40 std::span<native_buffer_type> msg_iov;
42 std::span<std::byte> msg_control;
44 int flags{};
45
47 [[nodiscard]] explicit operator socket_message_type() noexcept;
48};
49
56template <AllocatorLike Allocator = std::allocator<native_buffer_type>>
58public:
60 using buffer_type = std::vector<native_buffer_type, Allocator>;
62 using iterator = typename buffer_type::iterator;
64 using const_iterator = typename buffer_type::const_iterator;
66 using size_type = typename buffer_type::size_type;
67
72 message_buffer(const Allocator &alloc = Allocator()) noexcept(
73 noexcept(Allocator()));
74
80 template <ScatterGatherLike... Bufs>
81 constexpr message_buffer(const Bufs &...bufs) noexcept;
82
89 template <ScatterGatherLike Buf>
90 constexpr auto push_back(const Buf &buf) -> void;
91
96 constexpr auto push_back(native_buffer_type buf) -> void;
97
105 template <typename... Args>
106 constexpr auto emplace_back(Args &&...args) -> decltype(auto);
107
109 [[nodiscard]] constexpr auto begin() noexcept -> iterator;
110
113 [[nodiscard]] constexpr auto begin() const noexcept -> const_iterator;
114
116 [[nodiscard]] constexpr auto end() noexcept -> iterator;
117
119 [[nodiscard]] constexpr auto end() const noexcept -> const_iterator;
120
122 [[nodiscard]] constexpr auto size() const noexcept -> size_type;
123
125 [[nodiscard]] constexpr auto empty() const noexcept -> bool;
126
131 [[nodiscard]] explicit constexpr operator bool() const noexcept;
132
142 auto operator+=(std::size_t len) noexcept -> message_buffer &;
143
144private:
145 buffer_type buffer_;
146};
147
154template <SocketAddress Addr = sockaddr_storage_type,
155 AllocatorLike Allocator = std::allocator<char>>
158 using message_allocator = std::allocator_traits<
159 Allocator>::template rebind_alloc<native_buffer_type>;
162 std::allocator_traits<Allocator>::template rebind_alloc<std::byte>;
164 using address_type = std::optional<socket_address<Addr>>;
168 using control_type = std::vector<std::byte, control_allocator>;
169
172
175
178
180 int flags{};
181
183 [[nodiscard]] explicit operator message_header() noexcept;
185 [[nodiscard]] explicit operator socket_message_type() noexcept;
186};
187
188} // namespace io::socket
189
190#include "impl/socket_message_impl.hpp" // IWYU pragma: export
191
192#endif // IO_SOCKET_MESSAGE_HPP
A container for managing buffers for scatter-gather I/O operations.
Definition socket_message.hpp:57
typename buffer_type::const_iterator const_iterator
Constant iterator for the buffer.
Definition socket_message.hpp:64
std::vector< native_buffer_type, Allocator > buffer_type
The underlying buffer type.
Definition socket_message.hpp:60
message_buffer(const Allocator &alloc=Allocator()) noexcept(noexcept(Allocator()))
Construct message buffer with a custom allocator.
typename buffer_type::iterator iterator
Iterator for the buffer.
Definition socket_message.hpp:62
typename buffer_type::size_type size_type
Size type for the buffer.
Definition socket_message.hpp:66
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:59
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:36
std::span< std::byte > msg_name
Optional address of the sender/receiver.
Definition socket_message.hpp:38
std::span< native_buffer_type > msg_iov
I/O vectors for scatter/gather operations.
Definition socket_message.hpp:40
std::span< std::byte > msg_control
Ancillary data (control information).
Definition socket_message.hpp:42
int flags
Flags on the received message.
Definition socket_message.hpp:44
Represents a complete socket message.
Definition socket_message.hpp:156
std::vector< std::byte, control_allocator > control_type
The control buffer type.
Definition socket_message.hpp:168
std::optional< socket_address< Addr > > address_type
The socket address type.
Definition socket_message.hpp:164
address_type address
Optional address of the sender/receiver.
Definition socket_message.hpp:171
std::allocator_traits< Allocator >::template rebind_alloc< std::byte > control_allocator
The allocator type for control data.
Definition socket_message.hpp:162
message_type buffers
Buffers for scatter/gather I/O.
Definition socket_message.hpp:174
control_type control
Ancillary data (control information).
Definition socket_message.hpp:177
std::allocator_traits< Allocator >::template rebind_alloc< native_buffer_type > message_allocator
The allocator type for message buffers.
Definition socket_message.hpp:159