Move new code out of network::socket

This commit is contained in:
Igor Pissolati
2025-05-21 11:59:56 -03:00
parent 4b83b20e19
commit d75d70e5ec
4 changed files with 20 additions and 50 deletions

View File

@@ -77,33 +77,6 @@ namespace network
return ::bind(this->socket_, &target.get_addr(), target.get_size()) == 0;
}
// NOLINTNEXTLINE(readability-make-member-function-const)
bool socket::connect(const address& target)
{
return ::connect(this->socket_, &target.get_addr(), target.get_size()) == 0;
}
// NOLINTNEXTLINE(readability-make-member-function-const)
bool socket::listen(int backlog)
{
return ::listen(this->socket_, backlog) == 0;
}
// NOLINTNEXTLINE(readability-make-member-function-const)
SOCKET socket::accept(address& address)
{
sockaddr addr{};
socklen_t addrlen = sizeof(sockaddr);
const auto s = ::accept(this->socket_, &addr, &addrlen);
if (s != INVALID_SOCKET)
{
address.set_address(&addr, addrlen);
}
return s;
}
// NOLINTNEXTLINE(readability-make-member-function-const)
bool socket::set_blocking(const bool blocking)
{
@@ -185,11 +158,6 @@ namespace network
return this->is_valid() && is_socket_ready(this->socket_, in_poll);
}
bool socket::is_listening() const
{
return this->is_valid() && is_socket_listening(this->socket_);
}
bool socket::sleep_sockets(const std::span<const socket*>& sockets, const std::chrono::milliseconds timeout,
const bool in_poll)
{
@@ -246,14 +214,6 @@ namespace network
return !socket_is_ready;
}
bool socket::is_socket_listening(SOCKET s)
{
int val{};
socklen_t len = sizeof(val);
return getsockopt(s, SOL_SOCKET, SO_ACCEPTCONN, reinterpret_cast<char*>(&val), &len) != SOCKET_ERROR &&
val == 1;
}
bool socket::sleep_sockets_until(const std::span<const socket*>& sockets,
const std::chrono::high_resolution_clock::time_point time_point,
const bool in_poll)

View File

@@ -47,9 +47,6 @@ namespace network
bool is_valid() const;
bool bind(const address& target);
bool connect(const address& target);
bool listen(int backlog);
SOCKET accept(address& address);
bool set_blocking(bool blocking);
static bool set_blocking(SOCKET s, bool blocking);
@@ -65,7 +62,6 @@ namespace network
int get_address_family() const;
bool is_ready(bool in_poll) const;
bool is_listening() const;
static bool sleep_sockets(const std::span<const socket*>& sockets, std::chrono::milliseconds timeout,
bool in_poll);
@@ -73,7 +69,6 @@ namespace network
std::chrono::high_resolution_clock::time_point time_point, bool in_poll);
static bool is_socket_ready(SOCKET s, bool in_poll);
static bool is_socket_listening(SOCKET s);
void close();

View File

@@ -254,7 +254,7 @@ namespace
{
int16_t socket_events{};
if (poll_events & (AFD_POLL_ACCEPT | AFD_POLL_RECEIVE))
if (poll_events & (AFD_POLL_DISCONNECT | AFD_POLL_ACCEPT | AFD_POLL_RECEIVE))
{
socket_events |= POLLRDNORM;
}

View File

@@ -30,7 +30,17 @@ namespace network
bool socket_wrapper::is_listening()
{
return this->socket_.is_listening();
if (!this->socket_.is_valid())
{
return false;
}
int val{};
socklen_t len = sizeof(val);
const auto res =
getsockopt(this->socket_.get_socket(), SOL_SOCKET, SO_ACCEPTCONN, reinterpret_cast<char*>(&val), &len);
return res != SOCKET_ERROR && val == 1;
}
bool socket_wrapper::bind(const address& addr)
@@ -40,22 +50,27 @@ namespace network
bool socket_wrapper::connect(const address& addr)
{
return this->socket_.connect(addr);
return ::connect(this->socket_.get_socket(), &addr.get_addr(), addr.get_size()) == 0;
}
bool socket_wrapper::listen(int backlog)
{
return this->socket_.listen(backlog);
return ::listen(this->socket_.get_socket(), backlog) == 0;
}
std::unique_ptr<i_socket> socket_wrapper::accept(address& address)
{
const auto s = this->socket_.accept(address);
sockaddr addr{};
socklen_t addrlen = sizeof(sockaddr);
const auto s = ::accept(this->socket_.get_socket(), &addr, &addrlen);
if (s == INVALID_SOCKET)
{
return nullptr;
}
address.set_address(&addr, addrlen);
return std::make_unique<socket_wrapper>(s);
}