Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 15 additions & 3 deletions src/execute_packet_callback.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "internal/internal.h"
#include "swift_net.h"
#include <stdatomic.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand All @@ -77,15 +89,15 @@ 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;
}

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;
}
11 changes: 0 additions & 11 deletions src/process_packets.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down