From 4be79ace7cf8ac3e9a13a62260f751414f6c9fa8 Mon Sep 17 00:00:00 2001 From: Stephen Seo Date: Wed, 6 Mar 2019 13:23:22 +0900 Subject: [PATCH] Fix received callback, minor change to NetworkTest --- src/UDPConnection.c | 6 +++++- src/UDPConnection.h | 4 ++-- src/test/UDPC_NetworkTest.c | 43 +++++++++++++++++++++++++++++++++++-- 3 files changed, 48 insertions(+), 5 deletions(-) diff --git a/src/UDPConnection.c b/src/UDPConnection.c index 488ba16..f626001 100644 --- a/src/UDPConnection.c +++ b/src/UDPConnection.c @@ -279,7 +279,11 @@ void UDPC_check_events(UDPC_Context *ctx) { UDPC_INTERNAL_PacketInfo *pinfo = UDPC_Deque_index_ptr( ctx->receivedPackets, sizeof(UDPC_INTERNAL_PacketInfo), x); - ctx->callbackReceived(ctx->callbackReceivedUserData, pinfo->data, pinfo->size); + ctx->callbackReceived( + ctx->callbackReceivedUserData, + pinfo->addr, + pinfo->data, + pinfo->size); free(pinfo->data); } UDPC_Deque_clear(ctx->receivedPackets); diff --git a/src/UDPConnection.h b/src/UDPConnection.h index 91af890..af5cdc2 100644 --- a/src/UDPConnection.h +++ b/src/UDPConnection.h @@ -39,14 +39,14 @@ typedef void (*UDPC_callback_connected)(void*, uint32_t); */ typedef void (*UDPC_callback_disconnected)(void*, uint32_t); -/// (void *userData, char *packetData, uint32_t packetSize) +/// (void *userData, uint32_t address, char *packetData, uint32_t packetSize) /*! * The data pointed to by the packetData argument is to data internally managed * by the UDPC_Context. It will change every time this callback is called so do * not depend on it persisting. This means you should copy the data out of it * when the callback is invoked and work with the copied data. */ -typedef void (*UDPC_callback_received)(void*, char*, uint32_t); +typedef void (*UDPC_callback_received)(void*, uint32_t, char*, uint32_t); /// This struct should not be used outside of this library typedef struct { diff --git a/src/test/UDPC_NetworkTest.c b/src/test/UDPC_NetworkTest.c index 4e8475d..f05716d 100644 --- a/src/test/UDPC_NetworkTest.c +++ b/src/test/UDPC_NetworkTest.c @@ -9,6 +9,7 @@ typedef struct { int isConnected; int hasConnectedOnce; + uint32_t addr; } TestContext; void printUsage() @@ -21,6 +22,7 @@ void conCallback(void *userdata, uint32_t addr) TestContext *ctx = userdata; ctx->isConnected = 1; ctx->hasConnectedOnce = 1; + ctx->addr = addr; printf("Connected callback called\n"); } @@ -31,8 +33,33 @@ void discCallback(void *userdata, uint32_t addr) printf("Disconnected callback called\n"); } -void recCallback(void *userdata, char *data, uint32_t size) +void recCallback(void *userdata, uint32_t addr, char *data, uint32_t size) { + TestContext *ctx = userdata; + if(ctx->addr == addr && size == 7 && data[6] == '\0') + { + printf("Got %s\n", data); + } +} + +void updateSendBuffer(uint32_t *index, char *buffer) +{ + ++(*index); + if(*index >= 26 * 26 * 26 * 26 * 26 * 26) + { + *index = 0; + } + + uint32_t temp; + for(int x = 0; x < 6; ++x) + { + temp = 1; + for(int y = 0; y < x; ++y) + { + temp *= 26; + } + buffer[x] = (*index / temp) % 26 + 'a'; + } } int main(int argc, char** argv) @@ -44,6 +71,13 @@ int main(int argc, char** argv) uint16_t listenPort = 0; TestContext testCtx = {0, 0}; + uint32_t sendIndex = 0; + char sendBuffer[7] = { + 'a', 'a', 'a', + 'a', 'a', 'a', + '\0' + }; + --argc; ++argv; while(argc > 0) { @@ -99,7 +133,7 @@ int main(int argc, char** argv) UDPC_set_logging_type(ctx, 4); UDPC_set_callback_connected(ctx, conCallback, &testCtx); UDPC_set_callback_disconnected(ctx, discCallback, &testCtx); - UDPC_set_callback_received(ctx, recCallback, NULL); + UDPC_set_callback_received(ctx, recCallback, &testCtx); while(UDPC_get_error(ctx) == UDPC_SUCCESS) { if(isClient && testCtx.isConnected == 0) @@ -108,6 +142,11 @@ int main(int argc, char** argv) } if(isThreaded == 0) { UDPC_update(ctx); } UDPC_check_events(ctx); + if(testCtx.isConnected != 0 && UDPC_get_queue_send_available(ctx, testCtx.addr) > 0) + { + UDPC_queue_send(ctx, testCtx.addr, 0, sendBuffer, 7); + updateSendBuffer(&sendIndex, sendBuffer); + } thrd_sleep(&(struct timespec){0, 16666666}, NULL); if(testCtx.hasConnectedOnce != 0 && testCtx.isConnected == 0) {