Skip to content

Commit 01d60d5

Browse files
deadlightrealMorcules
authored andcommitted
Finished. Instead of checking packet handler set in process_packets.c, now it checks before executing callback.
1 parent 269bb2e commit 01d60d5

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/execute_packet_callback.c

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "internal/internal.h"
22
#include "swift_net.h"
33
#include <stdatomic.h>
4+
#include <stdint.h>
45
#include <stdio.h>
56
#include <stdlib.h>
67
#include <unistd.h>
@@ -35,7 +36,7 @@ static struct PacketCallbackQueueNode* const wait_for_next_packet_callback(struc
3536
return node_to_process;
3637
}
3738

38-
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) {
39+
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) {
3940
while (1) {
4041
if (atomic_load_explicit(closing, memory_order_acquire) == true) {
4142
break;
@@ -67,6 +68,17 @@ void execute_packet_callback(struct PacketCallbackQueue* const queue, void (* co
6768
}
6869

6970
void (*const packet_handler_loaded)(void*) = atomic_load(packet_handler);
71+
if (unlikely(packet_handler_loaded == NULL)) {
72+
if (connection_type == CONNECTION_TYPE_CLIENT) {
73+
swiftnet_client_destroy_packet_data(node->packet_data, connection);
74+
} else {
75+
swiftnet_client_destroy_packet_data(node->packet_data, connection);
76+
}
77+
78+
allocator_free(&packet_callback_queue_node_memory_allocator, (void*)node);
79+
80+
continue;
81+
}
7082

7183
(*packet_handler_loaded)(node->packet_data);
7284

@@ -77,15 +89,15 @@ void execute_packet_callback(struct PacketCallbackQueue* const queue, void (* co
7789
void* execute_packet_callback_client(void* const void_client) {
7890
struct SwiftNetClientConnection* const client = void_client;
7991

80-
execute_packet_callback(&client->packet_callback_queue, (void*)&client->packet_handler, 0, &client->pending_messages_memory_allocator, &client->closing, void_client, &client->pending_messages);
92+
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);
8193

8294
return NULL;
8395
}
8496

8597
void* execute_packet_callback_server(void* const void_server) {
8698
struct SwiftNetServer* const server = void_server;
8799

88-
execute_packet_callback(&server->packet_callback_queue, (void*)&server->packet_handler, 1, &server->pending_messages_memory_allocator, &server->closing, void_server, &server->pending_messages);
100+
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);
89101

90102
return NULL;
91103
}

src/process_packets.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -360,17 +360,6 @@ static inline void swiftnet_process_packets(
360360

361361
uint8_t* const packet_data = &packet_buffer[prepend_size + PACKET_HEADER_SIZE];
362362

363-
// Check if user set a function that will execute with the packet data received as arg
364-
#ifdef SWIFT_NET_ERROR
365-
const void* const packet_handler_derenfernced = atomic_load(packet_handler);
366-
if(unlikely(packet_handler_derenfernced == NULL)) {
367-
allocator_free(&packet_queue_node_memory_allocator, (void*)node);
368-
allocator_free(&packet_buffer_memory_allocator, packet_buffer);
369-
fprintf(stderr, "Message Handler not set!!\n");
370-
continue;
371-
}
372-
#endif
373-
374363
struct ip ip_header;
375364
memcpy(&ip_header, packet_buffer + prepend_size, sizeof(ip_header));
376365

0 commit comments

Comments
 (0)