Browse Source

Merge pull request #20 from OptoCloud/master

Add callbacks for enet_packet_create, and enet_packet_destroy
Vladyslav Hrytsenko 3 years ago
parent
commit
fb0de8c975
1 changed files with 25 additions and 12 deletions
  1. 25 12
      include/enet.h

+ 25 - 12
include/enet.h

@@ -222,15 +222,21 @@ extern "C" {
     typedef uint64_t enet_uint64;   /**< unsigned 64-bit type */
 
     typedef enet_uint32 ENetVersion;
+    typedef struct _ENetPacket ENetPacket;
 
     typedef struct _ENetCallbacks {
         void *(ENET_CALLBACK *malloc) (size_t size);
         void (ENET_CALLBACK *free) (void *memory);
         void (ENET_CALLBACK *no_memory) (void);
+
+        ENetPacket *(ENET_CALLBACK *packet_create)        (const void *data, size_t dataLength, enet_uint32 flags);
+        void        (ENET_CALLBACK *packet_destroy)       (ENetPacket *packet);
     } ENetCallbacks;
 
     extern void *enet_malloc(size_t);
     extern void enet_free(void *);
+    extern ENetPacket* enet_packet_create(const void*,size_t,enet_uint32);
+    extern void enet_packet_destroy(ENetPacket*);
 
 // =======================================================================//
 // !
@@ -937,9 +943,7 @@ extern "C" {
     ENET_API enet_uint32 enet_packet_get_length(ENetPacket *);
     ENET_API void        enet_packet_set_free_callback(ENetPacket *, void *);
 
-    ENET_API ENetPacket * enet_packet_create(const void *, size_t, enet_uint32);
     ENET_API ENetPacket * enet_packet_create_offset(const void *, size_t, size_t, enet_uint32);
-    ENET_API void         enet_packet_destroy(ENetPacket *);
     ENET_API enet_uint32  enet_crc32(const ENetBuffer *, size_t);
 
     ENET_API ENetHost * enet_host_create(const ENetAddress *, size_t, size_t, enet_uint32, enet_uint32);
@@ -1200,7 +1204,7 @@ extern "C" {
 // !
 // =======================================================================//
 
-    static ENetCallbacks callbacks = { malloc, free, abort };
+    static ENetCallbacks callbacks = { malloc, free, abort, enet_packet_create, enet_packet_destroy };
 
     int enet_initialize_with_callbacks(ENetVersion version, const ENetCallbacks *inits) {
         if (version < ENET_VERSION_CREATE(1, 3, 0)) {
@@ -1220,6 +1224,15 @@ extern "C" {
             callbacks.no_memory = inits->no_memory;
         }
 
+        if (inits->packet_create != NULL || inits->packet_destroy != NULL) {
+            if (inits->packet_create == NULL || inits->packet_destroy == NULL) {
+                return -1;
+            }
+
+            callbacks.packet_create = inits->packet_create;
+            callbacks.packet_destroy = inits->packet_destroy;
+        }
+
         return enet_initialize();
     }
 
@@ -1618,7 +1631,7 @@ extern "C" {
 
                 if (outgoingCommand->packet->referenceCount == 0) {
                     outgoingCommand->packet->flags |= ENET_PACKET_FLAG_SENT;
-                    enet_packet_destroy(outgoingCommand->packet);
+                    callbacks.packet_destroy(outgoingCommand->packet);
                 }
             }
 
@@ -1690,7 +1703,7 @@ extern "C" {
 
             if (outgoingCommand->packet->referenceCount == 0) {
                 outgoingCommand->packet->flags |= ENET_PACKET_FLAG_SENT;
-                enet_packet_destroy(outgoingCommand->packet);
+                callbacks.packet_destroy(outgoingCommand->packet);
             }
         }
 
@@ -2775,7 +2788,7 @@ extern "C" {
                         --outgoingCommand->packet->referenceCount;
 
                         if (outgoingCommand->packet->referenceCount == 0) {
-                            enet_packet_destroy(outgoingCommand->packet);
+                            callbacks.packet_destroy(outgoingCommand->packet);
                         }
 
                         enet_list_remove(&outgoingCommand->outgoingCommandList);
@@ -3621,7 +3634,7 @@ extern "C" {
                 --outgoingCommand->packet->referenceCount;
 
                 if (outgoingCommand->packet->referenceCount == 0) {
-                    enet_packet_destroy(outgoingCommand->packet);
+                    callbacks.packet_destroy(outgoingCommand->packet);
                 }
             }
 
@@ -3644,7 +3657,7 @@ extern "C" {
                 --incomingCommand->packet->referenceCount;
 
                 if (incomingCommand->packet->referenceCount == 0) {
-                    enet_packet_destroy(incomingCommand->packet);
+                    callbacks.packet_destroy(incomingCommand->packet);
                 }
             }
 
@@ -4248,7 +4261,7 @@ extern "C" {
             goto notifyError;
         }
 
-        packet = enet_packet_create(data, dataLength, flags);
+        packet = callbacks.packet_create(data, dataLength, flags);
         if (packet == NULL) {
             goto notifyError;
         }
@@ -4306,14 +4319,14 @@ extern "C" {
         }
 
         if (packet != NULL && packet->referenceCount == 0) {
-            enet_packet_destroy(packet);
+            callbacks.packet_destroy(packet);
         }
 
         return &dummyCommand;
 
     notifyError:
         if (packet != NULL && packet->referenceCount == 0) {
-            enet_packet_destroy(packet);
+            callbacks.packet_destroy(packet);
         }
 
         return NULL;
@@ -4572,7 +4585,7 @@ extern "C" {
         }
 
         if (packet->referenceCount == 0) {
-            enet_packet_destroy(packet);
+            callbacks.packet_destroy(packet);
         }
     }