|
| 1 | +#include "fly/net/socket/detail/base_socket.hpp" |
| 2 | + |
| 3 | +#include "fly/net/endpoint.hpp" |
| 4 | +#include "fly/net/ipv4_address.hpp" |
| 5 | +#include "fly/net/ipv6_address.hpp" |
| 6 | +#include "fly/net/socket/detail/socket_operations.hpp" |
| 7 | + |
| 8 | +#include <atomic> |
| 9 | + |
| 10 | +namespace fly::net::detail { |
| 11 | + |
| 12 | +namespace { |
| 13 | + |
| 14 | + std::atomic_uint64_t s_num_sockets {0}; |
| 15 | + |
| 16 | +} // namespace |
| 17 | + |
| 18 | +//================================================================================================== |
| 19 | +template <typename EndpointType> |
| 20 | +BaseSocket<EndpointType>::BaseSocket(socket_type handle, fly::net::IOMode mode) noexcept : |
| 21 | + m_socket_handle(handle), |
| 22 | + m_socket_id(s_num_sockets.fetch_add(1)), |
| 23 | + m_mode(mode) |
| 24 | +{ |
| 25 | + set_io_mode(mode); |
| 26 | +} |
| 27 | + |
| 28 | +//================================================================================================== |
| 29 | +template <typename EndpointType> |
| 30 | +BaseSocket<EndpointType>::BaseSocket(BaseSocket &&socket) noexcept : |
| 31 | + m_packet_size(socket.m_packet_size), |
| 32 | + m_socket_handle(socket.m_socket_handle), |
| 33 | + m_socket_id(socket.m_socket_id), |
| 34 | + m_mode(socket.m_mode) |
| 35 | +{ |
| 36 | + socket.m_socket_handle = fly::net::detail::invalid_socket(); |
| 37 | +} |
| 38 | + |
| 39 | +//================================================================================================== |
| 40 | +template <typename EndpointType> |
| 41 | +BaseSocket<EndpointType>::~BaseSocket() noexcept |
| 42 | +{ |
| 43 | + close(); |
| 44 | +} |
| 45 | + |
| 46 | +//================================================================================================== |
| 47 | +template <typename EndpointType> |
| 48 | +BaseSocket<EndpointType> &BaseSocket<EndpointType>::operator=(BaseSocket &&socket) noexcept |
| 49 | +{ |
| 50 | + m_packet_size = socket.m_packet_size; |
| 51 | + m_socket_handle = socket.m_socket_handle; |
| 52 | + m_socket_id = socket.m_socket_id; |
| 53 | + m_mode = socket.m_mode; |
| 54 | + |
| 55 | + socket.m_socket_handle = fly::net::detail::invalid_socket(); |
| 56 | + |
| 57 | + return *this; |
| 58 | +} |
| 59 | + |
| 60 | +//================================================================================================== |
| 61 | +template <typename EndpointType> |
| 62 | +std::optional<EndpointType> |
| 63 | +BaseSocket<EndpointType>::hostname_to_endpoint(std::string_view hostname) |
| 64 | +{ |
| 65 | + return fly::net::detail::hostname_to_endpoint<EndpointType>(std::move(hostname)); |
| 66 | +} |
| 67 | + |
| 68 | +//================================================================================================== |
| 69 | +template <typename EndpointType> |
| 70 | +socket_type BaseSocket<EndpointType>::handle() const |
| 71 | +{ |
| 72 | + return m_socket_handle; |
| 73 | +} |
| 74 | + |
| 75 | +//================================================================================================== |
| 76 | +template <typename EndpointType> |
| 77 | +std::uint64_t BaseSocket<EndpointType>::socket_id() const |
| 78 | +{ |
| 79 | + return m_socket_id; |
| 80 | +} |
| 81 | + |
| 82 | +//================================================================================================== |
| 83 | +template <typename EndpointType> |
| 84 | +bool BaseSocket<EndpointType>::set_io_mode(fly::net::IOMode mode) |
| 85 | +{ |
| 86 | + if (fly::net::detail::set_io_mode(m_socket_handle, mode)) |
| 87 | + { |
| 88 | + m_mode = mode; |
| 89 | + } |
| 90 | + else |
| 91 | + { |
| 92 | + close(); |
| 93 | + } |
| 94 | + |
| 95 | + return is_valid(); |
| 96 | +} |
| 97 | + |
| 98 | +//================================================================================================== |
| 99 | +template <typename EndpointType> |
| 100 | +fly::net::IOMode BaseSocket<EndpointType>::io_mode() const |
| 101 | +{ |
| 102 | + return m_mode; |
| 103 | +} |
| 104 | + |
| 105 | +//================================================================================================== |
| 106 | +template <typename EndpointType> |
| 107 | +bool BaseSocket<EndpointType>::is_valid() const |
| 108 | +{ |
| 109 | + return m_socket_handle != fly::net::detail::invalid_socket(); |
| 110 | +} |
| 111 | + |
| 112 | +//================================================================================================== |
| 113 | +template <typename EndpointType> |
| 114 | +std::optional<EndpointType> BaseSocket<EndpointType>::local_endpoint() const |
| 115 | +{ |
| 116 | + return fly::net::detail::local_endpoint<EndpointType>(m_socket_handle); |
| 117 | +} |
| 118 | + |
| 119 | +//================================================================================================== |
| 120 | +template <typename EndpointType> |
| 121 | +void BaseSocket<EndpointType>::close() |
| 122 | +{ |
| 123 | + if (is_valid()) |
| 124 | + { |
| 125 | + fly::net::detail::close(m_socket_handle); |
| 126 | + m_socket_handle = fly::net::detail::invalid_socket(); |
| 127 | + } |
| 128 | +} |
| 129 | + |
| 130 | +//================================================================================================== |
| 131 | +template <typename EndpointType> |
| 132 | +bool BaseSocket<EndpointType>::bind(const EndpointType &endpoint, BindMode option) const |
| 133 | +{ |
| 134 | + return fly::net::detail::bind(m_socket_handle, endpoint, option); |
| 135 | +} |
| 136 | + |
| 137 | +//================================================================================================== |
| 138 | +template <typename EndpointType> |
| 139 | +bool BaseSocket<EndpointType>::bind(std::string_view hostname, port_type port, BindMode option) |
| 140 | + const |
| 141 | +{ |
| 142 | + if (auto endpoint = hostname_to_endpoint(std::move(hostname)); endpoint) |
| 143 | + { |
| 144 | + endpoint->set_port(port); |
| 145 | + return bind(*endpoint, option); |
| 146 | + } |
| 147 | + |
| 148 | + return false; |
| 149 | +} |
| 150 | + |
| 151 | +//================================================================================================== |
| 152 | +template class BaseSocket<fly::net::Endpoint<fly::net::IPv4Address>>; |
| 153 | +template class BaseSocket<fly::net::Endpoint<fly::net::IPv6Address>>; |
| 154 | + |
| 155 | +} // namespace fly::net::detail |
0 commit comments