]> git.seodisparate.com - UDPConnection/commitdiff
Add get/set protocol id capability
authorStephen Seo <seo.disparate@gmail.com>
Thu, 7 Mar 2019 02:57:06 +0000 (11:57 +0900)
committerStephen Seo <seo.disparate@gmail.com>
Thu, 7 Mar 2019 02:57:06 +0000 (11:57 +0900)
src/UDPC_Defines.h
src/UDPConnection.c
src/UDPConnection.h

index f9c59dcbbddd019aa18a994bcc98a2c3fe46ff47..5032e36199804013a180f623edf4be688c6f2474 100644 (file)
@@ -40,7 +40,7 @@ extern const char *UDPC_ERR_THREADFAIL_STR;
 #define UDPC_HEARTBEAT_PKT_INTERVAL (15.0f/100.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_PKT_DEFAULT_PROTOCOL_ID 1357924680
 
 #define UDPC_ID_CONNECT        0x80000000
 #define UDPC_ID_PING           0x40000000
index ba276e01afc9cb4aef8a5c1c034d62f85676ae30..edaacff57529722d8ea94a2f5f0ba9701aeda36b 100644 (file)
@@ -17,6 +17,7 @@ const char *UDPC_ERR_THREADFAIL_STR = "Failed to create thread";
 UDPC_Context* UDPC_init(uint16_t listenPort, uint32_t listenAddr, int isClient)
 {
     UDPC_Context *context = malloc(sizeof(UDPC_Context));
+    context->protocolID = UDPC_PKT_DEFAULT_PROTOCOL_ID;
     context->error = UDPC_SUCCESS;
     context->flags = 0x4C;
     if(isClient != 0) context->flags |= 0x2;
@@ -445,6 +446,26 @@ void UDPC_set_accept_new_connections(UDPC_Context *ctx, int isAccepting)
     if((ctx->flags & 0x1) != 0) { mtx_unlock(&ctx->tCVMtx); }
 }
 
+uint32_t UDPC_get_protocol_id(UDPC_Context *ctx)
+{
+    if((ctx->flags & 0x1) != 0) { mtx_lock(&ctx->tCVMtx); }
+
+    uint32_t id = ctx->protocolID;
+
+    if((ctx->flags & 0x1) != 0) { mtx_unlock(&ctx->tCVMtx); }
+
+    return id;
+}
+
+void UDPC_set_protocol_id(UDPC_Context *ctx, uint32_t id)
+{
+    if((ctx->flags & 0x1) != 0) { mtx_lock(&ctx->tCVMtx); }
+
+    ctx->protocolID = id;
+
+    if((ctx->flags & 0x1) != 0) { mtx_unlock(&ctx->tCVMtx); }
+}
+
 uint32_t UDPC_get_error(UDPC_Context *ctx)
 {
     if((ctx->flags & 0x1) != 0) { mtx_lock(&ctx->tCVMtx); }
@@ -583,7 +604,7 @@ void UDPC_update(UDPC_Context *ctx)
     }
 
     uint32_t temp = ntohl(*((uint32_t*)ctx->recvBuf));
-    if(temp != UDPC_PKT_PROTOCOL_ID)
+    if(temp != ctx->protocolID)
     {
         UDPC_INTERNAL_log(ctx, 2, "Got invalid packet from %s port %d (invalid protocol id)",
             UDPC_INTERNAL_atostr(ctx, receivedData.sin_addr.s_addr),
@@ -906,6 +927,7 @@ void UDPC_INTERNAL_update_send(void *userData, uint32_t addr, char *data)
             char *data = malloc(20);
             UDPC_INTERNAL_prepare_pkt(
                 data,
+                us->ctx->protocolID,
                 UDPC_ID_CONNECT,
                 0,
                 0xFFFFFFFF,
@@ -941,6 +963,7 @@ void UDPC_INTERNAL_update_send(void *userData, uint32_t addr, char *data)
             char *data = malloc(20);
             UDPC_INTERNAL_prepare_pkt(
                 data,
+                us->ctx->protocolID,
                 UDPC_ID_CONNECT | cd->id,
                 cd->rseq,
                 cd->ack,
@@ -979,7 +1002,8 @@ void UDPC_INTERNAL_update_send(void *userData, uint32_t addr, char *data)
         }
 
         char *data = malloc(20);
-        UDPC_INTERNAL_prepare_pkt(data, cd->id, cd->rseq, cd->ack, &cd->lseq, 0);
+        UDPC_INTERNAL_prepare_pkt(
+            data, us->ctx->protocolID, cd->id, cd->rseq, cd->ack, &cd->lseq, 0);
 
         struct sockaddr_in destinationInfo;
         destinationInfo.sin_family = AF_INET;
@@ -1042,6 +1066,7 @@ void UDPC_INTERNAL_update_send(void *userData, uint32_t addr, char *data)
         char *data = malloc(20 + pinfo->size);
         UDPC_INTERNAL_prepare_pkt(
             data,
+            us->ctx->protocolID,
             cd->id,
             cd->rseq,
             cd->ack,
@@ -1313,6 +1338,7 @@ int UDPC_INTERNAL_threadfn(void *context)
 
 void UDPC_INTERNAL_prepare_pkt(
     void *data,
+    uint32_t protocolID,
     uint32_t conID,
     uint32_t rseq,
     uint32_t ack,
@@ -1322,7 +1348,7 @@ void UDPC_INTERNAL_prepare_pkt(
     char *d = data;
     uint32_t temp;
 
-    temp = htonl(UDPC_PKT_PROTOCOL_ID);
+    temp = htonl(protocolID);
     memcpy(d, &temp, 4);
     if((flags & 0x4) == 0)
     {
index def82e583fc51279b2d231a5e362d444258eba87..e1bd4f2f7283deb88bc27b84539a21082b3f2a77 100644 (file)
@@ -112,6 +112,7 @@ typedef struct {
      * 0x1 - thread should stop
      */
     uint32_t threadFlags;
+    uint32_t protocolID;
     uint32_t error;
     int socketHandle;
     struct sockaddr_in socketInfo;
@@ -223,6 +224,16 @@ int UDPC_get_accept_new_connections(UDPC_Context *ctx);
  */
 void UDPC_set_accept_new_connections(UDPC_Context *ctx, int isAccepting);
 
+/// Gets the currently set protocol id
+uint32_t UDPC_get_protocol_id(UDPC_Context *ctx);
+
+/// Sets the protocol id
+/*!
+ * Note that UDPC can only connect to other UDPC instances that use the same
+ * protocol id.
+ */
+void UDPC_set_protocol_id(UDPC_Context *ctx, uint32_t id);
+
 /*!
  * \brief Get the currently set error code, and clear it internally
  * Error codes and their meanings are defined in UDPC_Defines.h .
@@ -275,6 +286,7 @@ int UDPC_INTERNAL_threadfn(void *context);
  */
 void UDPC_INTERNAL_prepare_pkt(
     void *data,
+    uint32_t protocolID,
     uint32_t conID,
     uint32_t rseq,
     uint32_t ack,