Commit 690ae3aa authored by Simon Wüllhorst's avatar Simon Wüllhorst

Several updates:

- use own struct to wrap the buffer
- updated message types structs
parent 1c39ffd6
......@@ -58,12 +58,24 @@ 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_peer_info_t {
struct msg_reg_t {
uint16_t type;
uint16_t length;
uint32_t net_addr;
uint16_t net_port;
uint16_t name_len;
char* name;
char* password;
} __attribute__((packed));
typedef struct msg_reg_t msg_reg;
struct msg_peer_info_t {
uint16_t type;
uint16_t length;
uint32_t net_addr;
uint16_t net_port;
uint16_t start_flag;
char* name;
} __attribute__((packed));
typedef struct msg_peer_info_t msg_peer_info;
......@@ -97,6 +109,12 @@ struct game_timer_t {
unsigned int hb_timeout_ms;
};
typedef struct game_timer_t game_timers;
struct message_buffer_t {
ssize_t buf_offset;
char buf[BUFFER_SIZE];
};
struct game_peers_info_t {
struct addrinfo* local;
struct addrinfo* remote;
......@@ -104,6 +122,7 @@ struct game_peers_info_t {
int net_fd;
struct game_state_t state;
struct game_timer_t timer;
struct message_buffer_t message;
};
typedef struct game_peers_info_t game_peers_info;
......@@ -113,8 +132,7 @@ struct client_info_t {
int client_fd;
struct timer* hb_timer;
unsigned int hb_timeout_ms;
ssize_t buf_offset;
char buf[BUFFER_SIZE];
struct message_buffer_t message;
};
typedef struct client_info_t client_info;
struct game_server_info_t {
......
#include "gamepeer.h"
static char buf[BUFFER_SIZE];
int parse_remote_client(int argc, char** argv, game_peers_info* peers_info)
{
struct addrinfo hints;
......@@ -20,8 +18,43 @@ int parse_remote_client(int argc, char** argv, game_peers_info* peers_info)
Getaddrinfo(argv[3], argv[4], &hints, &peers_info->remote);
return 0;
}
// ssize_t packet_sequenzer(client_info* a_client, ssize_t n)
// {
// ssize_t len;
// uint16_t msg_len;
// struct msg_meta_t* a_msg;
// len = n + a_client->buf_offset;
// if (len < sizeof(struct msg_meta_t))
// return len;
// a_msg = (struct msg_meta_t*)a_client->buf;
// a_msg->length = ntohs(a_msg->length);
// a_msg->type = ntohs(a_msg->type);
// msg_len = sizeof(struct msg_meta_t) + a_msg->length + (4 - a_msg->length % 4) % 4;
// if (len < msg_len)
// return len;
// handle_client_message(a_client, a_msg);
// memmove(a_client->buf, a_client->buf + msg_len, len - msg_len);
// return packet_sequenzer(a_client, n - msg_len);
// }
int parse_remote_server(int argc, char** argv, game_peers_info* peers_info)
{
struct addrinfo hints;
msg_heartbeat hb;
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
hints.ai_socktype = SOCK_STREAM;
Getaddrinfo(argv[3], argv[4], &hints, &peers_info->remote);
peers_info->net_fd = Socket(peers_info->remote->ai_family, peers_info->remote->ai_socktype, peers_info->remote->ai_protocol);
Connect(peers_info->net_fd, peers_info->remote->ai_addr, peers_info->remote->ai_addrlen);
hb.length = htons(0);
hb.type = htons(MSG_HEARTBEAT);
Send(peers_info->net_fd, (const void*)&hb, sizeof(hb), 0);
Close(peers_info->net_fd);
printf("ERR: Server-Modus not implemented, yet.\n");
return -1;
}
......@@ -186,7 +219,7 @@ void handle_rec(game_peers_info* peers_info)
//printf("GOT MESSAGE\n");
n = Recv(peers_info->net_fd, (void*)buf, BUFFER_SIZE, 0);
n = Recv(peers_info->net_fd, (void*)peers_info->message.buf, BUFFER_SIZE, 0);
if (n <= 0) {
finalize(peers_info, -1, "Connection closed.\nLeaving the game.\n");
}
......@@ -195,7 +228,7 @@ void handle_rec(game_peers_info* peers_info)
send_unknown_packet(peers_info);
return;
}
meta_data = (struct msg_meta_t*)buf;
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) {
......@@ -213,10 +246,10 @@ void handle_rec(game_peers_info* peers_info)
peers_info->timer.hb_timeout_ms = TIMEOUT_HB_MIN_MS;
break;
case MSG_MOVE:
handle_foreign_move(peers_info, (msg_move*)buf);
handle_foreign_move(peers_info, (msg_move*)peers_info->message.buf);
break;
case MSG_MOVE_ACK:
handle_msg_ack(peers_info, (msg_move_ack*)buf);
handle_msg_ack(peers_info, (msg_move_ack*)peers_info->message.buf);
break;
default:
send_unknown_packet(peers_info);
......@@ -262,11 +295,11 @@ void handle_stdin(game_peers_info* peers_info)
int column;
ssize_t len;
len = Read(STDIN_FILENO, (void*)buf, BUFFER_SIZE);
len = Read(STDIN_FILENO, (void*)peers_info->message.buf, BUFFER_SIZE);
if (len == 0)
return;
//special commands
switch (*buf) {
switch (*peers_info->message.buf) {
case 'p':
print_board();
return;
......@@ -277,7 +310,7 @@ void handle_stdin(game_peers_info* peers_info)
if (peers_info->state.current_state != STATE_WAIT_USER_INPUT)
return;
column = strtol(buf, NULL, 10) - 1;
column = strtol(peers_info->message.buf, NULL, 10) - 1;
if (make_own_move(peers_info, column) != 0)
return;
......
......@@ -39,7 +39,7 @@ void finalize_client(client_info* a_client)
delete_timer(a_client->hb_timer);
deregister_fd_callback(a_client->client_fd);
Shutdown(a_client->client_fd, SHUT_WR);
while (Recv(a_client->client_fd, (void*)a_client->buf, (size_t)BUFFER_SIZE, 0) != 0) {
while (Recv(a_client->client_fd, (void*)a_client->message.buf, (size_t)BUFFER_SIZE, 0) != 0) {
}
Close(a_client->client_fd);
client_list_remove(&a_client->server_info->client_queue, a_client);
......@@ -85,11 +85,11 @@ ssize_t packet_sequenzer(client_info* a_client, ssize_t n)
ssize_t len;
uint16_t msg_len;
struct msg_meta_t* a_msg;
len = n + a_client->buf_offset;
len = n + a_client->message.buf_offset;
if (len < sizeof(struct msg_meta_t))
return len;
a_msg = (struct msg_meta_t*)a_client->buf;
a_msg = (struct msg_meta_t*)a_client->message.buf;
a_msg->length = ntohs(a_msg->length);
a_msg->type = ntohs(a_msg->type);
......@@ -98,7 +98,7 @@ ssize_t packet_sequenzer(client_info* a_client, ssize_t n)
return len;
handle_client_message(a_client, a_msg);
memmove(a_client->buf, a_client->buf + msg_len, len - msg_len);
memmove(a_client->message.buf, a_client->message.buf + msg_len, len - msg_len);
return packet_sequenzer(a_client, n - msg_len);
}
......@@ -107,10 +107,10 @@ void handle_client_rec(client_info* a_client)
{
printf("handle_client_rec\n");
ssize_t n;
if ((n = Recv(a_client->client_fd, (void*)(a_client->buf + a_client->buf_offset), BUFFER_SIZE, 0)) == 0)
if ((n = Recv(a_client->client_fd, (void*)(a_client->message.buf + a_client->message.buf_offset), BUFFER_SIZE - a_client->message.buf_offset, 0)) == 0)
finalize_client(a_client);
printf("Received %zd bytes.\n", n);
a_client->buf_offset = packet_sequenzer(a_client, n);
a_client->message.buf_offset = packet_sequenzer(a_client, n);
}
client_info* initialize_client(game_server_info* server_info, int a_client_fd)
{
......
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