summaryrefslogtreecommitdiff
path: root/src/net/comm.c
blob: 7f9c6c4129bd67c11ea3a44e7f72b2e0f98dfac3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <stdio.h>
#include <assert.h>
#include "comm.h"
#include "client.h"
#include "server.h"

bool net_recv(const int sock, const msg_type_t type)
{
	msg_t m;
	bool result;
	ssize_t len = recv(sock, &m.hdr, sizeof(msg_header_t), MSG_PEEK); // just peek into packet to determine message header

	assert(len != -1);

	if(m.hdr.type != type)
	{
		printf("net_recv: received message type %d instead of %d", m.hdr.type, type);
		return false;
	}

	m.payload = malloc(m.hdr.payload_length);	// Allocate space for message payload
	recv(sock, &m.hdr, sizeof(msg_header_t), 0);	// Remove message header from socket
	recv(sock, m.payload, m.hdr.payload_length, 0);// And then receive the payload

	printf("net_recv: received msg type %d with payload length %d\n", m.hdr.type, m.hdr.payload_length);

	switch(type)
	{
		case msg_type_hello:
			result = server_parse_hello(&m);
			break;
		case msg_type_start_game:
			result = client_parse_player_list(&m);
			break;
		case msg_type_deal_hand:
			result = client_parse_deal_hand(&m);
			break;
		case msg_type_initial_stacks:
			result = client_parse_initial_stacks(&m);
			break;
		case msg_type_selected_card:
			result = server_parse_selected_card(&m);
			break;
		case msg_type_selected_stack_c:
			result = server_parse_selected_stack(&m);
			break;
		case msg_type_selected_stack_s:
			result = client_parse_selected_stack(&m);
			break;
		default:
			printf("net_recv: Unknown message type %d received!\n", type);
			exit(EXIT_FAILURE);
			break;
	}

	free(m.payload);

	return result;
}

bool net_send(const int sock, const msg_type_t type, const void *data)
{
	bool result = true;

	msg_t m;
	m.payload = malloc(NET_MSG_MAX_PAYLOAD_LENGTH);

	switch(type)
	{
		case msg_type_hello:
			client_prep_hello(&m);
			break;
		case msg_type_selected_card:
			client_prep_selected_card(&m);
			break;
		case msg_type_selected_stack_c:
			client_prep_selected_stack(&m);
			break;
		case msg_type_start_game:
			server_prep_start_game(&m);
			break;
		case msg_type_selected_stack_s:
			server_prep_selected_stack(&m);
			break;
		case msg_type_deal_hand:
			server_prep_deal_hand(&m, data);
			break;
		case msg_type_initial_stacks:
			server_prep_initial_stacks(&m);
			break;
		default:
			printf("net_send: Unknown message type %d given\n", type);
			exit(EXIT_FAILURE);
			break;
	}

	printf("net_send: sending msg type %d with payload length %d\n", m.hdr.type, m.hdr.payload_length);
	send(sock, &m, sizeof(msg_header_t), 0);	// Send message header first
	send(sock, m.payload, m.hdr.payload_length, 0);	// Then send payload

	free(m.payload);

	return result;
}