]> git.seodisparate.com - UDPConnection/commitdiff
More fixes to UDPConnection
authorStephen Seo <seo.disparate@gmail.com>
Mon, 4 Mar 2019 10:28:05 +0000 (19:28 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 4 Mar 2019 10:28:05 +0000 (19:28 +0900)
src/UDPConnection.c

index 26d2e88c04ec3dc4daef4b9ebea618aa38663cb6..816ea9fd17b45d8e80d9572a72966594322a1dd1 100644 (file)
@@ -506,8 +506,8 @@ void UDPC_update(UDPC_Context *ctx)
                 UDPC_INTERNAL_atostr(ctx, receivedData.sin_addr.s_addr),
                 ntohs(receivedData.sin_port));
             UDPC_INTERNAL_ConnectionData newCD = {
-                1,
-                (ctx->flags & 0x2) != 0 ? conID : UDPC_INTERNAL_generate_id(ctx),
+                0x9,
+                UDPC_INTERNAL_generate_id(ctx),
                 0,
                 0,
                 0xFFFFFFFF,
@@ -778,39 +778,76 @@ void UDPC_INTERNAL_update_send(void *userData, uint32_t addr, char *data)
 
     if((cd->flags & 0x8) != 0)
     {
-        // initiate connection to server
-        if(UDPC_INTERNAL_ts_diff(&us->tsNow, &cd->sent) < UDPC_INIT_PKT_INTERVAL_F)
+        if((us->ctx->flags & 0x2) != 0)
         {
-            return;
-        }
-        cd->sent = us->tsNow;
-
-        char *data = malloc(20);
-        UDPC_INTERNAL_prepare_pkt(
-            data,
-            UDPC_ID_CONNECT,
-            0,
-            0xFFFFFFFF,
-            NULL,
-            0);
+            // initiate connection to server
+            if(UDPC_INTERNAL_ts_diff(&us->tsNow, &cd->sent) < UDPC_INIT_PKT_INTERVAL_F)
+            {
+                return;
+            }
+            cd->sent = us->tsNow;
 
-        struct sockaddr_in destinationInfo;
-        destinationInfo.sin_family = AF_INET;
-        destinationInfo.sin_addr.s_addr = addr;
-        destinationInfo.sin_port = htons(cd->port);
-        long int sentBytes = sendto(
-            us->ctx->socketHandle,
-            data,
-            20,
-            0,
-            (struct sockaddr*) &destinationInfo,
-            sizeof(struct sockaddr_in));
-        if(sentBytes != 20)
+            char *data = malloc(20);
+            UDPC_INTERNAL_prepare_pkt(
+                data,
+                UDPC_ID_CONNECT,
+                0,
+                0xFFFFFFFF,
+                NULL,
+                0);
+
+            struct sockaddr_in destinationInfo;
+            destinationInfo.sin_family = AF_INET;
+            destinationInfo.sin_addr.s_addr = addr;
+            destinationInfo.sin_port = htons(cd->port);
+            long int sentBytes = sendto(
+                us->ctx->socketHandle,
+                data,
+                20,
+                0,
+                (struct sockaddr*) &destinationInfo,
+                sizeof(struct sockaddr_in));
+            if(sentBytes != 20)
+            {
+                UDPC_INTERNAL_log(us->ctx, 0, "Failed to send init packet to %s "
+                    "port %d", UDPC_INTERNAL_atostr(us->ctx, addr), cd->port);
+                free(data);
+                return;
+            }
+        }
+        else
         {
-            UDPC_INTERNAL_log(us->ctx, 0, "Failed to send init packet to %s "
-                "port %d", UDPC_INTERNAL_atostr(us->ctx, addr), cd->port);
-            free(data);
-            return;
+            // initiate connection to client
+            cd->flags &= 0xFFFFFFF7;
+            cd->sent = us->tsNow;
+
+            char *data = malloc(20);
+            UDPC_INTERNAL_prepare_pkt(
+                data,
+                UDPC_ID_CONNECT | cd->id,
+                cd->rseq,
+                cd->ack,
+                &cd->lseq,
+                0);
+
+            struct sockaddr_in destinationInfo;
+            destinationInfo.sin_family = AF_INET;
+            destinationInfo.sin_addr.s_addr = addr;
+            destinationInfo.sin_port = htons(cd->port);
+            long int sentBytes = sendto(
+                us->ctx->socketHandle,
+                data,
+                20,
+                0,
+                (struct sockaddr*) &destinationInfo,
+                sizeof(struct sockaddr_in));
+            if(sentBytes != 20)
+            {
+                UDPC_INTERNAL_log(us->ctx, 0, "Failed to send init packet to %s "
+                    "port %d", UDPC_INTERNAL_atostr(us->ctx, addr), cd->port);
+                free(data);
+                return;
+            }
         }
         return;
     }