From e314a337f4ee6342be47aa4b78fc75dcda1a29e6 Mon Sep 17 00:00:00 2001 From: Myun2 Date: Sat, 19 May 2012 16:39:16 +0900 Subject: [PATCH] windows/socket_impl2.hpp --- roast/include/roast/io/socket/socket_base.hpp | 4 +- roast/include/roast/windows/socket_impl2.hpp | 115 +++++++++++--------------- 2 files changed, 48 insertions(+), 71 deletions(-) diff --git a/roast/include/roast/io/socket/socket_base.hpp b/roast/include/roast/io/socket/socket_base.hpp index 9d9565e2..7f63ba48 100644 --- a/roast/include/roast/io/socket/socket_base.hpp +++ b/roast/include/roast/io/socket/socket_base.hpp @@ -6,7 +6,6 @@ #define __SFJP_ROAST__io__socket__socket_base_HPP__ #include "roast/io/io_base.hpp" -#include namespace roast { @@ -25,10 +24,9 @@ namespace roast ///////////////////////////////////////////////////// - class socket_base : public io_base + class socket_impl_base { private: - void open(const void* options){} //virtual void create()=0; public: diff --git a/roast/include/roast/windows/socket_impl2.hpp b/roast/include/roast/windows/socket_impl2.hpp index ed219e0c..1662a4da 100644 --- a/roast/include/roast/windows/socket_impl2.hpp +++ b/roast/include/roast/windows/socket_impl2.hpp @@ -21,17 +21,59 @@ namespace roast { public: // Types: - typedef ::SOCKET handler_type; + typedef ::SOCKET handler_type, sock_type, socket_type; struct open_params_type {}; struct ioctl_data {}; - // Const Values: + // Const Values const static handler_type invalid_handle; - - // Methods: - handler_type open(const open_params_type& params); + + private: + // Private Methods + ::std::string get_last_error_msg() + { + char *msg_buf; + ::FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, + NULL, ::WSAGetLastError(), LANG_USER_DEFAULT, (LPSTR)&msg_buf, 0, NULL ); + ::std::string msg; + if ( msg_buf != NULL ){ + msg = msg_buf; + ::LocalFree(msg_buf); + } + return msg; + } + + void lasterr_assert(const char* msg){ + if ( ::WSAGetLastError() != 0 ) + throw socket_exception(code, ::std::string(msg) + ' ' + get_last_error_msg()); + } + void nozero_assert(int code, const char* msg){ + if ( code != 0 ) + throw socket_exception(code, ::std::string(msg) + ' ' + get_last_error_msg()); + } + + public: + // Implement Methods + handler_type create(const open_params_type& params){ + sock_type sock = ::socket(af, type, protocol); + lasterr_assert(); + return sock; + } + handler_type open(const open_params_type& params){ + + } void close(handler_type handler); + + + + void close(handler_type handler) + { + nozero_assert( + ::closesocket(handler), + "roast::windows::socket::close() ::closesocket() function was error."); + } + int read(handler_type handler, void *buf, size_t size); int write(handler_type handler, const void *buf, size_t size); int ioctl(handler_type handler, const ioctl_data& data); @@ -44,45 +86,15 @@ namespace roast //////////////////////////////////////////////////////////////////////////////// - class socket : public socket_base { private: bool m_opened; - bool m_owner; int m_af; int m_socket_type; int m_socket_protocol; - - // Get Message - ::std::string get_last_error_msg() - { - char *msg_buf; - ::FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM, - NULL, ::WSAGetLastError(), LANG_USER_DEFAULT, (LPSTR)&msg_buf, 0, NULL ); - ::std::string msg; - if ( msg_buf != NULL ){ - msg = msg_buf; - ::LocalFree(msg_buf); - } - return msg; - } - - void nozero_assert(int code, const char* msg){ - if ( code != 0 ) - throw socket_exception(code, ::std::string(msg) + ' ' + get_last_error_msg()); - } public: - socket(::SOCKET sock) : m_sock(sock) - { - m_cleanup.add(); - m_opened = true; - m_owner = false; - - if ( m_sock == INVALID_SOCKET ) - throw socket_exception(::WSAGetLastError(), "roast::socket::socket() m_sock is INVALID_SOCKET."); - } socket(int af=AF_INET, int type=SOCK_STREAM, int protocol=0) : m_sock(NULL) { @@ -102,11 +114,6 @@ namespace roast throw socket_exception(::WSAGetLastError(), "roast::socket::socket() m_sock is INVALID_SOCKET."); } - virtual ~socket(){ - //shutdown(); - close(); - m_cleanup.del(); - } ////////////////////////////////////////////////////////////////// @@ -119,20 +126,6 @@ namespace roast "roast::socket::close() ::closesocket() function was error."); } } - - void close() - { - if ( m_sock != INVALID_SOCKET && m_opened ) - { - nozero_assert( - ::closesocket(m_sock), - "roast::socket::close() ::closesocket() function was error."); - - m_sock = INVALID_SOCKET; - m_opened = false; - } - } - void connect(const char* host, int port) { // Already Socket was Close...? @@ -229,20 +222,6 @@ namespace roast 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;} - - void set_owner(){ m_owner = true; } - void deprive(){ m_owner = false; } - void deprive_clne(){ m_owner = false; } - }; - - ////////////////////////////////////////////////////////////////// - - typedef socket tcp_socket; - - class udp_socket : public socket - { - public: - udp_socket(int af=AF_INET, int type=SOCK_DGRAM, int protocol=0) : socket(af, type, protocol){} }; //////////////////////////////////////////////////////////////////////////////// -- 2.11.0