diff options
author | Mike Buland <eichlan@xagasoft.com> | 2009-01-16 23:55:53 +0000 |
---|---|---|
committer | Mike Buland <eichlan@xagasoft.com> | 2009-01-16 23:55:53 +0000 |
commit | 8c1f4d7bace6ff2c99d546cedaba890b349e88f8 (patch) | |
tree | e5c6e29deaaa7db42fbc75d0fdef32457bb0c8f2 /src/win32_compatibility.h | |
parent | 0a43e790cf9798948ab989abe88f890813c935df (diff) | |
download | libbu++-8c1f4d7bace6ff2c99d546cedaba890b349e88f8.tar.gz libbu++-8c1f4d7bace6ff2c99d546cedaba890b349e88f8.tar.bz2 libbu++-8c1f4d7bace6ff2c99d546cedaba890b349e88f8.tar.xz libbu++-8c1f4d7bace6ff2c99d546cedaba890b349e88f8.zip |
I...think that's a little better. Wow, function pointers in windows have a
lot of problems. This may require a little more research, but basically, you
can't just call them inline wherever you'd like. I managed to get it to work
by providing simple one line wrapper functions for each function we acquired as
a pointer. Crazy mess. Anyway, it should load the library just once now, and
Bu::Socket looks a little bit cleaner, but not a heck of a lot.
I also added some more docs and removed the author references.
Diffstat (limited to '')
-rw-r--r-- | src/win32_compatibility.h | 110 |
1 files changed, 76 insertions, 34 deletions
diff --git a/src/win32_compatibility.h b/src/win32_compatibility.h index 2d3c52b..36613d1 100644 --- a/src/win32_compatibility.h +++ b/src/win32_compatibility.h | |||
@@ -9,12 +9,6 @@ | |||
9 | #define WIN32_COMPATIBILITY__H | 9 | #define WIN32_COMPATIBILITY__H |
10 | 10 | ||
11 | #ifdef WIN32 | 11 | #ifdef WIN32 |
12 | #define DYNLOAD DynamicWinsock2:: | ||
13 | #else | ||
14 | #define DYNLOAD | ||
15 | #endif | ||
16 | |||
17 | #ifdef WIN32 | ||
18 | 12 | ||
19 | #ifdef __cplusplus | 13 | #ifdef __cplusplus |
20 | extern "C" | 14 | extern "C" |
@@ -36,37 +30,19 @@ extern "C" | |||
36 | __result; })) | 30 | __result; })) |
37 | #endif | 31 | #endif |
38 | 32 | ||
33 | #define decltype( ret, name, ... ) \ | ||
34 | typedef ret (__cdecl *FNDEF_DYN_ ##name)( __VA_ARGS__ ); \ | ||
35 | static FNDEF_DYN_ ##name _fnptr_ ##name; \ | ||
36 | static ret name( __VA_ARGS__ ) | ||
37 | |||
39 | __extension__ typedef int socklen_t; | 38 | __extension__ typedef int socklen_t; |
40 | 39 | ||
41 | namespace DynamicWinsock2 | 40 | #ifdef gai_strerror |
42 | { | 41 | #undef gai_strerror |
43 | int WSAStartup(WORD wVersionRequested,LPWSADATA lpWSAData); | 42 | #endif |
44 | int WSACleanup(void); | ||
45 | int WSAGetLastError(); | ||
46 | void inet_ntoa( Bu::FString &out, struct in_addr addr_in ); | ||
47 | unsigned long inet_addr( const char *s_in ); | ||
48 | int select(int nfds, fd_set *readfds, fd_set *writefds, | ||
49 | fd_set *exceptfds, const struct timeval *timeout); | ||
50 | SOCKET socket(int domain, int type, int protocol); | ||
51 | int ioctlsocket(SOCKET s, long cmd, u_long *argp); | ||
52 | u_short htons(u_short in); | ||
53 | u_long htonl(u_long in); | ||
54 | struct hostent *gethostbyname(const char *name); | ||
55 | void freeaddrinfo(struct addrinfo *ai); | ||
56 | int getaddrinfo( | ||
57 | const char *nodename, const char *servname, | ||
58 | const struct addrinfo *hints, struct addrinfo **res ); | ||
59 | int connect(SOCKET s, const struct sockaddr *serv_addr, int addrlen); | ||
60 | int getpeername(SOCKET s, struct sockaddr *name, int *namelen); | ||
61 | int setsockopt(SOCKET s, int level, int optname, | ||
62 | const char *optval, int optlen); | ||
63 | int bind(SOCKET s, const struct sockaddr *my_addr, int addrlen); | ||
64 | int listen(SOCKET s, int backlog); | ||
65 | SOCKET accept(SOCKET s, struct sockaddr *addr, int *addrlen); | ||
66 | int recv( SOCKET s, char *buf, int len, int flags ); | ||
67 | int send( SOCKET s, const char *buf, int len, int flags ); | ||
68 | int DYN_FD_ISSET(SOCKET s, fd_set *set); | ||
69 | 43 | ||
44 | namespace Bu | ||
45 | { | ||
70 | class Winsock2 : public Bu::Singleton<Winsock2> | 46 | class Winsock2 : public Bu::Singleton<Winsock2> |
71 | { | 47 | { |
72 | friend class Bu::Singleton<Winsock2>; | 48 | friend class Bu::Singleton<Winsock2>; |
@@ -75,11 +51,77 @@ namespace DynamicWinsock2 | |||
75 | virtual ~Winsock2(); | 51 | virtual ~Winsock2(); |
76 | 52 | ||
77 | WSADATA wsaData; | 53 | WSADATA wsaData; |
54 | HINSTANCE Ws2_32; | ||
78 | 55 | ||
79 | public: | 56 | public: |
57 | // decltype( return type, function name<, optional parameters> ) | ||
58 | decltype( int, WSAStartup, WORD, LPWSADATA ); | ||
59 | decltype( int, WSACleanup ); | ||
60 | decltype( int, WSAGetLastError ); | ||
61 | decltype( char *, inet_ntoa, struct in_addr ); | ||
62 | decltype( unsigned long, inet_addr, const char *s_in ); | ||
63 | decltype( int, select, int nfds, fd_set *readfds, fd_set *writefds, | ||
64 | fd_set *exceptfds, const struct timeval *timeout ); | ||
65 | decltype( SOCKET, socket, int domain, int type, int protocol ); | ||
66 | decltype( int, ioctlsocket, SOCKET s, long cmd, u_long *argp ); | ||
67 | decltype( u_short, htons, u_short in ); | ||
68 | decltype( u_long, htonl, u_long in ); | ||
69 | decltype( struct hostent *, gethostbyname, const char *name ); | ||
70 | decltype( void, freeaddrinfo, struct addrinfo *ai ); | ||
71 | decltype( int, getaddrinfo, const char *nodename, const char *servname, | ||
72 | const struct addrinfo *hints, struct addrinfo **res ); | ||
73 | decltype( int, connect, SOCKET s, const struct sockaddr *serv_addr, | ||
74 | int addrlen ); | ||
75 | decltype( int, getpeername, SOCKET s, struct sockaddr *name, | ||
76 | int *namelen ); | ||
77 | decltype( int, setsockopt, SOCKET s, int level, int optname, | ||
78 | const char *optval, int optlen ); | ||
79 | decltype( int, bind, SOCKET s, const struct sockaddr *my_addr, | ||
80 | int addrlen ); | ||
81 | decltype( int, listen, SOCKET s, int backlog ); | ||
82 | decltype( SOCKET, accept, SOCKET s, struct sockaddr *addr, | ||
83 | int *addrlen); | ||
84 | decltype( int, recv, SOCKET s, char *buf, int len, int flags ); | ||
85 | decltype( int, send, SOCKET s, const char *buf, int len, int flags ); | ||
86 | decltype( int, __WSAFDIsSet, SOCKET s, fd_set *set ); | ||
87 | |||
88 | static char scode[15]; | ||
89 | static char *gai_strerror( int iCode ); | ||
80 | }; | 90 | }; |
81 | }; | 91 | }; |
82 | 92 | ||
93 | #ifdef FD_ISSET | ||
94 | #undef FD_ISSET | ||
95 | #endif | ||
96 | |||
97 | #define bu_WSAStartup (*Bu::Winsock2::WSAStartup) | ||
98 | #define bu_WSACleanup (*Bu::Winsock2::WSACleanup) | ||
99 | #define bu_WSAGetLastError (*Bu::Winsock2::WSAGetLastError) | ||
100 | #define bu_inet_ntoa (*Bu::Winsock2::inet_ntoa) | ||
101 | #define bu_inet_addr (*Bu::Winsock2::inet_addr) | ||
102 | #define bu_select (*Bu::Winsock2::select) | ||
103 | #define bu_socket (*Bu::Winsock2::socket) | ||
104 | #define bu_ioctlsocket (*Bu::Winsock2::ioctlsocket) | ||
105 | #define bu_htons (*Bu::Winsock2::htons) | ||
106 | #define bu_htonl (*Bu::Winsock2::htonl) | ||
107 | #define bu_gethostbyname (*Bu::Winsock2::gethostbyname) | ||
108 | #define bu_freeaddrinfo (*Bu::Winsock2::freeaddrinfo) | ||
109 | #define bu_getaddrinfo (*Bu::Winsock2::getaddrinfo) | ||
110 | #define bu_connect (*Bu::Winsock2::connect) | ||
111 | #define bu_getpeername (*Bu::Winsock2::getpeername) | ||
112 | #define bu_setsockopt (*Bu::Winsock2::setsockopt) | ||
113 | #define bu_bind (*Bu::Winsock2::bind) | ||
114 | #define bu_listen (*Bu::Winsock2::listen) | ||
115 | #define bu_accept (*Bu::Winsock2::accept) | ||
116 | #define bu_recv (*Bu::Winsock2::recv) | ||
117 | #define bu_send (*Bu::Winsock2::send) | ||
118 | #define bu___WSAFDIsSet (*Bu::Winsock2::__WSAFDIsSet) | ||
119 | |||
120 | #define FD_ISSET (*Bu::Winsock2::__WSAFDIsSet) | ||
121 | #define bu_gai_strerror Bu::Winsock2::gai_strerror | ||
122 | |||
123 | #undef decltype | ||
124 | |||
83 | #endif /* WIN32 */ | 125 | #endif /* WIN32 */ |
84 | #endif | 126 | #endif |
85 | 127 | ||