diff --git a/src/packages/game/IZ_app.c b/src/packages/game/IZ_app.c index 5104537..070d72e 100644 --- a/src/packages/game/IZ_app.c +++ b/src/packages/game/IZ_app.c @@ -41,8 +41,15 @@ void IZ_AppHandleNetworkingOutboundEvents(IZ_App* app) { lws_get_protocol(wsi) ); - IZ_WebsocketMessage* amsg = IZ_WSClientCreateMessage(wsi, false, "hello", 5); - lws_ring_insert(vhd->ring, amsg, 1); + IZ_WebsocketMessage amsg; + i32 result; + result = IZ_WebsocketCreateTextMessage(wsi, &amsg, "hello", 5); + + if (result < 0) { + return; + } + + lws_ring_insert(vhd->ring, &amsg, 1); lws_callback_on_writable(wsi); } @@ -384,23 +391,6 @@ IZ_ProcedureResult IZ_WSClientWritable(struct lws* wsi) { return 0; } -IZ_WebsocketMessage* IZ_WSClientCreateMessage(struct lws* wsi, bool binary, void* in, size_t len) { - static IZ_WebsocketMessage amsg; - amsg.first = (char)lws_is_first_fragment(wsi); - amsg.final = (char)lws_is_final_fragment(wsi); - amsg.binary = binary; - - amsg.len = len; - /* notice we over-allocate by LWS_PRE */ - amsg.payload = malloc(LWS_PRE + len); - if (!amsg.payload) { - return NULL; - } - - memcpy((char*) amsg.payload + LWS_PRE, in, len); - return &amsg; -} - void IZ_WSClientOnReceive(struct lws* wsi, IZ_WSClientSessionData* pss, void* in, size_t len) { i32 n = (i32) lws_ring_get_count_free_elements(pss->ring); if (!n) { @@ -416,8 +406,13 @@ void IZ_WSClientOnReceive(struct lws* wsi, IZ_WSClientSessionData* pss, void* in // lwsl_hexdump_notice(in, len); - IZ_WebsocketMessage* amsg = IZ_WSClientCreateMessage(wsi, (bool) lws_frame_is_binary(wsi), in, len); - if (!amsg) { + IZ_WebsocketMessage amsg; + i32 result = ( + lws_frame_is_binary(wsi) + ? IZ_WebsocketCreateBinaryMessage(wsi, &amsg, in, len) + : IZ_WebsocketCreateTextMessage(wsi, &amsg, in, len) + ); + if (result < 0) { lwsl_user("OOM: dropping\n"); return; } @@ -428,14 +423,14 @@ void IZ_WSClientOnReceive(struct lws* wsi, IZ_WSClientSessionData* pss, void* in ); IZ_App* app = (IZ_App*) vhd->app; - if (amsg->binary) { + if (amsg.binary) { IZ_AppHandleNetworkingInboundBinaryEvents(app, in, len); } else { IZ_AppHandleNetworkingInboundTextEvents(app, in, len); } - if (!lws_ring_insert(pss->ring, amsg, 1)) { - IZ_WebsocketDestroyMessage(amsg); + if (!lws_ring_insert(pss->ring, &amsg, 1)) { + IZ_WebsocketDestroyMessage(&amsg); lwsl_user("dropping!\n"); return; } diff --git a/src/packages/game/net/core/IZ_websocket.c b/src/packages/game/net/core/IZ_websocket.c index 1a863ba..cecc301 100644 --- a/src/packages/game/net/core/IZ_websocket.c +++ b/src/packages/game/net/core/IZ_websocket.c @@ -19,7 +19,8 @@ void IZ_WebsocketTeardown(IZ_Websocket* ws) { ws->context = NULL; } -void IZ_WebsocketDestroyMessage(IZ_WebsocketMessage* msg) { +void IZ_WebsocketDestroyMessage(void* msg_raw) { + IZ_WebsocketMessage* msg = msg_raw; free(msg->payload); msg->payload = NULL; msg->len = 0; diff --git a/src/packages/game/net/core/IZ_websocket.h b/src/packages/game/net/core/IZ_websocket.h index a535a75..f458c97 100644 --- a/src/packages/game/net/core/IZ_websocket.h +++ b/src/packages/game/net/core/IZ_websocket.h @@ -33,7 +33,7 @@ IZ_ProcedureResult IZ_WebsocketCreateBinaryMessage(struct lws*, IZ_WebsocketMess IZ_ProcedureResult IZ_WebsocketCreateTextMessage(struct lws*, IZ_WebsocketMessage*, void*, size_t); -void IZ_WebsocketDestroyMessage(IZ_WebsocketMessage*); +void IZ_WebsocketDestroyMessage(void*); void IZ_WebsocketCancelService(IZ_Websocket*); diff --git a/src/packages/game/net/svc/IZ_wsclient.h b/src/packages/game/net/svc/IZ_wsclient.h index 35310c8..9064aaf 100644 --- a/src/packages/game/net/svc/IZ_wsclient.h +++ b/src/packages/game/net/svc/IZ_wsclient.h @@ -64,6 +64,4 @@ IZ_ProcedureResult IZ_WSClientWritable(struct lws*); void IZ_WSClientOnReceive(struct lws*, IZ_WSClientSessionData*, void*, size_t); -IZ_WebsocketMessage* IZ_WSClientCreateMessage(struct lws*, bool, void*, size_t); - #endif