core/socket_proxy: Final nits

This commit is contained in:
FearlessTobi 2022-08-15 23:31:01 +02:00
parent 72b90a5bbf
commit 4976d14009

View file

@ -32,6 +32,7 @@ void ProxySocket::HandleProxyPacket(const ProxyPacket& packet) {
template <typename T> template <typename T>
Errno ProxySocket::SetSockOpt(SOCKET fd_, int option, T value) { Errno ProxySocket::SetSockOpt(SOCKET fd_, int option, T value) {
LOG_DEBUG(Network, "(STUBBED) called");
return Errno::SUCCESS; return Errno::SUCCESS;
} }
@ -95,8 +96,12 @@ std::pair<s32, Errno> ProxySocket::RecvFrom(int flags, std::vector<u8>& message,
ASSERT(flags == 0); ASSERT(flags == 0);
ASSERT(message.size() < static_cast<size_t>(std::numeric_limits<int>::max())); ASSERT(message.size() < static_cast<size_t>(std::numeric_limits<int>::max()));
// TODO (flTobi): Verify the timeout behavior and break when connection is lost
const auto timestamp = std::chrono::steady_clock::now(); const auto timestamp = std::chrono::steady_clock::now();
// When receive_timeout is set to zero, the socket is supposed to wait indefinitely until a
// packet arrives. In order to prevent lost packets from hanging the emulation thread, we set
// the timeout to 5s instead
const auto timeout = receive_timeout == 0 ? 5000 : receive_timeout;
while (true) { while (true) {
{ {
std::lock_guard guard(packets_mutex); std::lock_guard guard(packets_mutex);
@ -109,19 +114,13 @@ std::pair<s32, Errno> ProxySocket::RecvFrom(int flags, std::vector<u8>& message,
return {-1, Errno::AGAIN}; return {-1, Errno::AGAIN};
} }
// TODO: break if socket connection is lost
std::this_thread::yield(); std::this_thread::yield();
if (receive_timeout == 0) {
continue;
}
const auto time_diff = std::chrono::steady_clock::now() - timestamp; const auto time_diff = std::chrono::steady_clock::now() - timestamp;
const auto time_diff_ms = const auto time_diff_ms =
std::chrono::duration_cast<std::chrono::milliseconds>(time_diff).count(); std::chrono::duration_cast<std::chrono::milliseconds>(time_diff).count();
if (time_diff_ms > receive_timeout) { if (time_diff_ms > timeout) {
return {-1, Errno::TIMEDOUT}; return {-1, Errno::TIMEDOUT};
} }
} }