From e5427282f171bbf7d431fe2ed2691a11bb61513c Mon Sep 17 00:00:00 2001 From: Myun2 Date: Mon, 21 May 2012 18:49:56 +0900 Subject: [PATCH] =?utf8?q?=E3=82=BD=E3=82=B1=E3=83=83=E3=83=88?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- roast/include/roast/io/socket/socket.hpp | 12 ++++-- roast/include/roast/windows/socket_impl2.hpp | 55 +++++++++++++++++++++++++++- 2 files changed, 61 insertions(+), 6 deletions(-) diff --git a/roast/include/roast/io/socket/socket.hpp b/roast/include/roast/io/socket/socket.hpp index e00b48a7..81c35d12 100644 --- a/roast/include/roast/io/socket/socket.hpp +++ b/roast/include/roast/io/socket/socket.hpp @@ -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); diff --git a/roast/include/roast/windows/socket_impl2.hpp b/roast/include/roast/windows/socket_impl2.hpp index ff31e153..8811350e 100644 --- a/roast/include/roast/windows/socket_impl2.hpp +++ b/roast/include/roast/windows/socket_impl2.hpp @@ -6,6 +6,7 @@ #define __SFJP_ROAST__windows__net__socket_impl_HPP__ #include "roast/io/socket/socket_base.hpp" +#include // sprintf #include #include #include @@ -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) { -- 2.11.0