Commit 20c187f0 authored by Simon Wüllhorst's avatar Simon Wüllhorst

Implemented PEER_INFO on server side.

And a few minor changes:
- Use an own buffer for stdin.
- Initialize hb_timeout even in pre game mode.
parent 2d0a05a5
......@@ -38,6 +38,10 @@
#define STATE_WAIT_USER_INPUT 3
#define STATE_FINALIZE 4
#define CLIENT_STATE_INIT 0
#define CLIENT_STATE_WAIT_FOR_PEER 1
#define CLIENT_STATE_WAIT_PEER_INFO_ACK 2
#define FIRST_SEQ_NUMBER 0
#define TIMEOUT_ACK_MIN_MS 200
#define TIMEOUT_ACK_MAX_MS 20000
......@@ -71,7 +75,7 @@ struct msg_peer_info_t {
uint32_t net_addr;
uint16_t net_port;
uint16_t start_flag;
char* name;
//char* name;
} __attribute__((packed));
typedef struct msg_peer_info_t msg_peer_info;
......@@ -138,7 +142,10 @@ struct client_info_t {
int client_fd;
struct timer* hb_timer;
unsigned int hb_timeout_ms;
int client_state;
struct credentials_t credentials;
uint32_t net_addr;
uint16_t net_port;
struct message_buffer_t message;
};
typedef struct client_info_t client_info;
......
......@@ -214,6 +214,7 @@ void handle_rec_msg(game_peers_info* peers_info, struct msg_meta_t* meta_data)
finalize(peers_info, -1, "Damn it, got a msg_reg_nack, going to die!\n");
return;
case MSG_PEER_INFO:
printf("Got Peer Info\n");
return;
};
} else {
......@@ -284,12 +285,12 @@ ssize_t packet_sequenzer(game_peers_info* peers_info, ssize_t n)
void handle_rec_tcp(game_peers_info* peers_info)
{
ssize_t n;
printf("handle_client_rec\n");
//printf("handle_client_rec\n");
if ((n = Recv(peers_info->net_fd, (void*)(peers_info->message.buf + peers_info->message.buf_offset), BUFFER_SIZE - peers_info->message.buf_offset, 0)) == 0) {
finalize(peers_info, -1, NULL);
return;
}
printf("Received %zd bytes, buf offset: %zd \n", n, peers_info->message.buf_offset);
//printf("Received %zd bytes, buf offset: %zd \n", n, peers_info->message.buf_offset);
peers_info->message.buf_offset = packet_sequenzer(peers_info, n);
}
int make_own_move(game_peers_info* peers_info, int column)
......@@ -333,13 +334,14 @@ int make_own_move(game_peers_info* peers_info, int column)
void handle_stdin(game_peers_info* peers_info)
{
int column;
char inbuf[BUFFER_SIZE];
ssize_t len;
len = Read(STDIN_FILENO, (void*)peers_info->message.buf, BUFFER_SIZE);
len = Read(STDIN_FILENO, (void*)inbuf, BUFFER_SIZE);
if (len == 0)
return;
//special commands
switch (*peers_info->message.buf) {
switch (*inbuf) {
case 'p':
print_board();
return;
......@@ -350,7 +352,7 @@ void handle_stdin(game_peers_info* peers_info)
if (peers_info->state.current_state != STATE_WAIT_USER_INPUT)
return;
column = strtol(peers_info->message.buf, NULL, 10) - 1;
column = strtol(inbuf, NULL, 10) - 1;
if (make_own_move(peers_info, column) != 0)
return;
......@@ -397,7 +399,8 @@ int init_gs_4gw(game_peers_info* peers_info)
register_fd_callback(peers_info->net_fd, (void (*)(void*)) & handle_rec_tcp, peers_info);
peers_info->timer.hb_timer = create_timer((void (*)(void*)) & handle_hb_timer, peers_info, "HB_TIMER");
start_timer(peers_info->timer.hb_timer, 200);
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;
......@@ -412,9 +415,6 @@ int init_4gw(game_peers_info* peers_info)
Bind(peers_info->net_fd, peers_info->local->ai_addr, peers_info->local->ai_addrlen);
Connect(peers_info->net_fd, (const struct sockaddr*)peers_info->remote->ai_addr, peers_info->remote->ai_addrlen);
memset(&peers_info->state, 0, sizeof(peers_info->state));
memset(&peers_info->timer, 0, sizeof(peers_info->timer));
register_stdin_callback((void (*)(void*)) & handle_stdin, peers_info);
register_fd_callback(peers_info->net_fd, (void (*)(void*)) & handle_rec_udp, peers_info);
peers_info->timer.hb_timer = create_timer((void (*)(void*)) & handle_hb_timer, peers_info, "HB_TIMER");
......@@ -444,6 +444,7 @@ int init_4gw(game_peers_info* peers_info)
int main(int argc, char** argv)
{
game_peers_info peers_info;
memset(&peers_info, 0, sizeof(peers_info));
init_4clib();
init_cblib();
peers_info.state.current_mode = parse_args(argc, argv, &peers_info);
......
......@@ -85,12 +85,56 @@ void handle_client_hb_timer(client_info* a_client)
start_timer(a_client->hb_timer, a_client->hb_timeout_ms);
return;
}
void send_peer_info(client_info* a_client, client_info* a_clients_peer, uint16_t start_flag)
{
ssize_t name_padding, s;
char *a_msg, *a_name;
msg_peer_info* a_peer_info;
name_padding = (4 - a_clients_peer->credentials.name_len % 4) % 4;
s = sizeof(msg_peer_info) + a_clients_peer->credentials.name_len + name_padding;
a_msg = calloc(1, s);
a_peer_info = (msg_peer_info*)a_msg;
a_peer_info->type = htons(MSG_PEER_INFO);
a_peer_info->length = htons(s - name_padding);
a_peer_info->net_addr = htonl(a_clients_peer->net_addr);
a_peer_info->net_port = htons(a_clients_peer->net_port);
a_peer_info->start_flag = htons(start_flag);
a_name = a_msg + sizeof(msg_peer_info);
memcpy(a_name, a_clients_peer->credentials.name, a_clients_peer->credentials.name_len);
Send(a_client->client_fd, a_msg, s, 0);
//TODO: Set ACK-Timer
a_client->client_state = CLIENT_STATE_WAIT_PEER_INFO_ACK;
}
void find_game_peers(game_server_info* server_info)
{
client_info *a_client, *b_client, *list;
a_client = NULL;
b_client = NULL;
list = server_info->client_queue.next;
for (; list != NULL; list = list->next) {
if (list->client_state == CLIENT_STATE_WAIT_FOR_PEER) {
if (a_client == NULL) {
a_client = list;
} else {
b_client = list;
send_peer_info(a_client, b_client, 0);
send_peer_info(b_client, a_client, 1);
return;
}
}
}
}
void handle_peer_reg(client_info* a_client, struct msg_meta_t* a_msg)
{
ssize_t name_padd, pass_len;
char *a_name, *a_pass;
msg_reg* a_reg_msg;
//printf("Got a peer_reg message\n");
a_reg_msg = (msg_reg*)a_msg;
a_reg_msg->net_addr = ntohl(a_reg_msg->net_addr);
a_reg_msg->net_port = ntohs(a_reg_msg->net_port);
......@@ -107,18 +151,22 @@ void handle_peer_reg(client_info* a_client, struct msg_meta_t* a_msg)
a_client->credentials.pass = malloc(a_client->credentials.pass_len);
memcpy(a_client->credentials.name, a_name, a_client->credentials.name_len);
memcpy(a_client->credentials.pass, a_pass, a_client->credentials.pass_len);
a_client->net_addr = a_reg_msg->net_addr;
a_client->net_port = a_reg_msg->net_port;
// printf("s_len: %d, c_len: %d\n", a_client->server_info->credentials.pass_len, a_client->credentials.pass_len);
// printf("s: %.*s, p: %.*s\n", a_client->server_info->credentials.pass_len, a_client->server_info->credentials.pass, a_client->credentials.pass_len, a_client->credentials.pass);
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_primitive_message(a_client, MSG_REG_ACK);
a_client->client_state = CLIENT_STATE_WAIT_FOR_PEER;
find_game_peers(a_client->server_info);
} else {
//TODO: send peer_reg_nack
printf("Entered password is invalid.\n");
send_primitive_message(a_client, MSG_REG_NACK);
}
printf("Got a peer_reg message\n");
}
void handle_client_message(client_info* a_client, struct msg_meta_t* a_msg)
......@@ -186,6 +234,7 @@ client_info* initialize_client(game_server_info* server_info, int a_client_fd)
a_client->server_info = server_info;
a_client->client_fd = a_client_fd;
a_client->hb_timeout_ms = TIMEOUT_HB_MIN_MS;
a_client->client_state = CLIENT_STATE_INIT;
register_fd_callback(a_client->client_fd, (void (*)(void*)) & handle_client_rec, a_client);
a_client->hb_timer = create_timer((void (*)(void*)) & handle_client_hb_timer, a_client, "A_REQUIRED_STRING");
start_timer(a_client->hb_timer, a_client->hb_timeout_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