socket_traits_win32.hpp

Go to the documentation of this file.
00001 /*
00002   CLAW - a C++ Library Absolutely Wonderful
00003 
00004   CLAW is a free library without any particular aim but being useful to 
00005   anyone.
00006 
00007   Copyright (C) 2005-2008 Julien Jorge
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Lesser General Public
00011   License as published by the Free Software Foundation; either
00012   version 2.1 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Lesser General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public
00020   License along with this library; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023   contact: julien_jorge@yahoo.fr
00024 */
00030 #ifndef __CLAW_SOCKET_TRAITS_WIN32_HPP__
00031 #define __CLAW_SOCKET_TRAITS_WIN32_HPP__
00032 
00033 #include <sys/types.h>
00034 #include <winsock2.h>
00035 #include <sys/stat.h>
00036 #include <unistd.h>
00037 
00038 #include <claw/assert.hpp>
00039 
00040 namespace claw
00041 {
00046   class socket_traits_win32
00047   {
00048   public:
00050     typedef SOCKET descriptor;
00051 
00052   public:
00054     static const descriptor invalid_socket = INVALID_SOCKET;
00055 
00056   public:
00057     /*------------------------------------------------------------------------*/
00062     static bool init()
00063     {
00064       WORD version;
00065       WSADATA data;
00066  
00067       version = MAKEWORD( 2, 2 );
00068       
00069       return WSAStartup( version, &data ) == 0;
00070     } // socket_traits_win32::init()
00071 
00072     /*------------------------------------------------------------------------*/
00077     static bool release()
00078     {
00079       return WSACleanup() == 0;
00080     } // socket_traits_win32::release()
00081 
00082     /*------------------------------------------------------------------------*/
00087     static descriptor open()
00088     {
00089       descriptor fd = invalid_socket;
00090 
00091       fd = socket(AF_INET, SOCK_STREAM, 0);
00092 
00093       return fd;
00094     } // socket_traits_win32::open()
00095 
00096     /*------------------------------------------------------------------------*/
00102     static bool close( descriptor d )
00103     {
00104       return ::closesocket(d) == 0; 
00105     } // socket_traits_win32::close()
00106 
00107     /*------------------------------------------------------------------------*/
00115     static bool connect( descriptor d, const std::string& address, int port )
00116     {
00117       CLAW_PRECOND( d != invalid_socket );
00118 
00119       bool result=false;
00120       struct hostent* hp = gethostbyname(address.c_str());
00121 
00122       if (hp)
00123   {
00124     struct sockaddr_in sa;
00125 
00126     memset (&sa, '\0', sizeof(sa));
00127     sa.sin_family = hp->h_addrtype;
00128     sa.sin_port = htons(port);
00129     memcpy( &sa.sin_addr, hp->h_addr, hp->h_length );
00130       
00131     if ( ::connect(d, (struct sockaddr*)&sa, sizeof(sa)) != SOCKET_ERROR )
00132       result = true;
00133   }
00134 
00135       return result;
00136     } // socket_traits_win32::connect()
00137 
00138     /*------------------------------------------------------------------------*/
00146     static bool listen( descriptor d, int port, unsigned int queue_size )
00147     {
00148       CLAW_PRECOND( d != invalid_socket );
00149 
00150       struct sockaddr_in addr;
00151 
00152       memset (&addr, '\0', sizeof(addr));
00153       addr.sin_family = AF_INET;
00154       addr.sin_port = htons(port);
00155       addr.sin_addr.s_addr = htonl(INADDR_ANY);
00156 
00157       if ( bind(d, (struct sockaddr*)&addr, sizeof(addr)) != SOCKET_ERROR )
00158         return ::listen(d, queue_size) != SOCKET_ERROR;
00159       else
00160         return false;
00161     } // socket_traits_win32::connect()
00162 
00163     /*------------------------------------------------------------------------*/
00172     static bool select_read( descriptor d, int time_limit = -1 )
00173     {
00174       CLAW_PRECOND( d != invalid_socket );
00175 
00176       struct timeval tv, *ptv;
00177       fd_set fds;
00178 
00179       if ( time_limit < 0 )
00180         ptv = NULL;
00181       else
00182         {
00183           tv.tv_sec  = time_limit;
00184           tv.tv_usec = 0;
00185           
00186           ptv = &tv;
00187         }
00188 
00189       FD_ZERO(&fds);
00190       FD_SET(d, &fds);
00191 
00192       select( d+1, &fds, NULL, NULL, ptv );
00193 
00194       return FD_ISSET( d, &fds );
00195     } // socket_traits_win32::select_read()
00196 
00197     /*------------------------------------------------------------------------*/
00203     static descriptor accept( descriptor d )
00204     {
00205       return ::accept( d, NULL, NULL );
00206     } // socket_traits_win32::accept()
00207 
00208     /*------------------------------------------------------------------------*/
00213     static bool valid_descriptor( descriptor d )
00214     {
00215       return d != invalid_socket;
00216     } // socket_traits_win32::valid_descriptor()
00217 
00218     /*------------------------------------------------------------------------*/
00223     static bool is_open( descriptor d )
00224     {
00225       return valid_descriptor(d);
00226     } // socket_traits_win32::is_open()
00227 
00228   }; // class socket_traits_win32
00229 
00230   typedef socket_traits_win32 socket_traits;
00231 } // namespace claw
00232 
00233 #endif // __CLAW_SOCKET_TRAITS_WIN32_HPP__

Generated on Mon Nov 9 05:08:18 2009 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.4.7