Public Types | Static Public Member Functions | Static Public Attributes

claw::socket_traits_win32 Class Reference

Win32 interface for using sockets. More...

#include <socket_traits_win32.hpp>

List of all members.

Public Types

typedef SOCKET 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 = INVALID_SOCKET
 Invalid socket descriptor.

Detailed Description

Win32 interface for using sockets.

Author:
Julien Jorge

Definition at line 46 of file socket_traits_win32.hpp.


Member Typedef Documentation

Type of the system description of the socket.

Definition at line 50 of file socket_traits_win32.hpp.


Member Function Documentation

static descriptor claw::socket_traits_win32::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 203 of file socket_traits_win32.hpp.

    {
      return ::accept( d, NULL, NULL );
    } // socket_traits_win32::accept()

static bool claw::socket_traits_win32::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 102 of file socket_traits_win32.hpp.

    {
      return ::closesocket(d) == 0; 
    } // socket_traits_win32::close()

static bool claw::socket_traits_win32::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 115 of file socket_traits_win32.hpp.

References CLAW_PRECOND, and invalid_socket.

    {
      CLAW_PRECOND( d != invalid_socket );

      bool result=false;
      struct hostent* hp = gethostbyname(address.c_str());

      if (hp)
  {
    struct sockaddr_in sa;

    memset (&sa, '\0', sizeof(sa));
    sa.sin_family = hp->h_addrtype;
    sa.sin_port = htons(port);
    memcpy( &sa.sin_addr, hp->h_addr, hp->h_length );
      
    if ( ::connect(d, (struct sockaddr*)&sa, sizeof(sa)) != SOCKET_ERROR )
      result = true;
  }

      return result;
    } // socket_traits_win32::connect()

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

Initialize the use of the socket library.

Returns:
true if the initialization is successful.

Definition at line 62 of file socket_traits_win32.hpp.

    {
      WORD version;
      WSADATA data;
 
      version = MAKEWORD( 2, 2 );
      
      return WSAStartup( version, &data ) == 0;
    } // socket_traits_win32::init()

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

Tell if a descriptor is a opened socket.

Parameters:
d The descriptor to test.

Definition at line 223 of file socket_traits_win32.hpp.

References valid_descriptor().

    {
      return valid_descriptor(d);
    } // socket_traits_win32::is_open()

static bool claw::socket_traits_win32::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 146 of file socket_traits_win32.hpp.

References CLAW_PRECOND, and invalid_socket.

    {
      CLAW_PRECOND( d != invalid_socket );

      struct sockaddr_in addr;

      memset (&addr, '\0', sizeof(addr));
      addr.sin_family = AF_INET;
      addr.sin_port = htons(port);
      addr.sin_addr.s_addr = htonl(INADDR_ANY);

      if ( bind(d, (struct sockaddr*)&addr, sizeof(addr)) != SOCKET_ERROR )
        return ::listen(d, queue_size) != SOCKET_ERROR;
      else
        return false;
    } // socket_traits_win32::connect()

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

Open a socket.

Returns:
The descriptor on the loaded socket.

Definition at line 87 of file socket_traits_win32.hpp.

References invalid_socket.

    {
      descriptor fd = invalid_socket;

      fd = socket(AF_INET, SOCK_STREAM, 0);

      return fd;
    } // socket_traits_win32::open()

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

Close the socket library.

Returns:
true if the operation is successful.

Definition at line 77 of file socket_traits_win32.hpp.

    {
      return WSACleanup() == 0;
    } // socket_traits_win32::release()

static bool claw::socket_traits_win32::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 172 of file socket_traits_win32.hpp.

References CLAW_PRECOND, and invalid_socket.

    {
      CLAW_PRECOND( d != invalid_socket );

      struct timeval tv, *ptv;
      fd_set fds;

      if ( time_limit < 0 )
        ptv = NULL;
      else
        {
          tv.tv_sec  = time_limit;
          tv.tv_usec = 0;
          
          ptv = &tv;
        }

      FD_ZERO(&fds);
      FD_SET(d, &fds);

      select( d+1, &fds, NULL, NULL, ptv );

      return FD_ISSET( d, &fds );
    } // socket_traits_win32::select_read()

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

Tell if a descriptor is a valid socket descriptor.

Parameters:
d The descriptor to test.

Definition at line 213 of file socket_traits_win32.hpp.

References invalid_socket.

Referenced by is_open().

    {
      return d != invalid_socket;
    } // socket_traits_win32::valid_descriptor()


Member Data Documentation

const descriptor claw::socket_traits_win32::invalid_socket = INVALID_SOCKET [static]

Invalid socket descriptor.

Definition at line 54 of file socket_traits_win32.hpp.

Referenced by connect(), listen(), open(), select_read(), and valid_descriptor().


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