--- deliantra/server/socket/loop.C 2006/12/14 04:30:33 1.14 +++ deliantra/server/socket/loop.C 2006/12/15 04:21:29 1.18 @@ -43,13 +43,8 @@ #include #include -#ifdef HAVE_UNISTD_H -# include -#endif - -#ifdef HAVE_ARPA_INET_H -# include -#endif +#include +#include #include #include @@ -67,111 +62,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_socket *ns) + { + return (!(flags & PF_PLAYER) || ns->pl) + && (!(flags & PF_PLAYING) || (ns->pl && ns->pl->state == ST_PLAYING)); + } + + void execute (client_socket *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_socket *))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. * @@ -229,39 +181,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; } } @@ -277,53 +206,12 @@ } } - -/***************************************************************************** - * - * Low level socket looping - select calls and watchdog udp packet - * sending. - * - ******************************************************************************/ - -#ifdef WATCHDOG - -/** - * Tell watchdog that we are still alive - * - * I put the function here since we should hopefully already be getting - * all the needed include files for socket support - */ - -void -watchdog (void) -{ - static int fd = -1; - static struct sockaddr_in insock; - - if (fd == -1) - { - struct protoent *protoent; - - if ((protoent = getprotobyname ("udp")) == NULL || (fd = socket (PF_INET, SOCK_DGRAM, protoent->p_proto)) == -1) - { - return; - } - insock.sin_family = AF_INET; - insock.sin_port = htons ((unsigned short) 13325); - insock.sin_addr.s_addr = inet_addr ("127.0.0.1"); - } - sendto (fd, (void *) &fd, 1, 0, (struct sockaddr *) &insock, sizeof (insock)); -} -#endif - void flush_sockets (void) { - player *pl; - - for (pl = first_player; pl != NULL; pl = pl->next) - if (pl->socket->status != Ns_Dead) - Socket_Flush (pl->socket); + for (sockvec::iterator i = client_sockets.begin (); i != client_sockets.end (); ++i) + if ((*i)->status != Ns_Dead) + (*i)->flush (); } /** @@ -337,7 +225,7 @@ doeric_server (void) { int i, pollret; - fd_set tmp_read, tmp_write; + fd_set tmp_read; struct sockaddr_in addr; socklen_t addrlen = sizeof (struct sockaddr); player *pl, *next; @@ -375,7 +263,6 @@ } FD_ZERO (&tmp_read); - FD_ZERO (&tmp_write); for (sockvec::iterator i = client_sockets.begin (); i != client_sockets.end (); ) { @@ -392,9 +279,6 @@ FD_SET (s->fd, &tmp_read); - if (s->outputbuffer.len) - FD_SET (s->fd, &tmp_write); - ++i; } } @@ -405,7 +289,7 @@ timeout.tv_usec = 0; pollret = select (maxfd + 1, - &tmp_read, &tmp_write, 0, + &tmp_read, 0, 0, &timeout); if (pollret == -1) @@ -422,13 +306,6 @@ client_socket *s = *i; player *pl = s->pl; - //TODO: writing should be independent of tick - if (FD_ISSET (s->fd, &tmp_write)) - { - s->can_write = 1; - write_socket_buffer (s); - } - //TODO: disassociate handleclient from socket readin if (s->inbuf_len || FD_ISSET (s->fd, &tmp_read)) HandleClient (s, pl);