io
C++ I/O scheduling library with asynchronous socket operations
Loading...
Searching...
No Matches
socket_address.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_address.hpp"
17
18#include <cassert>
19#include <cstring>
20
21namespace io::socket {
22
23socket_address::socket_address(socklen_type size) noexcept : size_{size} {
24 assert(size_ <= sizeof(sockaddr_storage_type) && size_ >= 0 &&
25 "size must be between 0 and sizeof(sockaddr_storage_type)");
26}
28 socklen_type size) noexcept
29 : size_{size} {
30 assert(addr != nullptr && "addr must not be nullptr.");
31 assert(size_ <= sizeof(sockaddr_storage_type) && size_ >= 0 &&
32 "size must be between 0 and sizeof(sockaddr_storage_type)");
33 std::memcpy(&storage_, addr, size_);
34}
35
36auto socket_address::data() noexcept -> sockaddr_type * {
37 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
38 return reinterpret_cast<sockaddr_type *>(&storage_);
39}
40
41auto socket_address::data() const noexcept -> const sockaddr_type * {
42 // NOLINTNEXTLINE(cppcoreguidelines-pro-type-reinterpret-cast)
43 return reinterpret_cast<const sockaddr_type *>(&storage_);
44}
45
46auto socket_address::size() noexcept -> socklen_type * { return &size_; }
47
48auto socket_address::size() const noexcept -> const socklen_type * {
49 return &size_;
50}
51
52auto socket_address::operator==(const socket_address &other) const noexcept
53 -> bool {
54 if (size_ != other.size_)
55 return false;
56 return std::memcmp(&storage_, &other.storage_, size_) == 0;
57}
58} // namespace io::socket
A platform-independent representation of a socket address.
auto data() noexcept -> sockaddr_type *
Returns a mutable pointer to the underlying socket address data.
auto size() noexcept -> socklen_type *
Returns a mutable pointer to the size of the socket address.
socket_address()=default
Constructs an empty socket address.
auto operator==(const socket_address &other) const noexcept -> bool
Compares two socket_address objects for equality.
The io::socket namespace provides a cross-platform abstraction for socket-level I/O operations.
Definition socket.hpp:31
sockaddr sockaddr_type
The generic socket address structure for POSIX systems.
Definition socket.hpp:137
socklen_t socklen_type
The type used to represent socket-related sizes on POSIX systems.
Definition socket.hpp:155
sockaddr_storage sockaddr_storage_type
The socket address storage structure for POSIX systems.
Definition socket.hpp:146
This file defines the socket_address class, a platform-independent abstraction for socket addresses.