Skip to content

Commit

Permalink
Update README.md
Browse files Browse the repository at this point in the history
  • Loading branch information
vp817 authored Jul 29, 2024
1 parent 6404b25 commit 26ad767
Showing 1 changed file with 133 additions and 0 deletions.
133 changes: 133 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,139 @@ RakNet uses a reassembly mechanism to reconstruct segmented datagrams that may b
### Flow Control
Flow Control is a RakNet mechanism used to manage the rate of data transmission between sender and receiver. It ensures that the receiver can handle the incoming data at a pace it can process, preventing overwhelming or overflowing the receiver's buffer. Flow control helps maintain a balance between the sender's transmission speed and the receiver's processing capability, optimizing the overall efficiency and stability of the communication.

### Congestion Manager

congestion manager holds congestion control, checking for skipped range numbers to send nacks, and many other things

TODO: document those but it's a waste of time since people will just copy the values so i will just paste my CongestionManager that i made for someone i know (You can make a pr that instead of code it will be a documentation about it if you want):

```c
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>

#define UINT24_MAX_SIZE 0xffffff
#define HALF_SPAN (((uint32_t) - 1 & UINT24_MAX_SIZE) >> 1)
#define MAX_POSSIBLE_MTU_SIZE 10000
#define MAX_MTU_SIZE 1492
#define DEFAULT_ADJUSTMENT 440
#define DEFAULT_SHIFT 7

typedef struct
{
int mtu_size;
int expected_next_range_number;
} congestion_manager_t;

bool cong_mgr_is_greater_than(int first, int last)
{
return last != first && (((last - first) & UINT24_MAX_SIZE) > HALF_SPAN);
}

int cong_mgr_calc_skipped_range_number_count(congestion_manager_t *cm, int range_number)
{
int result = 0;
bool update_prediction = false;

if (range_number == cm->expected_next_range_number)
{
update_prediction = true;
}
else if (cong_mgr_is_greater_than(range_number, cm->expected_next_range_number))
{
result = range_number - cm->expected_next_range_number;

if (result > 0x3e8)
{
// NAT RELATED
if (result > 0xc350)
{
return -1;
}
result = 0x3e8;
}

update_prediction = true;
}

if (update_prediction)
{
cm->expected_next_range_number = range_number + 1;
}

return result;
}

// only occured when segmenting large packets but this will be needed for sending valid datagrams instead
// because if you send to the server/client it's not able to receive them because of udp and it will lose them (which only happens if you are trying to ddos or sending large packets that has too much segments or something)
// only a bunch of segments got sent when i was sending a packet that had a segments count of 500-400 at high/normal speed and wasn't able to send more than little of them
// so this is a solution which you can use for sending dgrams instead of only segments
// the "max_dgrams_send_count" is used by checking if "datagrams sent count" which increments every time you send a dgram is greater than that max count
// then once it is true you can go inside and check for microseconds "current microsecond time" - "last dgram send tick" is smaller than "adjustment";
void cong_mgr_calc_max_dgram_send_count(congestion_manager_t *cm, int *max_dgrams_send_count, int *adjustment)
{
int shift = DEFAULT_SHIFT;
int adj = DEFAULT_ADJUSTMENT;

if (cm->mtu_size < MAX_POSSIBLE_MTU_SIZE)
{
int max_mtu_size = MAX_MTU_SIZE;
int min_adjustment = 145;
int min_shift = 4;

float adjustment_scale = 1.0f;
float shift_scale = 1.0f;

float factor = (float)(cm->mtu_size - max_mtu_size) / (3000 - max_mtu_size);

adj = (int)(min_adjustment + factor * (adjustment_scale * 200 - min_adjustment));
int shift_to_set = (int)(min_shift + factor * (shift_scale * 5 - min_shift));

if (shift_to_set == 3 && cm->mtu_size >= 1000)
{
shift_to_set = 4;
}
if (shift_to_set < shift)
{
shift = shift_to_set;
}
}

*max_dgrams_send_count = (cm->mtu_size - adj) >> shift;
*adjustment = adj;
}

// Usage example:
int main()
{
congestion_manager_t cm = {
.mtu_size = MAX_MTU_SIZE,
.expected_next_range_number = 1,
};

int skipped_range_number_count = cong_mgr_calc_skipped_range_number_count(&cm, 2);
if (skipped_range_number_count > 0)
{
for (int offset = skipped_range_number_count; offset > 0; offset--)
{
printf("insert to nack range list (received range number - offset)\n");
}
}
else if (skipped_range_number_count == -1)
{
printf("Nat related (free mem and skip if required)\n");
}

int max_dgrams_send_count = 0;
int adjustment = 0;

cong_mgr_calc_max_dgram_send_count(&cm, &max_dgrams_send_count, &adjustment);

printf("max dgram send count: %i\n", max_dgrams_send_count);
printf("adjustment: %i\n", adjustment);
}
```
### Congestion Control
Congestion control is a RakNet technique used to prevent network congestion by balancing data transmission rates. Techniques like TCP congestion control, packet dropping, rate limiting, traffic shaping, QoS, and load balancing are used. These techniques ensure reliable data delivery and efficient transmission in RakNet.
Expand Down

0 comments on commit 26ad767

Please sign in to comment.