Fixed WSAInit not being called by fr::Listener
This would mean that if a listener was created before a socket, then it would fail to bind.
This commit is contained in:
parent
f1db713069
commit
987becd8d0
@ -14,7 +14,7 @@ namespace fr
|
|||||||
Listener()
|
Listener()
|
||||||
: ai_family(AF_UNSPEC)
|
: ai_family(AF_UNSPEC)
|
||||||
{
|
{
|
||||||
|
init_wsa();
|
||||||
}
|
}
|
||||||
|
|
||||||
virtual ~Listener() = default;
|
virtual ~Listener() = default;
|
||||||
@ -102,6 +102,7 @@ namespace fr
|
|||||||
}
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
|
|
||||||
int ai_family;
|
int ai_family;
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|||||||
@ -7,6 +7,10 @@
|
|||||||
|
|
||||||
#include <cstring>
|
#include <cstring>
|
||||||
#include <cstdint>
|
#include <cstdint>
|
||||||
|
#include <atomic>
|
||||||
|
#include <exception>
|
||||||
|
#include <stdexcept>
|
||||||
|
#include <csignal>
|
||||||
|
|
||||||
//Windows and UNIX require some different headers.
|
//Windows and UNIX require some different headers.
|
||||||
//We also need some compatibility defines for cross platform support.
|
//We also need some compatibility defines for cross platform support.
|
||||||
@ -90,5 +94,23 @@ inline void set_unix_socket_blocking(int32_t socket_descriptor, bool is_blocking
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void init_wsa()
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
static WSADATA wsaData = WSAData();
|
||||||
|
static std::atomic<uint32_t> instance_count{0};
|
||||||
|
if(instance_count++ == 0)
|
||||||
|
{
|
||||||
|
int wsa_result = WSAStartup(MAKEWORD(2, 2), &wsaData);
|
||||||
|
if(wsa_result != 0)
|
||||||
|
{
|
||||||
|
throw std::runtime_error("Failed to initialise WSA: " + std::to_string(wsa_result));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
#else
|
||||||
|
signal(SIGPIPE, SIG_IGN);
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif //FRNETLIB_NETWORKENCODING_H
|
#endif //FRNETLIB_NETWORKENCODING_H
|
||||||
|
|||||||
@ -229,10 +229,6 @@ namespace fr
|
|||||||
std::mutex inbound_mutex;
|
std::mutex inbound_mutex;
|
||||||
int ai_family;
|
int ai_family;
|
||||||
uint32_t max_receive_size;
|
uint32_t max_receive_size;
|
||||||
#ifdef _WIN32
|
|
||||||
static WSADATA wsaData;
|
|
||||||
#endif // _WIN32
|
|
||||||
static uint32_t instance_count;
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -10,31 +10,12 @@
|
|||||||
|
|
||||||
namespace fr
|
namespace fr
|
||||||
{
|
{
|
||||||
#ifdef _WIN32
|
|
||||||
WSADATA Socket::wsaData = WSADATA();
|
|
||||||
#endif // _WIN32
|
|
||||||
uint32_t Socket::instance_count = 0;
|
|
||||||
|
|
||||||
Socket::Socket() noexcept
|
Socket::Socket() noexcept
|
||||||
: is_blocking(true),
|
: is_blocking(true),
|
||||||
ai_family(AF_UNSPEC),
|
ai_family(AF_UNSPEC),
|
||||||
max_receive_size(0)
|
max_receive_size(0)
|
||||||
{
|
{
|
||||||
if(instance_count == 0)
|
init_wsa();
|
||||||
{
|
|
||||||
#ifdef _WIN32
|
|
||||||
int wsa_result = WSAStartup(MAKEWORD(2, 2), &wsaData);
|
|
||||||
if(wsa_result != 0)
|
|
||||||
{
|
|
||||||
std::cout << "Failed to initialise WSA." << std::endl;
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
#else
|
|
||||||
//Disable SIGPIPE
|
|
||||||
signal(SIGPIPE, SIG_IGN);
|
|
||||||
#endif // _WIN32
|
|
||||||
}
|
|
||||||
instance_count++;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Socket::Status Socket::send(Sendable &obj)
|
Socket::Status Socket::send(Sendable &obj)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user