UDP packet reliability with packetID and bitfield

N

nougat

Guest
I'm working on packet reliability over udp. I'm using packet ids and bits to make this happen.
Everytime i send a packet i increment the packetID by 1.
This is kinda complicated as i have not worked with bit operations before.

Every time i recieve a packet i add the packet id to a queue which holds the previous 32 id's.
Code:
ds_queue_enqueue(packetQueue,packetID);
Then i make sure that it doesnt exceed a size of 32 values (discard the oldest ones),
aswell as checking if the new packet id is more recent than the previous one.
I'm using a 32byte bitfield to check which packets i got.
Code:
if(ds_queue_size(packetQueue)>32) ds_queue_dequeue(packetQueue);
if(packetID > last_packetID) last_packetID = packetID;
Here is where i'm struggling at the moment, i'm not sure if im doing it right.

Local instance, sending bitfield.
Code:
//End step
var bitfield = 0;
var q = ds_queue_create(); //new queue to read from
ds_queue_copy(q,packetQueue); //copy queue
//loop though queue
for(var i = 0; i < ds_queue_size(q); i++)
{
     var qValue = ds_queue_head(q); //get last recieved queue (this is last_packetID - i)
     ds_queue_dequeue(q); //remove last recieved for next loop
     var qPos = last_packetID - qValue; //get position of the last 32 packets recieved
     //If the position is within the range of [1,32] then we set the bit of that position to 1
     if(qPos <= 32) bitfield = bitfield | (1<<qPos);
}
I havn't gotten to the point where i read from the bitfield yet.
This is the article i got it from.
http://gafferongames.com/networking-for-game-programmers/reliability-and-flow-control/
 
Top