]> git.seodisparate.com - UDPConnection/commitdiff
Fix received callback, minor change to NetworkTest
authorStephen Seo <seo.disparate@gmail.com>
Wed, 6 Mar 2019 04:23:22 +0000 (13:23 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Wed, 6 Mar 2019 04:39:54 +0000 (13:39 +0900)
src/UDPConnection.c
src/UDPConnection.h
src/test/UDPC_NetworkTest.c

index 488ba1605372159bd6be0573522a8290e3c78bca..f626001338a3f84f24e4eacac111b11c250d7d65 100644 (file)
@@ -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);
index 91af8903b23c1004d1d888c3e76ff4f9d019f1b7..af5cdc2f1af05451dd9a7cdf487056c2b552c84a 100644 (file)
@@ -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 {
index 4e8475dabc0a31d7d83dc34beca73f127b3ccdcd..f05716d9bdb7f275e8ed6f9af09f3de7f02be623 100644 (file)
@@ -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)
             {