From e80a011a863fb7d28e89722fb28ef123b226bc16 Mon Sep 17 00:00:00 2001 From: water Date: Tue, 3 May 2022 18:57:59 -0400 Subject: [PATCH] a few small changes to fix tests --- common/cross_sockets/XSocketServer.cpp | 6 +++++- common/cross_sockets/XSocketServer.h | 1 - game/system/Deci2Server.cpp | 10 +++++----- game/system/Deci2Server.h | 2 ++ goalc/compiler/nrepl/ReplServer.cpp | 7 +++++-- 5 files changed, 17 insertions(+), 9 deletions(-) diff --git a/common/cross_sockets/XSocketServer.cpp b/common/cross_sockets/XSocketServer.cpp index 197ea646d..c11fc2478 100644 --- a/common/cross_sockets/XSocketServer.cpp +++ b/common/cross_sockets/XSocketServer.cpp @@ -3,6 +3,7 @@ #include "third-party/fmt/core.h" #include "common/cross_sockets/XSocket.h" +#include "common/util/Assert.h" #ifdef _WIN32 #define NOMINMAX @@ -21,7 +22,9 @@ XSocketServer::XSocketServer(std::function shutdown_callback, } XSocketServer::~XSocketServer() { - shutdown_server(); + if (listening_socket >= 0) { + close_server_socket(); + } } void XSocketServer::shutdown_server() { @@ -81,6 +84,7 @@ bool XSocketServer::init_server() { } void XSocketServer::close_server_socket() { + ASSERT(listening_socket >= 0); close_socket(listening_socket); listening_socket = -1; } diff --git a/common/cross_sockets/XSocketServer.h b/common/cross_sockets/XSocketServer.h index c9550ff39..c7db82206 100644 --- a/common/cross_sockets/XSocketServer.h +++ b/common/cross_sockets/XSocketServer.h @@ -33,7 +33,6 @@ class XSocketServer { std::vector buffer; bool server_initialized = false; - bool client_connected = false; std::function want_exit_callback; }; diff --git a/game/system/Deci2Server.cpp b/game/system/Deci2Server.cpp index 02e71e8ec..8af913244 100644 --- a/game/system/Deci2Server.cpp +++ b/game/system/Deci2Server.cpp @@ -29,6 +29,7 @@ Deci2Server::~Deci2Server() { accept_thread.join(); accept_thread_running = false; } + close_socket(accepted_socket); } @@ -42,15 +43,14 @@ void Deci2Server::post_init() { void Deci2Server::accept_thread_func() { socklen_t addr_len = sizeof(addr); while (!kill_accept_thread) { - if (accepted_socket == -1) { - accepted_socket = accept_socket(listening_socket, (sockaddr*)&addr, &addr_len); + accepted_socket = accept_socket(listening_socket, (sockaddr*)&addr, &addr_len); + if (accepted_socket >= 0) { set_socket_timeout(accepted_socket, 100000); u32 versions[2] = {versions::GOAL_VERSION_MAJOR, versions::GOAL_VERSION_MINOR}; - write_to_socket(accepted_socket, (char*)&versions, 8); + write_to_socket(accepted_socket, (char*)&versions, 8); // todo, check result? client_connected = true; - return; // stop accepting connections + return; } - std::this_thread::sleep_for(std::chrono::microseconds(50000)); } } diff --git a/game/system/Deci2Server.h b/game/system/Deci2Server.h index 52c73295a..0a4650965 100644 --- a/game/system/Deci2Server.h +++ b/game/system/Deci2Server.h @@ -37,5 +37,7 @@ class Deci2Server : public XSocketServer { std::thread accept_thread; std::mutex server_mutex; + bool client_connected = false; + void accept_thread_func(); }; diff --git a/goalc/compiler/nrepl/ReplServer.cpp b/goalc/compiler/nrepl/ReplServer.cpp index ed12dc94a..72b713688 100644 --- a/goalc/compiler/nrepl/ReplServer.cpp +++ b/goalc/compiler/nrepl/ReplServer.cpp @@ -54,8 +54,11 @@ std::optional ReplServer::get_msg() { } } - // Wait for activity on _something_ - auto activity = select(max_sd + 1, &read_sockets, NULL, NULL, NULL); + // Wait for activity on _something_, with a timeout so we don't get stuck here on exit. + struct timeval timeout; + timeout.tv_sec = 0; + timeout.tv_usec = 100000; + auto activity = select(max_sd + 1, &read_sockets, NULL, NULL, &timeout); if (activity < 0) { // TODO - || error! return std::nullopt;