Skip to content
Snippets Groups Projects
Commit 8c2eabed authored by Nicolas Pope's avatar Nicolas Pope
Browse files

#22 Change winsock startup

parent 2acc22b1
No related branches found
No related tags found
No related merge requests found
...@@ -39,19 +39,25 @@ bool ftl::net::internal::resolve_inet_address(const std::string& hostname, int p ...@@ -39,19 +39,25 @@ bool ftl::net::internal::resolve_inet_address(const std::string& hostname, int p
return true; return true;
} }
static std::atomic_bool is_initialized_; class WinSock {
static WSAData wsaData_; public:
WinSock() {
Socket::Socket(int domain, int type, int protocol) :
status_(STATUS::UNCONNECTED), fd_(-1), family_(domain) {
if (!is_initialized_.exchange(true)) {
if (WSAStartup(MAKEWORD(1, 1), &wsaData_) != 0) { if (WSAStartup(MAKEWORD(1, 1), &wsaData_) != 0) {
LOG(FATAL) << "could not initialize sockets"; LOG(FATAL) << "could not initialize sockets";
// is it possible to retry/recover? // is it possible to retry/recover?
} }
} }
// TODO(Seb): initialization might not be complete if called from another thread private:
WSAData wsaData_;
};
// Do WSAStartup as static initialisation so no threads active.
static WinSock winSock;
Socket::Socket(int domain, int type, int protocol) :
status_(STATUS::UNCONNECTED), fd_(-1), family_(domain) {
fd_ = ::socket(domain, type, protocol); fd_ = ::socket(domain, type, protocol);
if (fd_ == INVALID_SOCKET) { if (fd_ == INVALID_SOCKET) {
err_ = WSAGetLastError(); err_ = WSAGetLastError();
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment