]> git.seodisparate.com - UDPConnection/commitdiff
Fix endianness of addr, minor fixes
authorStephen Seo <seo.disparate@gmail.com>
Mon, 4 Mar 2019 10:05:07 +0000 (19:05 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Mon, 4 Mar 2019 10:05:07 +0000 (19:05 +0900)
src/UDPC_Defines.h
src/UDPConnection.c
src/test/UDPC_UnitTest.c

index 68ded166b2e9c3b698ff314311de8d14d01e9a69..650f14a7e719677ea675f3de1799cf21a51cff78 100644 (file)
@@ -37,7 +37,8 @@ static const char *UDPC_ERR_THREADFAIL_STR = "Failed to create thread";
 #define UDPC_BAD_MODE_SEND_INTERVAL (1.0f/10.0f)
 #define UDPC_TIMEOUT_SECONDS 10.0f
 #define UDPC_HEARTBEAT_PKT_INTERVAL (15.0f/100.0f)
-#define UDPC_INIT_PKT_INTERVAL 5.0f
+#define UDPC_INIT_PKT_INTERVAL 5
+#define UDPC_INIT_PKT_INTERVAL_F ((float)UDPC_INIT_PKT_INTERVAL)
 #define UDPC_PKT_PROTOCOL_ID 1357924680
 
 #define UDPC_ID_CONNECT        0x80000000
index 827a30ef6b29f9c0744ad59429cd32321ce0fe1d..653c23899c3ffe00f2f4795d981791aa5fd5ab9d 100644 (file)
@@ -320,7 +320,8 @@ void UDPC_client_initiate_connection(UDPC_Context *ctx, uint32_t addr, uint16_t
     };
 
     timespec_get(&cd.received, TIME_UTC);
-    // only set "received" to now, since "sent" will be set after sending packet
+    cd.sent = cd.received;
+    cd.sent.tv_sec -= UDPC_INIT_PKT_INTERVAL + 1;
 
     UDPC_HashMap_insert(ctx->conMap, addr, &cd);
 }
@@ -778,7 +779,7 @@ 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)
+        if(UDPC_INTERNAL_ts_diff(&us->tsNow, &cd->sent) < UDPC_INIT_PKT_INTERVAL_F)
         {
             return;
         }
@@ -1235,7 +1236,7 @@ char* UDPC_INTERNAL_atostr(UDPC_Context *ctx, uint32_t addr)
     int index = 0;
     for(int x = 0; x < 4; ++x)
     {
-        unsigned char temp = (addr >> (24 - x * 8)) & 0xFF;
+        unsigned char temp = (addr >> (x * 8)) & 0xFF;
 
         if(temp >= 100)
         {
@@ -1293,7 +1294,7 @@ uint32_t UDPC_strtoa(const char *addrStr)
         }
         else if(*addrStr == '.' && temp <= 0xFF && index < 3)
         {
-            addr |= (temp << (24 - 8 * index++));
+            addr |= (temp << (8 * index++));
             temp = 0;
         }
         else
@@ -1305,7 +1306,7 @@ uint32_t UDPC_strtoa(const char *addrStr)
 
     if(index == 3 && temp <= 0xFF)
     {
-        addr |= temp;
+        addr |= temp << 24;
         return addr;
     }
     else
index 2229ec8786786211fcc1e03fa1b2bb8783aa76b1..353c9bd5357f11546801fd5f28c35b09a7c7e5bf 100644 (file)
@@ -246,7 +246,7 @@ void TEST_ATOSTR()
 {
     UDPC_Context ctx;
     ASSERT_EQ_MEM(
-        UDPC_INTERNAL_atostr(&ctx, (0xAC << 24) | (0x1E << 16) | (0x1 << 8) | 0xFF),
+        UDPC_INTERNAL_atostr(&ctx, (0xFF << 24) | (0x1 << 16) | (0x1E << 8) | 0xAC),
         "172.30.1.255",
         13);
     UNITTEST_REPORT(ATOSTR);
@@ -387,9 +387,9 @@ void TEST_HASHMAP()
 
 void TEST_STRTOA()
 {
-    ASSERT_EQ(0x01020304, UDPC_strtoa("1.2.3.4"));
-    ASSERT_EQ(0x7F000001, UDPC_strtoa("127.0.0.1"));
-    ASSERT_EQ(0xC0A801FF, UDPC_strtoa("192.168.1.255"));
+    ASSERT_EQ(0x04030201, UDPC_strtoa("1.2.3.4"));
+    ASSERT_EQ(0x0100007F, UDPC_strtoa("127.0.0.1"));
+    ASSERT_EQ(0xFF01A8C0, UDPC_strtoa("192.168.1.255"));
     ASSERT_EQ(0, UDPC_strtoa("1.2.3.4.5"));
     ASSERT_EQ(0, UDPC_strtoa("100.20.30"));
     ASSERT_EQ(0, UDPC_strtoa("200.400.30.50"));