OSDN Git Service

windows/socket.hpp: とりあえず、コンパイルが通る
authorMyun2 <myun2@nwhite.info>
Sun, 13 May 2012 18:24:29 +0000 (03:24 +0900)
committerMyun2 <myun2@nwhite.info>
Sun, 13 May 2012 18:24:29 +0000 (03:24 +0900)
roast/include/roast/io/io_base.hpp
roast/include/roast/net/socket_base.hpp
roast/include/roast/windows/socket.hpp

index 3d7ff56..8aebea1 100644 (file)
@@ -33,8 +33,8 @@ namespace roast
        public:
                //virtual ~io_base(){ _close(); }
  
-               virtual bool connect(const void* options)=0;
-               virtual bool close()=0;
+               virtual void connect(const void* options)=0;
+               virtual void close()=0;
                
                virtual int read(void *buf, size_t size)=0;
                virtual int write(const void *buf, size_t size)=0;
index 1349452..1c3d014 100644 (file)
@@ -27,7 +27,7 @@ namespace roast
        class socket_base : public io_base
        {
        private:
-               bool connect(const void* options){ return false; }
+               void connect(const void* options){}
                
                //virtual void create()=0;
        public:
@@ -39,8 +39,8 @@ namespace roast
                
        public:
                //      Require Implelements (Virtual Methods)
-               virtual bool connect(const char* host, int port)=0;
-               virtual bool connect(const char* host, int port, int socktype, int family)=0;
+               virtual void connect(const char* host, int port)=0;
+               //virtual bool connect(const char* host, int port, int socktype, int family)=0;
                
                virtual int send(const void* s, size_t size, int flag=0)=0;
                virtual int recv(void* s, size_t size, int flag=0)=0;
index 8361720..19b2913 100644 (file)
@@ -23,53 +23,27 @@ namespace roast
        namespace windows
        {
                //////////////////////////////////////////////////////////////////////////////////////
-               
-               class safe_sockaddr
-               {
-               public:
-                       ::addrinfo /*hints,*/ *addr;
-
-                       /////////////////////////////////////////////////
-                       safe_sockaddr(const char* host, int port, int socktype, int family)
-                       {
-                               ::addrinfo hints/*, *addr*/;
-                               ::memset(&hints, 0, sizeof(hints));
-                               hints.ai_socktype = socktype;
-                               hints.ai_family = family;
-
-#pragma warning(disable: 4996)
-                               char port_str[12];
-                               sprintf(port_str, "%d", port);
-#pragma warning(default: 4996)
-
-                               //if ( !m_impl.getaddrinfo(host, port_str, &hints, &addr) )
-                               if ( !::getaddrinfo(host, port_str, &hints, &addr) )
-                                       throw socket_exception(::WSAGetLastError(), "roast::socket::socket() ::getaddrinfo() function is error.");
-                       }
-                       virtual ~safe_sockaddr()
-                       {
-                               if ( addr != NULL )
-                               {
-                                       ::freeaddrinfo(addr);
-                                       addr = NULL;
-                               }
-                       }
-               };
-
-               //////////////////////////////////////////////////////////////////////////////////////
        
                class socket : public socket_base
                {
                private:
                        ::SOCKET m_sock;
+                       int m_af;
+                       int m_socket_type;
+                       int m_socket_protocol;
+
                public:
                        socket(int af=AF_INET, int type=SOCK_STREAM, int protocol=0)
                                : m_sock(NULL)
                        {
+                               m_af = af;
+                               m_socket_type = type;
+                               m_socket_protocol = protocol;
+
                                m_sock = ::socket(af, type, protocol);
 
                                if ( ::WSAGetLastError() != 0 )
-                                       throw socket_exception(::WSAGetLastError(), "roast::socket::socket() ::socket() function is error.");
+                                       throw socket_exception(::WSAGetLastError(), "roast::socket::socket() ::socket() function was error.");
                                if ( m_sock == INVALID_SOCKET )
                                        throw socket_exception(::WSAGetLastError(), "roast::socket::socket() m_sock is INVALID_SOCKET.");
                        }
@@ -79,32 +53,51 @@ namespace roast
 
                        //////////////////////////////////////////////////////////////////
 
-                       bool close()
+                       void close()
                        {
                                if ( m_sock != INVALID_SOCKET )
                                {
                                        ::closesocket(m_sock);
 
                                        if ( ::WSAGetLastError() != 0 )
-                                               throw socket_exception(::WSAGetLastError(), "roast::socket::close() ::closesocket() function is error.");
+                                               throw socket_exception(::WSAGetLastError(), "roast::socket::close() ::closesocket() function was error.");
 
                                        m_sock = INVALID_SOCKET;
                                }
                        }
 
-                       bool connect(const char* host, int port)
+                       void connect(const char* host, int port)
                        {
-                               close();
+                               //      Already Socket was Close...?
+                               //close();
 
-                               ::connect(m_sock, addr, addrlen)
-                       }
-                       bool connect(const char* host, int port, int socktype, int family)
-                       {
+                               if ( m_sock != INVALID_SOCKET )
+                                       throw socket_exception(::WSAGetLastError(), "roast::socket::connect() Socket was already opened.");
+                               
+                               ::addrinfo hints;
+                               ::memset(&hints, 0, sizeof(hints));
+                               hints.ai_socktype = m_socket_type;
+                               hints.ai_family = m_af;
+
+#pragma warning(disable: 4996)
+                               char port_str[12];
+                               sprintf(port_str, "%d", port);
+#pragma warning(default: 4996)
+
+                               ::addrinfo *addr=NULL;
+                               if ( ::getaddrinfo(host, port_str, &hints, &addr) != 0 )
+                                       throw socket_exception(::WSAGetLastError(), "roast::socket::connect() ::getaddrinfo() function was error.");
+
+                               if ( ::connect(m_sock, addr->ai_addr, addr->ai_addrlen) != 0 ){
+                                       ::freeaddrinfo(addr);
+                                       throw socket_exception(::WSAGetLastError(), "roast::socket::connect() ::connect() function was error.");
+                               }
 
+                               ::freeaddrinfo(addr);
                        }
                        
-                       virtual int send(const void* s, size_t size, int flag=0)=0;
-                       virtual int recv(void* s, size_t size, int flag=0)=0;
+                       int send(const void* s, size_t size, int flag=0){return 0;}
+                       int recv(void* s, size_t size, int flag=0){return 0;}
                };
        }
 }