]> git.seodisparate.com - UDPConnection/commitdiff
Minor changes/fixes, clang-format fixes
authorStephen Seo <seo.disparate@gmail.com>
Sun, 21 Jul 2019 04:45:42 +0000 (13:45 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Sun, 21 Jul 2019 04:45:42 +0000 (13:45 +0900)
cpp_impl/src/UDPC_Defines.hpp
cpp_impl/src/UDPConnection.cpp

index 3e8f6f0a0799bf8537b77bdf810425dae106a0ae..743ab8beaa693d667122b01070c6f1adfa7797fc 100644 (file)
@@ -25,7 +25,7 @@ struct Context {
     char atostrBuf[16];
 }; // struct Context
 
-bool VerifyContext(void *ctx);
+Context* verifyContext(void *ctx);
 
 bool isBigEndian();
 
index f8282a9e7934ae66513b5f2efd172e2c1ca79c2c..cd0c7efccb97066257c481845fdae6937a2063bb 100644 (file)
@@ -1,7 +1,6 @@
 #include "UDPC_Defines.hpp"
 #include "UDPConnection.h"
 
-
 #include <optional>
 
 UDPC::Context::Context(bool isThreaded)
@@ -13,22 +12,22 @@ UDPC::Context::Context(bool isThreaded)
       loggingType(WARNING)
 #endif
 {
-    if (isThreaded) {
+    if(isThreaded) {
         flags.set(0);
     } else {
         flags.reset(0);
     }
 }
 
-bool UDPC::VerifyContext(void *ctx) {
-    if (ctx == nullptr) {
-        return false;
+UDPC::Context *UDPC::verifyContext(void *ctx) {
+    if(ctx == nullptr) {
+        return nullptr;
     }
     UDPC::Context *c = (UDPC::Context *)ctx;
-    if (c->_contextIdentifier == UDPC_CONTEXT_IDENTIFIER) {
-        return true;
+    if(c->_contextIdentifier == UDPC_CONTEXT_IDENTIFIER) {
+        return c;
     } else {
-        return false;
+        return nullptr;
     }
 }
 
@@ -40,13 +39,12 @@ bool UDPC::isBigEndian() {
     union {
         uint32_t i;
         char c[4];
-    } bint = { 0x01020304 };
+    } bint = {0x01020304};
 
     isBigEndian = (bint.c[0] == 1);
     return *isBigEndian;
 }
 
-
 void *UDPC_init(uint16_t listenPort, uint32_t listenAddr, int isClient) {
     UDPC::Context *ctx = new UDPC::Context(false);
 
@@ -61,17 +59,18 @@ void *UDPC_init_threaded_update(uint16_t listenPort, uint32_t listenAddr,
 }
 
 void UDPC_destroy(void *ctx) {
-    if (UDPC::VerifyContext(ctx)) {
-        delete (UDPC::Context *)ctx;
+    UDPC::Context *UDPC_ctx = UDPC::verifyContext(ctx);
+    if(UDPC_ctx) {
+        delete UDPC_ctx;
     }
 }
 
 void UDPC_update(void *ctx) {
-    if (!UDPC::VerifyContext(ctx)) {
+    UDPC::Context *c = UDPC::verifyContext(ctx);
+    if(!c) {
         return;
     }
-    UDPC::Context *c = (UDPC::Context *)ctx;
-    if (c->flags.test(0)) {
+    if(c->flags.test(0)) {
         // is threaded, update should not be called
         return;
     }
@@ -80,83 +79,83 @@ void UDPC_update(void *ctx) {
 }
 
 int UDPC_get_queue_send_available(void *ctx, uint32_t addr) {
-    if (!UDPC::VerifyContext(ctx)) {
+    UDPC::Context *c = UDPC::verifyContext(ctx);
+    if(!c) {
         return 0;
     }
-    UDPC::Context *c = (UDPC::Context *)ctx;
     // TODO impl
     return 0;
 }
 
 void UDPC_queue_send(void *ctx, uint32_t destAddr, uint16_t destPort,
                      uint32_t isChecked, void *data, uint32_t size) {
-    if (!UDPC::VerifyContext(ctx)) {
+    UDPC::Context *c = UDPC::verifyContext(ctx);
+    if(!c) {
         return;
     }
-    UDPC::Context *c = (UDPC::Context *)ctx;
     // TODO impl
 }
 
 int UDPC_set_accept_new_connections(void *ctx, int isAccepting) {
-    if (!UDPC::VerifyContext(ctx)) {
+    UDPC::Context *c = UDPC::verifyContext(ctx);
+    if(!c) {
         return 0;
     }
-    UDPC::Context *c = (UDPC::Context *)ctx;
     return c->isAcceptNewConnections.exchange(isAccepting == 0 ? false : true);
 }
 
 int UDPC_drop_connection(void *ctx, uint32_t addr, uint16_t port) {
-    if (!UDPC::VerifyContext(ctx)) {
+    UDPC::Context *c = UDPC::verifyContext(ctx);
+    if(!c) {
         return 0;
     }
-    UDPC::Context *c = (UDPC::Context *)ctx;
     // TODO impl
     return 0;
 }
 
 uint32_t UDPC_set_protocol_id(void *ctx, uint32_t id) {
-    if (!UDPC::VerifyContext(ctx)) {
+    UDPC::Context *c = UDPC::verifyContext(ctx);
+    if(!c) {
         return 0;
     }
-    UDPC::Context *c = (UDPC::Context *)ctx;
     return c->protocolID.exchange(id);
 }
 
 UDPC_LoggingType set_logging_type(void *ctx, UDPC_LoggingType loggingType) {
-    if (!UDPC::VerifyContext(ctx)) {
+    UDPC::Context *c = UDPC::verifyContext(ctx);
+    if(!c) {
         return static_cast<UDPC_LoggingType>(0);
     }
-    UDPC::Context *c = (UDPC::Context *)ctx;
     return static_cast<UDPC_LoggingType>(c->loggingType.exchange(loggingType));
 }
 
 PacketInfo UDPC_get_received(void *ctx) {
-    if (!UDPC::VerifyContext(ctx)) {
+    UDPC::Context *c = UDPC::verifyContext(ctx);
+    if(!c) {
         return PacketInfo{{0}, 0, 0, 0, 0, 0};
     }
-    UDPC::Context *c = (UDPC::Context *)ctx;
     // TODO impl
     return PacketInfo{{0}, 0, 0, 0, 0, 0};
 }
 
 const char *UDPC_atostr(void *ctx, uint32_t addr) {
-    if (!UDPC::VerifyContext(ctx)) {
+    UDPC::Context *c = UDPC::verifyContext(ctx);
+    if(!c) {
         return nullptr;
     }
-    UDPC::Context *c = (UDPC::Context *)ctx;
     int index = 0;
-    for (int x = 0; x < 4; ++x) {
+    for(int x = 0; x < 4; ++x) {
         unsigned char temp = (addr >> (x * 8)) & 0xFF;
 
-        if (temp >= 100) {
+        if(temp >= 100) {
             c->atostrBuf[index++] = '0' + temp / 100;
         }
-        if (temp >= 10) {
+        if(temp >= 10) {
             c->atostrBuf[index++] = '0' + ((temp / 10) % 10);
         }
         c->atostrBuf[index++] = '0' + temp % 10;
 
-        if (x < 3) {
+        if(x < 3) {
             c->atostrBuf[index++] = '.';
         }
     }
@@ -169,11 +168,11 @@ uint32_t UDPC_strtoa(const char *addrStr) {
     uint32_t addr = 0;
     uint32_t temp = 0;
     uint32_t index = 0;
-    while (*addrStr != 0) {
-        if (*addrStr >= '0' && *addrStr <= '9') {
+    while(*addrStr != 0) {
+        if(*addrStr >= '0' && *addrStr <= '9') {
             temp *= 10;
             temp += *addrStr - '0';
-        } else if (*addrStr == '.' && temp <= 0xFF && index < 3) {
+        } else if(*addrStr == '.' && temp <= 0xFF && index < 3) {
             if(UDPC::isBigEndian()) {
                 addr |= (temp << (24 - 8 * index++));
             } else {
@@ -186,7 +185,7 @@ uint32_t UDPC_strtoa(const char *addrStr) {
         ++addrStr;
     }
 
-    if (index == 3 && temp <= 0xFF) {
+    if(index == 3 && temp <= 0xFF) {
         if(UDPC::isBigEndian()) {
             addr |= temp;
         } else {