Browse Source

Unify message creation

Use common method for creating messages in client and server.
feature/data-structs
TheoryOfNekomata 2 years ago
parent
commit
8661cdc290
4 changed files with 22 additions and 28 deletions
  1. +19
    -24
      src/packages/game/IZ_app.c
  2. +2
    -1
      src/packages/game/net/core/IZ_websocket.c
  3. +1
    -1
      src/packages/game/net/core/IZ_websocket.h
  4. +0
    -2
      src/packages/game/net/svc/IZ_wsclient.h

+ 19
- 24
src/packages/game/IZ_app.c View File

@@ -41,8 +41,15 @@ void IZ_AppHandleNetworkingOutboundEvents(IZ_App* app) {
lws_get_protocol(wsi) 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); lws_callback_on_writable(wsi);
} }


@@ -384,23 +391,6 @@ IZ_ProcedureResult IZ_WSClientWritable(struct lws* wsi) {
return 0; 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) { 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); i32 n = (i32) lws_ring_get_count_free_elements(pss->ring);
if (!n) { if (!n) {
@@ -416,8 +406,13 @@ void IZ_WSClientOnReceive(struct lws* wsi, IZ_WSClientSessionData* pss, void* in


// lwsl_hexdump_notice(in, len); // 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"); lwsl_user("OOM: dropping\n");
return; return;
} }
@@ -428,14 +423,14 @@ void IZ_WSClientOnReceive(struct lws* wsi, IZ_WSClientSessionData* pss, void* in
); );


IZ_App* app = (IZ_App*) vhd->app; IZ_App* app = (IZ_App*) vhd->app;
if (amsg->binary) {
if (amsg.binary) {
IZ_AppHandleNetworkingInboundBinaryEvents(app, in, len); IZ_AppHandleNetworkingInboundBinaryEvents(app, in, len);
} else { } else {
IZ_AppHandleNetworkingInboundTextEvents(app, in, len); 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"); lwsl_user("dropping!\n");
return; return;
} }


+ 2
- 1
src/packages/game/net/core/IZ_websocket.c View File

@@ -19,7 +19,8 @@ void IZ_WebsocketTeardown(IZ_Websocket* ws) {
ws->context = NULL; ws->context = NULL;
} }


void IZ_WebsocketDestroyMessage(IZ_WebsocketMessage* msg) {
void IZ_WebsocketDestroyMessage(void* msg_raw) {
IZ_WebsocketMessage* msg = msg_raw;
free(msg->payload); free(msg->payload);
msg->payload = NULL; msg->payload = NULL;
msg->len = 0; msg->len = 0;


+ 1
- 1
src/packages/game/net/core/IZ_websocket.h View File

@@ -33,7 +33,7 @@ IZ_ProcedureResult IZ_WebsocketCreateBinaryMessage(struct lws*, IZ_WebsocketMess


IZ_ProcedureResult IZ_WebsocketCreateTextMessage(struct lws*, IZ_WebsocketMessage*, void*, size_t); 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*); void IZ_WebsocketCancelService(IZ_Websocket*);




+ 0
- 2
src/packages/game/net/svc/IZ_wsclient.h View File

@@ -64,6 +64,4 @@ IZ_ProcedureResult IZ_WSClientWritable(struct lws*);


void IZ_WSClientOnReceive(struct lws*, IZ_WSClientSessionData*, void*, size_t); void IZ_WSClientOnReceive(struct lws*, IZ_WSClientSessionData*, void*, size_t);


IZ_WebsocketMessage* IZ_WSClientCreateMessage(struct lws*, bool, void*, size_t);

#endif #endif

Loading…
Cancel
Save