mirror of
https://github.com/minetest/minetest.git
synced 2024-11-23 16:13:46 +01: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
|
// On Windows, cleanup sockets after use
|
||||||
WSACleanup();
|
WSACleanup();
|
||||||
#endif
|
#endif
|
||||||
|
g_sockets_initialized = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -87,10 +88,18 @@ UDPSocket::UDPSocket(bool ipv6)
|
|||||||
bool UDPSocket::init(bool ipv6, bool noExceptions)
|
bool UDPSocket::init(bool ipv6, bool noExceptions)
|
||||||
{
|
{
|
||||||
if (!g_sockets_initialized) {
|
if (!g_sockets_initialized) {
|
||||||
tracestream << "Sockets not initialized" << std::endl;
|
verbosestream << "Sockets not initialized" << std::endl;
|
||||||
return false;
|
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
|
// Use IPv6 if specified
|
||||||
m_addr_family = ipv6 ? AF_INET6 : AF_INET;
|
m_addr_family = ipv6 ? AF_INET6 : AF_INET;
|
||||||
m_handle = socket(m_addr_family, SOCK_DGRAM, IPPROTO_UDP);
|
m_handle = socket(m_addr_family, SOCK_DGRAM, IPPROTO_UDP);
|
||||||
@ -101,7 +110,7 @@ bool UDPSocket::init(bool ipv6, bool noExceptions)
|
|||||||
<< std::endl;
|
<< std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (m_handle <= 0) {
|
if (m_handle < 0) {
|
||||||
if (noExceptions) {
|
if (noExceptions) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
@ -131,11 +140,13 @@ UDPSocket::~UDPSocket()
|
|||||||
<< std::endl;
|
<< std::endl;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (m_handle >= 0) {
|
||||||
#ifdef _WIN32
|
#ifdef _WIN32
|
||||||
closesocket(m_handle);
|
closesocket(m_handle);
|
||||||
#else
|
#else
|
||||||
close(m_handle);
|
close(m_handle);
|
||||||
#endif
|
#endif
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void UDPSocket::Bind(Address addr)
|
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)
|
int UDPSocket::Receive(Address &sender, void *data, int size)
|
||||||
{
|
{
|
||||||
// Return on timeout
|
// Return on timeout
|
||||||
|
assert(m_timeout_ms >= 0);
|
||||||
if (!WaitData(m_timeout_ms))
|
if (!WaitData(m_timeout_ms))
|
||||||
return -1;
|
return -1;
|
||||||
|
|
||||||
|
size = MYMAX(size, 0);
|
||||||
|
|
||||||
int received;
|
int received;
|
||||||
if (m_addr_family == AF_INET6) {
|
if (m_addr_family == AF_INET6) {
|
||||||
struct sockaddr_in6 address;
|
struct sockaddr_in6 address;
|
||||||
@ -311,11 +325,6 @@ int UDPSocket::Receive(Address &sender, void *data, int size)
|
|||||||
return received;
|
return received;
|
||||||
}
|
}
|
||||||
|
|
||||||
int UDPSocket::GetHandle()
|
|
||||||
{
|
|
||||||
return m_handle;
|
|
||||||
}
|
|
||||||
|
|
||||||
void UDPSocket::setTimeoutMs(int timeout_ms)
|
void UDPSocket::setTimeoutMs(int timeout_ms)
|
||||||
{
|
{
|
||||||
m_timeout_ms = timeout_ms;
|
m_timeout_ms = timeout_ms;
|
||||||
|
@ -34,23 +34,24 @@ class UDPSocket
|
|||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
UDPSocket() = default;
|
UDPSocket() = default;
|
||||||
|
UDPSocket(bool ipv6); // calls init()
|
||||||
UDPSocket(bool ipv6);
|
|
||||||
~UDPSocket();
|
~UDPSocket();
|
||||||
void Bind(Address addr);
|
|
||||||
|
|
||||||
bool init(bool ipv6, bool noExceptions = false);
|
bool init(bool ipv6, bool noExceptions = false);
|
||||||
|
|
||||||
|
void Bind(Address addr);
|
||||||
|
|
||||||
void Send(const Address &destination, const void *data, int size);
|
void Send(const Address &destination, const void *data, int size);
|
||||||
// Returns -1 if there is no data
|
// Returns -1 if there is no data
|
||||||
int Receive(Address &sender, void *data, int size);
|
int Receive(Address &sender, void *data, int size);
|
||||||
int GetHandle(); // For debugging purposes only
|
|
||||||
void setTimeoutMs(int timeout_ms);
|
void setTimeoutMs(int timeout_ms);
|
||||||
// Returns true if there is data, false if timeout occurred
|
// Returns true if there is data, false if timeout occurred
|
||||||
bool WaitData(int timeout_ms);
|
bool WaitData(int timeout_ms);
|
||||||
|
|
||||||
|
// Debugging purposes only
|
||||||
|
int GetHandle() const { return m_handle; };
|
||||||
|
|
||||||
private:
|
private:
|
||||||
int m_handle;
|
int m_handle = -1;
|
||||||
int m_timeout_ms;
|
int m_timeout_ms = -1;
|
||||||
int m_addr_family;
|
unsigned short m_addr_family = 0;
|
||||||
};
|
};
|
||||||
|
Loading…
Reference in New Issue
Block a user