-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnet_prisoner_common.h
218 lines (176 loc) · 4.19 KB
/
net_prisoner_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
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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
/**
* @file net_prisoner_common.h
* @author Thomas Violent & Wolodia Zdetovetzky
* @brief
* @version 0.1
* @date 2021-11-26
*
* @copyright Copyright (c) 2021
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdarg.h>
#include <stdbool.h>
#include <pthread.h>
#include <sys/socket.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <semaphore.h>
#ifndef NET_PRISONER_COMMON_H
#define NET_PRISONER_COMMON_H
// ----------------------------------------------
// Settings
// ----------------------------------------------
/**
* @brief Enable logging for the net lib
* Set this to true to allow the lib to log messages to STDOUT
* It may be output usefull informations for debuging
*/
#define NETDEBUG true
/**
* @brief Used to protect lib user function from multiple executions by different thread
* if false, multithreading protection should be implemented by lib user to prevent
* concurency issues
*/
#define THREAD_SAFETY true
/**
* @brief max size of the buffer
*/
#define BUFFERSIZE 2048
/**
* @brief Max openned connections for the server
*/
#define MAXSIMULTANEOUSCLIENTS 100
/**
* @brief max round possible
*/
#define MAXROUND 10
// ----------------------------------------------
// Common
// ----------------------------------------------
/**
* @brief Internal.
* Allow the lib to log message (only if NETDEBUG == true)
*
* @param format même fonctionnement que printf
* @param ...
*/
void _net_common_dbg(const char *format, ...);
/**
* @brief private function
* Should init common variable used in client and server
*/
void _net_common_init();
/**
* @brief define all the messages type exchanged between the client and the server
*/
enum _net_common_msg_type {
/**
* @brief the client betray the other
*/
NET_ACTION_BETRAY = 0,
/**
* @brief the client collab with the other
*/
NET_ACTION_COLLAB = 1,
/**
* @brief the client quit the game
*/
NET_ACTION_QUIT = 2,
/**
* @brief the client need to display the waiting screen before the game start
*/
NET_SCREEN_WAITING = 4,
/**
* @brief the client need to make a choice
*/
NET_SCREEN_CHOICE = 5,
/**
* @brief the client need to display the round result
*/
NET_SCREEN_SCORE_ROUND = 6,
/**
* @brief the client sent his id just after
* initialising the connection with the server
*/
NET_INIT_CLIENT_ID = 7,
/**
* @brief the client need to display the final result
*/
NET_SCREEN_SCORE_FINAL = 8,
/**
* @brief The client may send this to client to indicate that he (the client) is
* ready to make a choice
*/
NET_ACTION_READY = 9
};
/**
* @brief player's actions
*/
enum _net_common_client_action {
/**
* @brief the player collab
*/
NET_COLLABORATE = 0,
/**
* @brief the player betray the other
*/
NET_BETRAY = 1
};
/**
* @brief player's round score
*/
typedef struct {
/**
* @brief player score
*/
int player_score;
/**
* @brief true if the player win
*/
bool round_has_win;
/**
* @brief actual round
*/
int round_actual;
/**
* @brief game total round
*/
int round_total;
} net_common_round_score;
typedef struct {
/**
* @brief the tab that contains each score of the game
* his size is set with the defined MAXROUND, for 2 players
*/
int result[MAXROUND][2];
} net_common_final_score;
/**
* @brief define the structure used to exchanged between the client and the server
*/
typedef struct {
/**
* @brief type of the message
*/
enum _net_common_msg_type msg_type;
/**
* @brief The delay the client took to make a choice
*/
ulong delay;
/**
* @brief the score at the end of each round
*/
net_common_round_score round_score;
/**
* @brief the final score at the end of the game
*/
net_common_final_score final_score;
/**
* @brief unique client id defined
* int the client config file
*/
int client_id;
} _net_common_netpacket;
#endif /* NET_PRISONER_COMMON_H */