diff --git a/src/process_packets.c b/src/process_packets.c index 0b91029..68d8762 100644 --- a/src/process_packets.c +++ b/src/process_packets.c @@ -234,9 +234,11 @@ static inline struct SwiftNetPendingMessage* create_new_pending_message(struct S .packet_info = *packet_info, .packet_data_start = allocated_memory, .chunks_received_number = 0x00, + #ifndef DISABLE_DYNAMIC_RATE_LIMITING .last_index_checked = 0, - .sending_lost_packets = false, .last_chunks_received_number = 0, + #endif + .sending_lost_packets = false, .chunks_received_length = chunks_received_byte_size, .chunks_received = calloc(chunks_received_byte_size, 1), .packet_id = packet_id, @@ -271,6 +273,7 @@ static inline struct SwiftNetPacketSending* get_packet_sending(struct SwiftNetHa return result; } +#ifndef DISABLE_DYNAMIC_RATE_LIMITING static inline void signal_delay_change(const enum PacketDelayUpdateStatus status, const struct ip* restrict const ip_header, const uint16_t source_port, const uint16_t destination_port, const struct ether_header* const eth_hdr, const struct SwiftNetNetworkData* const net_data) { struct ip send_server_info_ip_header; struct SwiftNetPacketInfo packet_info_new; @@ -306,6 +309,7 @@ static inline void signal_delay_change(const enum PacketDelayUpdateStatus status SWIFTNET_SEND_PACKET(net_data, buffer, sizeof(buffer)); } +#endif struct PacketQueueNode* wait_for_next_packet(struct PacketQueue* const packet_queue) { struct PacketQueueNode* node_to_process; @@ -640,6 +644,7 @@ static inline void swiftnet_process_packets( goto next_packet; } + #ifndef DISABLE_DYNAMIC_RATE_LIMITING case PACKET_DELAY_UPDATE: { enum PacketDelayUpdateStatus* status; @@ -665,6 +670,7 @@ static inline void swiftnet_process_packets( goto next_packet; } + #endif default: break; } @@ -873,6 +879,7 @@ static inline void swiftnet_process_packets( goto next_packet; } else { + #ifndef DISABLE_DYNAMIC_RATE_LIMITING uint32_t new_packets; uint32_t new_packets_validated; float ratio; @@ -894,6 +901,7 @@ static inline void swiftnet_process_packets( pending_message->last_index_checked = packet_info.chunk_index; } } + #endif memcpy(pending_message->packet_data_start + (chunk_data_size * packet_info.chunk_index), packet_data, bytes_to_write); diff --git a/src/send_packet.c b/src/send_packet.c index 509dcec..77a8f52 100644 --- a/src/send_packet.c +++ b/src/send_packet.c @@ -308,7 +308,9 @@ inline void swiftnet_send_packet( .packet_id = packet_id, }; + #ifndef DISABLE_DYNAMIC_RATE_LIMITING atomic_store_explicit(&new_packet_sending->current_send_delay, 50, memory_order_release); + #endif atomic_store_explicit(&new_packet_sending->updated, NO_UPDATE, memory_order_release); hashmap_insert(key_data_mem, sizeof(uint16_t), new_packet_sending, packets_sending); @@ -366,7 +368,9 @@ inline void swiftnet_send_packet( memcpy(buffer_header_location, temp_data_buffer, prepend_size + PACKET_HEADER_SIZE); + #ifndef DISABLE_DYNAMIC_RATE_LIMITING usleep(atomic_load_explicit(&new_packet_sending->current_send_delay, memory_order_acquire)); + #endif } } } else { diff --git a/src/swift_net.h b/src/swift_net.h index 4ef3a68..78a00d1 100644 --- a/src/swift_net.h +++ b/src/swift_net.h @@ -54,18 +54,22 @@ enum PacketType { REQUEST_INFORMATION = 0x02, SEND_LOST_PACKETS_REQUEST = 0x03, SEND_LOST_PACKETS_RESPONSE = 0x04, - SUCCESSFULLY_RECEIVED_PACKET = 0x05, + SUCCESSFULLY_RECEIVED_PACKET = 0x05, + #ifndef DISABLE_DYNAMIC_RATE_LIMITING PACKET_DELAY_UPDATE = 0x06, + #endif #ifdef SWIFT_NET_REQUESTS REQUEST = 0x07, RESPONSE = 0x08, #endif }; +#ifndef DISABLE_DYNAMIC_RATE_LIMITING enum PacketDelayUpdateStatus { LOWER_DELAY, INCREASE_DELAY }; +#endif #define PACKET_INFO_ID_NONE 0xFFFF @@ -126,8 +130,10 @@ struct SwiftNetPendingMessage { uint8_t* packet_data_start; uint32_t chunks_received_length; uint32_t chunks_received_number; + #ifndef DISABLE_DYNAMIC_RATE_LIMITING uint32_t last_index_checked; uint32_t last_chunks_received_number; + #endif uint16_t source_port; uint16_t packet_id; bool sending_lost_packets; @@ -156,7 +162,9 @@ enum PacketSendingUpdated { struct SwiftNetPacketSending { uint32_t* lost_chunks; _Atomic enum PacketSendingUpdated updated; + #ifndef DISABLE_DYNAMIC_RATE_LIMITING _Atomic uint32_t current_send_delay; + #endif uint32_t lost_chunks_size; uint16_t packet_id; _Atomic bool locked;