From 767ed02280b4393c69f27106df5a849d6d7793bf Mon Sep 17 00:00:00 2001 From: yhirose Date: Wed, 7 Nov 2012 20:54:04 -0500 Subject: [PATCH] Added WSInit class to initialize WinSock2. --- httplib.h | 40 ++++++++++++++++------------------------ 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/httplib.h b/httplib.h index 59bd4ef..e6f1865 100644 --- a/httplib.h +++ b/httplib.h @@ -87,7 +87,6 @@ public: typedef std::function Logger; Server(); - ~Server(); void get(const char* pattern, Handler handler); void post(const char* pattern, Handler handler); @@ -116,7 +115,6 @@ private: class Client { public: Client(const char* host, int port); - ~Client(); std::shared_ptr get(const char* url); std::shared_ptr head(const char* url); @@ -504,6 +502,22 @@ inline void parse_query_text(const std::string& s, Map& params) }); } +#ifdef _WIN32 +class WSInit { +public: + WSInit::WSInit() { + WSADATA wsaData; + WSAStartup(0x0002, &wsaData); + } + + WSInit::~WSInit() { + WSACleanup(); + } +}; + +static WSInit wsinit_; +#endif + } // namespace detail // Request implementation @@ -559,17 +573,6 @@ inline void Response::set_content(const std::string& s, const char* content_type inline Server::Server() : svr_sock_(-1) { -#ifdef _WIN32 - WSADATA wsaData; - WSAStartup(0x0002, &wsaData); -#endif -} - -inline Server::~Server() -{ -#ifdef _WIN32 - WSACleanup(); -#endif } inline void Server::get(const char* pattern, Handler handler) @@ -730,17 +733,6 @@ inline Client::Client(const char* host, int port) : host_(host) , port_(port) { -#ifdef _WIN32 - WSADATA wsaData; - WSAStartup(0x0002, &wsaData); -#endif -} - -inline Client::~Client() -{ -#ifdef _WIN32 - WSACleanup(); -#endif } inline bool Client::read_response_line(FILE* fp, Response& res)