1 | |
1 | |
2 | /* |
2 | /* |
3 | * static char *rcsid_loop_c = |
3 | * static char *rcsid_loop_c = |
4 | * "$Id: loop.c,v 1.1.1.1 2006/02/03 07:14:44 root Exp $"; |
4 | * "$Id: loop.c,v 1.6 2006/04/22 15:02:48 root Exp $"; |
5 | */ |
5 | */ |
6 | |
6 | |
7 | /* |
7 | /* |
8 | CrossFire, A Multiplayer game for X-windows |
8 | CrossFire, A Multiplayer game for X-windows |
9 | |
9 | |
… | |
… | |
78 | */ |
78 | */ |
79 | |
79 | |
80 | typedef void (*func_uint8_int_ns) (char*, int, NewSocket *); |
80 | typedef void (*func_uint8_int_ns) (char*, int, NewSocket *); |
81 | |
81 | |
82 | struct NsCmdMapping { |
82 | struct NsCmdMapping { |
83 | char *cmdname; |
83 | const char *cmdname; |
84 | func_uint8_int_ns cmdproc; |
84 | func_uint8_int_ns cmdproc; |
85 | }; |
85 | }; |
86 | |
86 | |
87 | typedef void (*func_uint8_int_pl)(char*, int, player *); |
87 | typedef void (*func_uint8_int_pl)(char*, int, player *); |
88 | struct PlCmdMapping { |
88 | struct PlCmdMapping { |
89 | char *cmdname; |
89 | const char *cmdname; |
90 | func_uint8_int_pl cmdproc; |
90 | func_uint8_int_pl cmdproc; |
91 | uint8 flag; |
91 | uint8 flag; |
92 | }; |
92 | }; |
93 | |
93 | |
94 | /** |
94 | /** |
… | |
… | |
112 | { "command", PlayerCmd, 1}, |
112 | { "command", PlayerCmd, 1}, |
113 | { "ncom", (func_uint8_int_pl)NewPlayerCmd, 1}, |
113 | { "ncom", (func_uint8_int_pl)NewPlayerCmd, 1}, |
114 | { "lookat", LookAt, 1}, |
114 | { "lookat", LookAt, 1}, |
115 | { "lock", (func_uint8_int_pl)LockItem, 1}, |
115 | { "lock", (func_uint8_int_pl)LockItem, 1}, |
116 | { "mark", (func_uint8_int_pl)MarkItem, 1}, |
116 | { "mark", (func_uint8_int_pl)MarkItem, 1}, |
117 | { "mapredraw", MapRedrawCmd, 0}, /* Added: phil */ |
117 | { "mapredraw", MapRedrawCmd, 2}, /* Added: phil */ |
|
|
118 | { "mapinfo", MapInfoCmd, 2}, /* CF+ */ |
118 | { NULL, NULL, 0} /* terminator */ |
119 | { NULL, NULL, 0} /* terminator */ |
119 | }; |
120 | }; |
120 | |
121 | |
121 | /** Face-related commands */ |
122 | /** Face-related commands */ |
122 | static struct NsCmdMapping nscommands[] = { |
123 | static struct NsCmdMapping nscommands[] = { |
… | |
… | |
182 | /* This is not the most efficient block, but keeps the code simpler - |
183 | /* This is not the most efficient block, but keeps the code simpler - |
183 | * we basically read a byte at a time until we get a newline, error, |
184 | * we basically read a byte at a time until we get a newline, error, |
184 | * or no more characters to read. |
185 | * or no more characters to read. |
185 | */ |
186 | */ |
186 | do { |
187 | do { |
|
|
188 | /* hack to disable old socket mode without creating too many conflicts */ |
187 | if (ns->inbuf.len >= MAXSOCKBUF-1) { |
189 | if (1 || ns->inbuf.len >= MAXSOCKBUF-1) { |
188 | ns->status = Ns_Dead; |
190 | ns->status = Ns_Dead; |
189 | LOG(llevDebug, "Old input socket sent too much data without newline\n"); |
191 | LOG(llevDebug, "Old input socket sent too much data without newline\n"); |
190 | return; |
192 | return; |
191 | } |
193 | } |
192 | #ifdef WIN32 /* ***win32: change oldsocket read() to recv() */ |
194 | #ifdef WIN32 /* ***win32: change oldsocket read() to recv() */ |
… | |
… | |
322 | * starting a connection) |
324 | * starting a connection) |
323 | */ |
325 | */ |
324 | |
326 | |
325 | void HandleClient(NewSocket *ns, player *pl) |
327 | void HandleClient(NewSocket *ns, player *pl) |
326 | { |
328 | { |
327 | int len=0,i; |
329 | int len=0,i,cnt; |
328 | unsigned char *data; |
330 | unsigned char *data; |
329 | |
331 | |
330 | /* Loop through this - maybe we have several complete packets here. */ |
332 | /* Loop through this - maybe we have several complete packets here. */ |
331 | while (1) { |
333 | // limit to a few commands only, though, as to not monopolise the server |
|
|
334 | for (cnt = 16; cnt--; ) { |
332 | /* If it is a player, and they don't have any speed left, we |
335 | /* If it is a player, and they don't have any speed left, we |
333 | * return, and will read in the data when they do have time. |
336 | * return, and will read in the data when they do have time. |
334 | */ |
337 | */ |
335 | if (pl && pl->state==ST_PLAYING && pl->ob != NULL && pl->ob->speed_left < 0) { |
338 | if (pl && pl->state==ST_PLAYING && pl->ob != NULL && pl->ob->speed_left < 0) { |
336 | return; |
339 | return; |
… | |
… | |
381 | ns->inbuf.buf[ns->inbuf.len]='\0'; /* Terminate buffer - useful for string data */ |
384 | ns->inbuf.buf[ns->inbuf.len]='\0'; /* Terminate buffer - useful for string data */ |
382 | for (i=0; nscommands[i].cmdname !=NULL; i++) { |
385 | for (i=0; nscommands[i].cmdname !=NULL; i++) { |
383 | if (strcmp((char*)ns->inbuf.buf+2,nscommands[i].cmdname)==0) { |
386 | if (strcmp((char*)ns->inbuf.buf+2,nscommands[i].cmdname)==0) { |
384 | nscommands[i].cmdproc((char*)data,len,ns); |
387 | nscommands[i].cmdproc((char*)data,len,ns); |
385 | ns->inbuf.len=0; |
388 | ns->inbuf.len=0; |
386 | return; |
389 | return;//D |
|
|
390 | goto next_packet; |
387 | } |
391 | } |
388 | } |
392 | } |
389 | /* Player must be in the playing state or the flag on the |
393 | /* Player must be in the playing state or the flag on the |
390 | * the command must be zero for the user to use the command - |
394 | * the command must be zero for the user to use the command - |
391 | * otherwise, a player cam save, be in the play_again state, and |
395 | * otherwise, a player cam save, be in the play_again state, and |
392 | * the map they were on getsswapped out, yet things that try to look |
396 | * the map they were on gets swapped out, yet things that try to look |
393 | * at the map causes a crash. If the command is valid, but |
397 | * at the map causes a crash. If the command is valid, but |
394 | * one they can't use, we still swallow it up. |
398 | * one they can't use, we still swallow it up. |
395 | */ |
399 | */ |
396 | if (pl) for (i=0; plcommands[i].cmdname !=NULL; i++) { |
400 | if (pl) for (i=0; plcommands[i].cmdname !=NULL; i++) { |
397 | if (strcmp((char*)ns->inbuf.buf+2,plcommands[i].cmdname)==0) { |
401 | if (strcmp((char*)ns->inbuf.buf+2,plcommands[i].cmdname)==0) { |
398 | if (pl->state == ST_PLAYING || plcommands[i].flag == 0) |
402 | if (pl->state == ST_PLAYING || !(plcommands[i].flag & 1)) |
399 | plcommands[i].cmdproc((char*)data,len,pl); |
403 | plcommands[i].cmdproc((char*)data,len,pl); |
400 | ns->inbuf.len=0; |
404 | ns->inbuf.len=0; |
401 | return; |
405 | if (plcommands[i].flag & 2) |
|
|
406 | goto next_packet; |
|
|
407 | return; |
402 | } |
408 | } |
403 | } |
409 | } |
404 | /* If we get here, we didn't find a valid command. Logging |
410 | /* If we get here, we didn't find a valid command. Logging |
405 | * this might be questionable, because a broken client/malicious |
411 | * this might be questionable, because a broken client/malicious |
406 | * user could certainly send a whole bunch of invalid commands. |
412 | * user could certainly send a whole bunch of invalid commands. |
407 | */ |
413 | */ |
408 | LOG(llevDebug,"Bad command from client (%s)\n",ns->inbuf.buf+2); |
414 | LOG(llevDebug,"Bad command from client (%s)\n",ns->inbuf.buf+2); |
|
|
415 | next_packet: |
|
|
416 | ; |
409 | } |
417 | } |
410 | } |
418 | } |
411 | |
419 | |
412 | |
420 | |
413 | /***************************************************************************** |
421 | /***************************************************************************** |
… | |
… | |
709 | if (pl->last_weight != -1 && pl->last_weight != WEIGHT(pl->ob)) { |
717 | if (pl->last_weight != -1 && pl->last_weight != WEIGHT(pl->ob)) { |
710 | esrv_update_item(UPD_WEIGHT, pl->ob, pl->ob); |
718 | esrv_update_item(UPD_WEIGHT, pl->ob, pl->ob); |
711 | if(pl->last_weight != WEIGHT(pl->ob)) |
719 | if(pl->last_weight != WEIGHT(pl->ob)) |
712 | LOG(llevError, "esrv_update_item(UPD_WEIGHT) did not set player weight: is %lu, should be %lu\n", (unsigned long)pl->last_weight, WEIGHT(pl->ob)); |
720 | LOG(llevError, "esrv_update_item(UPD_WEIGHT) did not set player weight: is %lu, should be %lu\n", (unsigned long)pl->last_weight, WEIGHT(pl->ob)); |
713 | } |
721 | } |
714 | if (pl->ob->map && pl->ob->map->in_memory==MAP_IN_MEMORY) |
722 | /* draw_client_map does sanity checking that map is |
|
|
723 | * valid, so don't do it here. |
|
|
724 | */ |
715 | draw_client_map(pl->ob); |
725 | draw_client_map(pl->ob); |
716 | if (pl->socket.update_look) esrv_draw_look(pl->ob); |
726 | if (pl->socket.update_look) esrv_draw_look(pl->ob); |
717 | } |
727 | } |
718 | } |
728 | } |
719 | } |
729 | } |
720 | } |
730 | } |