io
C++ I/O scheduling library with asynchronous socket operations
Loading...
Searching...
No Matches
socket_message.cpp
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
16#include "socket_message.hpp"
17
18namespace io::socket {
19
20socket_message::socket_message() : data_{std::make_unique<message_data>()} {}
21
23 : socket_message() {
24 swap(*this, other);
25}
26
28 -> socket_message & {
29 swap(*this, other);
30 return *this;
31}
32
33auto swap(socket_message &lhs, socket_message &rhs) noexcept -> void {
34 if (&lhs == &rhs)
35 return;
36
37 std::scoped_lock lock(lhs.mtx_, rhs.mtx_);
38
39 using std::swap;
40 swap(lhs.data_, rhs.data_);
41}
42
44 std::lock_guard lock{mtx_};
45
46 return *data_;
47}
48
50 std::lock_guard lock{mtx_};
51 data_ = std::make_unique<message_data>(std::move(data));
52 return *this;
53}
54
56 std::lock_guard lock{mtx_};
57 return data_->address;
58}
59
61 std::lock_guard lock{mtx_};
62 data_->address = address;
63 return *this;
64}
65
68 std::lock_guard lock{mtx_};
69 using std::swap;
70 swap(data_->address, address);
71 return address;
72}
73
75 std::lock_guard lock{mtx_};
76 return data_->buffers;
77}
78
80 -> socket_message & {
81 std::lock_guard lock{mtx_};
82 data_->buffers = std::move(buffers);
83 return *this;
84}
85
88 std::lock_guard lock{mtx_};
89 using std::swap;
90 swap(data_->buffers, buffers);
91 return buffers;
92}
93
95 std::lock_guard lock{mtx_};
96 data_->buffers.push_back(buffer);
97 return *this;
98}
99
101 std::lock_guard lock{mtx_};
102 return data_->control;
103}
104
106 std::lock_guard lock{mtx_};
107 data_->control = std::move(control);
108 return *this;
109}
110
113 std::lock_guard lock{mtx_};
114 using std::swap;
115 swap(data_->control, control);
116 return control;
117}
118
119auto socket_message::flags() const -> int {
120 std::lock_guard lock{mtx_};
121 return data_->flags;
122}
123
125 std::lock_guard lock{mtx_};
126 data_->flags = flags;
127 return *this;
128}
129
130auto socket_message::exchange_flags(int flags) -> int {
131 std::lock_guard lock{mtx_};
132 using std::swap;
133 swap(data_->flags, flags);
134 return flags;
135}
136
137} // namespace io::socket
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 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.
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.
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.
auto swap(socket_handle &lhs, socket_handle &rhs) noexcept -> void
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 defines the socket_message class, a thread-safe container for advanced socket I/O operation...
A data structure that contains all the components of a socket message.