summaryrefslogtreecommitdiff
path: root/src/net/client.c
blob: 17689265ff67a587b4af01001266ac317e48debb (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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include "client.h"
#include "../data_store.h"
#include "../player.h"
#include "../game.h"

/**
 * Client side function; connects to specified host:port
 * @param[in] host Hostname of server
 * @param[in] port Port of server
 * @return Socket with open connection to server
 */
int client_connect_server(const char* host, const char* port)
{
	assert(host != NULL);
	assert(port != NULL);

	int status;
	int sock;
	struct addrinfo hints, *result, *tmp;

	memset(&hints, 0, sizeof(hints));
	hints.ai_family = AF_UNSPEC;		// IPv4 or IPv6
	hints.ai_socktype = SOCK_STREAM;	// TCP socket

	status = getaddrinfo(host, port, &hints, &result);
	if(status != 0)
	{
		printf("getaddrinfo: %s\n", gai_strerror(status));
		exit(EXIT_FAILURE);
	}

	// connect to first result in linked addrinfo list
	for(tmp = result; tmp != NULL; tmp = tmp->ai_next)
	{
		// create socket
		sock = socket(tmp->ai_family, tmp->ai_socktype, tmp->ai_protocol);
		if(sock == -1)
			continue;

		// connect!
		if(connect(sock, tmp->ai_addr, tmp->ai_addrlen) != -1)
			break;	// Success!

		close(sock);
	}

	if(tmp == NULL)
	{
		printf("failed to connect\n");
		exit(EXIT_FAILURE);
	}
	freeaddrinfo(result);

	return sock;
}

bool client_parse_player_list(const msg_t *m)
{
	assert(m != NULL);

	data_store_t *ds = data_store();
	uint32_t pos = 0;

	ds->player_list.count = m->payload[pos++];

	// read usernames from buffer
	for(int i=0; i<ds->player_list.count; i++)
	{
		uint8_t namelen;
		ds->player_list.players[i].player_id = m->payload[pos++];
		namelen = m->payload[pos++];
		assert(namelen <= MAX_PLAYER_NAME_LENGTH);
		strncpy(ds->player_list.players[i].player_name, (const char*) m->payload+pos, namelen);
		ds->player_list.players[i].player_name[namelen] = '\0';
		pos += namelen;
	}

	return true;
}

bool client_parse_deal_hand(const msg_t *m)
{
	assert(m != NULL);
	assert(m->hdr.payload_length == MAX_HAND_CARDS); // deal_cards packet have fixed size

	data_store_t *ds = data_store();

	for(int i=0; i<MAX_HAND_CARDS; i++)
		ds->hand.cards[i] = m->payload[i];

	return true;
}

bool client_parse_selected_stack(const msg_t *m)
{
	assert(m != NULL);
	assert(m->hdr.payload_length == 1);

	data_store_t *ds = data_store();
	ds->stack_index = m->payload[0];

	assert(ds->stack_index <= NUM_TABLE_STACKS);

	return true;
}

bool client_parse_initial_stacks(const msg_t *m)
{
	assert(m != NULL);
	assert(m->hdr.payload_length == NUM_TABLE_STACKS);

	data_store_t *ds = data_store();
	for(int i=0; i<NUM_TABLE_STACKS; i++)
		ds->table_stacks.stacks[i].cards[0] = m->payload[i];

	return true;
}

bool client_parse_selected_card_all(const msg_t *m)
{
	assert(m != NULL);
	assert(m->hdr.payload_length % 2 == 0); // payload: n times id+card

	data_store_t *ds = data_store();
	for(int i=0; i<m->hdr.payload_length; i+=2)
	{
		player_id_t pid = m->payload[i];
		player_list_entry_t *ple = get_player_list_entry_by_player_id(&ds->player_list, pid);
		ple->open_card = m->payload[i+1];
	}

	return true;
}

bool client_parse_hello(const msg_t *m)
{
	assert(m != NULL);
	assert(m->hdr.payload_length == 1);

	data_store_t *ds = data_store();
	ds->own_player_id = m->payload[0];

	return true;
}

void client_prep_hello(msg_t *m)
{
	data_store_t *ds = data_store();
	uint8_t namelen = strlen(ds->nickname);
	m->hdr.type = msg_type_hello_c;
	memcpy(m->payload, ds->nickname, namelen);
	m->hdr.payload_length = namelen;
}

void client_prep_selected_card(msg_t *m)
{
	data_store_t *ds = data_store();
	card c = ds->selected_card;
	assert(c >= MIN_CARD && c <= MAX_CARD);

	m->hdr.type = msg_type_selected_card;
	m->payload[0] = c;
	m->hdr.payload_length = 1;
}

void client_prep_selected_stack(msg_t *m)
{
	data_store_t *ds = data_store();
	assert(ds->stack_index <= NUM_TABLE_STACKS);

	m->hdr.type = msg_type_selected_stack_c;
	m->payload[0] = ds->stack_index;
	m->hdr.payload_length = 1;
}