Преглед на файлове

Unify message creation

Use common method for creating messages in client and server.
feature/data-structs
TheoryOfNekomata преди 2 години
родител
ревизия
8661cdc290
променени са 4 файла, в които са добавени 22 реда и са изтрити 28 реда
  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 Целия файл

@@ -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;
}


+ 2
- 1
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;


+ 1
- 1
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*);



+ 0
- 2
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

Зареждане…
Отказ
Запис