OSDN Git Service

ソケット
authorMyun2 <myun2@nwhite.info>
Mon, 21 May 2012 09:49:56 +0000 (18:49 +0900)
committerMyun2 <myun2@nwhite.info>
Mon, 21 May 2012 09:49:56 +0000 (18:49 +0900)
roast/include/roast/io/socket/socket.hpp
roast/include/roast/windows/socket_impl2.hpp

index e00b48a..81c35d1 100644 (file)
@@ -44,17 +44,21 @@ namespace roast
                
        protected:
                _Impl m_impl;
+               open_params_type m_openparam;
                //socket_t m_socket;
                
                socket_type create_socket(){
-                       socket_type sock = m_impl.create(open_params_type());
+                       m_openparam = open_params_type();
+                       socket_type sock = m_impl.create(m_openparam);
                        if ( sock == _Impl::invalid_handle )
                                throw socket_exception("roast::io::socket::create_socket(): Failed create new socket.");
                        m_handle = sock;
                        return sock;
                }
-               socket_type create_socket(int family, int socktype, int protocol){
-                       socket_type sock = m_impl.create(open_params_type(family, socktype, protocol));
+               socket_type create_socket(int family, int socktype, int protocol)
+               {
+                       m_openparam = open_params_type(family, socktype, protocol);
+                       socket_type sock = m_impl.create(m_openparam);
                        if ( sock == _Impl::invalid_handle )
                                throw socket_exception("roast::io::socket::create_socket(): Failed create new socket.");
                        m_handle = sock;
@@ -65,7 +69,7 @@ namespace roast
                socket_(){ create_socket(); }
                socket_(int family, int socktype, int protocol){ create_socket(family, socktype, protocol); }
                //socket_(::FILE* fp) : _Base(fp){}
-               socket_(const socket_& from) : _Base(from){}
+               socket_(const socket_& from) : _Base(from), m_openparam(from.m_openparam) {}
                socket_(const char* host, int port) /*: _Base(open_params_type(host, port))*/{
                        create_socket();
                        open(host, port);
index ff31e15..8811350 100644 (file)
@@ -6,6 +6,7 @@
 #define __SFJP_ROAST__windows__net__socket_impl_HPP__
 
 #include "roast/io/socket/socket_base.hpp"
+#include <stdio.h>     //      sprintf
 #include <string>
 #include <ws2tcpip.h>
 #include <Wspiapi.h>
@@ -28,13 +29,26 @@ namespace roast
                                int af;
                                int type;
                                int protocol;
+                               const char* host;
+                               int port;
                                
                                open_params_type(){
+                                       host = NULL;
+                                       port = 0;
                                        af = AF_INET;
                                        type = SOCK_STREAM;
                                        protocol = protocol;
                                }
                                open_params_type(int af_in, int type_in, int protocol_in){
+                                       host = NULL;
+                                       port = 0;
+                                       af = af_in;
+                                       type = type_in;
+                                       protocol = protocol_in;
+                               }
+                               open_params_type(const char* host_in, int port_in, int af_in, int type_in, int protocol_in){
+                                       host = host_in;
+                                       port = port_in;
                                        af = af_in;
                                        type = type_in;
                                        protocol = protocol_in;
@@ -67,6 +81,39 @@ namespace roast
                        static void lasterr_assert(const char* msg){
                                nozero_assert(::WSAGetLastError(), msg);
                        }
+
+                       //      Private Classes
+                       class addr
+                       {
+                       private:
+                               ::addrinfo* data;
+                       public:
+                               addr(const char* host, int port, int af, int socktype) {
+                                       ::addrinfo hints;
+                                       ::memset(&hints, 0, sizeof(hints));
+                                       hints.ai_socktype = af;
+                                       hints.ai_family = socktype;
+
+#pragma warning(disable: 4996)
+                                       char port_str[12];
+                                       sprintf(port_str, "%d", port);
+#pragma warning(default: 4996)
+
+                                       nozero_assert(
+                                               ::getaddrinfo(host, port_str, &hints, &data),
+                                               "roast::windows::socket::addr() ::getaddrinfo() function was error.");
+
+                               }
+                               virtual ~addr(){
+                                       if ( data != NULL )
+                                               ::freeaddrinfo(data);
+                               }
+                               ///////////////
+                               operator ::addrinfo* (){ return data; }
+                               operator ::addrinfo* () const { return data; }
+                               ::addrinfo* operator -> (){ return data; }
+                               const ::addrinfo* operator -> () const { return data; }
+                       };
                        
                public:
                        socket_impl(){}
@@ -79,8 +126,12 @@ namespace roast
                                lasterr_assert("roast::windows::socket::create() ::socket() function was error.");
                                return sock;
                        }
-                       static handler_type open(const open_params_type& params){
-                       
+                       static handler_type open(handler_type handler, const open_params_type& params){
+                               addr ai(params.host, params.port, params.af, params.type);
+                               nozero_assert(
+                                       ::connect(handler, ai->ai_addr, ai->ai_addrlen),
+                                       "roast::socket::connect() ::connect() function was error.");
+                               return handler;
                        }
                        static void close(handler_type handler)
                        {