Commit be9655d8 authored by Simon Wüllhorst's avatar Simon Wüllhorst

Simplified sequence number handling.

Send acks for duplicated packets, too.
parent 4eaa3c4e
This diff is collapsed.
......@@ -45,10 +45,10 @@ enum { CLIENT_STATE_INIT,
CLIENT_STATE_WAIT_PEER_INFO_ACK };
#define FIRST_SEQ_NUMBER 0
#define TIMEOUT_ACK_MIN_MS 20000
#define TIMEOUT_ACK_MAX_MS 200000
#define TIMEOUT_HB_MIN_MS 20000
#define TIMEOUT_HB_MAX_MS 200000
#define TIMEOUT_ACK_MIN_MS 200
#define TIMEOUT_ACK_MAX_MS 20000
#define TIMEOUT_HB_MIN_MS 8000
#define TIMEOUT_HB_MAX_MS 120000
#define DEBUG
/*
......@@ -101,8 +101,6 @@ struct game_state_t {
uint32_t my_latest_move;
uint32_t sec_my;
uint32_t sec_rem_next_excepted;
bool initial_move_me;
bool initial_move_foreign;
};
typedef struct game_state_t game_state;
......@@ -131,7 +129,6 @@ struct game_peers_info_t {
struct addrinfo* remote;
int start_flag;
int net_fd;
int server_fd;
struct game_state_t state;
struct game_timer_t timer;
struct message_buffer_t message;
......
......@@ -37,7 +37,8 @@ int parse_remote_server(char** argv, game_peers_info* peers_info)
}
int parse_args(int argc, char** argv, game_peers_info* peers_info)
{
char fmt_string[] = "Wrong amount of parameters. Propper usage:\n %s LOCAL_PORT {(-c REMOTE_CLIENT_ADDRESS REMOTE_CLIENT_PORT) | (-s REMOTE_SERVER_ADDRESS REMOTE_SERVER_PORT USERNAME PASSWORD)}\n";
char fmt_string[] = "Wrong amount of parameters. Propper usage:\n %s LOCAL_PORT \
{(-c REMOTE_CLIENT_ADDRESS REMOTE_CLIENT_PORT) | (-s REMOTE_SERVER_ADDRESS REMOTE_SERVER_PORT USERNAME PASSWORD)}\n";
struct addrinfo hints;
if (argc < 6) {
printf(fmt_string, argv[0]);
......@@ -46,10 +47,7 @@ int parse_args(int argc, char** argv, game_peers_info* peers_info)
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
//hints.ai_family = AF_INET6;
//hints.ai_socktype = SOCK_STREAM;
hints.ai_socktype = SOCK_DGRAM;
//hints.ai_flags = AI_PASSIVE | AI_V4MAPPED;
hints.ai_flags = AI_PASSIVE;
Getaddrinfo(NULL, argv[1], &hints, &peers_info->local);
......@@ -123,28 +121,36 @@ void handle_ack_timer(game_peers_info* peers_info)
start_timer(peers_info->timer.ack_timer, peers_info->timer.ack_timeout_ms);
return;
}
void send_move_ack(game_peers_info* peers_info, uint32_t seq_nr)
{
msg_move_ack a_move_ack;
a_move_ack.length = htons(sizeof(uint32_t));
a_move_ack.type = htons(MSG_MOVE_ACK);
a_move_ack.seg_nr = htonl(seq_nr);
Send(peers_info->net_fd, &a_move_ack, sizeof(a_move_ack), 0);
}
int handle_foreign_move(game_peers_info* peers_info, msg_move* a_move)
{
int a_winner;
msg_move_ack a_move_ack;
a_move->seg_nr = ntohl(a_move->seg_nr);
a_move->column = ntohl(a_move->column);
printf("GOT MOVE: SEG_NR: %d, COLUMN: %d\n", a_move->seg_nr, a_move->column);
// Duplicate Packet, ignore it.
if (peers_info->state.initial_move_foreign) {
peers_info->state.initial_move_foreign = false;
if (peers_info->state.sec_rem_next_excepted == 0) {
// This seems to be the first foreign move. Since opposite player
// can start with a sequence number unlike 0 we set the seq num
// to this value.
peers_info->state.sec_rem_next_excepted = a_move->seg_nr;
} else {
if (a_move->seg_nr < peers_info->state.sec_rem_next_excepted) {
printf("GOT duplicate packet, seq_nr: %d\n", a_move->seg_nr);
return -1;
}
if (a_move->seg_nr > peers_info->state.sec_rem_next_excepted) {
//Strage sequence number, strange peer. Going to die.
printf("Excepted: %d, Received: %d\n", peers_info->state.sec_rem_next_excepted, a_move->seg_nr);
finalize(peers_info, -1, "ERR: Sequence Number higher than excepted.\n");
}
}
if (a_move->seg_nr < peers_info->state.sec_rem_next_excepted) {
printf("GOT duplicate packet, seq_nr: %d\n", a_move->seg_nr);
send_move_ack(peers_info, a_move->seg_nr);
return 0;
}
if (a_move->seg_nr > peers_info->state.sec_rem_next_excepted) {
//Strage sequence number, strange peer. Going to die.
printf("Excepted: %d, Received: %d\n", peers_info->state.sec_rem_next_excepted, a_move->seg_nr);
finalize(peers_info, -1, "ERR: Sequence Number higher than excepted.\n");
}
//Current state does not allow foreign move.
if (peers_info->state.current_state != STATE_WAIT_FOREIGN_MOVE)
......@@ -154,12 +160,9 @@ int handle_foreign_move(game_peers_info* peers_info, msg_move* a_move)
if (valid_move(a_move->column) != 1)
finalize(peers_info, -1, "ERR: Wrong column number or full column.\n");
send_move_ack(peers_info, a_move->seg_nr);
make_move(a_move->column, REMOTE_PLAYER);
a_move_ack.length = htons(sizeof(uint32_t));
a_move_ack.type = htons(MSG_MOVE_ACK);
a_move_ack.seg_nr = htonl(a_move->seg_nr);
Send(peers_info->net_fd, &a_move_ack, sizeof(a_move_ack), 0);
if ((a_winner = winner()) != 0) {
printf("We've a winner: %d\n", a_winner);
finalize(peers_info, 0, NULL);
......@@ -185,6 +188,7 @@ int handle_msg_ack(game_peers_info* peers_info, msg_move_ack* a_move_ack)
finalize(peers_info, 0, "Final finalize() call.\n");
}
peers_info->state.current_state = STATE_WAIT_FOREIGN_MOVE;
++peers_info->state.sec_my;
return 0;
}
......@@ -200,7 +204,6 @@ void finalize_game_server(game_peers_info* peers_info)
}
peers_info->state.current_mode = MODE_DIRECT;
Close(peers_info->net_fd);
//peers_info->server_fd = peers_info->net_fd;
peers_info->net_fd = -1;
}
void handle_msg_peer_info(game_peers_info* peers_info, struct msg_meta_t* meta_data)
......@@ -212,7 +215,6 @@ void handle_msg_peer_info(game_peers_info* peers_info, struct msg_meta_t* meta_d
char net_addr[16], net_port[6];
struct addrinfo hints;
struct sockaddr_in ip_addr;
//struct sockaddr_in a_game_peer;
a_msg = (char*)meta_data;
a_peer_info_msg = (msg_peer_info*)meta_data;
......@@ -250,7 +252,7 @@ void handle_msg_peer_info(game_peers_info* peers_info, struct msg_meta_t* meta_d
void handle_rec_msg(game_peers_info* peers_info, struct msg_meta_t* meta_data)
{
printf("GOT PACKET: LENGHT: %d, TYPE: %d\n", meta_data->length, meta_data->type);
//printf("GOT PACKET: LENGHT: %d, TYPE: %d\n", meta_data->length, meta_data->type);
switch (meta_data->type) {
case MSG_HEARTBEAT:
......@@ -371,11 +373,6 @@ int make_own_move(game_peers_info* peers_info, int column)
fflush(stdout);
return -1;
}
if (peers_info->state.initial_move_me == true) {
peers_info->state.initial_move_me = false;
} else {
++peers_info->state.sec_my;
}
make_move(column, LOCAL_PLAYER);
a_move.type = htons(MSG_MOVE);
......@@ -478,7 +475,6 @@ int init_gs_4gw(game_peers_info* peers_info)
peers_info->timer.hb_timeout_ms = TIMEOUT_HB_MIN_MS;
start_timer(peers_info->timer.hb_timer, peers_info->timer.hb_timeout_ms);
send_reg_msg(peers_info);
//Close(peers_info->net_fd);
peers_info->state.current_state = STATE_INIT;
return 0;
}
......@@ -495,8 +491,6 @@ int init_4gw(game_peers_info* peers_info)
peers_info->timer.ack_timer = create_timer((void (*)(void*)) & handle_ack_timer, peers_info, "ACK_TIMER");
start_timer(peers_info->timer.hb_timer, 10000);
peers_info->state.initial_move_me = true;
peers_info->state.initial_move_foreign = true;
peers_info->timer.ack_timeout_ms = TIMEOUT_ACK_MIN_MS;
peers_info->timer.hb_timeout_ms = TIMEOUT_HB_MIN_MS;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment