socket_traits_win32.hpp
Go to the documentation of this file.
/*
CLAW - a C++ Library Absolutely Wonderful
CLAW is a free library without any particular aim but being useful to
anyone.
Copyright (C) 2005-2011 Julien Jorge
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
contact: julien.jorge@gamned.org
*/
#ifndef __CLAW_SOCKET_TRAITS_WIN32_HPP__
#define __CLAW_SOCKET_TRAITS_WIN32_HPP__
#include <sys/types.h>
#include <winsock2.h>
#include <sys/stat.h>
#include <unistd.h>
#include <claw/assert.hpp>
namespace claw
{
class socket_traits_win32
{
public:
typedef SOCKET descriptor;
public:
static const descriptor invalid_socket = INVALID_SOCKET;
public:
/*------------------------------------------------------------------------*/
static bool init()
{
WORD version;
WSADATA data;
version = MAKEWORD( 2, 2 );
return WSAStartup( version, &data ) == 0;
} // socket_traits_win32::init()
/*------------------------------------------------------------------------*/
static bool release()
{
return WSACleanup() == 0;
} // socket_traits_win32::release()
/*------------------------------------------------------------------------*/
static descriptor open()
{
descriptor fd = invalid_socket;
fd = socket(AF_INET, SOCK_STREAM, 0);
return fd;
} // socket_traits_win32::open()
/*------------------------------------------------------------------------*/
static bool close( descriptor d )
{
return ::closesocket(d) == 0;
} // socket_traits_win32::close()
/*------------------------------------------------------------------------*/
static bool connect( descriptor d, const std::string& address, int port )
{
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 listen( descriptor d, int port, unsigned int queue_size )
{
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 bool select_read( descriptor d, int time_limit = -1 )
{
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 descriptor accept( descriptor d )
{
return ::accept( d, NULL, NULL );
} // socket_traits_win32::accept()
/*------------------------------------------------------------------------*/
static bool valid_descriptor( descriptor d )
{
return d != invalid_socket;
} // socket_traits_win32::valid_descriptor()
/*------------------------------------------------------------------------*/
static bool is_open( descriptor d )
{
return valid_descriptor(d);
} // socket_traits_win32::is_open()
}; // class socket_traits_win32
typedef socket_traits_win32 socket_traits;
} // namespace claw
#endif // __CLAW_SOCKET_TRAITS_WIN32_HPP__
claw::socket_traits_win32::connect
static bool connect(descriptor d, const std::string &address, int port)
Connect a socket to a port.
Definition: socket_traits_win32.hpp:115
claw::socket_traits_win32::open
static descriptor open()
Open a socket.
Definition: socket_traits_win32.hpp:87
claw::socket_traits_win32::valid_descriptor
static bool valid_descriptor(descriptor d)
Tell if a descriptor is a valid socket descriptor.
Definition: socket_traits_win32.hpp:213
claw::socket_traits_win32
Win32 interface for using sockets.
Definition: socket_traits_win32.hpp:46
claw::socket_traits_win32::descriptor
SOCKET descriptor
Type of the system description of the socket.
Definition: socket_traits_win32.hpp:50
claw::socket_traits_win32::accept
static descriptor accept(descriptor d)
Accept an incoming connexion.
Definition: socket_traits_win32.hpp:203
claw::socket_traits_win32::listen
static bool listen(descriptor d, int port, unsigned int queue_size)
Open a socket for incoming connexions.
Definition: socket_traits_win32.hpp:146
claw::socket_traits_unix
Unix interface for using sockets.
Definition: socket_traits_unix.hpp:50
claw::socket_traits_win32::close
static bool close(descriptor d)
Close a socket.
Definition: socket_traits_win32.hpp:102
claw::socket_traits_win32::invalid_socket
static const descriptor invalid_socket
Invalid socket descriptor.
Definition: socket_traits_win32.hpp:54
claw::socket_traits_win32::init
static bool init()
Initialize the use of the socket library.
Definition: socket_traits_win32.hpp:62
claw::socket_traits_win32::release
static bool release()
Close the socket library.
Definition: socket_traits_win32.hpp:77
claw::socket_traits_win32::is_open
static bool is_open(descriptor d)
Tell if a descriptor is a opened socket.
Definition: socket_traits_win32.hpp:223
assert.hpp
Some assert macros to strengthen you code.
claw
This is the main namespace.
Definition: algorithm.hpp:33
CLAW_PRECOND
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.
Definition: assert.hpp:98
claw::socket_traits_win32::select_read
static bool select_read(descriptor d, int time_limit=-1)
Select a socket for reading.
Definition: socket_traits_win32.hpp:172