]> git.seodisparate.com - UDPConnection/commitdiff
Add resendPktQueue to struct in UDPConnection
authorStephen Seo <seo.disparate@gmail.com>
Mon, 4 Mar 2019 02:32:44 +0000 (11:32 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 4 Mar 2019 02:32:44 +0000 (11:32 +0900)
Resending packets now put in higher priority queue in ConnectionData; if
queued both regular packet and resending packet, resending packets are
sent first.

src/UDPC_Defines.h
src/UDPConnection.c
src/UDPConnection.h

index 6549a1a23ddfb252e2b16d681af9a739a8a05678..c9bb2b8c77c0530ddca75e50ea1e62615295bdd5 100644 (file)
@@ -47,6 +47,7 @@ static const char *UDPC_ERR_THREADFAIL_STR = "Failed to create thread";
 #define UDPC_SENT_PKTS_MAX_SIZE 34
 #define UDPC_SENT_PKTS_ALLOC_SIZE 35
 #define UDPC_SEND_PKTS_ALLOC_SIZE 40
+#define UDPC_RESEND_PKTS_ALLOC_SIZE 40
 
 #define UDPC_PACKET_MAX_SIZE 8192
 
index 50eaa773e77a92805117eea20308a9c7804fa259..34ac07909d20efecca750ec20029ecb58ed5e13b 100644 (file)
@@ -167,6 +167,7 @@ void UDPC_destroy(UDPC_Context *ctx)
 void UDPC_INTERNAL_destroy_conMap(void *unused, uint32_t addr, char *data)
 {
     UDPC_INTERNAL_ConnectionData *cd = (UDPC_INTERNAL_ConnectionData*)data;
+
     for(int x = 0; x * sizeof(UDPC_INTERNAL_PacketInfo) < cd->sentPkts->size; ++x)
     {
         UDPC_INTERNAL_PacketInfo *pinfo = UDPC_Deque_index_ptr(
@@ -177,6 +178,7 @@ void UDPC_INTERNAL_destroy_conMap(void *unused, uint32_t addr, char *data)
         }
     }
     UDPC_Deque_destroy(cd->sentPkts);
+
     for(int x = 0; x * sizeof(UDPC_INTERNAL_PacketInfo) < cd->sendPktQueue->size; ++x)
     {
         UDPC_INTERNAL_PacketInfo *pinfo = UDPC_Deque_index_ptr(
@@ -187,6 +189,17 @@ void UDPC_INTERNAL_destroy_conMap(void *unused, uint32_t addr, char *data)
         }
     }
     UDPC_Deque_destroy(cd->sendPktQueue);
+
+    for(int x = 0; x * sizeof(UDPC_INTERNAL_PacketInfo) < cd->resendPktQueue->size; ++x)
+    {
+        UDPC_INTERNAL_PacketInfo *pinfo = UDPC_Deque_index_ptr(
+            cd->resendPktQueue, sizeof(UDPC_INTERNAL_PacketInfo), x);
+        if(pinfo->data)
+        {
+            free(pinfo->data);
+        }
+    }
+    UDPC_Deque_destroy(cd->resendPktQueue);
 }
 
 void UDPC_set_callback_connected(
@@ -452,6 +465,7 @@ void UDPC_update(UDPC_Context *ctx)
                 ntohs(receivedData.sin_port),
                 UDPC_Deque_init(sizeof(UDPC_INTERNAL_PacketInfo) * UDPC_SENT_PKTS_ALLOC_SIZE),
                 UDPC_Deque_init(sizeof(UDPC_INTERNAL_PacketInfo) * UDPC_SEND_PKTS_ALLOC_SIZE),
+                UDPC_Deque_init(sizeof(UDPC_INTERNAL_PacketInfo) * UDPC_RESEND_PKTS_ALLOC_SIZE),
                 {0, 0},
                 {0, 0},
                 0.0f
@@ -699,9 +713,9 @@ void UDPC_INTERNAL_update_send(void *userData, uint32_t addr, char *data)
     }
 
     cd->flags = cd->flags & 0xFFFFFFFE;
-    if(cd->sendPktQueue->size == 0)
+    if(cd->sendPktQueue->size == 0 && cd->resendPktQueue->size == 0)
     {
-        // send packet queue is empty, send heartbeat packet
+        // send and resend packet queue is empty, send heartbeat packet
         if(UDPC_INTERNAL_ts_diff(&us->tsNow, &cd->sent) < UDPC_HEARTBEAT_PKT_INTERVAL)
         {
             return;
@@ -752,10 +766,22 @@ void UDPC_INTERNAL_update_send(void *userData, uint32_t addr, char *data)
         }
         free(data);
     }
-    else // sendPktQueue not empty
+    else // sendPktQueue or resendPktQueue not empty
     {
-        UDPC_INTERNAL_PacketInfo *pinfo = UDPC_Deque_get_front_ptr(
-            cd->sendPktQueue, sizeof(UDPC_INTERNAL_PacketInfo));
+        UDPC_INTERNAL_PacketInfo *pinfo;
+        int isResendingPkt = 0;
+
+        if(cd->resendPktQueue->size != 0)
+        {
+            pinfo = UDPC_Deque_get_front_ptr(
+                cd->resendPktQueue, sizeof(UDPC_INTERNAL_PacketInfo));
+            isResendingPkt = 1;
+        }
+        else
+        {
+            pinfo = UDPC_Deque_get_front_ptr(
+                cd->sendPktQueue, sizeof(UDPC_INTERNAL_PacketInfo));
+        }
         char *data = malloc(20 + pinfo->size);
         UDPC_INTERNAL_prepare_pkt(
             data,
@@ -783,7 +809,14 @@ void UDPC_INTERNAL_update_send(void *userData, uint32_t addr, char *data)
                 UDPC_INTERNAL_atostr(us->ctx, addr), cd->port);
             free(data);
             free(pinfo->data);
-            UDPC_Deque_pop_front(cd->sendPktQueue, sizeof(UDPC_INTERNAL_PacketInfo));
+            if(isResendingPkt != 0)
+            {
+                UDPC_Deque_pop_front(cd->resendPktQueue, sizeof(UDPC_INTERNAL_PacketInfo));
+            }
+            else
+            {
+                UDPC_Deque_pop_front(cd->sendPktQueue, sizeof(UDPC_INTERNAL_PacketInfo));
+            }
             return;
         }
 
@@ -827,7 +860,14 @@ void UDPC_INTERNAL_update_send(void *userData, uint32_t addr, char *data)
         }
 
         free(pinfo->data);
-        UDPC_Deque_pop_front(cd->sendPktQueue, sizeof(UDPC_INTERNAL_PacketInfo));
+        if(isResendingPkt != 0)
+        {
+            UDPC_Deque_pop_front(cd->resendPktQueue, sizeof(UDPC_INTERNAL_PacketInfo));
+        }
+        else
+        {
+            UDPC_Deque_pop_front(cd->sendPktQueue, sizeof(UDPC_INTERNAL_PacketInfo));
+        }
     }
 }
 
@@ -923,9 +963,8 @@ void UDPC_INTERNAL_check_pkt_timeout(
                     pinfo->flags |= 0x4;
                     pinfo->data = NULL;
                     pinfo->size = 0;
-                    // TODO use separate queue for resending packets
                     UDPC_Deque_push_back(
-                        cd->sendPktQueue,
+                        cd->resendPktQueue,
                         &newPkt,
                         sizeof(UDPC_INTERNAL_PacketInfo));
                 }
index d470bcfb3bdf3834bcac6df0183da9c9d36b33e8..920ab3c180aed1c98f0ea7431487fc2726e09ae7 100644 (file)
@@ -83,6 +83,7 @@ typedef struct {
     uint16_t port;
     UDPC_Deque *sentPkts;
     UDPC_Deque *sendPktQueue;
+    UDPC_Deque *resendPktQueue;
     struct timespec received;
     struct timespec sent;
     float rtt;