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

Wait for ack on last move (won game), too.

parent 3e184a7a
......@@ -34,8 +34,8 @@
#define STATE_INIT 0
#define STATE_WAIT_FOREIGN_MOVE 1
#define STATE_WAIT_ACK 2
//#define STATE_GOT_FORGEIN_MOVE 2
#define STATE_WAIT_USER_INPUT 3
#define STATE_FINALIZE 4
#define FIRST_SEQ_NUMBER 0
#define TIMEOUT_ACK_MIN_MS 200
......
......@@ -70,6 +70,11 @@ void finalize(game_peers_info* peers_info, int exit_code, char* last_message)
// stop_timer(peers_info->timer.hb_timer);
if (last_message != NULL)
printf("%s", last_message);
if (peers_info->state.current_state == STATE_WAIT_ACK) {
printf("Wait for foreign ACK before finalize completely.\n");
peers_info->state.current_state = STATE_FINALIZE;
return;
}
Close(peers_info->net_fd);
print_board();
freeaddrinfo(peers_info->local);
......@@ -99,6 +104,7 @@ void handle_ack_timer(game_peers_info* peers_info)
{
//Resend latest move message
msg_move a_move;
peers_info->timer.ack_timeout_ms += peers_info->timer.ack_timeout_ms;
if (peers_info->timer.ack_timeout_ms > TIMEOUT_ACK_MAX_MS)
finalize(peers_info, -1, "Maximum Timeout reached, peers seems to be dead!\nGoing to die!\n");
......@@ -108,7 +114,6 @@ void handle_ack_timer(game_peers_info* peers_info)
a_move.column = htonl(peers_info->state.my_latest_move);
Send(peers_info->net_fd, &a_move, sizeof(a_move), 0);
start_timer(peers_info->timer.ack_timer, peers_info->timer.ack_timeout_ms);
peers_info->timer.ack_timeout_ms += peers_info->timer.ack_timeout_ms;
return;
}
......@@ -140,8 +145,10 @@ int handle_foreign_move(game_peers_info* peers_info, msg_move* a_move)
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)
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);
......@@ -176,13 +183,16 @@ int handle_foreign_move(game_peers_info* peers_info, msg_move* a_move)
int handle_msg_ack(game_peers_info* peers_info, msg_move_ack* a_move_ack)
{
if (peers_info->state.current_state != STATE_WAIT_ACK)
if (peers_info->state.current_state != STATE_WAIT_ACK && peers_info->state.current_state != STATE_FINALIZE)
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);
if (peers_info->state.current_state == STATE_FINALIZE) {
finalize(peers_info, 0, "Final finalize() call.\n");
}
peers_info->state.current_state = STATE_WAIT_FOREIGN_MOVE;
return 0;
}
......@@ -298,6 +308,8 @@ int make_own_move(game_peers_info* peers_info, int column)
Send(peers_info->net_fd, &a_move, sizeof(a_move), 0);
peers_info->state.current_state = STATE_WAIT_ACK;
if ((a_winner = winner()) != 0) {
printf("We've a winner: %d\n", a_winner);
finalize(peers_info, 0, NULL);
......@@ -333,8 +345,6 @@ void handle_stdin(game_peers_info* peers_info)
if (make_own_move(peers_info, column) != 0)
return;
peers_info->state.current_state = STATE_WAIT_ACK;
//printf("STATE->STATE_WAIT_ACK\n");
return;
}
......
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