Skip to content
Open
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
3 changes: 3 additions & 0 deletions include/config.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@

#define UDT_VERSION 4

#define MIN_DATA_SIZE (8 * sizeof(uint32_t))
#define MAX_DATA_SIZE 1024
#define MAX_PACKET_SIZE (4 * sizeof(uint32_t) + MAX_DATA_SIZE)

#define MAX_LOSS_SIZE 100

/* comment the following definition to turn off debuf */
#define DEBUG 1

Expand Down
7 changes: 7 additions & 0 deletions include/losslist.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
#ifndef LOSSLIST_H_OFAY39Z2
#define LOSSLIST_H_OFAY39Z2

int loss_list_add (uint32_t);
int loss_list_remove (uint32_t);

#endif /* end of include guard: LOSSLIST_H_OFAY39Z2 */
6 changes: 6 additions & 0 deletions include/packet.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,12 @@
#define packet_get_type(PACKET) \
((PACKET).header._head0 & PACKET_MASK_TYPE)

#define packet_get_seqnum(PACKET) \
((PACKET).header._head0 & PACKET_MASK_SEQ)

#define packet_get_order(PACKET) \
((PACKET).header._head1 & 0x20000000)

#define packet_clear_header(PACKET) \
((PACKET).header._head0 &= 0x00000000); \
((PACKET).header._head1 &= 0x00000000); \
Expand Down
43 changes: 43 additions & 0 deletions src/losslist.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
#include <inttypes.h>

#include "losslist.h"
#include "config.h"

static uint32_t loss_list[MAX_LOSS_SIZE];
static int last = 0;

int loss_list_getindex(uint32_t);

/* TODO: Optimize this to not use O(n) time */

int loss_list_add(uint32_t seqnum)
{
if (last >= MAX_LOSS_SIZE) return -1;
if (loss_list_getindex(seqnum) == -1) return -1;

loss_list[last] = seqnum;
last++;
return 0;
}

int loss_list_remove(uint32_t seqnum)
{
int i;
i = loss_list_getindex(seqnum);
if (i == -1) return -1;

for (; i < MAX_LOSS_SIZE - 1; ++i) {
loss_list[i] = loss_list[i + 1];
}

last--;
return 0;
}

int loss_list_getindex(uint32_t seqnum)
{
for (int i = 0; i < MAX_LOSS_SIZE; ++i) {
if (loss_list[i] == seqnum) return i;
}
return -1;
}
42 changes: 38 additions & 4 deletions src/packet.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#include "util.h"

extern conn_t connection;
int last_packet = 0;

#define loss_list_add(NUM)

void packet_deserialize(packet_t *packet)
{
Expand Down Expand Up @@ -128,16 +131,47 @@ void packet_parse(packet_t packet)

if (packet.header._head1 & 0x80000000 &&
packet.header._head1 & 0x40000000) /* solo packet */
{
console_log_mod("%x: solo\n", packet_get_seqnum(packet));
recv_buffer_write(packet.data, PACKET_DATA_SIZE);

else if (packet.header._head1 & 0x40000000) /* last packet */
recv_buffer_write(packet.data, PACKET_DATA_SIZE);
}

else if (packet.header._head1 & 0x80000000) /* first packet */
{
console_log_mod("%x: first\n", packet_get_seqnum(packet));
if (packet_get_order(packet))
last_packet = packet_get_seqnum(packet);
recv_buffer_write(packet.data, -1);
}

else /* middle packet */
recv_buffer_write(packet.data, -1);
{
if (packet_get_order(packet)) {
uint32_t seqnum = packet_get_seqnum(packet);
if (seqnum == last_packet + 1) {
console_log_mod("%x: correct\n", seqnum);

if (packet.header._head1 & 0x40000000) /* last packet */
recv_buffer_write(packet.data, PACKET_DATA_SIZE);
else
recv_buffer_write(packet.data, -1);

last_packet++;
} else {
console_log_mod("%x: loss\n", seqnum);
uint32_t num = last_packet;
while (num < seqnum) {
packet_clear_header(packet);
packet_set_ctrl(packet);
packet_set_type(packet, PACKET_TYPE_NAK);
packet_new(&packet, (char *) &num, sizeof(uint32_t));
send_packet_buffer_write(&packet);
loss_list_add(num);
num++;
}
}
}
}
}
return;
}