Skip to content

Commit

Permalink
nxlink: fix Windows build
Browse files Browse the repository at this point in the history
  • Loading branch information
fincs committed Dec 2, 2020
1 parent 1b660e2 commit c2977a7
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 4 deletions.
2 changes: 1 addition & 1 deletion configure.ac
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ NET_LIBS=""
case "$host" in
*-*-mingw*)
NET_LIBS="-lws2_32"
CFLAGS="$CFLAGS -D__USE_MINGW_ANSI_STDIO"
CFLAGS="$CFLAGS -D__USE_MINGW_ANSI_STDIO -D_WIN32_WINNT=0x0600"
;;
esac

Expand Down
25 changes: 22 additions & 3 deletions src/nxlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,12 @@ typedef uint32_t in_addr_t;
#define SHUT_RD SD_RECEIVE
#define SHUT_WR SD_SEND
#define SHUT_RDWR SD_BOTH
#ifndef EWOULDBLOCK
#ifdef EWOULDBLOCK
#undef EWOULDBLOCK
#endif
#define EWOULDBLOCK WSAEWOULDBLOCK
#define poll WSAPoll
#endif
#endif

#include <zlib.h>
#include <assert.h>
Expand All @@ -54,13 +55,21 @@ static void shutdownSocket(int socket, int flags) {
static int setSocketNonblocking(int sock) {
//---------------------------------------------------------------------------------

#ifndef __WIN32__
int flags = fcntl(sock, F_GETFL);

if (flags == -1) return -1;

int rc = fcntl(sock, F_SETFL, flags | O_NONBLOCK);

if (rc != 0) return -1;
#else
u_long iMode = 1; // non-blocking

int rc = ioctlsocket(sock, FIONBIO, &iMode);

if (rc != NO_ERROR) return -1;
#endif

return 0;
}
Expand Down Expand Up @@ -92,7 +101,11 @@ static int socketError(const char *msg) {
//---------------------------------------------------------------------------------
int pollSocket(int fd, int events, int timeout) {
//---------------------------------------------------------------------------------
#ifndef __WIN32__
struct pollfd pfd;
#else
WSAPOLLFD pfd;
#endif

pfd.fd = fd;
pfd.events = events;
Expand Down Expand Up @@ -463,6 +476,12 @@ static int addExtraArgs(int len, char *buf, char *extra_args) {

#define NRO_ARGS 1000

#ifdef __WIN32__
static void win32_socket_cleanup(void) {
WSACleanup();
}
#endif

//---------------------------------------------------------------------------------
int main(int argc, char **argv) {
//---------------------------------------------------------------------------------
Expand Down Expand Up @@ -595,7 +614,7 @@ int main(int argc, char **argv) {
printf ("WSAStartup failed\n");
return EXIT_FAILURE;
}
atexit(&WSACleanup);
atexit(&win32_socket_cleanup);
#endif

struct in_addr nxaddr;
Expand Down

0 comments on commit c2977a7

Please sign in to comment.