diff options
Diffstat (limited to '')
-rw-r--r-- | src/serversocket.cpp | 33 | ||||
-rw-r--r-- | src/serversocket.h | 11 | ||||
-rw-r--r-- | src/socket.cpp | 21 | ||||
-rw-r--r-- | src/socket.h | 4 | ||||
-rw-r--r-- | src/win32_compatibility.h | 27 |
5 files changed, 76 insertions, 20 deletions
diff --git a/src/serversocket.cpp b/src/serversocket.cpp index 6f7fc00..5ed661e 100644 --- a/src/serversocket.cpp +++ b/src/serversocket.cpp | |||
@@ -5,6 +5,13 @@ | |||
5 | * terms of the license contained in the file LICENSE. | 5 | * terms of the license contained in the file LICENSE. |
6 | */ | 6 | */ |
7 | 7 | ||
8 | #ifndef WIN32 | ||
9 | #include <sys/socket.h> | ||
10 | #include <netinet/in.h> | ||
11 | #include <netdb.h> | ||
12 | #include <arpa/inet.h> | ||
13 | #endif | ||
14 | |||
8 | #include <time.h> | 15 | #include <time.h> |
9 | #include <string.h> | 16 | #include <string.h> |
10 | #include <stdio.h> | 17 | #include <stdio.h> |
@@ -12,14 +19,11 @@ | |||
12 | #include <stdlib.h> | 19 | #include <stdlib.h> |
13 | #include <unistd.h> | 20 | #include <unistd.h> |
14 | #include <sys/types.h> | 21 | #include <sys/types.h> |
15 | #include <sys/socket.h> | 22 | //#include <termios.h> |
16 | #include <termios.h> | ||
17 | #include <netinet/in.h> | ||
18 | #include <netdb.h> | ||
19 | #include <arpa/inet.h> | ||
20 | #include <fcntl.h> | 23 | #include <fcntl.h> |
21 | #include "bu/serversocket.h" | 24 | #include "bu/serversocket.h" |
22 | #include "bu/osx_compatibility.h" | 25 | #include "bu/osx_compatibility.h" |
26 | #include "bu/win32_compatibility.h" | ||
23 | 27 | ||
24 | namespace Bu { subExceptionDef( ServerSocketException ) } | 28 | namespace Bu { subExceptionDef( ServerSocketException ) } |
25 | 29 | ||
@@ -50,7 +54,11 @@ Bu::ServerSocket::ServerSocket(const FString &sAddr,int nPort, int nPoolSize) : | |||
50 | name.sin_family = AF_INET; | 54 | name.sin_family = AF_INET; |
51 | name.sin_port = htons( nPort ); | 55 | name.sin_port = htons( nPort ); |
52 | 56 | ||
57 | #ifdef WIN32 | ||
58 | name.sin_addr.s_addr = inet_addr( sAddr.getStr() ); | ||
59 | #else | ||
53 | inet_aton( sAddr.getStr(), &name.sin_addr ); | 60 | inet_aton( sAddr.getStr(), &name.sin_addr ); |
61 | #endif | ||
54 | 62 | ||
55 | startServer( name, nPoolSize ); | 63 | startServer( name, nPoolSize ); |
56 | } | 64 | } |
@@ -139,14 +147,17 @@ int Bu::ServerSocket::accept( int nTimeoutSec, int nTimeoutUSec ) | |||
139 | "Error accepting a new connection: %s", strerror( errno ) | 147 | "Error accepting a new connection: %s", strerror( errno ) |
140 | ); | 148 | ); |
141 | } | 149 | } |
150 | |||
151 | #ifndef WIN32 | ||
142 | char tmpa[20]; | 152 | char tmpa[20]; |
143 | inet_ntop( AF_INET, (void *)&clientname.sin_addr, tmpa, 20 ); | 153 | inet_ntop( AF_INET, (void *)&clientname.sin_addr, tmpa, 20 ); |
144 | //"New connection from host %s, port %hd.", | 154 | //"New connection from host %s, port %hd.", |
145 | // tmpa, ntohs (clientname.sin_port) ); | 155 | // tmpa, ntohs (clientname.sin_port) ); |
156 | #endif | ||
146 | 157 | ||
147 | { | 158 | { |
159 | #ifndef WIN32 | ||
148 | int flags; | 160 | int flags; |
149 | |||
150 | flags = fcntl( nClient, F_GETFL, 0 ); | 161 | flags = fcntl( nClient, F_GETFL, 0 ); |
151 | flags |= O_NONBLOCK; | 162 | flags |= O_NONBLOCK; |
152 | if( fcntl( nClient, F_SETFL, flags ) < 0) | 163 | if( fcntl( nClient, F_SETFL, flags ) < 0) |
@@ -156,6 +167,16 @@ int Bu::ServerSocket::accept( int nTimeoutSec, int nTimeoutUSec ) | |||
156 | strerror( errno ) | 167 | strerror( errno ) |
157 | ); | 168 | ); |
158 | } | 169 | } |
170 | #else | ||
171 | //------------------------- | ||
172 | // Set the socket I/O mode: In this case FIONBIO | ||
173 | // enables or disables the blocking mode for the | ||
174 | // socket based on the numerical value of iMode. | ||
175 | // If iMode = 0, blocking is enabled; | ||
176 | // If iMode != 0, non-blocking mode is enabled. | ||
177 | u_long iMode = 1; | ||
178 | ioctlsocket(nClient, FIONBIO, &iMode); | ||
179 | #endif | ||
159 | } | 180 | } |
160 | 181 | ||
161 | return nClient; | 182 | return nClient; |
diff --git a/src/serversocket.h b/src/serversocket.h index 1742786..168c5e1 100644 --- a/src/serversocket.h +++ b/src/serversocket.h | |||
@@ -11,7 +11,12 @@ | |||
11 | #include <stdint.h> | 11 | #include <stdint.h> |
12 | #include "bu/fstring.h" | 12 | #include "bu/fstring.h" |
13 | #include "bu/exceptionbase.h" | 13 | #include "bu/exceptionbase.h" |
14 | #include <sys/select.h> | 14 | |
15 | #ifdef WIN32 | ||
16 | #include <Winsock2.h> | ||
17 | #else | ||
18 | #include <sys/select.h> | ||
19 | #endif | ||
15 | 20 | ||
16 | namespace Bu | 21 | namespace Bu |
17 | { | 22 | { |
@@ -45,7 +50,11 @@ namespace Bu | |||
45 | void startServer( struct sockaddr_in &name, int nPoolSize ); | 50 | void startServer( struct sockaddr_in &name, int nPoolSize ); |
46 | 51 | ||
47 | fd_set fdActive; | 52 | fd_set fdActive; |
53 | #ifdef WIN32 | ||
54 | unsigned int nServer; | ||
55 | #else | ||
48 | int nServer; | 56 | int nServer; |
57 | #endif | ||
49 | int nPort; | 58 | int nPort; |
50 | }; | 59 | }; |
51 | } | 60 | } |
diff --git a/src/socket.cpp b/src/socket.cpp index 1e9a2f9..aea66f8 100644 --- a/src/socket.cpp +++ b/src/socket.cpp | |||
@@ -16,6 +16,7 @@ | |||
16 | #include <fcntl.h> | 16 | #include <fcntl.h> |
17 | #include "socket.h" | 17 | #include "socket.h" |
18 | #include "osx_compatibility.h" | 18 | #include "osx_compatibility.h" |
19 | #include "win32_compatibility.h" | ||
19 | 20 | ||
20 | #ifndef WIN32 | 21 | #ifndef WIN32 |
21 | #include <sys/socket.h> | 22 | #include <sys/socket.h> |
@@ -194,11 +195,7 @@ void Bu::Socket::read() | |||
194 | 195 | ||
195 | size_t Bu::Socket::read( void *pBuf, size_t nBytes ) | 196 | size_t Bu::Socket::read( void *pBuf, size_t nBytes ) |
196 | { | 197 | { |
197 | #ifndef WIN32 | ||
198 | int nRead = TEMP_FAILURE_RETRY( ::read( nSocket, pBuf, nBytes ) ); | 198 | int nRead = TEMP_FAILURE_RETRY( ::read( nSocket, pBuf, nBytes ) ); |
199 | #else | ||
200 | int nRead = ::read( nSocket, pBuf, nBytes ); | ||
201 | #endif | ||
202 | if( nRead < 0 ) | 199 | if( nRead < 0 ) |
203 | { | 200 | { |
204 | throw SocketException( SocketException::cRead, strerror(errno) ); | 201 | throw SocketException( SocketException::cRead, strerror(errno) ); |
@@ -209,7 +206,7 @@ size_t Bu::Socket::read( void *pBuf, size_t nBytes ) | |||
209 | size_t Bu::Socket::read( void *pBuf, size_t nBytes, | 206 | size_t Bu::Socket::read( void *pBuf, size_t nBytes, |
210 | uint32_t nSec, uint32_t nUSec ) | 207 | uint32_t nSec, uint32_t nUSec ) |
211 | { | 208 | { |
212 | struct timeval tv, nt, ct; | 209 | struct timeval tv; |
213 | size_t nRead = 0; | 210 | size_t nRead = 0; |
214 | 211 | ||
215 | fd_set rfds; | 212 | fd_set rfds; |
@@ -217,6 +214,7 @@ size_t Bu::Socket::read( void *pBuf, size_t nBytes, | |||
217 | FD_SET(nSocket, &rfds); | 214 | FD_SET(nSocket, &rfds); |
218 | 215 | ||
219 | #ifndef WIN32 | 216 | #ifndef WIN32 |
217 | struct timeval nt, ct; | ||
220 | gettimeofday( &nt, NULL ); | 218 | gettimeofday( &nt, NULL ); |
221 | nt.tv_sec += nSec; | 219 | nt.tv_sec += nSec; |
222 | nt.tv_usec += nUSec; | 220 | nt.tv_usec += nUSec; |
@@ -251,11 +249,7 @@ size_t Bu::Socket::read( void *pBuf, size_t nBytes, | |||
251 | 249 | ||
252 | size_t Bu::Socket::write( const void *pBuf, size_t nBytes ) | 250 | size_t Bu::Socket::write( const void *pBuf, size_t nBytes ) |
253 | { | 251 | { |
254 | #ifndef WIN32 | ||
255 | int nWrote = TEMP_FAILURE_RETRY( ::write( nSocket, pBuf, nBytes ) ); | 252 | int nWrote = TEMP_FAILURE_RETRY( ::write( nSocket, pBuf, nBytes ) ); |
256 | #else | ||
257 | int nWrote = ::write( nSocket, pBuf, nBytes ); | ||
258 | #endif | ||
259 | if( nWrote < 0 ) | 253 | if( nWrote < 0 ) |
260 | { | 254 | { |
261 | if( errno == EAGAIN ) return 0; | 255 | if( errno == EAGAIN ) return 0; |
@@ -266,7 +260,7 @@ size_t Bu::Socket::write( const void *pBuf, size_t nBytes ) | |||
266 | 260 | ||
267 | size_t Bu::Socket::write( const void *pBuf, size_t nBytes, uint32_t nSec, uint32_t nUSec ) | 261 | size_t Bu::Socket::write( const void *pBuf, size_t nBytes, uint32_t nSec, uint32_t nUSec ) |
268 | { | 262 | { |
269 | struct timeval tv, nt, ct; | 263 | struct timeval tv; |
270 | size_t nWrote = 0; | 264 | size_t nWrote = 0; |
271 | 265 | ||
272 | fd_set wfds; | 266 | fd_set wfds; |
@@ -274,6 +268,7 @@ size_t Bu::Socket::write( const void *pBuf, size_t nBytes, uint32_t nSec, uint32 | |||
274 | FD_SET(nSocket, &wfds); | 268 | FD_SET(nSocket, &wfds); |
275 | 269 | ||
276 | #ifndef WIN32 | 270 | #ifndef WIN32 |
271 | struct timeval nt, ct; | ||
277 | gettimeofday( &nt, NULL ); | 272 | gettimeofday( &nt, NULL ); |
278 | nt.tv_sec += nSec; | 273 | nt.tv_sec += nSec; |
279 | nt.tv_usec += nUSec; | 274 | nt.tv_usec += nUSec; |
@@ -421,9 +416,9 @@ bool Bu::Socket::isOpen() | |||
421 | return bActive; | 416 | return bActive; |
422 | } | 417 | } |
423 | 418 | ||
424 | #ifdef WIN32 | 419 | //#ifdef WIN32 |
425 | typedef int socklen_t; | 420 | // typedef int socklen_t; |
426 | #endif | 421 | //#endif |
427 | 422 | ||
428 | void Bu::Socket::setAddress() | 423 | void Bu::Socket::setAddress() |
429 | { | 424 | { |
diff --git a/src/socket.h b/src/socket.h index 337b797..572c2ad 100644 --- a/src/socket.h +++ b/src/socket.h | |||
@@ -73,7 +73,11 @@ namespace Bu | |||
73 | private: | 73 | private: |
74 | void setAddress(); | 74 | void setAddress(); |
75 | 75 | ||
76 | #ifdef WIN32 | ||
77 | unsigned int nSocket; | ||
78 | #else | ||
76 | int nSocket; | 79 | int nSocket; |
80 | #endif | ||
77 | bool bActive; | 81 | bool bActive; |
78 | FString sReadBuf; | 82 | FString sReadBuf; |
79 | FString sAddress; | 83 | FString sAddress; |
diff --git a/src/win32_compatibility.h b/src/win32_compatibility.h new file mode 100644 index 0000000..a1b9c97 --- /dev/null +++ b/src/win32_compatibility.h | |||
@@ -0,0 +1,27 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2008 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #ifndef WIN32_COMPATIBILITY__H | ||
9 | #define WIN32_COMPATIBILITY__H | ||
10 | |||
11 | #ifdef WIN32 | ||
12 | |||
13 | #ifndef TEMP_FAILURE_RETRY | ||
14 | #define TEMP_FAILURE_RETRY(expression) \ | ||
15 | (__extension__ \ | ||
16 | ({ long int __result; \ | ||
17 | do __result = (long int) (expression); \ | ||
18 | while (__result == -1L && errno == EINTR); \ | ||
19 | __result; })) | ||
20 | #endif | ||
21 | |||
22 | //__extension__ typedef unsigned int socklen_t; | ||
23 | __extension__ typedef int socklen_t; | ||
24 | |||
25 | #endif /* WIN32 */ | ||
26 | #endif | ||
27 | |||