claw  1.9.0
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@stuff-o-matic.com
24 */
30 #ifndef __CLAW_SOCKET_TRAITS_UNIX_HPP__
31 #define __CLAW_SOCKET_TRAITS_UNIX_HPP__
32 
33 #include <claw/assert.hpp>
34 
35 #include <cstring>
36 #include <netdb.h>
37 #include <netinet/in.h>
38 #include <netinet/tcp.h>
39 #include <sys/socket.h>
40 #include <sys/stat.h>
41 #include <sys/types.h>
42 #include <unistd.h>
43 
44 namespace claw
45 {
51  {
52  public:
54  typedef int descriptor;
55 
56  public:
58  static const descriptor invalid_socket = -1;
59 
60  public:
65  static bool init()
66  {
67  return true;
68  }
69 
74  static bool release()
75  {
76  return true;
77  }
78 
83  static descriptor open()
84  {
86 
87  fd = socket(AF_INET, SOCK_STREAM, 0);
88 
89  return fd;
90  }
91 
97  static bool close(descriptor d)
98  {
99  return ::close(d) == 0;
100  }
101 
109  static bool connect(descriptor d, const std::string& address, int port)
110  {
112 
113  bool result = false;
114  struct hostent* hp = gethostbyname(address.c_str());
115 
116  if(hp)
117  {
118  struct sockaddr_in sa;
119 
120  memset(&sa, '\0', sizeof(sa));
121  sa.sin_family = hp->h_addrtype;
122  sa.sin_port = htons(port);
123  memcpy(&sa.sin_addr, hp->h_addr, hp->h_length);
124 
125  if(::connect(d, (struct sockaddr*)&sa, (socklen_t)sizeof(sa)) != -1)
126  result = true;
127  }
128 
129  return result;
130  }
131 
139  static bool listen(descriptor d, int port, unsigned int queue_size)
140  {
142 
143  struct sockaddr_in addr;
144 
145  memset(&addr, '\0', sizeof(addr));
146  addr.sin_family = AF_INET;
147  addr.sin_port = htons(port);
148  addr.sin_addr.s_addr = htonl(INADDR_ANY);
149 
150  if(bind(d, (struct sockaddr*)&addr, sizeof(addr)) != -1)
151  return ::listen(d, queue_size) != -1;
152  else
153  return false;
154  }
155 
164  static bool select_read(descriptor d, int time_limit = -1)
165  {
167 
168  struct timeval tv, *ptv;
169  fd_set fds;
170 
171  if(time_limit < 0)
172  ptv = NULL;
173  else
174  {
175  tv.tv_sec = time_limit;
176  tv.tv_usec = 0;
177 
178  ptv = &tv;
179  }
180 
181  FD_ZERO(&fds);
182  FD_SET(d, &fds);
183 
184  select(d + 1, &fds, NULL, NULL, ptv);
185 
186  return FD_ISSET(d, &fds);
187  }
188 
195  {
196  return ::accept(d, NULL, NULL);
197  }
198 
204  {
205  return d != invalid_socket;
206  }
207 
212  static bool is_open(descriptor d)
213  {
214  struct stat buf;
215 
216  return fstat(d, &buf) == 0;
217  }
218 
219  }; // class socket_traits_unix
220 
221  typedef socket_traits_unix socket_traits;
222 }
223 
224 #endif // __CLAW_SOCKET_TRAITS_UNIX_HPP__
static descriptor accept(descriptor d)
Accept an incoming connexion.
static const descriptor invalid_socket
Invalid socket descriptor.
static bool valid_descriptor(descriptor d)
Tell if a descriptor is a valid socket descriptor.
int descriptor
Type of the system description of the socket.
Unix interface for using sockets.
static bool close(descriptor d)
Close a socket.
static bool init()
Initialize the use of the socket library.
static descriptor open()
Open a socket.
static bool is_open(descriptor d)
Tell if a descriptor is a opened socket.
static bool listen(descriptor d, int port, unsigned int queue_size)
Open a socket for incoming connexions.
Some assert macros to strengthen you code.
static bool connect(descriptor d, const std::string &address, int port)
Connect a socket to a port.
static bool select_read(descriptor d, int time_limit=-1)
Select a socket for reading.
static bool release()
Close the socket library.
This is the main namespace.
Definition: application.hpp:49
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.
Definition: assert.hpp:94