00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
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 }
00071
00072
00077 static bool release()
00078 {
00079 return WSACleanup() == 0;
00080 }
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 }
00095
00096
00102 static bool close( descriptor d )
00103 {
00104 return ::closesocket(d) == 0;
00105 }
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 }
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 }
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 }
00196
00197
00203 static descriptor accept( descriptor d )
00204 {
00205 return ::accept( d, NULL, NULL );
00206 }
00207
00208
00213 static bool valid_descriptor( descriptor d )
00214 {
00215 return d != invalid_socket;
00216 }
00217
00218
00223 static bool is_open( descriptor d )
00224 {
00225 return valid_descriptor(d);
00226 }
00227
00228 };
00229
00230 typedef socket_traits_win32 socket_traits;
00231 }
00232
00233 #endif // __CLAW_SOCKET_TRAITS_WIN32_HPP__