From 20692d54dea378278fbceb4c072225f0c3d12abe Mon Sep 17 00:00:00 2001 From: sfan5 Date: Mon, 1 Jan 2024 16:08:53 +0100 Subject: [PATCH] Some minor cleanups for UDPSocket class --- src/network/socket.cpp | 27 ++++++++++++++++++--------- src/network/socket.h | 17 +++++++++-------- 2 files changed, 27 insertions(+), 17 deletions(-) diff --git a/src/network/socket.cpp b/src/network/socket.cpp index 07fcf7062..210e7aef3 100644 --- a/src/network/socket.cpp +++ b/src/network/socket.cpp @@ -73,6 +73,7 @@ void sockets_cleanup() // On Windows, cleanup sockets after use WSACleanup(); #endif + g_sockets_initialized = false; } /* @@ -87,10 +88,18 @@ UDPSocket::UDPSocket(bool ipv6) bool UDPSocket::init(bool ipv6, bool noExceptions) { if (!g_sockets_initialized) { - tracestream << "Sockets not initialized" << std::endl; + verbosestream << "Sockets not initialized" << std::endl; return false; } + if (m_handle >= 0) { + auto msg = "Cannot initialize socket twice"; + verbosestream << msg << std::endl; + if (noExceptions) + return false; + throw SocketException(msg); + } + // Use IPv6 if specified m_addr_family = ipv6 ? AF_INET6 : AF_INET; m_handle = socket(m_addr_family, SOCK_DGRAM, IPPROTO_UDP); @@ -101,7 +110,7 @@ bool UDPSocket::init(bool ipv6, bool noExceptions) << std::endl; } - if (m_handle <= 0) { + if (m_handle < 0) { if (noExceptions) { return false; } @@ -131,11 +140,13 @@ UDPSocket::~UDPSocket() << std::endl; } + if (m_handle >= 0) { #ifdef _WIN32 - closesocket(m_handle); + closesocket(m_handle); #else - close(m_handle); + close(m_handle); #endif + } } void UDPSocket::Bind(Address addr) @@ -251,9 +262,12 @@ void UDPSocket::Send(const Address &destination, const void *data, int size) int UDPSocket::Receive(Address &sender, void *data, int size) { // Return on timeout + assert(m_timeout_ms >= 0); if (!WaitData(m_timeout_ms)) return -1; + size = MYMAX(size, 0); + int received; if (m_addr_family == AF_INET6) { struct sockaddr_in6 address; @@ -311,11 +325,6 @@ int UDPSocket::Receive(Address &sender, void *data, int size) return received; } -int UDPSocket::GetHandle() -{ - return m_handle; -} - void UDPSocket::setTimeoutMs(int timeout_ms) { m_timeout_ms = timeout_ms; diff --git a/src/network/socket.h b/src/network/socket.h index d34186b44..c3758a9d8 100644 --- a/src/network/socket.h +++ b/src/network/socket.h @@ -34,23 +34,24 @@ class UDPSocket { public: UDPSocket() = default; - - UDPSocket(bool ipv6); + UDPSocket(bool ipv6); // calls init() ~UDPSocket(); - void Bind(Address addr); - bool init(bool ipv6, bool noExceptions = false); + void Bind(Address addr); + void Send(const Address &destination, const void *data, int size); // Returns -1 if there is no data int Receive(Address &sender, void *data, int size); - int GetHandle(); // For debugging purposes only void setTimeoutMs(int timeout_ms); // Returns true if there is data, false if timeout occurred bool WaitData(int timeout_ms); + // Debugging purposes only + int GetHandle() const { return m_handle; }; + private: - int m_handle; - int m_timeout_ms; - int m_addr_family; + int m_handle = -1; + int m_timeout_ms = -1; + unsigned short m_addr_family = 0; };