mirror of
https://github.com/minetest/minetest.git
synced 2024-08-19 21:14:15 +02:00
Some minor cleanups for UDPSocket class
This commit is contained in:
parent
dc7fb26921
commit
20692d54de
@ -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;
|
||||
|
@ -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;
|
||||
};
|
||||
|
Loading…
Reference in New Issue
Block a user