Commit 3e184a7a authored by Simon Wüllhorst's avatar Simon Wüllhorst

Updated sequence number handling:

- Allow foreign client to start first move with any sequence number.
- move_ack will not ack seq+1 rather than seq now.
parent b36c92f7
......@@ -98,8 +98,8 @@ typedef struct msg_move_ack_t msg_move_ack;
struct game_state_t {
uint16_t current_state;
uint32_t my_latest_move;
uint16_t sec_my;
uint16_t sec_rem_next_excepted;
int16_t sec_my;
int16_t sec_rem_next_excepted;
};
typedef struct game_state_t game_state;
......
......@@ -137,12 +137,16 @@ int handle_foreign_move(game_peers_info* peers_info, msg_move* a_move)
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 (a_move->seg_nr < peers_info->state.sec_rem_next_excepted)
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 (peers_info->state.sec_rem_next_excepted == -1) {
peers_info->state.sec_rem_next_excepted = a_move->seg_nr;
} else {
if (a_move->seg_nr < peers_info->state.sec_rem_next_excepted)
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");
}
}
//Current state does not allow foreign move.
if (peers_info->state.current_state != STATE_WAIT_FOREIGN_MOVE)
......@@ -156,7 +160,7 @@ int handle_foreign_move(game_peers_info* peers_info, msg_move* a_move)
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 + 1);
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);
......@@ -175,6 +179,7 @@ int handle_msg_ack(game_peers_info* peers_info, msg_move_ack* a_move_ack)
if (peers_info->state.current_state != STATE_WAIT_ACK)
return -1;
a_move_ack->seg_nr = ntohl(a_move_ack->seg_nr);
printf("seg_ack: %d, my_sec: %d\n", a_move_ack->seg_nr, peers_info->state.sec_my);
if (a_move_ack->seg_nr != peers_info->state.sec_my)
return -1;
stop_timer(peers_info->timer.ack_timer);
......@@ -281,7 +286,7 @@ int make_own_move(game_peers_info* peers_info, int column)
fflush(stdout);
return -1;
}
++peers_info->state.sec_my;
make_move(column, LOCAL_PLAYER);
a_move.type = htons(MSG_MOVE);
......@@ -290,7 +295,6 @@ int make_own_move(game_peers_info* peers_info, int column)
a_move.column = htonl((uint32_t)column);
peers_info->state.my_latest_move = (uint32_t)column;
++peers_info->state.sec_my;
Send(peers_info->net_fd, &a_move, sizeof(a_move), 0);
......@@ -370,8 +374,8 @@ 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.sec_my = FIRST_SEQ_NUMBER;
peers_info->state.sec_rem_next_excepted = FIRST_SEQ_NUMBER;
peers_info->state.sec_my = -1;
peers_info->state.sec_rem_next_excepted = -1;
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