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()
|
||||
: ai_family(AF_UNSPEC)
|
||||
{
|
||||
|
||||
init_wsa();
|
||||
}
|
||||
|
||||
virtual ~Listener() = default;
|
||||
@ -102,6 +102,7 @@ namespace fr
|
||||
}
|
||||
|
||||
protected:
|
||||
|
||||
int ai_family;
|
||||
};
|
||||
}
|
||||
|
||||
@ -7,6 +7,10 @@
|
||||
|
||||
#include <cstring>
|
||||
#include <cstdint>
|
||||
#include <atomic>
|
||||
#include <exception>
|
||||
#include <stdexcept>
|
||||
#include <csignal>
|
||||
|
||||
//Windows and UNIX require some different headers.
|
||||
//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
|
||||
}
|
||||
|
||||
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
|
||||
|
||||
@ -229,10 +229,6 @@ namespace fr
|
||||
std::mutex inbound_mutex;
|
||||
int ai_family;
|
||||
uint32_t max_receive_size;
|
||||
#ifdef _WIN32
|
||||
static WSADATA wsaData;
|
||||
#endif // _WIN32
|
||||
static uint32_t instance_count;
|
||||
};
|
||||
}
|
||||
|
||||
|
||||
@ -10,31 +10,12 @@
|
||||
|
||||
namespace fr
|
||||
{
|
||||
#ifdef _WIN32
|
||||
WSADATA Socket::wsaData = WSADATA();
|
||||
#endif // _WIN32
|
||||
uint32_t Socket::instance_count = 0;
|
||||
|
||||
Socket::Socket() noexcept
|
||||
: is_blocking(true),
|
||||
ai_family(AF_UNSPEC),
|
||||
max_receive_size(0)
|
||||
{
|
||||
if(instance_count == 0)
|
||||
{
|
||||
#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++;
|
||||
init_wsa();
|
||||
}
|
||||
|
||||
Socket::Status Socket::send(Sendable &obj)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user