-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommon.h
executable file
·153 lines (115 loc) · 3.77 KB
/
common.h
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
#ifndef _COMMON_H
#define _COMMON_H
#include <sys/types.h>
#include <stdint.h>
#include <stdarg.h>
/* ===[ Constants ]========================================================== */
#define MIN_USERNAME_LENGTH 3
#define MAX_USERNAME_LENGTH 30
#define USERNAME_ALPHABET \
"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_.-"
#define DEFAULT_TIMEOUT_INIT {60, 0} /* 60 seconds */
#define HELLO_TIMEOUT (10 * 1000) /* 10 seconds */
#define PLAY_RESPONSE_TIMEOUT (60 * 1000) /* 60 seconds */
#define BUFFER_SIZE 1024
/* ===[ Magic constants ]==================================================== */
#define REQ_LOGIN 0x07
#define REQ_WHO 0x62
#define REQ_END 0x99
#define REQ_PLAY 0x26
#define RESP_OK_LOGIN 0x94
#define RESP_EXIST 0x49
#define RESP_BADUSR 0x24
#define RESP_WHO 0x36
#define RESP_BUSY 0x41
#define RESP_NONEXIST 0x83
#define RESP_REFUSE 0x47
#define RESP_OK_PLAY 0x16
#define REQ_HELLO 0x44
#define REQ_HIT 0x66
#define RESP_OK_FREE 0x72
#define RESP_BADREQ 0x54
/* ===[ Data types ]========================================================= */
/**
* Describes a client state by the viewpoint of the server.
*/
enum client_state {
NONE, /* default value, client does not exist yet */
CONNECTED, /* client is connected, not logged in yet */
FREE, /* client is connected and logged in (username and udp port) */
BUSY, /* client has/is requested to play a match */
PLAY /* client is playing */
};
typedef unsigned char bool;
#define TRUE 1
#define FALSE 0
/* ===[ Functions ]========================================================== */
bool username_is_valid(const char *username, uint8_t length);
/**
* Translates a magic constant into its name, or its hexadecimal representation
* if not recognized.
*/
const char *magic_name(uint8_t);
/**
* Translates a client_state into its name.
*/
const char *state_name(enum client_state);
/**
* Encodes a client_state into a byte to be sent over the network.
*/
uint8_t state_encode(enum client_state);
/**
* Decodes an encoded client_state.
*/
enum client_state state_decode(uint8_t);
#define check_alloc(ptr)\
if ( ptr == NULL ) {\
log_error("Errore su malloc()");\
exit(EXIT_FAILURE);\
}
/**
* Get a line from stdin. Must compile in C89 (-ansi).
* @param char *buffer the buffer used to store the string
* @param int size the size of the buffer
* @return int the length of the line
*/
int get_line(char *buffer, int size);
/**
* Send a buffer through socket, iterating until all data is sent.
* @param int socket
* @param const char *buffer
* @param int length the number of bytes to be sent
* @return int 0 on success, < 0 on send() error
*/
int send_buffer(int socket, const char *buffer, int length);
/**
* Send a byte through socket.
* @param int socket
* @param uint8_t byte
* @return int @see send_buffer()
*/
int send_byte(int socket, uint8_t byte);
/**
* Format specifiers:
* b - 8 bit
* w - 16 bit
* l - 32 bit
* s - 0-ended string, strip the \0
* S - 0-ended string, keep the \0
* All numbers are to be provided in host-byte-order, they will be converted to
* network-byte-order.
*/
int pack(void *buffer, const char *format, ...);
/**
* Format specifiers:
* b
* w
* l
* s - fixed length string. The length must be provided as argument, before the
* char* (e.g. unpack(buff, "s", 12, &str) extracts 12 chars).
* All numbers are extracted from network-byte-order to host-byte-order.
* Strings will be 0-ended.
*/
void unpack(const void *buffer, const char *format, ...);
/* ========================================================================== */
#endif