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:
Fred Nicolson 2017-08-23 11:23:00 +01:00
parent f1db713069
commit 987becd8d0
4 changed files with 45 additions and 45 deletions

View File

@ -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;
};
}

View File

@ -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.
@ -40,38 +44,38 @@
inline float htonf(float val)
{
uint32_t ret;
memcpy(&ret, &val, sizeof(ret));
ret = htonl(ret);
memcpy(&val, &ret, sizeof(val));
return val;
uint32_t ret;
memcpy(&ret, &val, sizeof(ret));
ret = htonl(ret);
memcpy(&val, &ret, sizeof(val));
return val;
}
inline float ntohf(float val)
{
uint32_t ret;
memcpy(&ret, &val, sizeof(ret));
ret = ntohl(ret);
memcpy(&val, &ret, sizeof(val));
return val;
uint32_t ret;
memcpy(&ret, &val, sizeof(ret));
ret = ntohl(ret);
memcpy(&val, &ret, sizeof(val));
return val;
}
inline double htond(double val)
{
uint64_t ret;
memcpy(&ret, &val, sizeof(ret));
ret = htonll(ret);
memcpy(&val, &ret, sizeof(val));
return val;
uint64_t ret;
memcpy(&ret, &val, sizeof(ret));
ret = htonll(ret);
memcpy(&val, &ret, sizeof(val));
return val;
}
inline double ntohd(double val)
{
uint64_t ret;
memcpy(&ret, &val, sizeof(ret));
ret = ntohll(ret);
memcpy(&val, &ret, sizeof(val));
return val;
uint64_t ret;
memcpy(&ret, &val, sizeof(ret));
ret = ntohll(ret);
memcpy(&val, &ret, sizeof(val));
return val;
}
inline void set_unix_socket_blocking(int32_t socket_descriptor, bool is_blocking_already, bool should_block)
@ -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

View File

@ -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;
};
}

View File

@ -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)