OSDN Git Service

windows/socket_sub.hpp: 直し
authorMyun2 <myun2@nwhite.info>
Sun, 13 May 2012 18:39:02 +0000 (03:39 +0900)
committerMyun2 <myun2@nwhite.info>
Sun, 13 May 2012 18:39:02 +0000 (03:39 +0900)
roast/include/roast/windows/socket_sub.hpp

index 17a2417..01e60c1 100644 (file)
@@ -17,10 +17,10 @@ namespace roast
                class socket_cleanup
                {
                private:
-                       static ::WSADATA* m_wsaInfo;
+                       static ::WSADATA m_wsaInfo;
                        static int m_count;
-               public:
-                       static void add(
+                       
+                       static void startup(
                                unsigned char ver_major=ROAST_WINDOWS_SOCKET_DEFAULT_VER_MAJOR,
                                unsigned char ver_miner=ROAST_WINDOWS_SOCKET_DEFAULT_VER_MINER)
                        {
@@ -28,17 +28,40 @@ namespace roast
                                
                                WORD wVersionRequested = MAKEWORD(ver_major, ver_miner);
                                
-                               if ( ::WSAStartup(wVersionRequested, m_pWsaInfo) != 0 )
+                               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++;
                        }
-                       const ::WSADATA& get_wsadata(){ return m_wsaInfo; }
                        
                        static void free()
                        {
-                               if ( ::WSACleanup() != 0 )
-                                       throw socket_exception(::WSAGetLastError(), "roast::socket_cleanup::free() ::WSACleanup() function was error.");
+                               m_count--;
+                               if ( m_count == 0 )
+                               {
+                                       cleanup();
+                               }
                        }
+                       
+                       static const ::WSADATA& get_wsadata(){ return m_wsaInfo; }
                };
+               
                int socket_cleanup::m_count = 0;
        }
 }