claw
1.9.0
Toggle main menu visibility
Loading...
Searching...
No Matches
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@stuff-o-matic.com
24
*/
30
#ifndef __CLAW_SOCKET_TRAITS_WIN32_HPP__
31
#define __CLAW_SOCKET_TRAITS_WIN32_HPP__
32
33
#include <
claw/assert.hpp
>
34
35
#include <sys/stat.h>
36
#include <sys/types.h>
37
#include <unistd.h>
38
#include <winsock2.h>
39
40
namespace
claw
41
{
46
class
socket_traits_win32
47
{
48
public
:
50
typedef
SOCKET
descriptor
;
51
52
public
:
54
static
const
descriptor
invalid_socket
= INVALID_SOCKET;
55
56
public
:
61
static
bool
init
()
62
{
63
WORD version;
64
WSADATA data;
65
66
version = MAKEWORD(2, 2);
67
68
return
WSAStartup(version, &data) == 0;
69
}
70
75
static
bool
release
()
76
{
77
return
WSACleanup() == 0;
78
}
79
84
static
descriptor
open
()
85
{
86
descriptor
fd =
invalid_socket
;
87
88
fd = socket(AF_INET, SOCK_STREAM, 0);
89
90
return
fd;
91
}
92
98
static
bool
close
(
descriptor
d)
99
{
100
return ::closesocket(d) == 0;
101
}
102
110
static
bool
connect
(
descriptor
d,
const
std::string& address,
int
port)
111
{
112
CLAW_PRECOND
(d !=
invalid_socket
);
113
114
bool
result =
false
;
115
struct
hostent* hp = gethostbyname(address.c_str());
116
117
if
(hp)
118
{
119
struct
sockaddr_in sa;
120
121
memset(&sa,
'\0'
,
sizeof
(sa));
122
sa.sin_family = hp->h_addrtype;
123
sa.sin_port = htons(port);
124
memcpy(&sa.sin_addr, hp->h_addr, hp->h_length);
125
126
if
(
::connect
(d, (
struct
sockaddr*)&sa,
sizeof
(sa)) != SOCKET_ERROR)
127
result =
true
;
128
}
129
130
return
result;
131
}
132
140
static
bool
listen
(
descriptor
d,
int
port,
unsigned
int
queue_size)
141
{
142
CLAW_PRECOND
(d !=
invalid_socket
);
143
144
struct
sockaddr_in addr;
145
146
memset(&addr,
'\0'
,
sizeof
(addr));
147
addr.sin_family = AF_INET;
148
addr.sin_port = htons(port);
149
addr.sin_addr.s_addr = htonl(INADDR_ANY);
150
151
if
(bind(d, (
struct
sockaddr*)&addr,
sizeof
(addr)) != SOCKET_ERROR)
152
return ::listen(d, queue_size) != SOCKET_ERROR;
153
else
154
return
false
;
155
}
156
165
static
bool
select_read
(
descriptor
d,
int
time_limit = -1)
166
{
167
CLAW_PRECOND
(d !=
invalid_socket
);
168
169
struct
timeval tv, *ptv;
170
fd_set fds;
171
172
if
(time_limit < 0)
173
ptv = NULL;
174
else
175
{
176
tv.tv_sec = time_limit;
177
tv.tv_usec = 0;
178
179
ptv = &tv;
180
}
181
182
FD_ZERO(&fds);
183
FD_SET(d, &fds);
184
185
select(d + 1, &fds, NULL, NULL, ptv);
186
187
return
FD_ISSET(d, &fds);
188
}
189
195
static
descriptor
accept
(
descriptor
d)
196
{
197
return ::accept(d, NULL, NULL);
198
}
199
204
static
bool
valid_descriptor
(
descriptor
d)
205
{
206
return
d !=
invalid_socket
;
207
}
208
213
static
bool
is_open
(
descriptor
d)
214
{
215
return
valid_descriptor
(d);
216
}
217
218
};
// class socket_traits_win32
219
220
typedef
socket_traits_win32
socket_traits
;
221
}
222
223
#endif
// __CLAW_SOCKET_TRAITS_WIN32_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:94
socket_traits
Common interface for platform specific methods needed for using sockets.
claw::socket_traits_win32
Win32 interface for using sockets.
Definition
socket_traits_win32.hpp:47
claw::socket_traits_win32::is_open
static bool is_open(descriptor d)
Tell if a descriptor is a opened socket.
Definition
socket_traits_win32.hpp:213
claw::socket_traits_win32::valid_descriptor
static bool valid_descriptor(descriptor d)
Tell if a descriptor is a valid socket descriptor.
Definition
socket_traits_win32.hpp:204
claw::socket_traits_win32::invalid_socket
static const descriptor invalid_socket
Invalid socket descriptor.
Definition
socket_traits_win32.hpp:54
claw::socket_traits_win32::descriptor
SOCKET descriptor
Type of the system description of the socket.
Definition
socket_traits_win32.hpp:50
claw::socket_traits_win32::close
static bool close(descriptor d)
Close a socket.
Definition
socket_traits_win32.hpp:98
claw::socket_traits_win32::connect
static bool connect(descriptor d, const std::string &address, int port)
Connect a socket to a port.
Definition
socket_traits_win32.hpp:110
claw::socket_traits_win32::accept
static descriptor accept(descriptor d)
Accept an incoming connexion.
Definition
socket_traits_win32.hpp:195
claw::socket_traits_win32::init
static bool init()
Initialize the use of the socket library.
Definition
socket_traits_win32.hpp:61
claw::socket_traits_win32::listen
static bool listen(descriptor d, int port, unsigned int queue_size)
Open a socket for incoming connexions.
Definition
socket_traits_win32.hpp:140
claw::socket_traits_win32::select_read
static bool select_read(descriptor d, int time_limit=-1)
Select a socket for reading.
Definition
socket_traits_win32.hpp:165
claw::socket_traits_win32::open
static descriptor open()
Open a socket.
Definition
socket_traits_win32.hpp:84
claw::socket_traits_win32::release
static bool release()
Close the socket library.
Definition
socket_traits_win32.hpp:75
claw
This is the main namespace.
Definition
application.hpp:50
lib
net
include
claw
net
socket_traits_win32.hpp
Generated by
1.17.0