From: Myun2 Date: Mon, 21 May 2012 09:54:25 +0000 (+0900) Subject: windows/socket_startup.hpp Add. X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=dba595fa88e72a02f76d0e44360c70ff054ec1bc;p=roast%2Froast.git windows/socket_startup.hpp Add. --- diff --git a/roast/include/roast/windows/socket_startup.hpp b/roast/include/roast/windows/socket_startup.hpp new file mode 100644 index 00000000..e95bd124 --- /dev/null +++ b/roast/include/roast/windows/socket_startup.hpp @@ -0,0 +1,71 @@ +// Roast+ License + +/* +*/ +#ifndef __SFJP_ROAST__windows__socket_sub_HPP__ +#define __SFJP_ROAST__windows__socket_sub_HPP__ + +#define ROAST_WINDOWS_SOCKET_DEFAULT_VER_MAJOR (2) +#define ROAST_WINDOWS_SOCKET_DEFAULT_VER_MINER (0) + +////////////////////////////////////////////////// + +namespace roast +{ + namespace windows + { + class socket_cleanup + { + private: + static ::WSADATA m_wsaInfo; + static int m_count; + + static void startup( + unsigned char ver_major=ROAST_WINDOWS_SOCKET_DEFAULT_VER_MAJOR, + unsigned char ver_miner=ROAST_WINDOWS_SOCKET_DEFAULT_VER_MINER) + { + ZeroMemory(&m_wsaInfo,sizeof(m_wsaInfo)); + + WORD wVersionRequested = MAKEWORD(ver_major, ver_miner); + + if ( ::WSAStartup(wVersionRequested, &m_wsaInfo) != 0 ) + throw socket_exception(::WSAGetLastError(), "roast::socket_cleanup::add() ::WSAStartup() function was error."); + + } + static void cleanup() + { + if ( ::WSACleanup() != 0 ) + throw socket_exception(::WSAGetLastError(), "roast::socket_cleanup::free() ::WSACleanup() function was error."); + } + + public: + static void add( + unsigned char ver_major=ROAST_WINDOWS_SOCKET_DEFAULT_VER_MAJOR, + unsigned char ver_miner=ROAST_WINDOWS_SOCKET_DEFAULT_VER_MINER) + { + if ( m_count == 0 ) + { + startup(); + } + m_count++; + } + + static void del() + { + m_count--; + if ( m_count == 0 ) + { + cleanup(); + } + } + + static const ::WSADATA& get_wsadata(){ return m_wsaInfo; } + }; + + // Static Variables + ::WSADATA socket_cleanup::m_wsaInfo; + int socket_cleanup::m_count = 0; + } +} + +#endif//__SFJP_ROAST__windows__socket_sub_HPP__