--- deliantra/server/socket/loop.C 2006/12/14 22:45:41 1.16 +++ deliantra/server/socket/loop.C 2006/12/15 19:59:20 1.19 @@ -47,7 +47,6 @@ #include #include -#include /***************************************************************************** * Start of command dispatch area. @@ -62,111 +61,68 @@ * we end up having 2 tables. */ -typedef void (*func_uint8_int_ns) (char *, int, client_socket *); - -struct NsCmdMapping -{ - const char *cmdname; - func_uint8_int_ns cmdproc; - uint8 flag; +enum { + PF_PLAYER = 0x01, // must have valid player + PF_IMMEDIATE = 0x02, // TODO: hack, can be executed immediately + PF_PLAYING = 0x04, // must be in playing state }; -typedef void (*func_uint8_int_pl) (char *, int, player *); -struct PlCmdMapping +struct pkt_type { - const char *cmdname; - func_uint8_int_pl cmdproc; - uint8 flag; + const char *name; + void *cb; + int flags; + + bool may_execute (client *ns) + { + return (!(flags & PF_PLAYER) || ns->pl) + && (!(flags & PF_PLAYING) || (ns->pl && ns->pl->state == ST_PLAYING)); + } + + void execute (client *ns, char *data, int datalen) + { + //TODO: only one format + if (flags & PF_PLAYER) + ((void (*)(char *, int, player * ))cb)((char *)data, datalen, ns->pl); + else + ((void (*)(char *, int, client *))cb)((char *)data, datalen, ns); + } }; +// SocketCommand, PlayingCommand, should not exist with those ugly casts +#define SC(cb) (void *)static_cast(cb), +#define PC(cb) (void *)static_cast(cb), PF_PLAYER | + /** * Dispatch table for the server. - * - * CmdMapping is the dispatch table for the server, used in HandleClient, - * which gets called when the client has input. All commands called here - * use the same parameter form (char* data, int len, int clientnum. - * We do implicit casts, because the data that is being passed is - * unsigned (pretty much needs to be for binary data), however, most - * of these treat it only as strings, so it makes things easier - * to cast it here instead of a bunch of times in the function itself. - * flag is 1 if the player must be in the playing state to issue the - * command, 0 if they can issue it at any time. */ -static struct PlCmdMapping plcommands[] = { - {"examine", ExamineCmd, 1}, - {"apply", ApplyCmd, 1}, - {"move", MoveCmd, 1}, - {"reply", ReplyCmd, 0}, - {"command", PlayerCmd, 1}, - {"ncom", (func_uint8_int_pl) NewPlayerCmd, 1}, - {"lookat", LookAt, 1}, - {"lock", (func_uint8_int_pl) LockItem, 1}, - {"mark", (func_uint8_int_pl) MarkItem, 1}, - {"mapredraw", MapRedrawCmd, 0}, /* Added: phil */ - {"mapinfo", MapInfoCmd, 2}, /* CF+ */ - {"ext", ExtCmd, 2}, /* CF+ */ - { 0 } /* terminator */ +static struct pkt_type packets[] = { + {"ncom", PC(NewPlayerCmd) PF_PLAYING }, + {"command", PC(PlayerCmd) PF_PLAYING }, + + {"examine", PC(ExamineCmd) PF_PLAYING | PF_IMMEDIATE }, + {"apply", PC(ApplyCmd) PF_PLAYING | PF_IMMEDIATE }, + {"reply", PC(ReplyCmd) PF_IMMEDIATE }, + {"lookat", PC(LookAt) PF_PLAYING | PF_IMMEDIATE }, + {"lock", PC(LockItem) PF_PLAYING | PF_IMMEDIATE }, + {"mark", PC(MarkItem) PF_PLAYING | PF_IMMEDIATE }, + {"move", PC(MoveCmd) PF_PLAYING | PF_IMMEDIATE }, + {"ext", PC(ExtCmd) PF_IMMEDIATE }, /* CF+ */ + {"mapredraw", PC(MapRedrawCmd) PF_IMMEDIATE }, /* Added: phil */ + {"mapinfo", PC(MapInfoCmd) PF_IMMEDIATE }, /* CF+ */ + + {"addme", SC(AddMeCmd) PF_IMMEDIATE }, + {"askface", SC(SendFaceCmd) 0 }, /* Added: phil */ + {"requestinfo", SC(RequestInfo) 0 }, + {"setfacemode", SC(SetFaceMode) PF_IMMEDIATE }, + {"setsound", SC(SetSound) PF_IMMEDIATE }, + {"setup", SC(SetUp) PF_IMMEDIATE }, + {"version", SC(VersionCmd) PF_IMMEDIATE }, + {"toggleextendedinfos", SC(ToggleExtendedInfos) PF_IMMEDIATE }, /*Added: tchize */ + {"toggleextendedtext", SC(ToggleExtendedText) PF_IMMEDIATE }, /*Added: tchize */ + {"asksmooth", SC(AskSmooth) 0 }, /*Added: tchize (smoothing technologies) */ }; -/** Face-related commands */ -static struct NsCmdMapping nscommands[] = { - {"addme", AddMeCmd, 2}, - {"askface", SendFaceCmd}, /* Added: phil */ - {"requestinfo", RequestInfo}, - {"setfacemode", SetFaceMode, 2}, - {"setsound", SetSound, 2}, - {"setup", SetUp, 2}, - {"version", VersionCmd, 2}, - {"toggleextendedinfos", ToggleExtendedInfos, 2}, /*Added: tchize */ - {"toggleextendedtext", ToggleExtendedText, 2}, /*Added: tchize */ - {"asksmooth", AskSmooth}, /*Added: tchize (smoothing technologies) */ - { 0 } /* terminator (I, II & III) */ -}; - -/** - * RequestInfo is sort of a meta command. There is some specific - * request of information, but we call other functions to provide - * that information. - */ -void -RequestInfo (char *buf, int len, client_socket * ns) -{ - char *params = NULL, *cp; - - /* No match */ - char bigbuf[MAX_BUF]; - int slen; - - /* Set up replyinfo before we modify any of the buffers - this is used - * if we don't find a match. - */ - strcpy (bigbuf, "replyinfo "); - slen = strlen (bigbuf); - safe_strcat (bigbuf, buf, &slen, MAX_BUF); - - /* find the first space, make it null, and update the - * params pointer. - */ - for (cp = buf; *cp != '\0'; cp++) - if (*cp == ' ') - { - *cp = '\0'; - params = cp + 1; - break; - } - - if (!strcmp (buf, "image_info")) - send_image_info (ns, params); - else if (!strcmp (buf, "image_sums")) - send_image_sums (ns, params); - else if (!strcmp (buf, "skill_info")) - send_skill_info (ns, params); - else if (!strcmp (buf, "spell_paths")) - send_spell_paths (ns, params); - else - ns->send_packet (bigbuf, len); -} - /** * Handle client input. * @@ -176,9 +132,8 @@ * with this socket, null if no player (one of the init_sockets for just * starting a connection) */ - void -HandleClient (client_socket *ns, player *pl) +HandleClient (client *ns, player *pl) { /* Loop through this - maybe we have several complete packets here. */ // limit to a few commands only, though, as to not monopolise the server @@ -224,39 +179,16 @@ ns->inbuf [pkt_len] = 0; /* Terminate buffer - useful for string data */ - for (int i = 0; nscommands[i].cmdname; i++) - { - if (!strcmp ((char *)ns->inbuf + 2, nscommands[i].cmdname)) - { - nscommands[i].cmdproc (data, datalen, ns); - ns->skip_packet (pkt_len); - //D// not doing this causes random memory corruption - if (nscommands[i].flag & 2) - goto next_packet; - return; - } - } - - /* Player must be in the playing state or the flag on the - * the command must be zero for the user to use the command - - * otherwise, a player cam save, be in the play_again state, and - * the map they were on gets swapped out, yet things that try to look - * at the map causes a crash. If the command is valid, but - * one they can't use, we still swallow it up. - */ - if (pl) - for (int i = 0; plcommands[i].cmdname; i++) + for (pkt_type *pkt = packets; pkt < packets + (sizeof (packets) / sizeof (packets[0])); ++pkt) + if (!strcmp ((char *)ns->inbuf + 2, pkt->name)) { - if (!strcmp ((char *)ns->inbuf + 2, plcommands[i].cmdname)) + if (pkt->may_execute (ns)) { - if (pl->state == ST_PLAYING || !(plcommands[i].flag & 1)) - plcommands[i].cmdproc ((char *) data, datalen, pl); - + pkt->execute (ns, data, datalen); ns->skip_packet (pkt_len); - //D// not doing this causes random memory corruption - if (plcommands[i].flag & 2) + //D//TODO not doing this causes random memory corruption + if (pkt->flags & PF_IMMEDIATE) goto next_packet; - return; } } @@ -275,7 +207,7 @@ void flush_sockets (void) { - for (sockvec::iterator i = client_sockets.begin (); i != client_sockets.end (); ++i) + for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) if ((*i)->status != Ns_Dead) (*i)->flush (); } @@ -330,13 +262,13 @@ FD_ZERO (&tmp_read); - for (sockvec::iterator i = client_sockets.begin (); i != client_sockets.end (); ) + for (sockvec::iterator i = clients.begin (); i != clients.end (); ) { - client_socket *s = *i; + client *s = *i; if (s->status == Ns_Dead) { - client_sockets.erase (i); + clients.erase (i); delete s; } else @@ -367,9 +299,9 @@ /* We need to do some of the processing below regardless */ /* Check for any input on the sockets */ - for (sockvec::iterator i = client_sockets.begin (); i != client_sockets.end (); ++i) + for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) { - client_socket *s = *i; + client *s = *i; player *pl = s->pl; //TODO: disassociate handleclient from socket readin