Claw 1.7.3
socket_traits_win32.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_WIN32_HPP__
31#define __CLAW_SOCKET_TRAITS_WIN32_HPP__
32
33#include <sys/types.h>
34#include <winsock2.h>
35#include <sys/stat.h>
36#include <unistd.h>
37
38#include <claw/assert.hpp>
39
40namespace claw
41{
47 {
48 public:
50 typedef SOCKET descriptor;
51
52 public:
54 static const descriptor invalid_socket = INVALID_SOCKET;
55
56 public:
57 /*------------------------------------------------------------------------*/
62 static bool init()
63 {
64 WORD version;
65 WSADATA data;
66
67 version = MAKEWORD( 2, 2 );
68
69 return WSAStartup( version, &data ) == 0;
70 } // socket_traits_win32::init()
71
72 /*------------------------------------------------------------------------*/
77 static bool release()
78 {
79 return WSACleanup() == 0;
80 } // socket_traits_win32::release()
81
82 /*------------------------------------------------------------------------*/
88 {
90
91 fd = socket(AF_INET, SOCK_STREAM, 0);
92
93 return fd;
94 } // socket_traits_win32::open()
95
96 /*------------------------------------------------------------------------*/
102 static bool close( descriptor d )
103 {
104 return ::closesocket(d) == 0;
105 } // socket_traits_win32::close()
106
107 /*------------------------------------------------------------------------*/
115 static bool connect( descriptor d, const std::string& address, int port )
116 {
118
119 bool result=false;
120 struct hostent* hp = gethostbyname(address.c_str());
121
122 if (hp)
123 {
124 struct sockaddr_in sa;
125
126 memset (&sa, '\0', sizeof(sa));
127 sa.sin_family = hp->h_addrtype;
128 sa.sin_port = htons(port);
129 memcpy( &sa.sin_addr, hp->h_addr, hp->h_length );
130
131 if ( ::connect(d, (struct sockaddr*)&sa, sizeof(sa)) != SOCKET_ERROR )
132 result = true;
133 }
134
135 return result;
136 } // socket_traits_win32::connect()
137
138 /*------------------------------------------------------------------------*/
146 static bool listen( descriptor d, int port, unsigned int queue_size )
147 {
149
150 struct sockaddr_in addr;
151
152 memset (&addr, '\0', sizeof(addr));
153 addr.sin_family = AF_INET;
154 addr.sin_port = htons(port);
155 addr.sin_addr.s_addr = htonl(INADDR_ANY);
156
157 if ( bind(d, (struct sockaddr*)&addr, sizeof(addr)) != SOCKET_ERROR )
158 return ::listen(d, queue_size) != SOCKET_ERROR;
159 else
160 return false;
161 } // socket_traits_win32::connect()
162
163 /*------------------------------------------------------------------------*/
172 static bool select_read( descriptor d, int time_limit = -1 )
173 {
175
176 struct timeval tv, *ptv;
177 fd_set fds;
178
179 if ( time_limit < 0 )
180 ptv = NULL;
181 else
182 {
183 tv.tv_sec = time_limit;
184 tv.tv_usec = 0;
185
186 ptv = &tv;
187 }
188
189 FD_ZERO(&fds);
190 FD_SET(d, &fds);
191
192 select( d+1, &fds, NULL, NULL, ptv );
193
194 return FD_ISSET( d, &fds );
195 } // socket_traits_win32::select_read()
196
197 /*------------------------------------------------------------------------*/
204 {
205 return ::accept( d, NULL, NULL );
206 } // socket_traits_win32::accept()
207
208 /*------------------------------------------------------------------------*/
214 {
215 return d != invalid_socket;
216 } // socket_traits_win32::valid_descriptor()
217
218 /*------------------------------------------------------------------------*/
223 static bool is_open( descriptor d )
224 {
225 return valid_descriptor(d);
226 } // socket_traits_win32::is_open()
227
228 }; // class socket_traits_win32
229
230 typedef socket_traits_win32 socket_traits;
231} // namespace claw
232
233#endif // __CLAW_SOCKET_TRAITS_WIN32_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.
Win32 interface for using sockets.
static bool is_open(descriptor d)
Tell if a descriptor is a opened socket.
static bool valid_descriptor(descriptor d)
Tell if a descriptor is a valid socket descriptor.
static const descriptor invalid_socket
Invalid socket descriptor.
SOCKET descriptor
Type of the system description of the 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 descriptor accept(descriptor d)
Accept an incoming connexion.
static bool init()
Initialize the use of the socket library.
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 open()
Open a socket.
static bool release()
Close the socket library.
This is the main namespace.
Definition algorithm.hpp:34