Skip to content

Commit

Permalink
Some minor cleanups for UDPSocket class
Browse files Browse the repository at this point in the history
  • Loading branch information
sfan5 committed Jan 7, 2024
1 parent dc7fb26 commit 20692d5
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 17 deletions.
27 changes: 18 additions & 9 deletions src/network/socket.cpp
Expand Up @@ -73,6 +73,7 @@ void sockets_cleanup()
// On Windows, cleanup sockets after use
WSACleanup();
#endif
g_sockets_initialized = false;
}

/*
Expand All @@ -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);
Expand All @@ -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;
}
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
17 changes: 9 additions & 8 deletions src/network/socket.h
Expand Up @@ -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;
};

0 comments on commit 20692d5

Please sign in to comment.