-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path#client.c#
488 lines (404 loc) · 10.8 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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
#include <stdio.h>
#include <stdlib.h>
#include "constants.h"
#include <sys/types.h>
#include <sys/socket.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <ctype.h>
#include <unistd.h>
#include "header.h"
#include "helper.h"
#include "messages.h"
#include "aux.h"
#define STDIN 0
#define HEADER_LENGTH 4
// Flags
#define HEADER 0
#define PAYLOAD 1
// Debugging variables
int mc; // malloc counter
int fc; // free counter
typedef struct P{
char name[10];
int hp;
int exp;
int x;
int y;
int visible;
} Player;
struct list_el {
Player * datum;
struct list_el * next;
};
typedef struct list_el Node;
typedef struct ll{
Node * head;
Node * tail;
} LinkedList;
void stats(Player * p){
fprintf(stdout, "%s: location=(%u,%u), HP=%u, EXP=%u\n",
p->name, p->x, p->y, p->hp, p->exp);
}
void printPlayers(LinkedList * list){
Node * n;
printf("People in the list:\n");
for(n = list->head; n != NULL; n = n->next){
stats(n->datum);
}
printf("---------------------\n");
}
void updateSelfVision(int x, int y, LinkedList * list){
Node * p;
for(p = list->head; p != NULL; p = p->next){
if (isVisible(x,y,p->datum->x,p->datum->y)){
p->datum->visible = 1;
}
}
}
void checkSelfVision(int x, int y, LinkedList * list){
Node * p;
for(p = list->head; p != NULL; p = p->next){
if (isVisible(x,y,p->datum->x,p->datum->y)){
if (p->datum->visible==0){
p->datum->visible = 1;
on_move_notify(p->datum->name,
p->datum->x,
p->datum->y,
p->datum->hp,
p->datum->exp);
}
} else {
p->datum->visible = 0;
}
}
}
Node * findPlayer(char * name, LinkedList * list){
Node * p;
for(p = list->head; p != NULL; p = p->next){
if (strcmp(p->datum->name,name)==0){
return p;
}
}
return NULL;
}
unsigned int removePlayer(char * name, LinkedList * list)
{
Node * prev = NULL;
Node * curr = list->head ;
Node * temp;
/* check for empty list */
if(!curr){ printf("The list is empty"); return 0; }
/* check if datum is in head of list */
if(strcmp(list->head->datum->name,name)==0){
if(strcmp(list->tail->datum->name,name)==0){ // if tail == head, this is the only guy
free(list->head->datum);
free(list->head);
list->head = NULL;
list->tail = NULL;
return 1;
}
curr = list->head;
free(curr->datum);
free(curr);
list->head = list->head->next;
return 1;
}
while( curr->next ){
prev = curr;
curr = curr->next;
if(strcmp( curr->datum->name,name )==0){
if(strcmp(list->tail->datum->name,name)==0){
list->tail = prev;}
prev->next = curr->next;
free(curr->datum);
free(curr);
}
}
return 1;
}
Node * freePlayers(LinkedList * list)
{
// To free all the players after log out
Node * curr;
while(list->head)
{
curr = list->head;
list->head = curr->next;
free(curr->datum);
fc++;
free(curr);
fc++;
}
}
void initialize(Player * object,char * name, int hp, int exp, int x, int y){
strcpy(object->name,name);
object->hp = hp;
object->exp = exp;
object->x = x;
object->y = y;
}
// Printing out the relevant statistics
void printStat(){
printf("\n");
printf("Number of mallocs:%d\n",mc);
printf("Number of frees:%d\n",fc);
printf("\n");
}
#include "payloadHelper.h"
int main(int argc, char* argv[]){
// Model Variables
Player * self = (Player *) malloc(sizeof(Player)); // Remember to free this
mc++; // Debugging memory leak
LinkedList * mylist = (LinkedList *) malloc (sizeof(LinkedList));
mc++;
mylist->head = NULL;
mylist->tail = NULL;
Node * p;
int isLogin = 0;
char command[80];
char arg[4000];
// Connection variables
int sock;
int serverIP;
int serverPort;
int done = 0;
int status;
// Select
fd_set readfds;
fd_set writefds;
struct sockaddr_in sin;
memset(&sin, 0, sizeof(sin));
if(argc < 4){ printf("Usage: ./client -s <server IP address> -p <server port>");}
// Initilizations
int c;
char* svalue=NULL;
char* pvalue=NULL;
while( (c=getopt( argc,argv,"s:p:"))!=-1){
switch(c){
case 's':
svalue=optarg;
break;
case 'p':
pvalue=optarg;
break;
default:
printf("Usage: ./client -s <server IP address> -p <server port>");
return 0;
}
}
serverIP = svalue;
serverPort = atoi(pvalue);
sock = socket(AF_INET, SOCK_STREAM, 0);
if(sock < 0){
perror("socket() failed\n");
abort();
}
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = inet_addr(serverIP);
sin.sin_port = htons(serverPort);
if(connect(sock,(struct sockaddr *) &sin, sizeof(sin)) < 0){
perror("client - connect");
freePlayers(mylist);
free(self);
free(mylist);
close(sock);
on_client_connect_failure();
abort();
}
show_prompt();
char * buffer;
int buffer_size = 0;
int flag = 0;
int desire_length = 4;
unsigned char header_c[HEADER_LENGTH];
struct header * hdr;
while(1){
// Clear the set readfds;
FD_ZERO(&readfds);
FD_SET(STDIN, &readfds);
FD_SET(sock, &readfds);
if (select(sock+1,&readfds,NULL,NULL,NULL) == -1){
perror("select");
return 0;
}
/*
* Handling stdin
*
*/
if(FD_ISSET(STDIN, &readfds)){
if(!readstdin(command,arg)){
continue;
}
if (strcmp(command,"login") == 0){ // LOGIN
char* name = arg; // TODO: Sanity check the input.'
if(!check_player_name(name)){
printf("! Invalid syntax.\n");
show_prompt();
continue;
}
strcpy(self->name,arg);
int status = handlelogin(name,sock);
} else if(strcmp(command,"move") == 0){ // MOVE
char* direction = arg;
if (strcmp(arg,"")==0x40){
printf("! Invalid syntax\n");
show_prompt();
continue;
}
unsigned char d;
if(strcmp(direction,"north")==0){ d = NORTH;
}else if(strcmp(direction,"south")==0){ d = SOUTH;
}else if(strcmp(direction,"east")==0){ d = EAST;
}else if(strcmp(direction,"west")==0){ d = WEST;
} else {
printf("! Invalid direction: %s\n", arg);
continue;
}
int status = handlemove(d,sock);
} else if(strcmp(command,"attack") == 0){ // ATTACK
char* victimname = arg;
if(strcmp(victimname,self->name)== 0){
show_prompt();
continue;
}
if(!check_player_name(victimname)){
printf("! Invalid name: %s\n", victimname);
continue;
}
// Search for the guy in the list
Node * vic = findPlayer(victimname,mylist);
if (!vic){ // If it is NULL
on_not_visible();
continue;
}
Player * victim = vic->datum;
int isvisible = isVisible(self->x,self->y,victim->x,victim->y);
if(!isvisible){ on_not_visible(); continue;}
int status = handleattack(victimname,sock);
} else if(strcmp(command,"speak") == 0){
char * m = arg;
// Check the message
if(!check_player_message(m)){ printf("! Invalid text message.\n"); show_prompt(); continue;}
if(handlespeak(m,sock)){ perror("handlespeak"); }
} else if(strcmp(command,"logout") == 0){
if(!isLogin){
show_prompt();
printf("You must login first.\n");
show_prompt();
continue;
}
if (handlelogout(self->name,sock) < 0){ perror("handlelogout");}
free(buffer);
free(self);
freePlayers(mylist); // Free every player in the list
free(mylist); // Free the list
show_prompt();
on_disconnection_from_server();
break;
} else if(strcmp(command,"whois") == 0){
printPlayers(mylist);
} else if(strcmp(command,"whoami")==0){
if (isLogin)
stats(self);
else printf("Not yet logged in.\n");
} else {
printf("Available command: login, move, attack, speak, logout.\n");
}
show_prompt();
/*
* Handling data from sock
*
*/
} else if (FD_ISSET(sock, &readfds)){ // Receiving from sock
// Block and wait for response from the server
unsigned char read_buffer[4096];
int expected_data_len = sizeof(read_buffer);
unsigned char *p = (char*) read_buffer; // Introduce a new pointer
int offset = 0;
int read_bytes = recv(sock,read_buffer,expected_data_len,0);
if (read_bytes == 0){ // Disconnected from the server
// Free memory
freePlayers(mylist);
free(self);
free(mylist);
on_disconnection_from_server();
break;
} else if (read_bytes < 0){
perror("recv failed");
return -1;
} else {
// Append the read_buffer to the buffer
buffer = realloc(buffer,buffer_size+read_bytes);
memcpy(buffer,read_buffer,read_bytes);
buffer_size += read_bytes;
while (buffer_size >= desire_length){
if(flag == HEADER){
// Copy the header
int j;
for(j = 0; j < HEADER_LENGTH; j++){ header_c[j] = *(buffer+j);}
printMessage(header_c,4);
hdr = (struct header *) header_c;
check_malformed_header(hdr->version,hdr->len,hdr->msgtype);
// Move the pointers
char * temp = (char*) malloc(sizeof(char)*(buffer_size-HEADER_LENGTH));
memcpy(temp,buffer+4,buffer_size-4);
free(buffer);
buffer = temp;
buffer_size -= 4;
desire_length = ntohs(hdr->len)-4;
flag = PAYLOAD;
} else { // Payload
// Move the pointers
// Copy the payload
char payload_c[desire_length];
int j;
for(j = 0; j < desire_length; j++){ payload_c[j] = *(buffer+j);}
printMessage(payload_c,desire_length);
if(hdr->msgtype == LOGIN_REPLY){ // LOGIN REPLY
if(isLogin) on_malformed_message_from_server();
if(process_login_reply(payload_c,self)==1){
isLogin = 1;
}
} else if(hdr->msgtype == MOVE_NOTIFY){
process_move_notify(payload_c,self,mylist);
} else if(hdr->msgtype == ATTACK_NOTIFY){
process_attack_notify(payload_c,self,mylist);
} else if(hdr->msgtype == SPEAK_NOTIFY){ // SPEAK_NOTIFY
int i;
for(i = 0; i < strlen(payload_c); i++){
sleep(5);
process_speak_notify(payload_c[i]);
}
} else if(hdr->msgtype == LOGOUT_NOTIFY){
printf("Got a logout notify.\n");
process_logout_notify(payload_c,mylist);
} else if(hdr->msgtype == INVALID_STATE){
process_invalid_state(payload_c);
} // End of processing reply
// Move the pointers
char * temp = (char*) malloc(sizeof(char)*(buffer_size-desire_length));
memcpy(temp,buffer+desire_length,buffer_size-desire_length);
free(buffer);
buffer = temp;
buffer_size -= desire_length;
desire_length = HEADER_LENGTH;
flag = HEADER;
} // end of handling payload
} // End of while
}
}
}
}
command> login a
command> Welcome to the tiny world of warcraft.
command> a: location=(86,77), HP=101, EXP=0
command> b: aaaaaaa
command> Player b has left the tiny world of warcraft.
command>
command> login a
command> Welcome to the tiny world of warcraft.
command> a: location=(86,77), HP=101, EXP=0
command> b: this will be shown
command> Player b has left the tiny world of warcraft.
! Exception test (truncated last packet): FAIL w/ an exception: # LOGOUT_NOTIFY is not 1. Got 0.