mirror of
https://github.com/momo5502/emulator.git
synced 2026-01-25 14:41:02 +00:00
Handle connect & Fix polling
This commit is contained in:
@@ -77,23 +77,23 @@ 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)
|
||||
{
|
||||
int result = ::listen(this->socket_, backlog);
|
||||
if (result == 0)
|
||||
{
|
||||
listening_ = true;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
return ::listen(this->socket_, backlog) == 0;
|
||||
}
|
||||
|
||||
// NOLINTNEXTLINE(readability-make-member-function-const)
|
||||
SOCKET socket::accept(address& address)
|
||||
{
|
||||
sockaddr addr{};
|
||||
int addrlen = sizeof(sockaddr);
|
||||
socklen_t addrlen = sizeof(sockaddr);
|
||||
const auto s = ::accept(this->socket_, &addr, &addrlen);
|
||||
|
||||
if (s != INVALID_SOCKET)
|
||||
@@ -187,7 +187,7 @@ namespace network
|
||||
|
||||
bool socket::is_listening() const
|
||||
{
|
||||
return this->is_valid() && listening_;
|
||||
return this->is_valid() && is_socket_listening(this->socket_);
|
||||
}
|
||||
|
||||
bool socket::sleep_sockets(const std::span<const socket*>& sockets, const std::chrono::milliseconds timeout,
|
||||
@@ -246,6 +246,14 @@ 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)
|
||||
|
||||
@@ -47,6 +47,7 @@ namespace network
|
||||
bool is_valid() const;
|
||||
|
||||
bool bind(const address& target);
|
||||
bool connect(const address& target);
|
||||
bool listen(int backlog);
|
||||
SOCKET accept(address& address);
|
||||
|
||||
@@ -72,11 +73,11 @@ 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();
|
||||
|
||||
private:
|
||||
SOCKET socket_ = INVALID_SOCKET;
|
||||
bool listening_{};
|
||||
};
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user