-
Notifications
You must be signed in to change notification settings - Fork 0
/
connection.h
341 lines (336 loc) · 15.2 KB
/
connection.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
//
// Created by dillu24 on 14/04/2017.
//
/**
* Connection header file along with all the necessary structures
*/
#ifndef SYSTEMS_CONNECTION_H //used against multiple definition
#define SYSTEMS_CONNECTION_H
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <strings.h>
#include <stdlib.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include "game.h"
/**
* Server connection struct i.e all the necessary TCP/IP connection details and variables that the server requires ,
* this struct was created in order to not let many variables running around in the server
*/
typedef struct server_connection{
int sockfd,newsockfd,portno,clilen;
struct sockaddr_in serv_addr,cli_addr;
}server_connection;
/**
* The server connection struct i.e. alll the necessary TCP/IP connection details and variables that the client requires,
* this struct was created in order to not let many variables running around in the client
*/
typedef struct client_connection{
int sockfd,portno;
struct sockaddr_in serv_addr;
struct hostent *server;
}client_connection;
/**
* This method creates a TCP/IP socket for the server
* @param cn
* The connection struct created in the server to be passed to this method to store the necessary values.
* @return -1 on error and 0 on success
*/
int create_server_socket(server_connection *cn);
/**
* This method binds the host address to the TCP/IP socket
* @param
* cn The connection struct created in the server to be passed to this method to store the necessary values.
* @return -1 on error and 0 on success
*/
int bind_host_address(server_connection *cn);
/**
* The method that accepts a given TCP/IP connection and sets all values to the variables as required
* @param cn
* The connection struct created in the server to be passed to this method to store the necessary values.
* @return -1 on error and 0 on success
*/
int accept_connection_from_client(server_connection *cn);
/**
* The method that closes a TCP/IP connection from the server end , note that this closure only effects one connection
* from the socket because as it could be seen from the server.c file an array of newsockfd's will be created to
* represent different connections , and hence the only connection that will be closed here is the most recent connection
* , the other connections should be closed in the server.c file.
* @param cn
* The connection struct created in the server to be passed to this method to store the necessary values
* @return -1 on error and 0 on success
*/
int close_connection_from_server (server_connection *cn);
/**
* The method that creates the client TCP/IP socket point
* @param cn
* THe connection struct created in the client to be passed to this method to store the necessary values
* @return -1 on error 0 on success
*/
int client_create_socket_point(client_connection *cn);
/**
* The method that connects to the server and hence create a TCP/IP connection
* @param cn
* The connection struct created in the client to be passed to this method to store the necessary values
* @return -1 on error 0 on success
*/
int connect_to_server(client_connection *cn);
/**
* The method that closes the client's end of the TCP/IP connection
* @param cn
* The connection struct created in the client to be passed to this method to store the necessary values
* @return
* -1 on error 0 on success
*/
int close_connection_from_client(client_connection *cn);
/**
* This method is used to convert an integer into a series of bytes in order to be sent over tcp/ip connection , this
* was done because structs cannot be sent as a whole over connections hence they must be serialized at the sender
* and deserialized at the receiver
* @param buffer
* The placeholder where the bytes will be placed
* @param value
* The integer value that is going to be converted
* @return
* The next free slot in the buffer in order to send more data.
*/
unsigned char * serialize_int(unsigned char *buffer, int value);
/**
* This method is used to convert a series of bytes into an integer so that the sent data in bytes over the TCP/IP connection
* could be converted back to the original integer value , this was done because structs cannot be sent as a whole over
* connections and hence they must be deserialized and serialized at the receiver and server respectively
* @param buffer
* THe placeholder where the received bytes are placed
* @param value
* THe integer variable where the new converted value will be placed
* @return
* The next slot in the buffer that was not deserialized
*/
unsigned char * deserialize_int(unsigned char *buffer, int *value);
/**
* This method is used to convert a sending_client_info struct into a series of bytes in order to be sent over tcp/ip
* connection, this was done because structs cannot be sent as a whole over connections and hence they must be serialized
* at the sender and deserialized at the receiver
* @param buffer
* THe placeholder where the bytes will be placed
* @param value
* The struct value that will be converted;
* @return
* THe next free slot in the buffer in order to send more data
*/
unsigned char * serialize_sending_connection(unsigned char *buffer, struct sending_client_info *value);
/**
* This method is used to convert a series of bytes into the original sent sending_client_info struct over TCP/IP
* connection , this was done because structs cannot be sent as a whole over connections and hence they must be
* deserialized and serialized at the receiver and server respectively
* @param buffer
* The placeholder where the received bytes are placed
* @param value
* The sending_client_info struct where the new converted struct will be placed
* @return
* The next slot in the buffer that was not deserialized
*/
unsigned char * deserialize_sending_connection(unsigned char *buffer, struct sending_client_info *value);
/**
* This method is used to convert a character value into a series of bytes in order to be sent over tcp/ip
* connection, this was done because structs cannot be sent as a whole over connections and hence they must be serialized
* at the sender and deserialized at the receiver
* @param buffer
* THe placeholder where the bytes will be placed
* @param value The character value that will be converted
* @return
* The next free slot in the buffer in order to send more data
*/
unsigned char * serialize_char(unsigned char *buffer, char value);
/**
* This method is used to convert a series of bytes into the original sent character data over TCP/IP connection , this
* was done because structs cannot be sent as a whole over connections and hence they must be deserialized and serialized
* at the receiver and server respectively
* @param buffer
* The placeholder where the received bytes are placed
* @param value
* The character variable where the new converted character will be placed
* @return
* The next slot in the buffer that was not deserialized
*/
unsigned char * deserialize_char(unsigned char *buffer, char *value);
/**
* This method is used to convert a short value into a series of bytes in order to be sent over tcp/ip
* connection, this was done because structs cannot be sent as a whole over connections and hence they must be serialized
* at the sender and deserialized at the receiver
* @param buffer
* The placeholder where the bytes will be placed
* @param value
* The short value that will be converted
* @return
* The next free slot in the buffer in order to send more data
*/
unsigned char * serialize_short(unsigned char *buffer, short value);
/**
* This method is used to convert a struct element value into a series of bytes in order to be sent over tcp/ip
* connection, this was done because structs cannot be sent as a whole over connections and hence they must be serialized
* at the sender and deserialized at the receiver
* @param buffer
* The placeholder where the bytes will be placed
* @param value
* The struct that will be converted
* @return
* The next free slot in the buffer in order to send more data
*/
unsigned char* serialize_element(unsigned char *buffer,element *value);
/**
* This method is used to convert a struct snakeobject value into a series of bytes in order to be sent over tcp/ip
* connection, this was done because structs cannot be sent as a whole over connections and hence they must be serialized
* at the sender and deserialized at the receiver
* @param buffer
* The placeholder where the bytes will be placed
* @param snake
* The struct that will be converted
* @return
* The next free slot in the buffer in order to send more data
*/
unsigned char* serialize_snakeobject(unsigned char*buffer,snakeobject *snake);
/**
* This method is used to convert a series of bytes into the original sent snakeobject struct over TCP/IP connection , this
* was done because structs cannot be sent as a whole over connections and hence they must be deserialized and serialized
* at the receiver and server respectively
* @param buffer
* The placeholder where the received bytes are placed
* @param snake
* The snakeobject variable where the new converted snakeobject will be placed
* @return
* The next slot in the buffer that is still not deserialized
*/
unsigned char* deserialize_snakeobject(unsigned char* buffer,snakeobject *snake);
/**
* This method is used to convert a struct element array into a series of bytes in order to be sent over tcp/ip
* connection, this was done because structs cannot be sent as a whole over connections and hence they must be serialized
* at the sender and deserialized at the receiver
* @param buffer
* The placeholder where the bytes will be placed
* @param array
* The array that will be converted
* @param size
* The size of the array that will be converted
* @return The next free slot in the buffer in order to send more data
*/
unsigned char* serialize_element_array(unsigned char* buffer, element *array,int size);
/**
* This method is used to convert a series of bytes into the original sent struct element array over TCP/IP connection , this
* was done because structs cannot be sent as a whole over connections and hence they must be deserialized and serialized
* at the receiver and server respectively
* @param buffer
* The placeholder where the received bytes are placed
* @param array
* The element array where the new converted element array will be placed
* @param size
* The size of the converted element array
* @return
* The next slot in the buffer that is still not deserialized
*/
unsigned char *deserialize_element_array(unsigned char* buffer, element *array,int size);
/**
* This method is used to convert a series of bytes into the original sent short value over TCP/IP connection , this
* was done because structs cannot be sent as a whole over connections and hence they must be deserialized and serialized
* at the receiver and server respectively
* @param buffer
* The placeholder where the received bytes are placed
* @param value
* The short variable where the new converted element will be placed
* @return
* The next slot in the buffer that is still not deserialized
*/
unsigned char * deserialize_short(unsigned char *buffer, short *value);
/**
* This method is used to convert a series of bytes into the original sent struct element value over TCP/IP connection , this
* was done because structs cannot be sent as a whole over connections and hence they must be deserialized and serialized
* at the receiver and server respectively
* @param buffer
* The placeholder where the received bytes are placed
* @param value
* The element value where the new converted element will be placed
* @return
* The next slot in the buffer that is still not deserialized
*/
unsigned char* deserialize_element(unsigned char *buffer,element *value);
/**
* * This method is used to convert a struct player into a series of bytes in order to be sent over tcp/ip
* connection, this was done because structs cannot be sent as a whole over connections and hence they must be serialized
* at the sender and deserialized at the receiver
* @param buffer
* The placeholder where bytes will be placed
* @param value
* The struct player value that will be converted
* @return
* The next next free slot in the buffer in order to send more data
*/
unsigned char* serialize_player(unsigned char*buffer,player *value);
/**
* This method is used to convert a series of bytes into the original sent struct player value over TCP/IP connection , this
* was done because structs cannot be sent as a whole over connections and hence they must be deserialized and serialized
* at the receiver and server respectively
* @param buffer
* THe placeholder where the received bytes are placed
* @param value
* The player value where the new converted element will be placed
* @return
* The next slot in the buffer that is still not deserialized
*/
unsigned char* deserialize_player(unsigned char*buffer,player *value);
/**
* This method is used to convert a struct player array into a series of bytes in order to be sent over tcp/ip
* connection, this was done because structs cannot be sent as a whole over connections and hence they must be serialized
* at the sender and deserialized at the receiver
* @param buffer
* THe placeholder where bytes will be placed
* @param value
* The struct player array that will be converted
* @param size
* The size of the player array
* @return
* The next free slot in the buffer in order to send more data
*/
unsigned char* serialize_player_array(unsigned char* buffer, player *value,int size);
/**
* This method is used to convert a series of bytes into the original sent struct player array value over TCP/IP connection , this
* was done because structs cannot be sent as a whole over connections and hence they must be deserialized and serialized
* at the receiver and server respectively
* @param buffer
* The placeholder where the received bytes are placed
* @param value
* The player array value where the new converted element will be placed
* @param size
* The size of the converted array
* @return
* The next slot in the buffer that is still not deserialized
*/
unsigned char* deserialize_player_array(unsigned char* buffer,player *value,int size);
/**
* This method is used to convert a game struct into a series of bytes in order to be sent over tcp/ip
* connection, this was done because structs cannot be sent as a whole over connections and hence they must be serialized
* at the sender and deserialized at the receiver
* @param buffer
* The placeholder where bytes will be placed
* @param game
* The game struct that will be converted
* @return
* The next free slot in the buffer in order to send more data
*/
unsigned char* serialize_game(unsigned char*buffer,game *game);
/**
* This method is used to convert a series of bytes into the original sent game struct value over TCP/IP connection , this
* was done because structs cannot be sent as a whole over connections and hence they must be deserialized and serialized
* at the receiver and server respectively
* @param buffer
* The placeholder where the received bytes are placed
* @param game
* THe game struct variable where the new converted element will be placed
* @return
* The next slot in the buffer that is still not deserialized
*/
unsigned char* deserialize_game(unsigned char*buffer,game *game);
#endif //SYSTEMS_CONNECTION_H