-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclient.c
426 lines (315 loc) · 7.96 KB
/
client.c
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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
#include "client.h"
#include "server.h"
#include "print.h"
#include "commands.h"
#include "auction.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
void CreateClientList (struct clientlist **, char **);
int MaxDescriptor (struct clientlist *, fd_set *, int);
void AcceptQuery (int, struct clientlist *);
void ConnectClient (struct clientlist *, int);
void DisconnectClient (struct clientlist *);
void DeniedClient (int);
int ReadToBuffer (struct client *, int);
char * StatusUsersConnecting (struct clientlist *);
char * StatusUsersPlaying (struct clientlist *);
char * GetInfoPlayer (struct client *);
void AddClientToList (struct client *, struct clientlist *);
void InitFlags (struct userFlags *);
void InitUser (struct client *, struct clientlist *, int);
void ReadyToStartGame (struct clientlist *);
void GiveUserId (struct clientlist *);
void DeleteCurrentClientFromList (struct clientlist *);
void CheckCountOfUsers (struct clientlist *);
/* Malloc memory for list of clients, NULL ptrs for 1th and last.
* set maxPlayers, and status of start game (0 for now)
*/
void CreateClientList (struct clientlist ** clList, char ** argv)
{
(*clList) = (struct clientlist *)malloc(sizeof(struct clientlist));
(*clList)->first = (*clList)->last = (*clList)->current = NULL;
(*clList)->maxPlayers = GetNumberPlayers(argv);
(*clList)->statusStartGame = 0;
}
/* */
int MaxDescriptor (struct clientlist *clList, fd_set *readfds, int ls)
{
int max_d;
int fd;
struct client * user;
max_d = ls;
user = clList->first;
while ( user != NULL )
{
fd = user->contact->fd;
FD_SET (fd, readfds);
if (fd > max_d)
{
max_d = fd;
}
user = user->next;
}
return max_d;
}
/* */
void AcceptQuery (int ls, struct clientlist * clList)
{
int fd;
fd = accept (ls, NULL, NULL);
if ( clList->cnt < clList->maxPlayers && !clList->statusStartGame)
{
ConnectClient (clList, fd);
}
else
{
DeniedClient (fd);
}
}
/* Add client to struct, Init, print about it, check for start game.
*/
void ConnectClient (struct clientlist *clList, int fd)
{
struct client * user;
user = (struct client *) malloc (sizeof(struct client));
AddClientToList (user, clList);
InitUser (user, clList, fd);
printf ("New client connected. FD=%d. #%d.\n", fd, clList->cnt);
PrintToFDSuccessConnect (fd);
PrintToAll(clList, StatusUsersConnecting(clList));
if ( clList->cnt == clList->maxPlayers )
{
ReadyToStartGame (clList);
}
}
/* Delete current user
* close fd, print current cnt to all,
* check cnt of all of zero.
*/
void DisconnectClient (struct clientlist *clList)
{
int fd;
DeleteCurrentClientFromList (clList);
fd = clList->current->contact->fd;
shutdown(fd, 2);
close (fd);
printf ("Client disconnected. FD=%d.\n", fd);
//Must be fn FreeUser (struct clList *);
free(clList->current->buf->str);
free(clList->current->buf);
free(clList->current);
clList->cnt --;
printf ("Clients now:%d.\n", clList->cnt);
CheckCountOfUsers (clList);
}
/* Fn write to fd that access denied
*/
void DeniedClient (int fd)
{
const char strFullServer[60] = "Sorry! The server is full or game started.\n";
write ( fd, strFullServer, strlen(strFullServer) + 1);
}
/* Return:
* -1 if error read (perror)
* 0 close fd (check not here)
* >0 read from fd one symbol. write to buffer then check overflow buf
* and extend if it need.
*/
int ReadToBuffer (struct client * user, int fd)
{
int rc;
char c;
user->buf->str = user->buf->str;
rc = read (fd, &c, sizeof(char));
if ( rc == -1 )
{
perror ("read");
}
if ( user->buf->cnt == BUF_SIZE * user->buf->part - 1)
{
ExtendBuffer (user->buf);
}
if ( rc != 0 )
{
user->buf->str[user->buf->cnt++] = c;
user->buf->str[user->buf->cnt] = '\0';
}
return rc;
}
/* Fn to create string status connected clients at the moment.
* return string.
*/
char * StatusUsersConnecting (struct clientlist * clList)
{
char * strInfo;
int maxPlayers;
int curPlayers;
strInfo = (char *) malloc (MESSAGE_LENGHT * sizeof(char));
maxPlayers = clList->maxPlayers;
curPlayers = clList->cnt;
sprintf (strInfo,
"Now connected / Max Players:\n$\t%d\t\t%d\n",
curPlayers, maxPlayers);
return strInfo;
}
/* Fn create string of players still active in game.
* retur string
*/
char * StatusUsersPlaying (struct clientlist * clList)
{
char * strInfo;
int curPlayers;
strInfo = (char *) malloc (MESSAGE_LENGHT * sizeof(char));
curPlayers = clList->cnt;
sprintf (strInfo,
"Players still active:\n$\t%d\n", curPlayers);
return strInfo;
}
/* Return string with his gamestatic ("player X") of current user */
char * GetInfoPlayer (struct client * user)
{
const int STR_INFO = 5;
char * strInfo;
strInfo = (char *) malloc (MESSAGE_LENGHT * STR_INFO);
sprintf (strInfo,
"# %d\tMONEY | RAW | PRODUCT | FACTORY | BUILDING\n$\t%d\t%d\t%d\t%d\t\t%d\n",
user->number,
user->data->money,
user->data->raw,
user->data->product,
user->data->factory,
user->data->cntBuild
);
return strInfo;
}
/*
* Begin of fn ConnectClient ()
*/
/* Add user to list,
* change ptr first or last
* incremebt count of users
*/
void AddClientToList (struct client * user, struct clientlist * clList)
{
if ( clList->first == NULL )
{
clList->first = user;
}
else
{
clList->last->next = user;
}
clList->last = user;
clList->cnt ++;
}
/* Initializating all flags
*/
void InitFlags (struct userFlags * f)
{
f->turn = 0;
}
/* Init most of all firms of client
*/
void InitUser (struct client * user, struct clientlist *clList, int fd)
{
//user->number = clList->cnt; It's not need.fn better GetUserId()
user->next = NULL;
user->contact = (struct settings*)malloc(sizeof(struct settings));
InitSettings (user->contact, fd, clList->cnt);
user->f = (struct userFlags *) malloc (sizeof(struct userFlags));
InitFlags (user->f);
user->sell = (struct auction *) malloc (sizeof(struct auction));
InitBuyOrSell (user->sell);
user->buy = (struct auction *) malloc (sizeof(struct auction));
InitBuyOrSell (user->buy);
user->data = (struct stuff *) malloc (sizeof(struct stuff));
InitStuff (user->data);
user->buf = (struct buffer * ) malloc (sizeof(struct buffer));
InitBuffer (user->buf);
user->cmd = (struct command * ) malloc (sizeof(struct command));
InitCommand (user->cmd);
}
/* Call fn GiveUserId(),
* change statusStartGame,
* print all about start.
*/
void ReadyToStartGame (struct clientlist * clList)
{
GiveUserId (clList);
clList->statusStartGame = 1;
PrintToAll ( clList,
"The game start now.\n>= = = 1th month. = = =<\n>"
);
}
/* Put to all users ID from 1 to N and write them*/
void GiveUserId (struct clientlist * clList)
{
int i;
int fd;
struct client * user;
char * strInfo;
strInfo = (char *) malloc (MESSAGE_LENGHT);
user = clList->first;
i = 1;
while ( user != NULL )
{
fd = user->contact->fd;
sprintf (strInfo, "You are player %d.\n", i);
write (fd, strInfo, strlen(strInfo) + 1);
user->number = i++;
user = user->next;
}
free (strInfo);
}
/*
* End of fn ConnectClient ()
*/
/*
* BEGIN OF FUNCTIONS FOR DisconnectClient (sturct clientlist *)
*/
/* Delete from struct current user. if first - spec.proccessing
* else simple delete and change pointer to last
*/
void DeleteCurrentClientFromList (struct clientlist * clList)
{
struct client * cur, * prev, * tmp;
if ( clList->current == clList->first)
{
tmp = clList->current->next;;
clList->first = tmp;
}
else
{
cur = clList->first;
while ( cur != clList->current )
{
prev = cur;
cur = cur->next;
}
tmp = cur->next;
prev->next = tmp;
if ( cur == clList->last )
{
clList->last = prev;
}
}
}
/* Check cnt and say to all current cnt
* or print in server "all went out"
*/
void CheckCountOfUsers (struct clientlist * clList)
{
if ( clList->cnt == 0)
{
clList->first = clList->last = NULL;
printf ("All players disconnected :-(\n");
}
else
{
PrintToAll (clList, StatusUsersConnecting (clList));
}
}
/*
* END OF FUNCTIONS FOR DisconnectClient (sturct clientlist *)
*/