From 566dd25530779388859f8a21191e60c62a21cd5f Mon Sep 17 00:00:00 2001 From: David Date: Fri, 24 Oct 2008 19:12:49 +0000 Subject: david - ok, fixed the compiler warnings in socket class. got serversocket compliling without warnings. added win32_compatibility.h along the same lines as osx_copatibility.h --- src/serversocket.cpp | 33 +++++++++++++++++++++++++++------ src/serversocket.h | 11 ++++++++++- src/socket.cpp | 21 ++++++++------------- src/socket.h | 4 ++++ src/win32_compatibility.h | 27 +++++++++++++++++++++++++++ 5 files changed, 76 insertions(+), 20 deletions(-) create mode 100644 src/win32_compatibility.h (limited to 'src') 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 @@ * terms of the license contained in the file LICENSE. */ +#ifndef WIN32 + #include + #include + #include + #include +#endif + #include #include #include @@ -12,14 +19,11 @@ #include #include #include -#include -#include -#include -#include -#include +//#include #include #include "bu/serversocket.h" #include "bu/osx_compatibility.h" +#include "bu/win32_compatibility.h" namespace Bu { subExceptionDef( ServerSocketException ) } @@ -50,7 +54,11 @@ Bu::ServerSocket::ServerSocket(const FString &sAddr,int nPort, int nPoolSize) : name.sin_family = AF_INET; name.sin_port = htons( nPort ); +#ifdef WIN32 + name.sin_addr.s_addr = inet_addr( sAddr.getStr() ); +#else inet_aton( sAddr.getStr(), &name.sin_addr ); +#endif startServer( name, nPoolSize ); } @@ -139,14 +147,17 @@ int Bu::ServerSocket::accept( int nTimeoutSec, int nTimeoutUSec ) "Error accepting a new connection: %s", strerror( errno ) ); } + +#ifndef WIN32 char tmpa[20]; inet_ntop( AF_INET, (void *)&clientname.sin_addr, tmpa, 20 ); //"New connection from host %s, port %hd.", // tmpa, ntohs (clientname.sin_port) ); +#endif { +#ifndef WIN32 int flags; - flags = fcntl( nClient, F_GETFL, 0 ); flags |= O_NONBLOCK; if( fcntl( nClient, F_SETFL, flags ) < 0) @@ -156,6 +167,16 @@ int Bu::ServerSocket::accept( int nTimeoutSec, int nTimeoutUSec ) strerror( errno ) ); } +#else + //------------------------- + // Set the socket I/O mode: In this case FIONBIO + // enables or disables the blocking mode for the + // socket based on the numerical value of iMode. + // If iMode = 0, blocking is enabled; + // If iMode != 0, non-blocking mode is enabled. + u_long iMode = 1; + ioctlsocket(nClient, FIONBIO, &iMode); +#endif } 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 @@ #include #include "bu/fstring.h" #include "bu/exceptionbase.h" -#include + +#ifdef WIN32 + #include +#else + #include +#endif namespace Bu { @@ -45,7 +50,11 @@ namespace Bu void startServer( struct sockaddr_in &name, int nPoolSize ); fd_set fdActive; +#ifdef WIN32 + unsigned int nServer; +#else int nServer; +#endif int nPort; }; } 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 @@ #include #include "socket.h" #include "osx_compatibility.h" +#include "win32_compatibility.h" #ifndef WIN32 #include @@ -194,11 +195,7 @@ void Bu::Socket::read() size_t Bu::Socket::read( void *pBuf, size_t nBytes ) { -#ifndef WIN32 int nRead = TEMP_FAILURE_RETRY( ::read( nSocket, pBuf, nBytes ) ); -#else - int nRead = ::read( nSocket, pBuf, nBytes ); -#endif if( nRead < 0 ) { throw SocketException( SocketException::cRead, strerror(errno) ); @@ -209,7 +206,7 @@ size_t Bu::Socket::read( void *pBuf, size_t nBytes ) size_t Bu::Socket::read( void *pBuf, size_t nBytes, uint32_t nSec, uint32_t nUSec ) { - struct timeval tv, nt, ct; + struct timeval tv; size_t nRead = 0; fd_set rfds; @@ -217,6 +214,7 @@ size_t Bu::Socket::read( void *pBuf, size_t nBytes, FD_SET(nSocket, &rfds); #ifndef WIN32 + struct timeval nt, ct; gettimeofday( &nt, NULL ); nt.tv_sec += nSec; nt.tv_usec += nUSec; @@ -251,11 +249,7 @@ size_t Bu::Socket::read( void *pBuf, size_t nBytes, size_t Bu::Socket::write( const void *pBuf, size_t nBytes ) { -#ifndef WIN32 int nWrote = TEMP_FAILURE_RETRY( ::write( nSocket, pBuf, nBytes ) ); -#else - int nWrote = ::write( nSocket, pBuf, nBytes ); -#endif if( nWrote < 0 ) { if( errno == EAGAIN ) return 0; @@ -266,7 +260,7 @@ size_t Bu::Socket::write( const void *pBuf, size_t nBytes ) size_t Bu::Socket::write( const void *pBuf, size_t nBytes, uint32_t nSec, uint32_t nUSec ) { - struct timeval tv, nt, ct; + struct timeval tv; size_t nWrote = 0; fd_set wfds; @@ -274,6 +268,7 @@ size_t Bu::Socket::write( const void *pBuf, size_t nBytes, uint32_t nSec, uint32 FD_SET(nSocket, &wfds); #ifndef WIN32 + struct timeval nt, ct; gettimeofday( &nt, NULL ); nt.tv_sec += nSec; nt.tv_usec += nUSec; @@ -421,9 +416,9 @@ bool Bu::Socket::isOpen() return bActive; } -#ifdef WIN32 - typedef int socklen_t; -#endif +//#ifdef WIN32 +// typedef int socklen_t; +//#endif void Bu::Socket::setAddress() { 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 private: void setAddress(); +#ifdef WIN32 + unsigned int nSocket; +#else int nSocket; +#endif bool bActive; FString sReadBuf; 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 @@ +/* + * Copyright (C) 2007-2008 Xagasoft, All rights reserved. + * + * This file is part of the libbu++ library and is released under the + * terms of the license contained in the file LICENSE. + */ + +#ifndef WIN32_COMPATIBILITY__H +#define WIN32_COMPATIBILITY__H + +#ifdef WIN32 + +#ifndef TEMP_FAILURE_RETRY +#define TEMP_FAILURE_RETRY(expression) \ + (__extension__ \ + ({ long int __result; \ + do __result = (long int) (expression); \ + while (__result == -1L && errno == EINTR); \ + __result; })) +#endif + +//__extension__ typedef unsigned int socklen_t; +__extension__ typedef int socklen_t; + +#endif /* WIN32 */ +#endif + -- cgit v1.2.3