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

Do some refactoring:

Use a single function for all 'primitive' message types
parent fb65eaac
......@@ -53,13 +53,6 @@ struct msg_meta_t {
uint16_t length;
} __attribute__((packed));
typedef struct msg_meta_t msg_reg_ack;
typedef struct msg_meta_t msg_reg_nack;
typedef struct msg_meta_t msg_peer_info_ack;
typedef struct msg_meta_t msg_unknown_msg_type;
typedef struct msg_meta_t msg_heartbeat;
typedef struct msg_meta_t msg_heartbeat_ack;
struct msg_reg_t {
uint16_t type;
uint16_t length;
......
......@@ -94,15 +94,18 @@ int handle_have_winner(game_peers_info* peers_info)
{
return 0;
}
void send_primitive_message(game_peers_info* peers_info, uint16_t type)
{
struct msg_meta_t a_meta_msg;
a_meta_msg.length = htons(0);
a_meta_msg.type = htons(type);
Send(peers_info->net_fd, &a_meta_msg, sizeof(a_meta_msg), 0);
}
void handle_hb_timer(game_peers_info* peers_info)
{
if (peers_info->timer.hb_timeout_ms > TIMEOUT_HB_MAX_MS)
finalize(peers_info, -1, "Lost peer connection (HBs were not ACKed for a long time).\n");
msg_heartbeat a_hb;
a_hb.length = htons(0);
a_hb.type = htons(MSG_HEARTBEAT);
Send(peers_info->net_fd, &a_hb, sizeof(a_hb), 0);
send_primitive_message(peers_info, MSG_HEARTBEAT);
start_timer(peers_info->timer.hb_timer, peers_info->timer.hb_timeout_ms);
peers_info->timer.hb_timeout_ms += peers_info->timer.hb_timeout_ms;
return;
......@@ -125,23 +128,6 @@ void handle_ack_timer(game_peers_info* peers_info)
return;
}
int send_hb_ack(game_peers_info* peers_info)
{
msg_heartbeat_ack a_hb_ack;
a_hb_ack.length = htons(0);
a_hb_ack.type = htons(MSG_HEARTBEAT_ACK);
Send(peers_info->net_fd, &a_hb_ack, sizeof(a_hb_ack), 0);
return 0;
}
int send_unknown_packet(game_peers_info* peers_info)
{
msg_unknown_msg_type a_unknown;
a_unknown.length = htons(0);
a_unknown.type = htons(MSG_UNKNOWN_MSG_TYPE);
Send(peers_info->net_fd, &a_unknown, sizeof(a_unknown), 0);
return 0;
}
int handle_foreign_move(game_peers_info* peers_info, msg_move* a_move)
{
int a_winner;
......@@ -212,7 +198,7 @@ void handle_rec_msg(game_peers_info* peers_info, struct msg_meta_t* meta_data)
switch (meta_data->type) {
case MSG_HEARTBEAT:
send_hb_ack(peers_info);
send_primitive_message(peers_info, MSG_HEARTBEAT_ACK);
return;
case MSG_HEARTBEAT_ACK:
peers_info->timer.hb_timeout_ms = TIMEOUT_HB_MIN_MS;
......@@ -245,7 +231,7 @@ void handle_rec_msg(game_peers_info* peers_info, struct msg_meta_t* meta_data)
}
// DEFAULT
printf("Received unknown message, msg_type-ID: %d\n", meta_data->type);
send_unknown_packet(peers_info);
send_primitive_message(peers_info, MSG_UNKNOWN_MSG_TYPE);
}
/* for udp */
......@@ -262,14 +248,14 @@ void handle_rec_udp(game_peers_info* peers_info)
}
if (n < sizeof(struct msg_meta_t)) {
send_unknown_packet(peers_info);
send_primitive_message(peers_info, MSG_UNKNOWN_MSG_TYPE);
return;
}
meta_data = (struct msg_meta_t*)peers_info->message.buf;
meta_data->length = ntohs(meta_data->length);
meta_data->type = ntohs(meta_data->type);
if (n != meta_data->length + sizeof(struct msg_meta_t) + (4 - meta_data->length % 4) % 4) {
send_unknown_packet(peers_info);
send_primitive_message(peers_info, MSG_UNKNOWN_MSG_TYPE);
return;
}
handle_rec_msg(peers_info, meta_data);
......
......@@ -65,29 +65,18 @@ void finalize(game_server_info* server_info, int exit_code, char* last_message)
freeaddrinfo(server_info->server);
exit(exit_code);
}
int send_hb_ack(client_info* a_client)
void send_primitive_message(client_info* a_client, uint16_t type)
{
msg_heartbeat_ack a_hb_ack;
a_hb_ack.length = htons(0);
a_hb_ack.type = htons(MSG_HEARTBEAT_ACK);
Send(a_client->client_fd, &a_hb_ack, sizeof(a_hb_ack), 0);
return 0;
}
int send_unknown_packet(client_info* a_client)
{
msg_unknown_msg_type a_unknown;
a_unknown.length = htons(0);
a_unknown.type = htons(MSG_UNKNOWN_MSG_TYPE);
Send(a_client->client_fd, &a_unknown, sizeof(a_unknown), 0);
return 0;
struct msg_meta_t a_meta_msg;
a_meta_msg.length = htons(0);
a_meta_msg.type = htons(type);
Send(a_client->client_fd, &a_meta_msg, sizeof(a_meta_msg), 0);
}
void handle_client_hb_timer(client_info* a_client)
{
//printf("handle_client_hb_timer\n");
msg_heartbeat a_hb;
a_hb.length = htons(0);
a_hb.type = htons(MSG_HEARTBEAT);
Send(a_client->client_fd, &a_hb, sizeof(a_hb), 0);
send_primitive_message(a_client, MSG_HEARTBEAT);
a_client->hb_timeout_ms += a_client->hb_timeout_ms;
if (a_client->hb_timeout_ms > TIMEOUT_HB_MAX_MS) {
finalize_client(a_client);
......@@ -97,21 +86,6 @@ void handle_client_hb_timer(client_info* a_client)
return;
}
void send_reg_ack(client_info* a_client)
{
msg_reg_ack a_reg_ack;
a_reg_ack.length = htons(0);
a_reg_ack.type = htons(MSG_REG_ACK);
Send(a_client->client_fd, &a_reg_ack, sizeof(a_reg_ack), 0);
}
void send_reg_nack(client_info* a_client)
{
msg_reg_ack a_reg_ack;
a_reg_ack.length = htons(0);
a_reg_ack.type = htons(MSG_REG_NACK);
Send(a_client->client_fd, &a_reg_ack, sizeof(a_reg_ack), 0);
}
void handle_peer_reg(client_info* a_client, struct msg_meta_t* a_msg)
{
ssize_t name_padd, pass_len;
......@@ -138,11 +112,11 @@ void handle_peer_reg(client_info* a_client, struct msg_meta_t* a_msg)
if (a_client->server_info->credentials.pass_len == a_client->credentials.pass_len && memcmp(a_client->server_info->credentials.pass, a_client->credentials.pass, a_client->credentials.pass_len) == 0) {
//TODO: send peer_reg_ack, TODO: do further validation
printf("Entered password is valid.\n");
send_reg_ack(a_client);
send_primitive_message(a_client, MSG_REG_ACK);
} else {
//TODO: send peer_reg_nack
printf("Entered password is invalid.\n");
send_reg_nack(a_client);
send_primitive_message(a_client, MSG_REG_NACK);
}
printf("Got a peer_reg message\n");
}
......@@ -159,7 +133,7 @@ void handle_client_message(client_info* a_client, struct msg_meta_t* a_msg)
a_client->hb_timeout_ms = TIMEOUT_HB_MIN_MS;
break;
case MSG_HEARTBEAT:
send_hb_ack(a_client);
send_primitive_message(a_client, MSG_HEARTBEAT_ACK);
break;
case MSG_UNKNOWN_MSG_TYPE:
printf("Sended unknown message.\n");
......@@ -167,7 +141,7 @@ void handle_client_message(client_info* a_client, struct msg_meta_t* a_msg)
break;
default:
printf("Received unknown message, msg_type-ID: %d\n", a_msg->type);
send_unknown_packet(a_client);
send_primitive_message(a_client, MSG_UNKNOWN_MSG_TYPE);
break;
}
}
......
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