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_UNIX_HPP__
00031 #define __CLAW_SOCKET_TRAITS_UNIX_HPP__
00032
00033 #include <sys/types.h>
00034 #include <sys/socket.h>
00035 #include <sys/stat.h>
00036 #include <netinet/in.h>
00037 #include <netdb.h>
00038 #include <unistd.h>
00039 #include <cstring>
00040
00041 #include <claw/assert.hpp>
00042
00043 namespace claw
00044 {
00049 class socket_traits_unix
00050 {
00051 public:
00053 typedef int descriptor;
00054
00055 public:
00057 static const descriptor invalid_socket = -1;
00058
00059 public:
00060
00065 static bool init()
00066 {
00067 return true;
00068 }
00069
00070
00075 static bool release()
00076 {
00077 return true;
00078 }
00079
00080
00085 static descriptor open()
00086 {
00087 descriptor fd = invalid_socket;
00088
00089 fd = socket(AF_INET, SOCK_STREAM, 0);
00090
00091 return fd;
00092 }
00093
00094
00100 static bool close( descriptor d )
00101 {
00102 return ::close(d) == 0;
00103 }
00104
00105
00113 static bool connect( descriptor d, const std::string& address, int port )
00114 {
00115 CLAW_PRECOND( d != invalid_socket );
00116
00117 bool result = false;
00118 struct hostent* hp = gethostbyname(address.c_str());
00119
00120 if (hp)
00121 {
00122 struct sockaddr_in sa;
00123
00124 memset (&sa, '\0', sizeof(sa));
00125 sa.sin_family = hp->h_addrtype;
00126 sa.sin_port = htons(port);
00127 memcpy( &sa.sin_addr, hp->h_addr, hp->h_length );
00128
00129 if (::connect(d, (struct sockaddr*)&sa, (socklen_t)sizeof(sa)) != -1)
00130 result = true;
00131 }
00132
00133 return result;
00134 }
00135
00136
00144 static bool listen( descriptor d, int port, unsigned int queue_size )
00145 {
00146 CLAW_PRECOND( d != invalid_socket );
00147
00148 struct sockaddr_in addr;
00149
00150 memset (&addr, '\0', sizeof(addr));
00151 addr.sin_family = AF_INET;
00152 addr.sin_port = htons(port);
00153 addr.sin_addr.s_addr = htonl(INADDR_ANY);
00154
00155 if ( bind(d, (struct sockaddr*)&addr, sizeof(addr)) != -1 )
00156 return ::listen(d, queue_size) != -1;
00157 else
00158 return false;
00159 }
00160
00161
00170 static bool select_read( descriptor d, int time_limit = -1 )
00171 {
00172 CLAW_PRECOND( d != invalid_socket );
00173
00174 struct timeval tv, *ptv;
00175 fd_set fds;
00176
00177 if ( time_limit < 0 )
00178 ptv = NULL;
00179 else
00180 {
00181 tv.tv_sec = time_limit;
00182 tv.tv_usec = 0;
00183
00184 ptv = &tv;
00185 }
00186
00187 FD_ZERO(&fds);
00188 FD_SET(d, &fds);
00189
00190 select( d+1, &fds, NULL, NULL, ptv );
00191
00192 return FD_ISSET( d, &fds );
00193 }
00194
00195
00201 static descriptor accept( descriptor d )
00202 {
00203 return ::accept( d, NULL, NULL );
00204 }
00205
00206
00211 static bool valid_descriptor( descriptor d )
00212 {
00213 return d != invalid_socket;
00214 }
00215
00216
00221 static bool is_open( descriptor d )
00222 {
00223 struct stat buf;
00224
00225 return fstat(d, &buf) == 0;
00226 }
00227
00228 };
00229
00230 typedef socket_traits_unix socket_traits;
00231 }
00232
00233 #endif // __CLAW_SOCKET_TRAITS_UNIX_HPP__