claw 1.9.0
 
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
40namespace claw
41{
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
85 {
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 {
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 {
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 {
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
196 {
197 return ::accept(d, NULL, NULL);
198 }
199
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__
Some assert macros to strengthen you code.
#define CLAW_PRECOND(b)
Abort the program if a precondition is not true.
Definition assert.hpp:94
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.