Claw 1.7.3
socket_traits_unix.hpp
Go to the documentation of this file.
1/*
2 CLAW - a C++ Library Absolutely Wonderful
3
4 CLAW is a free library without any particular aim but being useful to
5 anyone.
6
7 Copyright (C) 2005-2011 Julien Jorge
8
9 This library is free software; you can redistribute it and/or
10 modify it under the terms of the GNU Lesser General Public
11 License as published by the Free Software Foundation; either
12 version 2.1 of the License, or (at your option) any later version.
13
14 This library is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public
20 License along with this library; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22
23 contact: julien.jorge@gamned.org
24*/
30#ifndef __CLAW_SOCKET_TRAITS_UNIX_HPP__
31#define __CLAW_SOCKET_TRAITS_UNIX_HPP__
32
33#include <sys/types.h>
34#include <sys/socket.h>
35#include <sys/stat.h>
36#include <netinet/in.h>
37#include <netinet/tcp.h>
38#include <netdb.h>
39#include <unistd.h>
40#include <cstring>
41
42#include <claw/assert.hpp>
43
44namespace claw
45{
51 {
52 public:
54 typedef int descriptor;
55
56 public:
58 static const descriptor invalid_socket = -1;
59
60 public:
61 /*------------------------------------------------------------------------*/
66 static bool init()
67 {
68 return true;
69 } // socket_traits_unix::init()
70
71 /*------------------------------------------------------------------------*/
76 static bool release()
77 {
78 return true;
79 } // socket_traits_unix::release()
80
81 /*------------------------------------------------------------------------*/
87 {
89
90 fd = socket(AF_INET, SOCK_STREAM, 0);
91
92 return fd;
93 } // socket_traits_unix::open()
94
95 /*------------------------------------------------------------------------*/
101 static bool close( descriptor d )
102 {
103 return ::close(d) == 0;
104 } // socket_traits_unix::close()
105
106 /*------------------------------------------------------------------------*/
114 static bool connect( descriptor d, const std::string& address, int port )
115 {
117
118 bool result = false;
119 struct hostent* hp = gethostbyname(address.c_str());
120
121 if (hp)
122 {
123 struct sockaddr_in sa;
124
125 memset (&sa, '\0', sizeof(sa));
126 sa.sin_family = hp->h_addrtype;
127 sa.sin_port = htons(port);
128 memcpy( &sa.sin_addr, hp->h_addr, hp->h_length );
129
130 if (::connect(d, (struct sockaddr*)&sa, (socklen_t)sizeof(sa)) != -1)
131 result = true;
132 }
133
134 return result;
135 } // socket_traits_unix::connect()
136
137 /*------------------------------------------------------------------------*/
145 static bool listen( descriptor d, int port, unsigned int queue_size )
146 {
148
149 struct sockaddr_in addr;
150
151 memset (&addr, '\0', sizeof(addr));
152 addr.sin_family = AF_INET;
153 addr.sin_port = htons(port);
154 addr.sin_addr.s_addr = htonl(INADDR_ANY);
155
156 if ( bind(d, (struct sockaddr*)&addr, sizeof(addr)) != -1 )
157 return ::listen(d, queue_size) != -1;
158 else
159 return false;
160 } // socket_traits_unix::listen()
161
162 /*------------------------------------------------------------------------*/
171 static bool select_read( descriptor d, int time_limit = -1 )
172 {
174
175 struct timeval tv, *ptv;
176 fd_set fds;
177
178 if ( time_limit < 0 )
179 ptv = NULL;
180 else
181 {
182 tv.tv_sec = time_limit;
183 tv.tv_usec = 0;
184
185 ptv = &tv;
186 }
187
188 FD_ZERO(&fds);
189 FD_SET(d, &fds);
190
191 select( d+1, &fds, NULL, NULL, ptv );
192
193 return FD_ISSET( d, &fds );
194 } // socket_traits_unix::select_read()
195
196 /*------------------------------------------------------------------------*/
203 {
204 return ::accept( d, NULL, NULL );
205 } // socket_traits_unix::accept()
206
207 /*------------------------------------------------------------------------*/
213 {
214 return d != invalid_socket;
215 } // socket_traits_unix::valid_descriptor()
216
217 /*------------------------------------------------------------------------*/
222 static bool is_open( descriptor d )
223 {
224 struct stat buf;
225
226 return fstat(d, &buf) == 0;
227 } // socket_traits_unix::is_open()
228
229 }; // class socket_traits_unix
230
231 typedef socket_traits_unix socket_traits;
232} // namespace claw
233
234#endif // __CLAW_SOCKET_TRAITS_UNIX_HPP__
Some assert macros to strengthen you code.
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.
Definition assert.hpp:98
Common interface for platform specific methods needed for using sockets.
Unix interface for using sockets.
static descriptor accept(descriptor d)
Accept an incoming connexion.
static bool is_open(descriptor d)
Tell if a descriptor is a opened socket.
int descriptor
Type of the system description of the socket.
static bool listen(descriptor d, int port, unsigned int queue_size)
Open a socket for incoming connexions.
static descriptor open()
Open a socket.
static bool release()
Close the socket library.
static const descriptor invalid_socket
Invalid socket descriptor.
static bool select_read(descriptor d, int time_limit=-1)
Select a socket for reading.
static bool close(descriptor d)
Close a socket.
static bool init()
Initialize the use of the socket library.
static bool connect(descriptor d, const std::string &address, int port)
Connect a socket to a port.
static bool valid_descriptor(descriptor d)
Tell if a descriptor is a valid socket descriptor.
This is the main namespace.
Definition algorithm.hpp:34