OSDN Git Service

windows/socket_startup.hpp Add.
authorMyun2 <myun2@nwhite.info>
Mon, 21 May 2012 09:54:25 +0000 (18:54 +0900)
committerMyun2 <myun2@nwhite.info>
Mon, 21 May 2012 09:54:25 +0000 (18:54 +0900)
roast/include/roast/windows/socket_startup.hpp [new file with mode: 0644]

diff --git a/roast/include/roast/windows/socket_startup.hpp b/roast/include/roast/windows/socket_startup.hpp
new file mode 100644 (file)
index 0000000..e95bd12
--- /dev/null
@@ -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__