From 2a943828287d2a861930d3facb2333c895ada205 Mon Sep 17 00:00:00 2001 From: Mike Buland Date: Thu, 13 May 2010 13:43:57 +0000 Subject: Finally rearranged the system to put all compatability files in a directory called compat. I've updated the linux and windows builds and it looks pretty good. I also added a config.h file which we have to edit by hand until I can work on build some more. Linux File operations now use 64 bit mode, windows can't, or at least, I don't feel like researching it right now. --- default.bld | 18 ++++- mingw.bld | 23 ++++-- src/compat/linux.h | 31 +++++++++ src/compat/osx.h | 27 +++++++ src/compat/win32.cpp | 166 ++++++++++++++++++++++++++++++++++++++++++++ src/compat/win32.h | 131 ++++++++++++++++++++++++++++++++++ src/config.h | 11 +++ src/fifo.cpp | 2 +- src/file.cpp | 6 ++ src/ito.cpp | 3 +- src/itoserver.cpp | 5 +- src/linux_compatibility.h | 31 --------- src/multiserver.cpp | 4 +- src/osx_compatibility.h | 27 ------- src/process.cpp | 5 +- src/server.cpp | 2 +- src/server.h | 3 +- src/serversocket.cpp | 5 +- src/socket.cpp | 5 +- src/win32_compatibility.cpp | 166 -------------------------------------------- src/win32_compatibility.h | 129 ---------------------------------- 21 files changed, 422 insertions(+), 378 deletions(-) create mode 100644 src/compat/linux.h create mode 100644 src/compat/osx.h create mode 100644 src/compat/win32.cpp create mode 100644 src/compat/win32.h create mode 100644 src/config.h delete mode 100644 src/linux_compatibility.h delete mode 100644 src/osx_compatibility.h delete mode 100644 src/win32_compatibility.cpp delete mode 100644 src/win32_compatibility.h diff --git a/default.bld b/default.bld index 9d94f73..ea83827 100644 --- a/default.bld +++ b/default.bld @@ -36,13 +36,23 @@ target files("src/*.h").replace("src/", "bu/") input OUTPUT.replace("bu/","src/"); profile "build" { - execute("ln -s ../${INPUT} ${OUTPUT}"); + execute("mkdir -p $(dirname ${OUTPUT}); ln -s ../${INPUT} ${OUTPUT}"); + } +} +target files("src/compat/*.h").replace("src/", "bu/") +{ + tag "header-links"; + display "symlink"; + input OUTPUT.replace("bu/","src/"); + profile "build" + { + execute("mkdir -p $(dirname ${OUTPUT}); ln -s ../../${INPUT} ${OUTPUT}"); } } target "libbu++.a" { - input files("src/*.cpp"); + input files("src/*.cpp", "src/compat/*.cpp"); rule "lib"; CXXFLAGS += "-I. -fPIC"; } @@ -120,7 +130,9 @@ target "libbu++-r$(svnversion "-n").tar.bz2" files("src/tools/*.cpp"), files("src/tools/*.h"), files("src/unit/*.unit"), - files("src/doxy/*.dox") + files("src/doxy/*.dox"), + files("src/compat/*.cpp"), + files("src/compat/*.h") ]; rule "tarball"; tag "pkg"; diff --git a/mingw.bld b/mingw.bld index 61dbeaf..7bdf474 100644 --- a/mingw.bld +++ b/mingw.bld @@ -6,13 +6,11 @@ CXXFLAGS += "-ggdb -W -Wall"; - action "default" { build: [targets("header-links"), "libbu++win.a"]; } - target files("src/*.h").replace("src/", "bu/") { tag "header-links"; @@ -20,13 +18,23 @@ target files("src/*.h").replace("src/", "bu/") input OUTPUT.replace("bu/","src/"); profile "build" { - execute("ln -s ../${INPUT} ${OUTPUT}"); + execute("mkdir -p $(dirname ${OUTPUT}); ln -s ../${INPUT} ${OUTPUT}"); + } +} +target files("src/compat/*.h").replace("src/", "bu/") +{ + tag "header-links"; + display "symlink"; + input OUTPUT.replace("bu/","src/"); + profile "build" + { + execute("mkdir -p $(dirname ${OUTPUT}); ln -s ../../${INPUT} ${OUTPUT}"); } } target "libbu++win.a" { - input files("src/*.cpp"); + input files("src/*.cpp", "src/compat/*.cpp"); rule "lib"; CXXFLAGS += "-I. -Isupport/windows"; } @@ -45,6 +53,13 @@ target "src/regex.win_o" execute("rm -f src/regex.win_o && touch src/regex.win_o && echo NOT BUILDING REGEX RIGHT NOW!!!"); } } +target "src/cachestorefiles.win_o" +{ + profile "build" + { + execute("rm -f src/cachestorefiles.win_o && touch src/cachestorefiles.win_o && echo NOT BUILDING REGEX RIGHT NOW!!!"); + } +} function cppToWinObj() { diff --git a/src/compat/linux.h b/src/compat/linux.h new file mode 100644 index 0000000..ccc8536 --- /dev/null +++ b/src/compat/linux.h @@ -0,0 +1,31 @@ +/* + * Copyright (C) 2007-2010 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. + */ + +#ifdef __linux__ + +#define bu_inet_ntoa inet_ntoa +#define bu_inet_addr inet_addr +#define bu_select select +#define bu_socket socket +#define bu_shutdown shutdown +#define bu_htons htons +#define bu_htonl htonl +#define bu_gethostbyname gethostbyname +#define bu_freeaddrinfo freeaddrinfo +#define bu_getaddrinfo getaddrinfo +#define bu_connect connect +#define bu_getpeername getpeername +#define bu_setsockopt setsockopt +#define bu_bind bind +#define bu_listen listen +#define bu_accept accept +#define bu_send send +#define bu_recv recv + +#define bu_gai_strerror gai_strerror + +#endif diff --git a/src/compat/osx.h b/src/compat/osx.h new file mode 100644 index 0000000..7169d7e --- /dev/null +++ b/src/compat/osx.h @@ -0,0 +1,27 @@ +/* + * Copyright (C) 2007-2010 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 OSX_COMPATIBILITY__H +#define OSX_COMPATIBILITY__H + +#ifdef __APPLE__ + +#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 + +#include + +#define pthread_yield() sched_yield() +#endif /* __APPLE__ */ +#endif + diff --git a/src/compat/win32.cpp b/src/compat/win32.cpp new file mode 100644 index 0000000..6fcac15 --- /dev/null +++ b/src/compat/win32.cpp @@ -0,0 +1,166 @@ +/* + * Copyright (C) 2007-2010 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. + */ + +#include "bu/compat/win32.h" + +#ifdef WIN32 + +#define deffunc( name ) \ + Bu::Winsock2::FNDEF_DYN_ ##name Bu::Winsock2::_fnptr_ ##name = NULL + +char Bu::Winsock2::scode[15]; + +deffunc( WSAStartup ); +deffunc( WSACleanup ); +deffunc( WSAGetLastError ); +deffunc( inet_ntoa ); +deffunc( inet_addr ); +deffunc( select ); +deffunc( socket ); +deffunc( shutdown ); +deffunc( ioctlsocket ); +deffunc( htons ); +deffunc( htonl ); +deffunc( gethostbyname ); +deffunc( freeaddrinfo ); +deffunc( getaddrinfo ); +deffunc( connect ); +deffunc( getpeername ); +deffunc( setsockopt ); +deffunc( bind ); +deffunc( listen ); +deffunc( accept ); +deffunc( recv ); +deffunc( send ); +deffunc( __WSAFDIsSet ); + +// Safely get a function from the library +#define getfunc( name ) \ + Bu::Winsock2::_fnptr_ ##name = (FNDEF_DYN_ ##name) \ + GetProcAddress( Ws2_32, #name ); \ + if( Bu::Winsock2::_fnptr_ ##name == NULL ) { \ + throw Bu::ExceptionBase("Error loading function " #name " from dll.");\ + } (void)0 + +Bu::Winsock2::Winsock2() +{ + Ws2_32 = LoadLibrary(TEXT("WS2_32.DLL")); + + getfunc( WSAStartup ); + getfunc( WSACleanup ); + getfunc( WSAGetLastError ); + getfunc( inet_ntoa ); + getfunc( inet_addr ); + getfunc( select ); + getfunc( socket ); + getfunc( shutdown ); + getfunc( ioctlsocket ); + getfunc( htons ); + getfunc( htonl ); + getfunc( gethostbyname ); + getfunc( freeaddrinfo ); + getfunc( getaddrinfo ); + getfunc( connect ); + getfunc( getpeername ); + getfunc( setsockopt ); + getfunc( bind ); + getfunc( listen ); + getfunc( accept ); + getfunc( recv ); + getfunc( send ); + getfunc( __WSAFDIsSet ); + + Bu::Winsock2::WSAStartup( MAKEWORD(2, 2), &wsaData ); +} + +Bu::Winsock2::~Winsock2() +{ + Bu::Winsock2::WSACleanup(); + FreeLibrary( Ws2_32 ); +} + +char *Bu::Winsock2::gai_strerror( int iCode ) +{ + sprintf( scode, "%d", Bu::Winsock2::WSAGetLastError() ); + return scode; +} + +int Bu::Winsock2::WSAStartup( WORD a, LPWSADATA b ) { + return (*Bu::Winsock2::_fnptr_WSAStartup)( a, b ); +} +int Bu::Winsock2::WSACleanup( ) { + return (*Bu::Winsock2::_fnptr_WSACleanup)(); +} +int Bu::Winsock2::WSAGetLastError( ) { + return (*Bu::Winsock2::_fnptr_WSAGetLastError)(); +} +char * Bu::Winsock2::inet_ntoa( struct in_addr a ) { + return (*Bu::Winsock2::_fnptr_inet_ntoa)( a ); +} +unsigned long Bu::Winsock2::inet_addr( const char *s_in ) { + return (*Bu::Winsock2::_fnptr_inet_addr)( s_in ); +} +int Bu::Winsock2::select( int a, fd_set *b, fd_set *c, fd_set *d, + const struct timeval *e ) { + return (*Bu::Winsock2::_fnptr_select)( a, b, c, d, e ); +} +SOCKET Bu::Winsock2::socket( int domain, int type, int protocol ) { + return (*Bu::Winsock2::_fnptr_socket)( domain, type, protocol ); +} +int Bu::Winsock2::shutdown( SOCKET s, int how ) { + return (*Bu::Winsock2::_fnptr_shutdown)( s, how ); +} +int Bu::Winsock2::ioctlsocket( SOCKET s, long cmd, u_long *argp ) { + return (*Bu::Winsock2::_fnptr_ioctlsocket)( s, cmd, argp ); +} +u_short Bu::Winsock2::htons( u_short in ) { + return (*Bu::Winsock2::_fnptr_htons)( in ); +} +u_long Bu::Winsock2::htonl( u_long in ) { + return (*Bu::Winsock2::_fnptr_htonl)( in ); +} +struct hostent * Bu::Winsock2::gethostbyname( const char *name ) { + return (*Bu::Winsock2::_fnptr_gethostbyname)( name ); +} +void Bu::Winsock2::freeaddrinfo( struct addrinfo *ai ) { + return (*Bu::Winsock2::_fnptr_freeaddrinfo)( ai ); +} +int Bu::Winsock2::getaddrinfo( const char *a, const char *b, + const struct addrinfo *c, struct addrinfo **d ) { + return (*Bu::Winsock2::_fnptr_getaddrinfo)( a, b, c, d ); +} +int Bu::Winsock2::connect( SOCKET s, const struct sockaddr *a, int b ) { + return (*Bu::Winsock2::_fnptr_connect)( s, a, b ); +} +int Bu::Winsock2::getpeername( SOCKET s, struct sockaddr *a, int *b ) { + return (*Bu::Winsock2::_fnptr_getpeername)( s, a, b); +} +int Bu::Winsock2::setsockopt( SOCKET s, int a, int b, + const char *c, int d ) { + return (*Bu::Winsock2::_fnptr_setsockopt)( s, a, b, c, d ); +} +int Bu::Winsock2::bind( SOCKET s, const struct sockaddr *a, int b ) { + return (*Bu::Winsock2::_fnptr_bind)( s, a, b ); +} +int Bu::Winsock2::listen( SOCKET s, int backlog ) { + return (*Bu::Winsock2::_fnptr_listen)( s, backlog ); +} +SOCKET Bu::Winsock2::accept( SOCKET s, struct sockaddr *a, int *b ) { + return (*Bu::Winsock2::_fnptr_accept)( s, a, b ); +} +int Bu::Winsock2::recv( SOCKET s, char *buf, int len, int flags ) { + return (*Bu::Winsock2::_fnptr_recv)( s, buf, len, flags ); +} +int Bu::Winsock2::send( SOCKET s, const char *buf, int len, int flags ) { + return (*Bu::Winsock2::_fnptr_send)( s, buf, len, flags ); +} +int Bu::Winsock2::__WSAFDIsSet( SOCKET s, fd_set *set ) { + return (*Bu::Winsock2::_fnptr___WSAFDIsSet)( s, set ); +} + +#endif + diff --git a/src/compat/win32.h b/src/compat/win32.h new file mode 100644 index 0000000..6304d4c --- /dev/null +++ b/src/compat/win32.h @@ -0,0 +1,131 @@ +/* + * Copyright (C) 2007-2010 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 + +#ifdef __cplusplus +extern "C" +{ +#include +#include +} +#endif + +#include "bu/fstring.h" +#include "bu/singleton.h" + +#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 + +#define decltype( ret, name, ... ) \ + typedef ret (__cdecl *FNDEF_DYN_ ##name)( __VA_ARGS__ ); \ + static FNDEF_DYN_ ##name _fnptr_ ##name; \ + static ret name( __VA_ARGS__ ) + +__extension__ typedef int socklen_t; + +#ifdef gai_strerror +#undef gai_strerror +#endif + +namespace Bu +{ + class Winsock2 : public Bu::Singleton + { + friend class Bu::Singleton; + private: + Winsock2(); + virtual ~Winsock2(); + + WSADATA wsaData; + HINSTANCE Ws2_32; + + public: + // decltype( return type, function name<, optional parameters> ) + decltype( int, WSAStartup, WORD, LPWSADATA ); + decltype( int, WSACleanup ); + decltype( int, WSAGetLastError ); + decltype( char *, inet_ntoa, struct in_addr ); + decltype( unsigned long, inet_addr, const char *s_in ); + decltype( int, select, int nfds, fd_set *readfds, fd_set *writefds, + fd_set *exceptfds, const struct timeval *timeout ); + decltype( SOCKET, socket, int domain, int type, int protocol ); + decltype( int, shutdown, SOCKET s, int how ); + decltype( int, ioctlsocket, SOCKET s, long cmd, u_long *argp ); + decltype( u_short, htons, u_short in ); + decltype( u_long, htonl, u_long in ); + decltype( struct hostent *, gethostbyname, const char *name ); + decltype( void, freeaddrinfo, struct addrinfo *ai ); + decltype( int, getaddrinfo, const char *nodename, const char *servname, + const struct addrinfo *hints, struct addrinfo **res ); + decltype( int, connect, SOCKET s, const struct sockaddr *serv_addr, + int addrlen ); + decltype( int, getpeername, SOCKET s, struct sockaddr *name, + int *namelen ); + decltype( int, setsockopt, SOCKET s, int level, int optname, + const char *optval, int optlen ); + decltype( int, bind, SOCKET s, const struct sockaddr *my_addr, + int addrlen ); + decltype( int, listen, SOCKET s, int backlog ); + decltype( SOCKET, accept, SOCKET s, struct sockaddr *addr, + int *addrlen); + decltype( int, recv, SOCKET s, char *buf, int len, int flags ); + 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 *gai_strerror( int iCode ); + }; +}; + +#ifdef FD_ISSET +#undef FD_ISSET +#endif + +#define bu_WSAStartup (*Bu::Winsock2::WSAStartup) +#define bu_WSACleanup (*Bu::Winsock2::WSACleanup) +#define bu_WSAGetLastError (*Bu::Winsock2::WSAGetLastError) +#define bu_inet_ntoa (*Bu::Winsock2::inet_ntoa) +#define bu_inet_addr (*Bu::Winsock2::inet_addr) +#define bu_select (*Bu::Winsock2::select) +#define bu_socket (*Bu::Winsock2::socket) +#define bu_shutdown (*Bu::Winsock2::shutdown) +#define bu_ioctlsocket (*Bu::Winsock2::ioctlsocket) +#define bu_htons (*Bu::Winsock2::htons) +#define bu_htonl (*Bu::Winsock2::htonl) +#define bu_gethostbyname (*Bu::Winsock2::gethostbyname) +#define bu_freeaddrinfo (*Bu::Winsock2::freeaddrinfo) +#define bu_getaddrinfo (*Bu::Winsock2::getaddrinfo) +#define bu_connect (*Bu::Winsock2::connect) +#define bu_getpeername (*Bu::Winsock2::getpeername) +#define bu_setsockopt (*Bu::Winsock2::setsockopt) +#define bu_bind (*Bu::Winsock2::bind) +#define bu_listen (*Bu::Winsock2::listen) +#define bu_accept (*Bu::Winsock2::accept) +#define bu_recv (*Bu::Winsock2::recv) +#define bu_send (*Bu::Winsock2::send) +#define bu___WSAFDIsSet (*Bu::Winsock2::__WSAFDIsSet) + +#define FD_ISSET (*Bu::Winsock2::__WSAFDIsSet) +#define bu_gai_strerror Bu::Winsock2::gai_strerror + +#undef decltype + +#undef USE_64BIT_IO + +#endif /* WIN32 */ +#endif + diff --git a/src/config.h b/src/config.h new file mode 100644 index 0000000..05ef99a --- /dev/null +++ b/src/config.h @@ -0,0 +1,11 @@ +#ifndef BU_CONFIG_H +#define BU_CONFIG_H + +// Use 64 bit IO functions where applicable (i.e. large file support) +#define USE_64BIT_IO + +#include "bu/compat/win32.h" +#include "bu/compat/osx.h" +#include "bu/compat/linux.h" + +#endif diff --git a/src/fifo.cpp b/src/fifo.cpp index ebea927..d1fa960 100644 --- a/src/fifo.cpp +++ b/src/fifo.cpp @@ -12,7 +12,7 @@ #include #include -#include "win32_compatibility.h" +#include "bu/config.h" namespace Bu { subExceptionDef( FifoException ) } diff --git a/src/file.cpp b/src/file.cpp index 4d79f1e..f1f63e4 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -13,13 +13,19 @@ #include #include // for mkstemp +#include "bu/config.h" + namespace Bu { subExceptionDef( FileException ) } Bu::File::File( const Bu::FString &sName, int iFlags ) : fd( -1 ), bEos( true ) { +#ifdef USE_64BIT_IO + fd = ::open64( sName.getStr(), getPosixFlags( iFlags ), 0666 ); +#else fd = ::open( sName.getStr(), getPosixFlags( iFlags ), 0666 ); +#endif if( fd < 0 ) { throw Bu::FileException( errno, "%s: %s", diff --git a/src/ito.cpp b/src/ito.cpp index 0218ecc..12aee6f 100644 --- a/src/ito.cpp +++ b/src/ito.cpp @@ -6,7 +6,8 @@ */ #include "bu/ito.h" -#include "bu/osx_compatibility.h" + +#include "bu/config.h" Bu::Ito::Ito() { diff --git a/src/itoserver.cpp b/src/itoserver.cpp index 279310d..a1d804a 100644 --- a/src/itoserver.cpp +++ b/src/itoserver.cpp @@ -5,14 +5,13 @@ * terms of the license contained in the file LICENSE. */ -#include "win32_compatibility.h" - #include "bu/itoserver.h" #include #include "bu/serversocket.h" #include "bu/client.h" #include "bu/socket.h" -#include "bu/osx_compatibility.h" + +#include "bu/config.h" Bu::ItoServer::ItoServer() : nTimeoutSec( 1 ), diff --git a/src/linux_compatibility.h b/src/linux_compatibility.h deleted file mode 100644 index ccc8536..0000000 --- a/src/linux_compatibility.h +++ /dev/null @@ -1,31 +0,0 @@ -/* - * Copyright (C) 2007-2010 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. - */ - -#ifdef __linux__ - -#define bu_inet_ntoa inet_ntoa -#define bu_inet_addr inet_addr -#define bu_select select -#define bu_socket socket -#define bu_shutdown shutdown -#define bu_htons htons -#define bu_htonl htonl -#define bu_gethostbyname gethostbyname -#define bu_freeaddrinfo freeaddrinfo -#define bu_getaddrinfo getaddrinfo -#define bu_connect connect -#define bu_getpeername getpeername -#define bu_setsockopt setsockopt -#define bu_bind bind -#define bu_listen listen -#define bu_accept accept -#define bu_send send -#define bu_recv recv - -#define bu_gai_strerror gai_strerror - -#endif diff --git a/src/multiserver.cpp b/src/multiserver.cpp index 4eccde8..a6cee36 100644 --- a/src/multiserver.cpp +++ b/src/multiserver.cpp @@ -5,12 +5,12 @@ * terms of the license contained in the file LICENSE. */ -#include "win32_compatibility.h" - #include "bu/multiserver.h" #include "bu/protocol.h" #include "bu/client.h" +#include "bu/config.h" + Bu::MultiServer::MultiServer() { } diff --git a/src/osx_compatibility.h b/src/osx_compatibility.h deleted file mode 100644 index 7169d7e..0000000 --- a/src/osx_compatibility.h +++ /dev/null @@ -1,27 +0,0 @@ -/* - * Copyright (C) 2007-2010 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 OSX_COMPATIBILITY__H -#define OSX_COMPATIBILITY__H - -#ifdef __APPLE__ - -#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 - -#include - -#define pthread_yield() sched_yield() -#endif /* __APPLE__ */ -#endif - diff --git a/src/process.cpp b/src/process.cpp index c6c86f2..da38e64 100644 --- a/src/process.cpp +++ b/src/process.cpp @@ -15,9 +15,8 @@ #include #include -#include "bu/osx_compatibility.h" -#include "bu/win32_compatibility.h" -#include "bu/linux_compatibility.h" + +#include "bu/config.h" Bu::Process::Process( Flags eFlags, const char *sName, char *const argv[] ) : iStdIn( -1 ), diff --git a/src/server.cpp b/src/server.cpp index e55713d..64ddf9f 100644 --- a/src/server.cpp +++ b/src/server.cpp @@ -11,7 +11,7 @@ #include "bu/serversocket.h" #include "bu/client.h" #include "bu/socket.h" -#include "bu/osx_compatibility.h" +#include "bu/config.h" Bu::Server::Server() : nTimeoutSec( 0 ), diff --git a/src/server.h b/src/server.h index d83c346..e09246f 100644 --- a/src/server.h +++ b/src/server.h @@ -20,7 +20,8 @@ #include "bu/clientlink.h" #include "bu/clientlinkfactory.h" #include "bu/hash.h" -#include "bu/win32_compatibility.h" + +#include "bu/config.h" namespace Bu { diff --git a/src/serversocket.cpp b/src/serversocket.cpp index 37eb3d4..87d0035 100644 --- a/src/serversocket.cpp +++ b/src/serversocket.cpp @@ -22,9 +22,8 @@ //#include #include #include "bu/serversocket.h" -#include "bu/osx_compatibility.h" -#include "bu/win32_compatibility.h" -#include "bu/linux_compatibility.h" + +#include "bu/config.h" namespace Bu { subExceptionDef( ServerSocketException ) } diff --git a/src/socket.cpp b/src/socket.cpp index a175ee9..6a30f44 100644 --- a/src/socket.cpp +++ b/src/socket.cpp @@ -15,9 +15,8 @@ #include #include #include "bu/socket.h" -#include "bu/osx_compatibility.h" -#include "bu/win32_compatibility.h" -#include "bu/linux_compatibility.h" + +#include "bu/config.h" #ifndef WIN32 #include diff --git a/src/win32_compatibility.cpp b/src/win32_compatibility.cpp deleted file mode 100644 index 98e63a7..0000000 --- a/src/win32_compatibility.cpp +++ /dev/null @@ -1,166 +0,0 @@ -/* - * Copyright (C) 2007-2010 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. - */ - -#include "bu/win32_compatibility.h" - -#ifdef WIN32 - -#define deffunc( name ) \ - Bu::Winsock2::FNDEF_DYN_ ##name Bu::Winsock2::_fnptr_ ##name = NULL - -char Bu::Winsock2::scode[15]; - -deffunc( WSAStartup ); -deffunc( WSACleanup ); -deffunc( WSAGetLastError ); -deffunc( inet_ntoa ); -deffunc( inet_addr ); -deffunc( select ); -deffunc( socket ); -deffunc( shutdown ); -deffunc( ioctlsocket ); -deffunc( htons ); -deffunc( htonl ); -deffunc( gethostbyname ); -deffunc( freeaddrinfo ); -deffunc( getaddrinfo ); -deffunc( connect ); -deffunc( getpeername ); -deffunc( setsockopt ); -deffunc( bind ); -deffunc( listen ); -deffunc( accept ); -deffunc( recv ); -deffunc( send ); -deffunc( __WSAFDIsSet ); - -// Safely get a function from the library -#define getfunc( name ) \ - Bu::Winsock2::_fnptr_ ##name = (FNDEF_DYN_ ##name) \ - GetProcAddress( Ws2_32, #name ); \ - if( Bu::Winsock2::_fnptr_ ##name == NULL ) { \ - throw Bu::ExceptionBase("Error loading function " #name " from dll.");\ - } (void)0 - -Bu::Winsock2::Winsock2() -{ - Ws2_32 = LoadLibrary(TEXT("WS2_32.DLL")); - - getfunc( WSAStartup ); - getfunc( WSACleanup ); - getfunc( WSAGetLastError ); - getfunc( inet_ntoa ); - getfunc( inet_addr ); - getfunc( select ); - getfunc( socket ); - getfunc( shutdown ); - getfunc( ioctlsocket ); - getfunc( htons ); - getfunc( htonl ); - getfunc( gethostbyname ); - getfunc( freeaddrinfo ); - getfunc( getaddrinfo ); - getfunc( connect ); - getfunc( getpeername ); - getfunc( setsockopt ); - getfunc( bind ); - getfunc( listen ); - getfunc( accept ); - getfunc( recv ); - getfunc( send ); - getfunc( __WSAFDIsSet ); - - Bu::Winsock2::WSAStartup( MAKEWORD(2, 2), &wsaData ); -} - -Bu::Winsock2::~Winsock2() -{ - Bu::Winsock2::WSACleanup(); - FreeLibrary( Ws2_32 ); -} - -char *Bu::Winsock2::gai_strerror( int iCode ) -{ - sprintf( scode, "%d", Bu::Winsock2::WSAGetLastError() ); - return scode; -} - -int Bu::Winsock2::WSAStartup( WORD a, LPWSADATA b ) { - return (*Bu::Winsock2::_fnptr_WSAStartup)( a, b ); -} -int Bu::Winsock2::WSACleanup( ) { - return (*Bu::Winsock2::_fnptr_WSACleanup)(); -} -int Bu::Winsock2::WSAGetLastError( ) { - return (*Bu::Winsock2::_fnptr_WSAGetLastError)(); -} -char * Bu::Winsock2::inet_ntoa( struct in_addr a ) { - return (*Bu::Winsock2::_fnptr_inet_ntoa)( a ); -} -unsigned long Bu::Winsock2::inet_addr( const char *s_in ) { - return (*Bu::Winsock2::_fnptr_inet_addr)( s_in ); -} -int Bu::Winsock2::select( int a, fd_set *b, fd_set *c, fd_set *d, - const struct timeval *e ) { - return (*Bu::Winsock2::_fnptr_select)( a, b, c, d, e ); -} -SOCKET Bu::Winsock2::socket( int domain, int type, int protocol ) { - return (*Bu::Winsock2::_fnptr_socket)( domain, type, protocol ); -} -int Bu::Winsock2::shutdown( SOCKET s, int how ) { - return (*Bu::Winsock2::_fnptr_shutdown)( s, how ); -} -int Bu::Winsock2::ioctlsocket( SOCKET s, long cmd, u_long *argp ) { - return (*Bu::Winsock2::_fnptr_ioctlsocket)( s, cmd, argp ); -} -u_short Bu::Winsock2::htons( u_short in ) { - return (*Bu::Winsock2::_fnptr_htons)( in ); -} -u_long Bu::Winsock2::htonl( u_long in ) { - return (*Bu::Winsock2::_fnptr_htonl)( in ); -} -struct hostent * Bu::Winsock2::gethostbyname( const char *name ) { - return (*Bu::Winsock2::_fnptr_gethostbyname)( name ); -} -void Bu::Winsock2::freeaddrinfo( struct addrinfo *ai ) { - return (*Bu::Winsock2::_fnptr_freeaddrinfo)( ai ); -} -int Bu::Winsock2::getaddrinfo( const char *a, const char *b, - const struct addrinfo *c, struct addrinfo **d ) { - return (*Bu::Winsock2::_fnptr_getaddrinfo)( a, b, c, d ); -} -int Bu::Winsock2::connect( SOCKET s, const struct sockaddr *a, int b ) { - return (*Bu::Winsock2::_fnptr_connect)( s, a, b ); -} -int Bu::Winsock2::getpeername( SOCKET s, struct sockaddr *a, int *b ) { - return (*Bu::Winsock2::_fnptr_getpeername)( s, a, b); -} -int Bu::Winsock2::setsockopt( SOCKET s, int a, int b, - const char *c, int d ) { - return (*Bu::Winsock2::_fnptr_setsockopt)( s, a, b, c, d ); -} -int Bu::Winsock2::bind( SOCKET s, const struct sockaddr *a, int b ) { - return (*Bu::Winsock2::_fnptr_bind)( s, a, b ); -} -int Bu::Winsock2::listen( SOCKET s, int backlog ) { - return (*Bu::Winsock2::_fnptr_listen)( s, backlog ); -} -SOCKET Bu::Winsock2::accept( SOCKET s, struct sockaddr *a, int *b ) { - return (*Bu::Winsock2::_fnptr_accept)( s, a, b ); -} -int Bu::Winsock2::recv( SOCKET s, char *buf, int len, int flags ) { - return (*Bu::Winsock2::_fnptr_recv)( s, buf, len, flags ); -} -int Bu::Winsock2::send( SOCKET s, const char *buf, int len, int flags ) { - return (*Bu::Winsock2::_fnptr_send)( s, buf, len, flags ); -} -int Bu::Winsock2::__WSAFDIsSet( SOCKET s, fd_set *set ) { - return (*Bu::Winsock2::_fnptr___WSAFDIsSet)( s, set ); -} - -#endif - diff --git a/src/win32_compatibility.h b/src/win32_compatibility.h deleted file mode 100644 index 220ed09..0000000 --- a/src/win32_compatibility.h +++ /dev/null @@ -1,129 +0,0 @@ -/* - * Copyright (C) 2007-2010 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 - -#ifdef __cplusplus -extern "C" -{ -#include -#include -} -#endif - -#include "bu/fstring.h" -#include "bu/singleton.h" - -#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 - -#define decltype( ret, name, ... ) \ - typedef ret (__cdecl *FNDEF_DYN_ ##name)( __VA_ARGS__ ); \ - static FNDEF_DYN_ ##name _fnptr_ ##name; \ - static ret name( __VA_ARGS__ ) - -__extension__ typedef int socklen_t; - -#ifdef gai_strerror -#undef gai_strerror -#endif - -namespace Bu -{ - class Winsock2 : public Bu::Singleton - { - friend class Bu::Singleton; - private: - Winsock2(); - virtual ~Winsock2(); - - WSADATA wsaData; - HINSTANCE Ws2_32; - - public: - // decltype( return type, function name<, optional parameters> ) - decltype( int, WSAStartup, WORD, LPWSADATA ); - decltype( int, WSACleanup ); - decltype( int, WSAGetLastError ); - decltype( char *, inet_ntoa, struct in_addr ); - decltype( unsigned long, inet_addr, const char *s_in ); - decltype( int, select, int nfds, fd_set *readfds, fd_set *writefds, - fd_set *exceptfds, const struct timeval *timeout ); - decltype( SOCKET, socket, int domain, int type, int protocol ); - decltype( int, shutdown, SOCKET s, int how ); - decltype( int, ioctlsocket, SOCKET s, long cmd, u_long *argp ); - decltype( u_short, htons, u_short in ); - decltype( u_long, htonl, u_long in ); - decltype( struct hostent *, gethostbyname, const char *name ); - decltype( void, freeaddrinfo, struct addrinfo *ai ); - decltype( int, getaddrinfo, const char *nodename, const char *servname, - const struct addrinfo *hints, struct addrinfo **res ); - decltype( int, connect, SOCKET s, const struct sockaddr *serv_addr, - int addrlen ); - decltype( int, getpeername, SOCKET s, struct sockaddr *name, - int *namelen ); - decltype( int, setsockopt, SOCKET s, int level, int optname, - const char *optval, int optlen ); - decltype( int, bind, SOCKET s, const struct sockaddr *my_addr, - int addrlen ); - decltype( int, listen, SOCKET s, int backlog ); - decltype( SOCKET, accept, SOCKET s, struct sockaddr *addr, - int *addrlen); - decltype( int, recv, SOCKET s, char *buf, int len, int flags ); - 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 *gai_strerror( int iCode ); - }; -}; - -#ifdef FD_ISSET -#undef FD_ISSET -#endif - -#define bu_WSAStartup (*Bu::Winsock2::WSAStartup) -#define bu_WSACleanup (*Bu::Winsock2::WSACleanup) -#define bu_WSAGetLastError (*Bu::Winsock2::WSAGetLastError) -#define bu_inet_ntoa (*Bu::Winsock2::inet_ntoa) -#define bu_inet_addr (*Bu::Winsock2::inet_addr) -#define bu_select (*Bu::Winsock2::select) -#define bu_socket (*Bu::Winsock2::socket) -#define bu_shutdown (*Bu::Winsock2::shutdown) -#define bu_ioctlsocket (*Bu::Winsock2::ioctlsocket) -#define bu_htons (*Bu::Winsock2::htons) -#define bu_htonl (*Bu::Winsock2::htonl) -#define bu_gethostbyname (*Bu::Winsock2::gethostbyname) -#define bu_freeaddrinfo (*Bu::Winsock2::freeaddrinfo) -#define bu_getaddrinfo (*Bu::Winsock2::getaddrinfo) -#define bu_connect (*Bu::Winsock2::connect) -#define bu_getpeername (*Bu::Winsock2::getpeername) -#define bu_setsockopt (*Bu::Winsock2::setsockopt) -#define bu_bind (*Bu::Winsock2::bind) -#define bu_listen (*Bu::Winsock2::listen) -#define bu_accept (*Bu::Winsock2::accept) -#define bu_recv (*Bu::Winsock2::recv) -#define bu_send (*Bu::Winsock2::send) -#define bu___WSAFDIsSet (*Bu::Winsock2::__WSAFDIsSet) - -#define FD_ISSET (*Bu::Winsock2::__WSAFDIsSet) -#define bu_gai_strerror Bu::Winsock2::gai_strerror - -#undef decltype - -#endif /* WIN32 */ -#endif - -- cgit v1.2.3