diff --git a/cmake/platforms/emscripten.cmake b/cmake/platforms/emscripten.cmake index 6948845..2c78e8c 100644 --- a/cmake/platforms/emscripten.cmake +++ b/cmake/platforms/emscripten.cmake @@ -19,7 +19,9 @@ set(USE_HTTP OFF CACHE INTERNAL "") set(CMAKE_INTERPROCEDURAL_OPTIMIZATION FALSE) list(APPEND CLIENT_LINK_OPTIONS + -lwebsocket.js -sTOTAL_MEMORY=256MB + -sALLOW_MEMORY_GROWTH=1 -sSTACK_SIZE=5MB -sMIN_WEBGL_VERSION=1 -sMAX_WEBGL_VERSION=2 diff --git a/code/qcommon/net_chan.c b/code/qcommon/net_chan.c index e1a27fd..711b036 100644 --- a/code/qcommon/net_chan.c +++ b/code/qcommon/net_chan.c @@ -535,6 +535,13 @@ void NET_FlushPacketQueue(void) } void NET_SendPacket( netsrc_t sock, int length, const void *data, netadr_t to ) { +#ifdef __EMSCRIPTEN__ + if (sock == NS_CLIENT && to.type != NA_LOOPBACK && to.type != NA_BOT && to.type != NA_BAD) { + extern void Q3WS_SendPacket(const void *data, int length, netadr_t to); + Q3WS_SendPacket(data, length, to); + return; + } +#endif // sequenced packets are shown in netchan, so just show oob if ( showpackets->integer && *(int *)data == -1 ) { diff --git a/code/qcommon/net_ip.c b/code/qcommon/net_ip.c index 294e4c0..e1c6d14 100644 --- a/code/qcommon/net_ip.c +++ b/code/qcommon/net_ip.c @@ -23,6 +23,109 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA #include "../qcommon/q_shared.h" #include "../qcommon/qcommon.h" +#ifdef __EMSCRIPTEN__ +#include +#define Q3WS_QUEUE_CAPACITY 32 +#define Q3WS_MAX_PACKET 1400 +typedef struct { int length; byte data[Q3WS_MAX_PACKET]; } q3ws_packet_t; +static EMSCRIPTEN_WEBSOCKET_T q3ws_socket; +static q3ws_packet_t q3ws_queue[Q3WS_QUEUE_CAPACITY]; +static q3ws_packet_t q3ws_pending[Q3WS_QUEUE_CAPACITY]; +static unsigned int q3ws_head, q3ws_count, q3ws_pending_head, q3ws_pending_count; +static netadr_t q3ws_remote_address; +static qboolean q3ws_remote_valid; + +static qboolean Q3WS_QueuePending(const void *data, int length) { + unsigned int tail; + if (q3ws_pending_count >= Q3WS_QUEUE_CAPACITY) { + Com_Printf("Q3WS: outbound queue full; dropping datagram\n"); + return qfalse; + } + tail = (q3ws_pending_head + q3ws_pending_count) % Q3WS_QUEUE_CAPACITY; + q3ws_pending[tail].length = length; + memcpy(q3ws_pending[tail].data, data, (size_t)length); + q3ws_pending_count++; + return qtrue; +} + +static qboolean Q3WS_FlushPending(void) { + unsigned short readyState = 0; + if (q3ws_socket <= 0 || emscripten_websocket_get_ready_state(q3ws_socket, &readyState) != EMSCRIPTEN_RESULT_SUCCESS || readyState != 1) return qfalse; + while (q3ws_pending_count > 0) { + q3ws_packet_t *packet = &q3ws_pending[q3ws_pending_head]; + if (emscripten_websocket_send_binary(q3ws_socket, packet->data, (uint32_t)packet->length) != EMSCRIPTEN_RESULT_SUCCESS) { + Com_Printf("Q3WS: failed to send pending datagram\n"); + return qfalse; + } + q3ws_pending_head = (q3ws_pending_head + 1) % Q3WS_QUEUE_CAPACITY; + q3ws_pending_count--; + } + return qtrue; +} + +static EM_BOOL Q3WS_OnOpen(int eventType, const EmscriptenWebSocketOpenEvent *event, void *userData) { + (void)eventType; (void)event; (void)userData; + Com_Printf("Q3WS: connected\n"); + Q3WS_FlushPending(); + return EM_TRUE; +} +static EM_BOOL Q3WS_OnError(int eventType, const EmscriptenWebSocketErrorEvent *event, void *userData) { + (void)eventType; (void)event; (void)userData; + Com_Printf("Q3WS: WebSocket error\n"); + return EM_TRUE; +} +static EM_BOOL Q3WS_OnMessage(int eventType, const EmscriptenWebSocketMessageEvent *event, void *userData) { + unsigned int tail; + (void)eventType; (void)userData; + /* Reject non-datagrams and drop newest when the bounded receive queue is full. */ + if (event->isText || event->numBytes == 0 || event->numBytes > Q3WS_MAX_PACKET || q3ws_count == Q3WS_QUEUE_CAPACITY) return EM_FALSE; + tail = (q3ws_head + q3ws_count) % Q3WS_QUEUE_CAPACITY; + q3ws_queue[tail].length = (int)event->numBytes; + memcpy(q3ws_queue[tail].data, event->data, event->numBytes); + q3ws_count++; + return EM_TRUE; +} + +static void Q3WS_Init(void) { + EmscriptenWebSocketCreateAttributes attributes; + cvar_t *url = Cvar_Get("q3_ws_url", "ws://127.0.0.1:27961/ws", CVAR_ARCHIVE); + if (!emscripten_websocket_is_supported()) { Com_Printf("Q3WS: browser WebSocket API unavailable\n"); return; } + emscripten_websocket_init_create_attributes(&attributes); + attributes.url = url->string; + attributes.createOnMainThread = EM_TRUE; + q3ws_socket = emscripten_websocket_new(&attributes); + if (q3ws_socket <= 0) { Com_Printf("Q3WS: failed to create WebSocket (%d)\n", q3ws_socket); return; } + emscripten_websocket_set_onopen_callback(q3ws_socket, NULL, Q3WS_OnOpen); + emscripten_websocket_set_onerror_callback(q3ws_socket, NULL, Q3WS_OnError); + emscripten_websocket_set_onmessage_callback(q3ws_socket, NULL, Q3WS_OnMessage); + Com_Printf("Q3WS: connecting to %s\n", url->string); +} + +void Q3WS_SendPacket(const void *data, int length, netadr_t to) { + unsigned short readyState = 0; + if (length <= 0 || length > Q3WS_MAX_PACKET || q3ws_socket <= 0) return; + q3ws_remote_address = to; + q3ws_remote_valid = qtrue; + if (emscripten_websocket_get_ready_state(q3ws_socket, &readyState) != EMSCRIPTEN_RESULT_SUCCESS) return; + if (readyState == 0) { + Q3WS_QueuePending(data, length); + return; + } + if (readyState == 1) { + if (!Q3WS_FlushPending()) { + Q3WS_QueuePending(data, length); + return; + } + if (emscripten_websocket_send_binary(q3ws_socket, (void *)data, (uint32_t)length) != EMSCRIPTEN_RESULT_SUCCESS) { + Com_Printf("Q3WS: failed to send datagram\n"); + Q3WS_QueuePending(data, length); + } + return; + } + Com_Printf("Q3WS: socket is not open (readyState=%u); dropping datagram\n", readyState); +} +#endif + #ifdef _WIN32 # include # include @@ -529,6 +632,19 @@ Receive one packet */ qboolean NET_GetPacket(netadr_t *net_from, msg_t *net_message, fd_set *fdr) { +#ifdef __EMSCRIPTEN__ + while (q3ws_count > 0 && q3ws_remote_valid) { + q3ws_packet_t *packet = &q3ws_queue[q3ws_head]; + q3ws_head = (q3ws_head + 1) % Q3WS_QUEUE_CAPACITY; + q3ws_count--; + if (packet->length > net_message->maxsize) continue; + memcpy(net_message->data, packet->data, (size_t)packet->length); + net_message->readcount = 0; + net_message->cursize = packet->length; + *net_from = q3ws_remote_address; + return qtrue; + } +#endif int ret; struct sockaddr_storage from; socklen_t fromlen; @@ -1578,6 +1694,9 @@ NET_Init ==================== */ void NET_Init( void ) { +#ifdef __EMSCRIPTEN__ + Q3WS_Init(); +#endif #ifdef _WIN32 int r; @@ -1661,6 +1780,12 @@ Sleeps msec or until something happens on the network */ void NET_Sleep(int msec) { +#ifdef __EMSCRIPTEN__ + fd_set emptySet; + FD_ZERO(&emptySet); + NET_Event(&emptySet); + return; +#endif struct timeval timeout; fd_set fdr; int retval; diff --git a/code/client/cl_main.c b/code/client/cl_main.c --- a/code/client/cl_main.c +++ b/code/client/cl_main.c @@ -22,4 +22,29 @@ // cl_main.c -- client main loop #include "client.h" +#ifdef __EMSCRIPTEN__ +#include + +int EMSCRIPTEN_KEEPALIVE Q3Web_IsConnected(void) { + return clc.state == CA_ACTIVE && !clc.demoplaying; +} + +/* True while the server has a vote running: it sets CS_VOTE_TIME for every client then. */ +int EMSCRIPTEN_KEEPALIVE Q3Web_VoteInProgress(void) { + int offset = cl.gameState.stringOffsets[CS_VOTE_TIME]; + return offset && cl.gameState.stringData[offset] ? 1 : 0; +} + +int EMSCRIPTEN_KEEPALIVE Q3Web_RequestMapVote(int mapIndex) { + if (clc.state != CA_ACTIVE || clc.demoplaying) return 0; + switch (mapIndex) { + case 0: Cbuf_AddText("callvote map q3dm17\n"); break; + case 1: Cbuf_AddText("callvote map q3dm6\n"); break; + case 2: Cbuf_AddText("callvote map q3dm7\n"); break; + case 3: Cbuf_AddText("callvote map q3dm13\n"); break; + default: return 0; + } + return 1; +} +#endif #include