--- deliantra/server/socket/loop.C 2006/12/15 19:59:20 1.19 +++ deliantra/server/socket/loop.C 2007/01/02 11:08:36 1.35 @@ -28,8 +28,7 @@ * \date 2003-12-02 * * loop.c mainly deals with initialization and higher level socket - * maintenance (checking for lost connections and if data has arrived.) - * The reading of data is handled in ericserver.c + * maintanance (checking for lost connections and if data has arrived.) */ @@ -48,168 +47,78 @@ #include -/***************************************************************************** - * Start of command dispatch area. - * The commands here are protocol commands. - ****************************************************************************/ - -/* Either keep this near the start or end of the file so it is - * at least reasonablye easy to find. - * There are really 2 commands - those which are sent/received - * before player joins, and those happen after the player has joined. - * As such, we have function types that might be called, so - * we end up having 2 tables. - */ - -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 -}; +#define MAX_QUEUE_DEPTH 500 //TODO +#define MAX_QUEUE_BACKLOG 3. //TODO -struct pkt_type +void +client::reset_state () { - 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 | + if (!pl) + return; -/** - * Dispatch table for the server. - */ -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) */ -}; + pl->run_on = 0; + pl->fire_on = 0; +} -/** - * Handle client input. - * - * HandleClient is actually not named really well - we only get here once - * there is input, so we don't do exception or other stuff here. - * sock is the output socket information. pl is the player associated - * with this socket, null if no player (one of the init_sockets for just - * starting a connection) - */ void -HandleClient (client *ns, player *pl) +client::queue_command (packet_type *handler, char *data, int datalen) { - /* Loop through this - maybe we have several complete packets here. */ - // limit to a few commands only, though, as to not monopolise the server - for (int repeat = 16; repeat--;) + tstamp stamp = now (); + + if (cmd_queue.size () >= MAX_QUEUE_DEPTH) + { + //TODO: just disconnect here? + reset_state (); + send_packet_printf ("drawinfo %d command queue overflow, ignoring.", NDI_RED); + } + else { - /* If it is a player, and they don't have any speed left, we - * return, and will read in the data when they do have time. - */ - if (pl && pl->state == ST_PLAYING && pl->ob && pl->ob->speed_left < 0) - return; + cmd_queue.push_back (command ()); + command &cmd = cmd_queue.back (); + cmd.stamp = stamp; + cmd.handler = handler; + cmd.data = salloc (datalen + 1, data); + cmd.datalen = datalen; + } +} - int pkt_len = ns->read_packet (); +bool +client::handle_command () +{ + bool skipping = false; - if (pkt_len < 0) - { - LOG (llevError, "read error on player %s\n", &pl->ob->name); - /* Caller will take care of cleaning this up */ - ns->status = Ns_Dead; - return; - } - else if (pkt_len == 0) - /* Still dont have a full packet */ - return; - - /* First, break out beginning word. There are at least - * a few commands that do not have any paremeters. If - * we get such a command, don't worry about trying - * to break it up. - */ - int datalen; - char *data = strchr ((char *)ns->inbuf + 2, ' '); + while (!cmd_queue.empty () + && !(state == ST_PLAYING && pl->ob && pl->ob->speed_left < 0)) + { + command &cmd = cmd_queue.front (); - if (data) + if (cmd.stamp + MAX_QUEUE_BACKLOG < now ()) { - *data++ = 0; - datalen = pkt_len - (data - (char *)ns->inbuf); + if (!skipping) + { + skipping = true; + reset_state (); + send_packet_printf ("drawinfo %d ignoring delayed commands.", NDI_RED); + } + + cmd_queue.pop_front (); } else { - data = (char *)ns->inbuf + 2; // better read garbage than segfault - datalen = 0; + execute (cmd.handler, cmd.data, cmd.datalen); + cmd_queue.pop_front (); + return true; } - - ns->inbuf [pkt_len] = 0; /* Terminate buffer - useful for string data */ - - for (pkt_type *pkt = packets; pkt < packets + (sizeof (packets) / sizeof (packets[0])); ++pkt) - if (!strcmp ((char *)ns->inbuf + 2, pkt->name)) - { - if (pkt->may_execute (ns)) - { - pkt->execute (ns, data, datalen); - ns->skip_packet (pkt_len); - //D//TODO not doing this causes random memory corruption - if (pkt->flags & PF_IMMEDIATE) - goto next_packet; - return; - } - } - - /* If we get here, we didn't find a valid command. Logging - * this might be questionable, because a broken client/malicious - * user could certainly send a whole bunch of invalid commands. - */ - LOG (llevDebug, "Bad command from client (%s)\n", ns->inbuf + 2); - ns->skip_packet (pkt_len); - next_packet: - ; } + + return false; } void flush_sockets (void) { for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) - if ((*i)->status != Ns_Dead) - (*i)->flush (); + (*i)->flush (); } /** @@ -222,99 +131,24 @@ void doeric_server (void) { - int i, pollret; - fd_set tmp_read; - struct sockaddr_in addr; - socklen_t addrlen = sizeof (struct sockaddr); - player *pl, *next; - int maxfd = 0; - #ifdef CS_LOGSTATS if ((time (NULL) - cst_lst.time_start) >= CS_LOGTIME) write_cs_stats (); #endif - /* Go through the players. Let the loop set the next pl value, - * since we may remove some - */ - //TODO: must be handled cleanly elsewhere - for (pl = first_player; pl; ) + //TODO: should not be done here, either + for (int i = 0; i < clients.size (); ++i) { - player *npl = pl->next; - - //TODO: must be handled cleanly elsewhere - if (pl->socket->status == Ns_Dead) - { - save_player (pl->ob, 0); - - if (!QUERY_FLAG (pl->ob, FLAG_REMOVED)) - { - terminate_all_pets (pl->ob); - pl->ob->remove (); - } - - leave (pl, 1); - final_free_player (pl); - } - - pl = npl; - } - - FD_ZERO (&tmp_read); - - for (sockvec::iterator i = clients.begin (); i != clients.end (); ) - { - client *s = *i; - - if (s->status == Ns_Dead) - { - clients.erase (i); - delete s; - } - else - { - if (s->fd > maxfd) maxfd = s->fd; - - FD_SET (s->fd, &tmp_read); - - ++i; - } - } - - struct timeval timeout; - - timeout.tv_sec = 0; - timeout.tv_usec = 0; - - pollret = select (maxfd + 1, - &tmp_read, 0, 0, - &timeout); - - if (pollret == -1) - { - LOG (llevError, "select failed: %s\n", strerror (errno)); - return; - } - - /* We need to do some of the processing below regardless */ - - /* Check for any input on the sockets */ - for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) - { - client *s = *i; + client *s = clients [i]; player *pl = s->pl; - //TODO: disassociate handleclient from socket readin - if (s->inbuf_len || FD_ISSET (s->fd, &tmp_read)) - HandleClient (s, pl); - - //TODO: should not be done here, either - if (s->status != Ns_Dead && pl) + if (pl && pl->ns && !pl->ns->destroyed ()) { /* Update the players stats once per tick. More efficient than * sending them whenever they change, and probably just as useful */ esrv_update_stats (pl); + if (pl->last_weight != -1 && pl->last_weight != WEIGHT (pl->ob)) { esrv_update_item (UPD_WEIGHT, pl->ob, pl->ob); @@ -323,13 +157,13 @@ (unsigned long) pl->last_weight, WEIGHT (pl->ob)); } - /* draw_client_map does sanity checking that map is - * valid, so don't do it here. - */ draw_client_map (pl->ob); + if (s->update_look) esrv_draw_look (pl->ob); } + + s->refcnt_chk (); } }