claw::socket_traits_unix Class Reference

Unix interface for using sockets. More...

#include <socket_traits_unix.hpp>

List of all members.

Public Types

typedef int descriptor
 Type of the system description of the socket.

Static Public Member Functions

static bool init ()
 Initialize the use of the socket library.
static bool release ()
 Close the socket library.
static descriptor open ()
 Open a socket.
static bool close (descriptor d)
 Close a socket.
static bool connect (descriptor d, const std::string &address, int port)
 Connect a socket to a port.
static bool listen (descriptor d, int port, unsigned int queue_size)
 Open a socket for incoming connexions.
static bool select_read (descriptor d, int time_limit=-1)
 Select a socket for reading.
static descriptor accept (descriptor d)
 Accept an incoming connexion.
static bool valid_descriptor (descriptor d)
 Tell if a descriptor is a valid socket descriptor.
static bool is_open (descriptor d)
 Tell if a descriptor is a opened socket.

Static Public Attributes

static const descriptor invalid_socket = -1
 Invalid socket descriptor.

Detailed Description

Unix interface for using sockets.

Author:
Julien Jorge

Definition at line 49 of file socket_traits_unix.hpp.


Member Typedef Documentation

Type of the system description of the socket.

Definition at line 53 of file socket_traits_unix.hpp.


Member Function Documentation

static descriptor claw::socket_traits_unix::accept ( descriptor  d  )  [inline, static]

Accept an incoming connexion.

Parameters:
d The descriptor of the socket to listen.
Returns:
The descriptor of the incoming connexion.

Definition at line 201 of file socket_traits_unix.hpp.

Referenced by claw::net::socket_server::accept().

00202     {
00203       return ::accept( d, NULL, NULL );
00204     } // socket_traits_unix::accept()

static bool claw::socket_traits_unix::close ( descriptor  d  )  [inline, static]

Close a socket.

Parameters:
d The descriptor of the socket to close.
Returns:
true if the socket has been closed.

Definition at line 100 of file socket_traits_unix.hpp.

Referenced by claw::net::basic_socket::close().

00101     {
00102       return ::close(d) == 0; 
00103     } // socket_traits_unix::close()

static bool claw::socket_traits_unix::connect ( descriptor  d,
const std::string &  address,
int  port 
) [inline, static]

Connect a socket to a port.

Parameters:
d The descriptor of the socket to connect.
address The adress to connect to.
port The port to connect to.
Returns:
true if the connection is available.

Definition at line 113 of file socket_traits_unix.hpp.

References CLAW_PRECOND, and invalid_socket.

Referenced by claw::net::basic_socketbuf< CharT, Traits >::connect().

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     } // socket_traits_unix::connect()

static bool claw::socket_traits_unix::init (  )  [inline, static]

Initialize the use of the socket library.

Returns:
true if the initialization is successful.

Definition at line 65 of file socket_traits_unix.hpp.

00066     {
00067       return true;
00068     } // socket_traits_unix::init()

static bool claw::socket_traits_unix::is_open ( descriptor  d  )  [inline, static]

Tell if a descriptor is a opened socket.

Parameters:
d The descriptor to test.

Definition at line 221 of file socket_traits_unix.hpp.

Referenced by claw::net::basic_socketbuf< CharT, Traits >::open().

00222     {
00223       struct stat buf;
00224 
00225       return fstat(d, &buf) == 0;
00226     } // socket_traits_unix::is_open()

static bool claw::socket_traits_unix::listen ( descriptor  d,
int  port,
unsigned int  queue_size 
) [inline, static]

Open a socket for incoming connexions.

Parameters:
d The descriptor of the socket to open.
port The port to connect to.
queue_size The size of the queue for incoming connexions.
Returns:
true if the socket has been opened.

Definition at line 144 of file socket_traits_unix.hpp.

References CLAW_PRECOND, and invalid_socket.

Referenced by claw::net::socket_server::open().

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     } // socket_traits_unix::connect()

static descriptor claw::socket_traits_unix::open (  )  [inline, static]

Open a socket.

Returns:
The descriptor on the loaded socket.

Definition at line 85 of file socket_traits_unix.hpp.

References invalid_socket.

00086     {
00087       descriptor fd = invalid_socket;
00088 
00089       fd = socket(AF_INET, SOCK_STREAM, 0);
00090 
00091       return fd;
00092     } // socket_traits_unix::open()

static bool claw::socket_traits_unix::release (  )  [inline, static]

Close the socket library.

Returns:
true if the operation is successful.

Definition at line 75 of file socket_traits_unix.hpp.

00076     {
00077       return true;
00078     } // socket_traits_unix::release()

static bool claw::socket_traits_unix::select_read ( descriptor  d,
int  time_limit = -1 
) [inline, static]

Select a socket for reading.

Parameters:
d The descriptor of the socket to read.
time_limit Maximum of seconds to wait before considering there's nothing to read. If time_limit is negative, the method wait until there is something to read.
Returns:
true if the socket is ready to be read.

Definition at line 170 of file socket_traits_unix.hpp.

References CLAW_PRECOND, and invalid_socket.

Referenced by claw::net::socket_server::accept(), and claw::net::basic_socketbuf< CharT, Traits >::underflow().

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     } // socket_traits_unix::select_read()

static bool claw::socket_traits_unix::valid_descriptor ( descriptor  d  )  [inline, static]

Tell if a descriptor is a valid socket descriptor.

Parameters:
d The descriptor to test.

Definition at line 211 of file socket_traits_unix.hpp.

References invalid_socket.

Referenced by claw::net::basic_socketbuf< CharT, Traits >::connect(), claw::net::basic_socket::is_open(), and claw::net::basic_socket::open().

00212     {
00213       return d != invalid_socket;
00214     } // socket_traits_unix::valid_descriptor()


Member Data Documentation

Invalid socket descriptor.

Definition at line 57 of file socket_traits_unix.hpp.

Referenced by claw::net::basic_socket::close(), connect(), listen(), open(), select_read(), and valid_descriptor().


The documentation for this class was generated from the following file:

Generated on 9 Nov 2009 for CLAW Library (a C++ Library Absolutely Wonderful) by  doxygen 1.6.1