io
C++ I/O scheduling library with asynchronous socket operations
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
21#pragma once
22#ifndef IO_SOCKET_MESSAGE_HPP
23#define IO_SOCKET_MESSAGE_HPP
24#include <boost/predef.h>
25
26#if BOOST_OS_WINDOWS
28#else
30#endif
31
32#include "socket_address.hpp"
33
34#include <memory>
35#include <mutex>
36#include <vector>
37
38namespace io::socket {
39
50using ancillary_buffer = std::vector<char>;
51
63using scatter_gather_buffer = std::vector<socket_buffer_type>;
64
79
111
112public:
119
126 socket_message(const socket_message &other) = delete;
127
134 auto operator=(const socket_message &other) -> socket_message & = delete;
135
144 socket_message(socket_message &&other) noexcept;
145
155 auto operator=(socket_message &&other) noexcept -> socket_message &;
156
169 friend auto swap(socket_message &lhs, socket_message &rhs) noexcept -> void;
170
174 [[nodiscard]] auto get() const -> message_data;
175
179 auto operator=(message_data data) -> socket_message &;
180
186 [[nodiscard]] auto address() const -> socket_address;
187
195
203
212 [[nodiscard]] auto buffers() const -> scatter_gather_buffer;
213
223
234
248
264 template <typename... Args>
265 auto emplace_back(Args &&...args) -> socket_message & {
266 std::lock_guard<std::mutex> lock{mtx_};
267 data_->buffers.emplace_back(std::forward<Args>(args)...);
268 return *this;
269 }
270
280 [[nodiscard]] auto control() const -> ancillary_buffer;
281
291
302
311 [[nodiscard]] auto flags() const -> int;
312
323 auto set_flags(int flags) -> socket_message &;
324
334 auto exchange_flags(int flags) -> int;
335
342 ~socket_message() = default;
343
344private:
346 std::unique_ptr<message_data> data_;
348 mutable std::mutex mtx_;
349};
350
351// TODO implement customization points for sendmsg and recvmsg that are passed
352// in socket_message types.
353
354} // namespace io::socket
355
356#endif // IO_SOCKET_MESSAGE_HPP
A platform-independent representation of a socket address.
A thread-safe container for socket messages used in advanced I/O operations.
socket_message()
Default constructor.
auto exchange_buffers(scatter_gather_buffer buffers) -> scatter_gather_buffer
Exchanges the data buffers.
auto emplace_back(Args &&...args) -> socket_message &
Constructs a new buffer in-place at the end of the buffer collection.
auto push_back(socket_buffer_type buffer) -> socket_message &
Adds a new data buffer to the end of the buffer collection.
auto get() const -> message_data
Gets the underlying message_data struct.
auto buffers() const -> scatter_gather_buffer
Gets the data buffers.
auto flags() const -> int
Gets the message flags.
auto set_flags(int flags) -> socket_message &
Sets the message flags.
auto set_address(socket_address address) -> socket_message &
Sets the socket address.
auto exchange_control(ancillary_buffer control) -> ancillary_buffer
Exchanges the ancillary data.
auto set_control(ancillary_buffer control) -> socket_message &
Sets the ancillary data.
friend auto swap(socket_message &lhs, socket_message &rhs) noexcept -> void
Swaps the contents of two socket_message instances.
auto set_buffers(scatter_gather_buffer buffers) -> socket_message &
Sets the data buffers.
auto exchange_address(socket_address address) -> socket_address
Exchanges the socket address.
auto operator=(const socket_message &other) -> socket_message &=delete
Deleted copy assignment operator.
auto address() const -> socket_address
Gets the socket address.
auto control() const -> ancillary_buffer
Gets the ancillary data.
socket_message(const socket_message &other)=delete
Deleted copy constructor.
auto exchange_flags(int flags) -> int
Exchanges the message flags.
The io::socket namespace provides a cross-platform abstraction for socket-level I/O operations.
Definition socket.hpp:31
std::vector< socket_buffer_type > scatter_gather_buffer
Type alias for scatter-gather I/O buffer collection.
std::span< char, std::dynamic_extent > socket_buffer_type
The socket buffer type for POSIX systems.
Definition socket.hpp:56
std::vector< char > ancillary_buffer
Type alias for ancillary data buffer used in socket messages.
This file contains the POSIX-specific socket definitions and functions.
This file contains the Windows-specific socket definitions and functions.
This file defines the socket_address class, a platform-independent abstraction for socket addresses.
A data structure that contains all the components of a socket message.
socket_address address
The socket address for the message.
ancillary_buffer control
The ancillary data (control information).
int flags
The message flags for socket operations.
scatter_gather_buffer buffers
A collection of data buffers for scatter-gather I/O.