From f97dcc72dc209d439397edbda0dfc261329ae73f Mon Sep 17 00:00:00 2001 From: gabime Date: Sun, 5 Sep 2021 17:28:28 +0300 Subject: [PATCH] cleanup tcp client WSA Startup/Shutdown --- include/spdlog/details/tcp_client-windows.h | 45 ++++++++------------- 1 file changed, 16 insertions(+), 29 deletions(-) diff --git a/include/spdlog/details/tcp_client-windows.h b/include/spdlog/details/tcp_client-windows.h index 95c0d22e..45b69630 100644 --- a/include/spdlog/details/tcp_client-windows.h +++ b/include/spdlog/details/tcp_client-windows.h @@ -24,21 +24,7 @@ namespace details { class tcp_client { SOCKET socket_ = INVALID_SOCKET; - - static bool winsock_initialized_() - { - SOCKET s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP); - if (s == INVALID_SOCKET) - { - return false; - } - else - { - closesocket(s); - return true; - } - } - + static void init_winsock_() { WSADATA wsaData; @@ -59,6 +45,18 @@ class tcp_client } public: + tcp_client() + { + init_winsock_(); + } + + ~tcp_client() + { + close(); + ::WSACleanup(); + } + + bool is_connected() const { return socket_ != INVALID_SOCKET; @@ -67,8 +65,7 @@ public: void close() { ::closesocket(socket_); - socket_ = INVALID_SOCKET; - WSACleanup(); + socket_ = INVALID_SOCKET; } SOCKET fd() const @@ -76,20 +73,10 @@ public: return socket_; } - ~tcp_client() - { - close(); - } - + // try to connect or throw on failure void connect(const std::string &host, int port) - { - // initialize winsock if needed - if (!winsock_initialized_()) - { - init_winsock_(); - } - + { if (is_connected()) { close();