|
CLAW Library (a C++ Library Absolutely Wonderful) 1.5.5
|
Unix interface for using sockets. More...
#include <socket_traits_unix.hpp>
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. | |
Unix interface for using sockets.
Definition at line 49 of file socket_traits_unix.hpp.
| typedef int claw::socket_traits_unix::descriptor |
Type of the system description of the socket.
Definition at line 53 of file socket_traits_unix.hpp.
| static descriptor claw::socket_traits_unix::accept | ( | descriptor | d | ) | [inline, static] |
Accept an incoming connexion.
| d | The descriptor of the socket to listen. |
Definition at line 201 of file socket_traits_unix.hpp.
Referenced by claw::net::socket_server::accept().
{
return ::accept( d, NULL, NULL );
} // socket_traits_unix::accept()
| static bool claw::socket_traits_unix::close | ( | descriptor | d | ) | [inline, static] |
Close a socket.
| d | The descriptor of the socket to close. |
Definition at line 100 of file socket_traits_unix.hpp.
Referenced by claw::net::basic_socket::close().
{
return ::close(d) == 0;
} // 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.
| d | The descriptor of the socket to connect. |
| address | The adress to connect to. |
| port | The port to connect to. |
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().
{
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 claw::socket_traits_unix::init | ( | ) | [inline, static] |
Initialize the use of the socket library.
Definition at line 65 of file socket_traits_unix.hpp.
Referenced by claw::net::socket_server::init().
{
return true;
} // socket_traits_unix::init()
| static bool claw::socket_traits_unix::is_open | ( | descriptor | d | ) | [inline, static] |
Tell if a descriptor is a opened socket.
| d | The descriptor to test. |
Definition at line 221 of file socket_traits_unix.hpp.
Referenced by claw::net::basic_socketbuf< CharT, Traits >::open().
{
struct stat buf;
return fstat(d, &buf) == 0;
} // 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.
| d | The descriptor of the socket to open. |
| port | The port to connect to. |
| queue_size | The size of the queue for incoming connexions. |
Definition at line 144 of file socket_traits_unix.hpp.
References CLAW_PRECOND, and invalid_socket.
Referenced by claw::net::socket_server::open().
{
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::connect()
| static descriptor claw::socket_traits_unix::open | ( | ) | [inline, static] |
Open a socket.
Definition at line 85 of file socket_traits_unix.hpp.
References invalid_socket.
Referenced by claw::net::basic_socket::open().
{
descriptor fd = invalid_socket;
fd = socket(AF_INET, SOCK_STREAM, 0);
return fd;
} // socket_traits_unix::open()
| static bool claw::socket_traits_unix::release | ( | ) | [inline, static] |
Close the socket library.
Definition at line 75 of file socket_traits_unix.hpp.
Referenced by claw::net::socket_server::release().
{
return true;
} // 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.
| 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. |
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().
{
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 bool claw::socket_traits_unix::valid_descriptor | ( | descriptor | d | ) | [inline, static] |
Tell if a descriptor is a valid socket descriptor.
| 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().
{
return d != invalid_socket;
} // socket_traits_unix::valid_descriptor()
const descriptor claw::socket_traits_unix::invalid_socket = -1 [static] |
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().
1.7.3