From d15dc9f40f5a20bff452547a2dcb15231f9f969d Mon Sep 17 00:00:00 2001 From: Patric Stout Date: Sat, 5 Dec 2020 21:57:47 +0100 Subject: Add: support for emscripten (play-OpenTTD-in-the-browser) Emscripten compiles to WASM, which can be loaded via HTML / JavaScript. This allows you to play OpenTTD inside a browser. Co-authored-by: milek7 --- src/network/core/address.cpp | 10 +++++++++- src/network/core/os_abstraction.h | 24 +++++++++++++++++++++--- 2 files changed, 30 insertions(+), 4 deletions(-) (limited to 'src/network/core') diff --git a/src/network/core/address.cpp b/src/network/core/address.cpp index c2fecc7ff..1aaa0b5fb 100644 --- a/src/network/core/address.cpp +++ b/src/network/core/address.cpp @@ -299,7 +299,15 @@ static SOCKET ConnectLoopProc(addrinfo *runp) if (!SetNoDelay(sock)) DEBUG(net, 1, "[%s] setting TCP_NODELAY failed", type); - if (connect(sock, runp->ai_addr, (int)runp->ai_addrlen) != 0) { + int err = connect(sock, runp->ai_addr, (int)runp->ai_addrlen); +#ifdef __EMSCRIPTEN__ + /* Emscripten is asynchronous, and as such a connect() is still in + * progress by the time the call returns. */ + if (err != 0 && errno != EINPROGRESS) +#else + if (err != 0) +#endif + { DEBUG(net, 1, "[%s] could not connect %s socket: %s", type, family, strerror(errno)); closesocket(sock); return INVALID_SOCKET; diff --git a/src/network/core/os_abstraction.h b/src/network/core/os_abstraction.h index 01ab68b27..be8b8f919 100644 --- a/src/network/core/os_abstraction.h +++ b/src/network/core/os_abstraction.h @@ -83,6 +83,16 @@ typedef unsigned long in_addr_t; # include # include # include + +# if defined(__EMSCRIPTEN__) +/* Emscripten doesn't support AI_ADDRCONFIG and errors out on it. */ +# undef AI_ADDRCONFIG +# define AI_ADDRCONFIG 0 +/* Emscripten says it supports FD_SETSIZE fds, but it really only supports 64. + * https://github.com/emscripten-core/emscripten/issues/1711 */ +# undef FD_SETSIZE +# define FD_SETSIZE 64 +# endif #endif /* UNIX */ /* OS/2 stuff */ @@ -148,12 +158,16 @@ typedef unsigned long in_addr_t; */ static inline bool SetNonBlocking(SOCKET d) { -#ifdef _WIN32 - u_long nonblocking = 1; +#ifdef __EMSCRIPTEN__ + return true; #else +# ifdef _WIN32 + u_long nonblocking = 1; +# else int nonblocking = 1; -#endif +# endif return ioctlsocket(d, FIONBIO, &nonblocking) == 0; +#endif } /** @@ -163,10 +177,14 @@ static inline bool SetNonBlocking(SOCKET d) */ static inline bool SetNoDelay(SOCKET d) { +#ifdef __EMSCRIPTEN__ + return true; +#else /* XXX should this be done at all? */ int b = 1; /* The (const char*) cast is needed for windows */ return setsockopt(d, IPPROTO_TCP, TCP_NODELAY, (const char*)&b, sizeof(b)) == 0; +#endif } /* Make sure these structures have the size we expect them to be */ -- cgit v1.2.3-70-g09d2