mirror of
https://github.com/luanti-org/luanti.git
synced 2025-06-27 16:36:03 +00:00
Some minor cleanups for UDPSocket class
This commit is contained in:
parent
dc7fb26921
commit
20692d54de
2 changed files with 27 additions and 17 deletions
|
@ -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;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue