From 411cdf39fc2b961a970a0ae91b9059614251247e Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Tue, 28 Aug 2012 17:42:54 +0000 Subject: Loads of win32 compilation issues fixed. Most are fairly minor unsigned/signed mismatches because of socket handles, but there were also some order-of-definition issues that were fixed in the FD_SETSIZE definition code. Fixed a few things that just never worked on windows, like Bu::Thread::yield(). --- src/compat/win32.cpp | 4 ++-- src/compat/win32.h | 2 +- src/experimental/cipher.h | 4 ++-- src/experimental/fastcgi.cpp | 2 +- src/stable/archive.cpp | 2 +- src/stable/myriad.cpp | 1 + src/stable/myriadstream.cpp | 10 +++++----- src/stable/server.cpp | 8 ++++---- src/stable/server.h | 14 ++++++++++---- src/stable/tcpserversocket.cpp | 14 +++++++++++--- src/stable/tcpserversocket.h | 13 +++++++------ src/stable/tcpsocket.cpp | 4 ++++ src/stable/thread.cpp | 5 ++--- src/stable/util.h | 21 ++++++++------------- src/tools/viewcsv.cpp | 8 ++++---- src/unstable/itoserver.cpp | 13 ++++++------- src/unstable/itoserver.h | 18 ++++++++++++------ src/unstable/myriadfs.cpp | 5 +++-- 18 files changed, 84 insertions(+), 64 deletions(-) (limited to 'src') diff --git a/src/compat/win32.cpp b/src/compat/win32.cpp index 6488d5f..e1151b0 100644 --- a/src/compat/win32.cpp +++ b/src/compat/win32.cpp @@ -12,7 +12,7 @@ #define deffunc( name ) \ Bu::Winsock2::FNDEF_DYN_ ##name Bu::Winsock2::_fnptr_ ##name = NULL -char Bu::Winsock2::scode[15]; +char Bu::Winsock2::scode[32]; deffunc( WSAStartup ); deffunc( WSACleanup ); @@ -85,7 +85,7 @@ Bu::Winsock2::~Winsock2() char *Bu::Winsock2::gai_strerror( int iCode ) { - sprintf( scode, "%d", Bu::Winsock2::WSAGetLastError() ); + sprintf( scode, "%d (%d)", iCode, Bu::Winsock2::WSAGetLastError() ); return scode; } diff --git a/src/compat/win32.h b/src/compat/win32.h index 512e6d3..7e44c13 100644 --- a/src/compat/win32.h +++ b/src/compat/win32.h @@ -87,7 +87,7 @@ namespace Bu decltype( int, send, SOCKET s, const char *buf, int len, int flags ); decltype( int, __WSAFDIsSet, SOCKET s, fd_set *set ); - static char scode[15]; + static char scode[32]; static char *gai_strerror( int iCode ); }; diff --git a/src/experimental/cipher.h b/src/experimental/cipher.h index 74f5124..5d5cb07 100644 --- a/src/experimental/cipher.h +++ b/src/experimental/cipher.h @@ -61,7 +61,7 @@ namespace Bu if( iReadBufFill == iBlockSize ) { - int iCpy = Bu::min( (int)(iBytes-iRead), iBlockSize-iReadBufPos ); + int iCpy = Bu::buMin( (int)(iBytes-iRead), iBlockSize-iReadBufPos ); memcpy( ((char *)pBuf)+iRead, aReadBuf+iReadBufPos, iCpy ); iRead += iCpy; iReadBufPos += iCpy; @@ -81,7 +81,7 @@ namespace Bu while( iPos < iBytes ) { - int iLeft = Bu::min((int)(iBytes-iPos),iBlockSize-iWriteBufFill); + int iLeft = Bu::buMin((int)(iBytes-iPos),iBlockSize-iWriteBufFill); memcpy( aWriteBuf+iWriteBufFill, (char *)pBuf+iPos, iLeft ); iPos += iLeft; iWriteBufFill += iLeft; diff --git a/src/experimental/fastcgi.cpp b/src/experimental/fastcgi.cpp index 1f945a3..7068fa8 100644 --- a/src/experimental/fastcgi.cpp +++ b/src/experimental/fastcgi.cpp @@ -24,7 +24,7 @@ Bu::FastCgi::FastCgi() : pSrv( NULL ), bRunning( true ) { - pSrv = new Bu::TcpServerSocket( STDIN_FILENO, false ); + pSrv = new Bu::TcpServerSocket( (Bu::TcpServerSocket::socket_t)STDIN_FILENO, false ); } Bu::FastCgi::FastCgi( int iPort ) : diff --git a/src/stable/archive.cpp b/src/stable/archive.cpp index 3a88b55..13480e1 100644 --- a/src/stable/archive.cpp +++ b/src/stable/archive.cpp @@ -35,7 +35,7 @@ void Bu::Archive::read( void *pData, size_t nSize ) if( nSize == 0 || pData == NULL ) return; - if( rStream.read( (char *)pData, nSize ) < nSize ) + if( (size_t)rStream.read( (char *)pData, nSize ) < nSize ) throw Bu::ExceptionBase("Insufficient data to unarchive object."); } diff --git a/src/stable/myriad.cpp b/src/stable/myriad.cpp index 4f65583..4be82f0 100644 --- a/src/stable/myriad.cpp +++ b/src/stable/myriad.cpp @@ -5,6 +5,7 @@ * terms of the license contained in the file LICENSE. */ +#include "bu/config.h" #include "bu/myriad.h" #include "bu/stream.h" #include "bu/myriadstream.h" diff --git a/src/stable/myriadstream.cpp b/src/stable/myriadstream.cpp index cb81355..58d3936 100644 --- a/src/stable/myriadstream.cpp +++ b/src/stable/myriadstream.cpp @@ -84,8 +84,8 @@ Bu::size Bu::MyriadStream::read( void *pBuf, Bu::size nBytes ) pCurBlock = rMyriad.getBlock( iCurBlock ); } - int iAmnt = Bu::min( - Bu::min( + int iAmnt = Bu::buMin( + Bu::buMin( rMyriad.iBlockSize - iPos%rMyriad.iBlockSize, iLeft ), @@ -165,8 +165,8 @@ Bu::size Bu::MyriadStream::write( const void *pBuf, Bu::size nBytes ) // happens when creating a new stream. if( iPos < pStream->iSize ) { - int iAmnt = Bu::min( - Bu::min( + int iAmnt = Bu::buMin( + Bu::buMin( rMyriad.iBlockSize - iPos%rMyriad.iBlockSize, iLeft ), @@ -189,7 +189,7 @@ Bu::size Bu::MyriadStream::write( const void *pBuf, Bu::size nBytes ) } else { - int iAmnt = Bu::min( + int iAmnt = Bu::buMin( rMyriad.iBlockSize - iPos%rMyriad.iBlockSize, iLeft ); diff --git a/src/stable/server.cpp b/src/stable/server.cpp index e4769a5..39ff7bb 100644 --- a/src/stable/server.cpp +++ b/src/stable/server.cpp @@ -29,7 +29,7 @@ Bu::Server::~Server() void Bu::Server::addPort( int nPort, int nPoolSize ) { TcpServerSocket *s = new TcpServerSocket( nPort, nPoolSize ); - int nSocket = s->getSocket(); + socket_t nSocket = s->getSocket(); FD_SET( nSocket, &fdActive ); hServers.insert( nSocket, s ); } @@ -37,7 +37,7 @@ void Bu::Server::addPort( int nPort, int nPoolSize ) void Bu::Server::addPort( const String &sAddr, int nPort, int nPoolSize ) { TcpServerSocket *s = new TcpServerSocket( sAddr, nPort, nPoolSize ); - int nSocket = s->getSocket(); + socket_t nSocket = s->getSocket(); FD_SET( nSocket, &fdActive ); hServers.insert( nSocket, s ); } @@ -131,7 +131,7 @@ void Bu::Server::scan() tick(); } -void Bu::Server::addClient( int nSocket, int nPort ) +void Bu::Server::addClient( socket_t nSocket, int nPort ) { FD_SET( nSocket, &fdActive ); @@ -202,7 +202,7 @@ void Bu::Server::shutdown() hClients.clear(); } -void Bu::Server::closeClient( int iSocket ) +void Bu::Server::closeClient( socket_t iSocket ) { Bu::Client *pClient = hClients.get( iSocket ); onClosedConnection( pClient ); diff --git a/src/stable/server.h b/src/stable/server.h index 68e5205..5a414d9 100644 --- a/src/stable/server.h +++ b/src/stable/server.h @@ -55,13 +55,19 @@ namespace Bu Server(); virtual ~Server(); +#ifdef WIN32 + typedef unsigned int socket_t; +#else + typedef int socket_t; +#endif + void addPort( int nPort, int nPoolSize=40 ); void addPort( const String &sAddr, int nPort, int nPoolSize=40 ); virtual void scan(); void setTimeout( int nTimeoutSec, int nTimeoutUSec=0 ); - void addClient( int nSocket, int nPort ); + void addClient( socket_t nSocket, int nPort ); void setAutoTick( bool bEnable=true ); void tick(); @@ -72,7 +78,7 @@ namespace Bu void shutdown(); private: - void closeClient( int iSocket ); + void closeClient( socket_t iSocket ); class SrvClientLink : public Bu::ClientLink { public: @@ -97,9 +103,9 @@ namespace Bu int nTimeoutSec; int nTimeoutUSec; fd_set fdActive; - typedef Hash SrvHash; + typedef Hash SrvHash; SrvHash hServers; - typedef Hash ClientHash; + typedef Hash ClientHash; ClientHash hClients; bool bAutoTick; }; diff --git a/src/stable/tcpserversocket.cpp b/src/stable/tcpserversocket.cpp index daedcd1..91da199 100644 --- a/src/stable/tcpserversocket.cpp +++ b/src/stable/tcpserversocket.cpp @@ -5,6 +5,8 @@ * terms of the license contained in the file LICENSE. */ +#include "bu/config.h" + #ifndef WIN32 #include #include @@ -23,8 +25,6 @@ #include #include "bu/tcpserversocket.h" -#include "bu/config.h" - namespace Bu { subExceptionDef( TcpServerSocketException ) } Bu::TcpServerSocket::TcpServerSocket( int nPort, int nPoolSize ) : @@ -72,7 +72,7 @@ Bu::TcpServerSocket::TcpServerSocket(const String &sAddr,int nPort, int nPoolSiz startServer( name, nPoolSize ); } -Bu::TcpServerSocket::TcpServerSocket( int nServer, bool bInit, int nPoolSize ) : +Bu::TcpServerSocket::TcpServerSocket( socket_t nServer, bool bInit, int nPoolSize ) : nServer( nServer ), nPort( 0 ) { @@ -109,7 +109,11 @@ Bu::TcpServerSocket::TcpServerSocket( const TcpServerSocket &rSrc ) Bu::TcpServerSocket::~TcpServerSocket() { +#ifdef WIN32 + if( nServer != INVALID_SOCKET ) +#else if( nServer > -1 ) +#endif ::close( nServer ); } @@ -118,7 +122,11 @@ void Bu::TcpServerSocket::startServer( struct sockaddr_in &name, int nPoolSize ) /* Create the socket. */ nServer = bu_socket( PF_INET, SOCK_STREAM, 0 ); +#ifdef WIN32 + if( nServer == INVALID_SOCKET ) +#else if( nServer < 0 ) +#endif { throw Bu::TcpServerSocketException("Couldn't create a listen socket."); } diff --git a/src/stable/tcpserversocket.h b/src/stable/tcpserversocket.h index 0f56bd6..9776668 100644 --- a/src/stable/tcpserversocket.h +++ b/src/stable/tcpserversocket.h @@ -37,9 +37,14 @@ namespace Bu class TcpServerSocket { public: +#ifdef WIN32 + typedef unsigned int socket_t; +#else + typedef int socket_t; +#endif TcpServerSocket( int nPort, int nPoolSize=40 ); TcpServerSocket( const String &sAddr, int nPort, int nPoolSize=40 ); - TcpServerSocket( int nSocket, bool bInit, int nPoolSize=40 ); + TcpServerSocket( socket_t nSocket, bool bInit, int nPoolSize=40 ); TcpServerSocket( const TcpServerSocket &rSrc ); virtual ~TcpServerSocket(); @@ -52,11 +57,7 @@ namespace Bu void initServer( struct sockaddr_in &name, int nPoolSize ); fd_set fdActive; -#ifdef WIN32 - unsigned int nServer; -#else - int nServer; -#endif + socket_t nServer; int nPort; }; } diff --git a/src/stable/tcpsocket.cpp b/src/stable/tcpsocket.cpp index 7ea6ca1..fa79a36 100644 --- a/src/stable/tcpsocket.cpp +++ b/src/stable/tcpsocket.cpp @@ -55,7 +55,11 @@ Bu::TcpSocket::TcpSocket( const Bu::String &sAddr, int nPort, int nTimeout, /* Create the socket. */ nTcpSocket = bu_socket( PF_INET, SOCK_STREAM, 0 ); +#ifdef WIN32 + if( nTcpSocket == INVALID_SOCKET ) +#else if( nTcpSocket < 0 ) +#endif { throw ExceptionBase("Couldn't create socket.\n"); } diff --git a/src/stable/thread.cpp b/src/stable/thread.cpp index 1bd2040..141f54a 100644 --- a/src/stable/thread.cpp +++ b/src/stable/thread.cpp @@ -5,9 +5,8 @@ * terms of the license contained in the file LICENSE. */ -#include "bu/thread.h" - #include "bu/config.h" +#include "bu/thread.h" namespace Bu { subExceptionDef( ThreadException ); } @@ -78,7 +77,7 @@ void Bu::Thread::yield() #ifndef WIN32 pthread_yield(); #else - #warning Bu::Thread::yield IS A STUB for WIN32!!!! + sched_yield(); #endif } diff --git a/src/stable/util.h b/src/stable/util.h index 087af6e..4514281 100644 --- a/src/stable/util.h +++ b/src/stable/util.h @@ -39,11 +39,6 @@ namespace Bu b = tmp; } -#ifdef WIN32 - #warning: removing min and max win32 macros because of compile conflict - #undef min - #undef max -#endif /** * Finds the lesser of the two objects, objects passed in must be * less-than-comparable. @@ -52,7 +47,7 @@ namespace Bu *@returns A reference to the lesser of a or b. */ template - const item &min( const item &a, const item &b ) + const item &buMin( const item &a, const item &b ) { return a - item &min( item &a, item &b ) + item &buMin( item &a, item &b ) { return a - const item &max( const item &a, const item &b ) + const item &buMax( const item &a, const item &b ) { return b - item &max( item &a, item &b ) + item &buMax( item &a, item &b ) { return b - const item &mid( const item &a, const item &b, const item &c ) + const item &buMid( const item &a, const item &b, const item &c ) { - return min( max( a, b ), c ); + return buMin( buMax( a, b ), c ); } /** @@ -117,9 +112,9 @@ namespace Bu *@returns A reference to the mid-value of a, b, and c. */ template - item &mid( item &a, item &b, item &c ) + item &buMid( item &a, item &b, item &c ) { - return min( max( a, b ), c ); + return buMin( buMax( a, b ), c ); } // diff --git a/src/tools/viewcsv.cpp b/src/tools/viewcsv.cpp index 46db319..5009ea8 100644 --- a/src/tools/viewcsv.cpp +++ b/src/tools/viewcsv.cpp @@ -105,8 +105,8 @@ public: int maxx, maxy; getmaxyx( stdscr, maxy, maxx ); - int iRows = min( (int)doc.sgData.getSize(), maxy-((bHeaderRow)?(4):(3)) ); - int iCols = min( doc.iMaxCols, (int)maxx-1 ); + int iRows = buMin( (int)doc.sgData.getSize(), maxy-((bHeaderRow)?(4):(3)) ); + int iCols = buMin( doc.iMaxCols, (int)maxx-1 ); int iHdrHeight = 1; if( bHeaderRow ) @@ -129,7 +129,7 @@ public: { if( iXPos >= maxx ) break; - int iWidth = min( doc.aWidths[iCol+iXOff], maxx-iXPos-1 ); + int iWidth = buMin( doc.aWidths[iCol+iXOff], maxx-iXPos-1 ); char buf[6]; snprintf( buf, 6, "%d", iCol+iXOff ); mvaddch( 0, iXPos, ACS_VLINE ); @@ -165,7 +165,7 @@ public: { if( iXPos >= maxx ) break; - int iWidth = min( doc.aWidths[iCol+iXOff], maxx-iXPos-1 ); + int iWidth = buMin( doc.aWidths[iCol+iXOff], maxx-iXPos-1 ); mvaddch( iRow+iHdrHeight+1, iXPos, ACS_VLINE ); mvaddnstr( iRow+iHdrHeight+1, iXPos+1, doc.sgData[iRow+iYOff][iCol+iXOff].getStr(), iWidth ); diff --git a/src/unstable/itoserver.cpp b/src/unstable/itoserver.cpp index 5b3b5a2..7dbce6c 100644 --- a/src/unstable/itoserver.cpp +++ b/src/unstable/itoserver.cpp @@ -5,14 +5,13 @@ * terms of the license contained in the file LICENSE. */ +#include "bu/config.h" #include "bu/itoserver.h" #include #include "bu/tcpserversocket.h" #include "bu/client.h" #include "bu/tcpsocket.h" -#include "bu/config.h" - Bu::ItoServer::ItoServer() : nTimeoutSec( 1 ), nTimeoutUSec( 0 ) @@ -42,7 +41,7 @@ Bu::ItoServer::~ItoServer() void Bu::ItoServer::addPort( int nPort, int nPoolSize ) { TcpServerSocket *s = new TcpServerSocket( nPort, nPoolSize ); - int nSocket = s->getSocket(); + socket_t nSocket = s->getSocket(); FD_SET( nSocket, &fdActive ); hServers.insert( nSocket, s ); } @@ -50,7 +49,7 @@ void Bu::ItoServer::addPort( int nPort, int nPoolSize ) void Bu::ItoServer::addPort( const String &sAddr, int nPort, int nPoolSize ) { TcpServerSocket *s = new TcpServerSocket( sAddr, nPort, nPoolSize ); - int nSocket = s->getSocket(); + socket_t nSocket = s->getSocket(); FD_SET( nSocket, &fdActive ); hServers.insert( nSocket, s ); } @@ -61,7 +60,7 @@ void Bu::ItoServer::setTimeout( int nTimeoutSec, int nTimeoutUSec ) this->nTimeoutUSec = nTimeoutUSec; } -void Bu::ItoServer::addClient( int nSocket, int nPort ) +void Bu::ItoServer::addClient( socket_t nSocket, int nPort ) { ItoClient *pC = new ItoClient( *this, nSocket, nPort, nTimeoutSec, nTimeoutUSec ); @@ -106,7 +105,7 @@ void Bu::ItoServer::run() } } -void Bu::ItoServer::clientCleanup( int iSocket ) +void Bu::ItoServer::clientCleanup( socket_t iSocket ) { imClients.lock(); ItoClient *pCli = hClients.get( iSocket ); @@ -114,7 +113,7 @@ void Bu::ItoServer::clientCleanup( int iSocket ) qClientCleanup.enqueue( pCli ); } -Bu::ItoServer::ItoClient::ItoClient( ItoServer &rSrv, int iSocket, int iPort, +Bu::ItoServer::ItoClient::ItoClient( ItoServer &rSrv, Bu::ItoServer::socket_t iSocket, int iPort, int nTimeoutSec, int nTimeoutUSec ) : rSrv( rSrv ), iSocket( iSocket ), diff --git a/src/unstable/itoserver.h b/src/unstable/itoserver.h index 57fece3..6a0df56 100644 --- a/src/unstable/itoserver.h +++ b/src/unstable/itoserver.h @@ -58,13 +58,19 @@ namespace Bu ItoServer(); virtual ~ItoServer(); +#ifdef WIN32 + typedef unsigned int socket_t; +#else + typedef int socket_t; +#endif + void addPort( int nPort, int nPoolSize=40 ); void addPort( const String &sAddr, int nPort, int nPoolSize=40 ); //void scan(); void setTimeout( int nTimeoutSec, int nTimeoutUSec=0 ); - void addClient( int nSocket, int nPort ); + void addClient( socket_t nSocket, int nPort ); virtual void onNewConnection( Client *pClient, int nPort )=0; virtual void onClosedConnection( Client *pClient )=0; @@ -78,7 +84,7 @@ namespace Bu { friend class Bu::ItoServer::SrvClientLink; public: - ItoClient( ItoServer &rSrv, int nSocket, int nPort, + ItoClient( ItoServer &rSrv, socket_t nSocket, int nPort, int nTimeoutSec, int nTimeoutUSec ); virtual ~ItoClient(); @@ -92,7 +98,7 @@ namespace Bu ItoServer &rSrv; Client *pClient; fd_set fdActive; - int iSocket; + socket_t iSocket; int iPort; int nTimeoutSec; int nTimeoutUSec; @@ -126,15 +132,15 @@ namespace Bu int nTimeoutSec; int nTimeoutUSec; fd_set fdActive; - typedef Hash ServerHash; + typedef Hash ServerHash; ServerHash hServers; - typedef Hash ClientHash; + typedef Hash ClientHash; typedef SynchroQueue ClientQueue; ClientHash hClients; ClientQueue qClientCleanup; Mutex imClients; - void clientCleanup( int iSocket ); + void clientCleanup( socket_t iSocket ); }; } diff --git a/src/unstable/myriadfs.cpp b/src/unstable/myriadfs.cpp index 32e1ad6..6ed176d 100644 --- a/src/unstable/myriadfs.cpp +++ b/src/unstable/myriadfs.cpp @@ -5,6 +5,7 @@ * terms of the license contained in the file LICENSE. */ +#include "bu/config.h" #include "bu/myriadfs.h" #include "bu/myriadstream.h" @@ -434,7 +435,7 @@ int32_t Bu::MyriadFs::lookupInode( Bu::String::const_iterator iStart, void Bu::MyriadFs::readInode( int32_t iNode, RawStat &rs, MyriadStream &rIs ) { rIs.setPos( hNodeIndex.get( iNode )*sizeof(RawStat) ); - if( rIs.read( &rs, sizeof(RawStat) ) < sizeof(RawStat) ) + if( rIs.read( &rs, sizeof(RawStat) ) < (int)sizeof(RawStat) ) throw Bu::MyriadFsException("Filesystem corruption detected."); if( rs.iNode != iNode ) throw Bu::MyriadFsException("Filesystem corruption detected."); @@ -451,7 +452,7 @@ void Bu::MyriadFs::writeInode( const RawStat &rs, { rOs.setSize( hNodeIndex.getSize()*sizeof(RawStat) ); rOs.setPos( hNodeIndex.get( rs.iNode )*sizeof(RawStat) ); - if( rOs.write( &rs, sizeof(RawStat) ) < sizeof(RawStat) ) + if( rOs.write( &rs, sizeof(RawStat) ) < (int)sizeof(RawStat) ) throw Bu::MyriadFsException("Error writing inode to header stream."); } -- cgit v1.2.3