socket_traits_unix.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_UNIX_HPP__
#define __CLAW_SOCKET_TRAITS_UNIX_HPP__
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <netdb.h>
#include <unistd.h>
#include <cstring>
#include <claw/assert.hpp>
namespace claw
{
class socket_traits_unix
{
public:
typedef int descriptor;
public:
static const descriptor invalid_socket = -1;
public:
/*------------------------------------------------------------------------*/
static bool init()
{
return true;
} // socket_traits_unix::init()
/*------------------------------------------------------------------------*/
static bool release()
{
return true;
} // socket_traits_unix::release()
/*------------------------------------------------------------------------*/
static descriptor open()
{
descriptor fd = invalid_socket;
fd = socket(AF_INET, SOCK_STREAM, 0);
return fd;
} // socket_traits_unix::open()
/*------------------------------------------------------------------------*/
static bool close( descriptor d )
{
return ::close(d) == 0;
} // socket_traits_unix::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, (socklen_t)sizeof(sa)) != -1)
result = true;
}
return result;
} // socket_traits_unix::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)) != -1 )
return ::listen(d, queue_size) != -1;
else
return false;
} // socket_traits_unix::listen()
/*------------------------------------------------------------------------*/
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_unix::select_read()
/*------------------------------------------------------------------------*/
static descriptor accept( descriptor d )
{
return ::accept( d, NULL, NULL );
} // socket_traits_unix::accept()
/*------------------------------------------------------------------------*/
static bool valid_descriptor( descriptor d )
{
return d != invalid_socket;
} // socket_traits_unix::valid_descriptor()
/*------------------------------------------------------------------------*/
static bool is_open( descriptor d )
{
struct stat buf;
return fstat(d, &buf) == 0;
} // socket_traits_unix::is_open()
}; // class socket_traits_unix
typedef socket_traits_unix socket_traits;
} // namespace claw
#endif // __CLAW_SOCKET_TRAITS_UNIX_HPP__
assert.hpp
Some assert macros to strengthen you code.
CLAW_PRECOND
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.
Definition assert.hpp:98
socket_traits
Common interface for platform specific methods needed for using sockets.
claw::socket_traits_unix
Unix interface for using sockets.
Definition socket_traits_unix.hpp:51
claw::socket_traits_unix::accept
static descriptor accept(descriptor d)
Accept an incoming connexion.
Definition socket_traits_unix.hpp:202
claw::socket_traits_unix::is_open
static bool is_open(descriptor d)
Tell if a descriptor is a opened socket.
Definition socket_traits_unix.hpp:222
claw::socket_traits_unix::listen
static bool listen(descriptor d, int port, unsigned int queue_size)
Open a socket for incoming connexions.
Definition socket_traits_unix.hpp:145
claw::socket_traits_unix::open
static descriptor open()
Open a socket.
Definition socket_traits_unix.hpp:86
claw::socket_traits_unix::release
static bool release()
Close the socket library.
Definition socket_traits_unix.hpp:76
claw::socket_traits_unix::invalid_socket
static const descriptor invalid_socket
Invalid socket descriptor.
Definition socket_traits_unix.hpp:58
claw::socket_traits_unix::select_read
static bool select_read(descriptor d, int time_limit=-1)
Select a socket for reading.
Definition socket_traits_unix.hpp:171
claw::socket_traits_unix::descriptor
int descriptor
Type of the system description of the socket.
Definition socket_traits_unix.hpp:54
claw::socket_traits_unix::close
static bool close(descriptor d)
Close a socket.
Definition socket_traits_unix.hpp:101
claw::socket_traits_unix::init
static bool init()
Initialize the use of the socket library.
Definition socket_traits_unix.hpp:66
claw::socket_traits_unix::connect
static bool connect(descriptor d, const std::string &address, int port)
Connect a socket to a port.
Definition socket_traits_unix.hpp:114
claw::socket_traits_unix::valid_descriptor
static bool valid_descriptor(descriptor d)
Tell if a descriptor is a valid socket descriptor.
Definition socket_traits_unix.hpp:212
claw
This is the main namespace.
Definition algorithm.hpp:34