Commit 283493ff authored by Simon Wüllhorst's avatar Simon Wüllhorst

Several changes:

- Improved stream len handling in packet_sequenzer.
- Use enum rather than a huge amount of defines.
- Fixed peer_info lenght calculation.
- Fixed cblib (workaround) for unregister and register the same fliedescriptor (different sockets close, socket) id.
- ...
parent f7e6b190
......@@ -480,6 +480,7 @@ static void handle_event(void)
fprintf(stderr, "Handling fd event for fd = %d.\n", cb[i].fd);
#endif
(*(cb[i].callback))(cb[i].arg);
return;
}
}
}
......
......@@ -10,21 +10,21 @@
#define BUFFER_SIZE (1 << 16)
#define BACKLOG 1000
#define MODE_SERVER 1
#define MODE_DIRECT 2
enum { MODE_SERVER,
MODE_DIRECT };
/*
* Message TYPES
*/
#define MSG_REG 0
#define MSG_REG_ACK 1
#define MSG_REG_NACK 2
#define MSG_PEER_INFO 3
#define MSG_PEER_INFO_ACK 9
#define MSG_MOVE 4
#define MSG_MOVE_ACK 5
#define MSG_HEARTBEAT 6
#define MSG_HEARTBEAT_ACK 7
#define MSG_UNKNOWN_MSG_TYPE 8
enum { MSG_REG = 0,
MSG_REG_ACK,
MSG_REG_NACK,
MSG_PEER_INFO,
MSG_MOVE,
MSG_MOVE_ACK,
MSG_HEARTBEAT,
MSG_HEARTBEAT_ACK,
MSG_UNKNOWN_MSG_TYPE,
MSG_PEER_INFO_ACK };
#define MSG_MAX_TYPE_ID 8
......@@ -34,21 +34,21 @@
#define LOCAL_PLAYER -1
#define REMOTE_PLAYER 1
#define STATE_INIT 0
#define STATE_WAIT_FOREIGN_MOVE 1
#define STATE_WAIT_ACK 2
#define STATE_WAIT_USER_INPUT 3
#define STATE_FINALIZE 4
enum { STATE_INIT,
STATE_WAIT_FOREIGN_MOVE,
STATE_WAIT_ACK,
STATE_WAIT_USER_INPUT,
STATE_FINALIZE };
#define CLIENT_STATE_INIT 0
#define CLIENT_STATE_WAIT_FOR_PEER 1
#define CLIENT_STATE_WAIT_PEER_INFO_ACK 2
enum { CLIENT_STATE_INIT,
CLIENT_STATE_WAIT_FOR_PEER,
CLIENT_STATE_WAIT_PEER_INFO_ACK };
#define FIRST_SEQ_NUMBER 0
#define TIMEOUT_ACK_MIN_MS 200
#define TIMEOUT_ACK_MAX_MS 20000
#define TIMEOUT_HB_MIN_MS 2000
#define TIMEOUT_HB_MAX_MS 20000
#define TIMEOUT_ACK_MIN_MS 20000
#define TIMEOUT_ACK_MAX_MS 200000
#define TIMEOUT_HB_MIN_MS 20000
#define TIMEOUT_HB_MAX_MS 200000
#define DEBUG
/*
......
......@@ -12,7 +12,6 @@ int parse_remote_client(char** argv, game_peers_info* peers_info)
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_INET;
//hints.ai_family = AF_INET6;
//hints.ai_socktype = SOCK_STREAM;
hints.ai_socktype = SOCK_DGRAM;
//hints.ai_flags = AI_V4MAPPED;
Getaddrinfo(argv[3], argv[4], &hints, &peers_info->remote);
......@@ -200,8 +199,8 @@ void finalize_game_server(game_peers_info* peers_info)
while (Recv(peers_info->net_fd, (void*)buf, (size_t)BUFFER_SIZE, 0) != 0) {
}
peers_info->state.current_mode = MODE_DIRECT;
//Close(peers_info->net_fd);
peers_info->server_fd = peers_info->net_fd;
Close(peers_info->net_fd);
//peers_info->server_fd = peers_info->net_fd;
peers_info->net_fd = -1;
}
void handle_msg_peer_info(game_peers_info* peers_info, struct msg_meta_t* meta_data)
......@@ -245,12 +244,13 @@ void handle_msg_peer_info(game_peers_info* peers_info, struct msg_meta_t* meta_d
} else {
peers_info->start_flag = LOCAL_START;
}
free(a_name);
}
void handle_rec_msg(game_peers_info* peers_info, struct msg_meta_t* meta_data)
{
//printf("GOT PACKET: LENGHT: %d, TYPE: %d\n", meta_data->length, meta_data->type);
printf("GOT PACKET: LENGHT: %d, TYPE: %d\n", meta_data->length, meta_data->type);
switch (meta_data->type) {
case MSG_HEARTBEAT:
......@@ -323,22 +323,20 @@ void handle_rec_udp(game_peers_info* peers_info)
}
/* for tcp */
ssize_t packet_sequenzer(game_peers_info* peers_info, ssize_t n)
ssize_t packet_sequenzer(game_peers_info* peers_info, ssize_t stream_len)
{
ssize_t len;
uint16_t msg_len;
struct msg_meta_t* a_msg;
len = n + peers_info->message.buf_offset;
if (len < (ssize_t)sizeof(struct msg_meta_t))
return len;
if (stream_len < (ssize_t)sizeof(struct msg_meta_t))
return stream_len;
a_msg = (struct msg_meta_t*)peers_info->message.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;
if (stream_len < msg_len)
return stream_len;
handle_rec_msg(peers_info, a_msg);
......@@ -346,12 +344,11 @@ ssize_t packet_sequenzer(game_peers_info* peers_info, ssize_t n)
if (peers_info->net_fd == -1) {
//tcp session has been finalized
init_4gw(peers_info);
Close(peers_info->server_fd);
//Close(peers_info->server_fd);
return 0;
}
memmove(peers_info->message.buf, peers_info->message.buf + msg_len, len - msg_len);
return packet_sequenzer(peers_info, n - msg_len);
memmove(peers_info->message.buf, peers_info->message.buf + msg_len, stream_len - msg_len);
return packet_sequenzer(peers_info, stream_len - msg_len);
}
void handle_rec_tcp(game_peers_info* peers_info)
{
......@@ -362,7 +359,7 @@ void handle_rec_tcp(game_peers_info* peers_info)
return;
}
//printf("Received %zd bytes, buf offset: %zd \n", n, peers_info->message.buf_offset);
peers_info->message.buf_offset = packet_sequenzer(peers_info, n);
peers_info->message.buf_offset = packet_sequenzer(peers_info, n + peers_info->message.buf_offset);
}
int make_own_move(game_peers_info* peers_info, int column)
{
......@@ -410,7 +407,6 @@ void handle_stdin(game_peers_info* peers_info)
ssize_t len;
len = Read(STDIN_FILENO, (void*)inbuf, BUFFER_SIZE);
printf("len: %zd ", len);
if (len <= 1)
return;
//special commands
......@@ -488,10 +484,8 @@ int init_gs_4gw(game_peers_info* peers_info)
}
int init_4gw(game_peers_info* peers_info)
{
//int option;
peers_info->net_fd = Socket(peers_info->local->ai_family, peers_info->local->ai_socktype, 0);
//option = 0;
//Setsockopt(peers_info->net_fd, IPPROTO_IPV6, IPV6_V6ONLY, &option, sizeof(option));
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);
......
......@@ -79,6 +79,7 @@ void handle_client_hb_timer(client_info* a_client)
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) {
printf("Lost peer connection (HBs were not ACKed for a long time).\n");
finalize_client(a_client);
return;
}
......@@ -97,7 +98,7 @@ void send_peer_info(client_info* a_client, client_info* a_clients_peer, uint16_t
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->length = htons(s - name_padding - sizeof(struct msg_meta_t));
// Note: net_addr and net_port are still in network byte order
a_peer_info->net_addr = a_clients_peer->net_addr;
a_peer_info->net_port = a_clients_peer->net_port;
......@@ -107,6 +108,7 @@ void send_peer_info(client_info* a_client, client_info* a_clients_peer, uint16_t
memcpy(a_name, a_clients_peer->credentials.name, a_clients_peer->credentials.name_len);
Send(a_client->client_fd, a_msg, s, 0);
free(a_msg);
//TODO: Set ACK-Timer
a_client->client_state = CLIENT_STATE_WAIT_PEER_INFO_ACK;
}
......@@ -179,6 +181,7 @@ void handle_client_message(client_info* a_client, struct msg_meta_t* a_msg)
finalize_client(a_client);
break;
case MSG_HEARTBEAT_ACK:
printf("GOT MSG_HEARTBEAT_ACK\n");
a_client->hb_timeout_ms = TIMEOUT_HB_MIN_MS;
break;
case MSG_HEARTBEAT:
......@@ -194,27 +197,28 @@ void handle_client_message(client_info* a_client, struct msg_meta_t* a_msg)
break;
}
}
ssize_t packet_sequenzer(client_info* a_client, ssize_t n)
ssize_t packet_sequenzer(client_info* a_client, ssize_t stream_len)
{
ssize_t len;
uint16_t msg_len;
struct msg_meta_t* a_msg;
len = n + a_client->message.buf_offset;
if (len < (ssize_t)sizeof(struct msg_meta_t))
return len;
if (stream_len < (ssize_t)sizeof(struct msg_meta_t))
return stream_len;
a_msg = (struct msg_meta_t*)a_client->message.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 (a_msg->length < a_client->server_info->msg_min_size[a_msg->type] || len != msg_len)
return len;
if (stream_len < msg_len)
return stream_len;
if (msg_len < a_client->server_info->msg_min_size[a_msg->type]) {
printf("Malformed packet, finalize connection.\n");
finalize_client(a_client);
}
handle_client_message(a_client, a_msg);
memmove(a_client->message.buf, a_client->message.buf + msg_len, len - msg_len);
memmove(a_client->message.buf, a_client->message.buf + msg_len, stream_len - msg_len);
return packet_sequenzer(a_client, n - msg_len);
return packet_sequenzer(a_client, stream_len - msg_len);
}
void handle_client_rec(client_info* a_client)
......@@ -225,7 +229,7 @@ void handle_client_rec(client_info* a_client)
return;
}
//printf("Received %zd bytes, buf offset: %zd \n", n, a_client->message.buf_offset);
a_client->message.buf_offset = packet_sequenzer(a_client, n);
a_client->message.buf_offset = packet_sequenzer(a_client, n + a_client->message.buf_offset);
}
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