diff --git a/src/execute_packet_callback.c b/src/execute_packet_callback.c index cd2b418..89131f5 100644 --- a/src/execute_packet_callback.c +++ b/src/execute_packet_callback.c @@ -1,6 +1,7 @@ #include "internal/internal.h" #include "swift_net.h" #include +#include #include #include #include @@ -35,7 +36,7 @@ static struct PacketCallbackQueueNode* const wait_for_next_packet_callback(struc return node_to_process; } -void execute_packet_callback(struct PacketCallbackQueue* const queue, void (* const _Atomic * const packet_handler) (void* const), const uint8_t connection_type, struct SwiftNetMemoryAllocator* const pending_message_memory_allocator, _Atomic bool* closing, const void* const connection, struct SwiftNetVector* const pending_messages) { +void execute_packet_callback(struct PacketCallbackQueue* const queue, void (* const _Atomic * const packet_handler) (void* const), const enum ConnectionType connection_type, struct SwiftNetMemoryAllocator* const pending_message_memory_allocator, _Atomic bool* closing, void* const connection, struct SwiftNetVector* const pending_messages) { while (1) { if (atomic_load_explicit(closing, memory_order_acquire) == true) { break; @@ -67,6 +68,17 @@ void execute_packet_callback(struct PacketCallbackQueue* const queue, void (* co } void (*const packet_handler_loaded)(void*) = atomic_load(packet_handler); + if (unlikely(packet_handler_loaded == NULL)) { + if (connection_type == CONNECTION_TYPE_CLIENT) { + swiftnet_client_destroy_packet_data(node->packet_data, connection); + } else { + swiftnet_client_destroy_packet_data(node->packet_data, connection); + } + + allocator_free(&packet_callback_queue_node_memory_allocator, (void*)node); + + continue; + } (*packet_handler_loaded)(node->packet_data); @@ -77,7 +89,7 @@ void execute_packet_callback(struct PacketCallbackQueue* const queue, void (* co void* execute_packet_callback_client(void* const void_client) { struct SwiftNetClientConnection* const client = void_client; - execute_packet_callback(&client->packet_callback_queue, (void*)&client->packet_handler, 0, &client->pending_messages_memory_allocator, &client->closing, void_client, &client->pending_messages); + execute_packet_callback(&client->packet_callback_queue, (void*)&client->packet_handler, CONNECTION_TYPE_CLIENT, &client->pending_messages_memory_allocator, &client->closing, void_client, &client->pending_messages); return NULL; } @@ -85,7 +97,7 @@ void* execute_packet_callback_client(void* const void_client) { void* execute_packet_callback_server(void* const void_server) { struct SwiftNetServer* const server = void_server; - execute_packet_callback(&server->packet_callback_queue, (void*)&server->packet_handler, 1, &server->pending_messages_memory_allocator, &server->closing, void_server, &server->pending_messages); + execute_packet_callback(&server->packet_callback_queue, (void*)&server->packet_handler, CONNECTION_TYPE_SERVER, &server->pending_messages_memory_allocator, &server->closing, void_server, &server->pending_messages); return NULL; } diff --git a/src/process_packets.c b/src/process_packets.c index 5027da1..ed8648b 100644 --- a/src/process_packets.c +++ b/src/process_packets.c @@ -360,17 +360,6 @@ static inline void swiftnet_process_packets( uint8_t* const packet_data = &packet_buffer[prepend_size + PACKET_HEADER_SIZE]; - // Check if user set a function that will execute with the packet data received as arg - #ifdef SWIFT_NET_ERROR - const void* const packet_handler_derenfernced = atomic_load(packet_handler); - if(unlikely(packet_handler_derenfernced == NULL)) { - allocator_free(&packet_queue_node_memory_allocator, (void*)node); - allocator_free(&packet_buffer_memory_allocator, packet_buffer); - fprintf(stderr, "Message Handler not set!!\n"); - continue; - } - #endif - struct ip ip_header; memcpy(&ip_header, packet_buffer + prepend_size, sizeof(ip_header));