--- deliantra/server/socket/loop.C 2006/09/10 13:43:33 1.6 +++ deliantra/server/socket/loop.C 2008/12/27 01:25:00 1.78 @@ -1,32 +1,26 @@ - /* - * static char *rcsid_loop_c = - * "$Id: loop.C,v 1.6 2006/09/10 13:43:33 root Exp $"; + * This file is part of Deliantra, the Roguelike Realtime MMORPG. + * + * Copyright (©) 2005,2006,2007,2008 Marc Alexander Lehmann / Robin Redeker / the Deliantra team + * Copyright (©) 2002-2003,2007 Mark Wedel & The Crossfire Development Team + * Copyright (©) 1992,2007 Frank Tore Johansen + * + * Deliantra is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + * The authors can be reached via e-mail to */ -/* - CrossFire, A Multiplayer game for X-windows - - Copyright (C) 2002-2003 Mark Wedel & The Crossfire Development Team - Copyright (C) 1992 Frank Tore Johansen - - This program is free software; you can redistribute it and/or modify - it under the terms of the GNU General Public License as published by - the Free Software Foundation; either version 2 of the License, or - (at your option) any later version. - - This program is distributed in the hope that it will be useful, - but WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - GNU General Public License for more details. - - You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. - - The author can be reached via e-mail to crossfire-devel@real-time.com -*/ - /** * \file * Main client/server loops. @@ -34,556 +28,257 @@ * \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.) */ - #include -#ifndef __CEXTRACT__ -# include -# include -#endif +#include +#include -#ifndef WIN32 /* ---win32 exclude unix headers */ -# include -# include -# include -# include -# include -#endif /* end win32 */ - -#ifdef HAVE_UNISTD_H -# include -#endif +#include +#include +#include +#include +#include -#ifdef HAVE_ARPA_INET_H -# include -#endif +#include +#include #include -#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. - */ +#define BG_SCRUB_RATE 4 // how often to send a face in the background -typedef void (*func_uint8_int_ns) (char *, int, NewSocket *); +#define MAX_QUEUE_DEPTH 50 +#define MAX_QUEUE_BACKLOG 3. -struct NsCmdMapping -{ - const char *cmdname; - func_uint8_int_ns cmdproc; -}; +// disconnect a socket after this many seconds without an ack +#define SOCKET_TIMEOUT 16. -typedef void (*func_uint8_int_pl) (char *, int, player *); -struct PlCmdMapping +void +client::reset_state () { - const char *cmdname; - func_uint8_int_pl cmdproc; - uint8 flag; -}; + if (!pl) + return; -/** - * 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+ */ - {NULL, NULL, 0} /* terminator */ -}; - -/** Face-related commands */ -static struct NsCmdMapping nscommands[] = { - {"addme", AddMeCmd}, - {"askface", SendFaceCmd}, /* Added: phil */ - {"requestinfo", RequestInfo}, - {"setfacemode", SetFaceMode}, - {"setsound", SetSound}, - {"setup", SetUp}, - {"version", VersionCmd}, - {"toggleextendedinfos", ToggleExtendedInfos}, /*Added: tchize */ - {"toggleextendedtext", ToggleExtendedText}, /*Added: tchize */ - {"asksmooth", AskSmooth}, /*Added: tchize (smoothing technologies) */ - {NULL, NULL} /* terminator (I, II & III) */ -}; + pl->run_on = 0; + pl->fire_on = 0; +} -/** - * 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, NewSocket * ns) +client::queue_command (packet_type *handler, char *data, int datalen) { - char *params = NULL, *cp; + tstamp stamp = NOW; - /* 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); + 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 - Write_String_To_Socket (ns, bigbuf, len); + { + cmd_queue.resize (cmd_queue.size () + 1); + command &cmd = cmd_queue.back (); + cmd.stamp = stamp; + cmd.handler = handler; + cmd.data = salloc (datalen + 1, data); + cmd.datalen = datalen; + } } -/** - * 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 (NewSocket * ns, player *pl) +bool +client::handle_command () { - int len = 0, i, cnt; - char *data; - - /* Loop through this - maybe we have several complete packets here. */ - // limit to a few commands only, though, as to not monopolise the server - for (cnt = 16; cnt--;) + if (!cmd_queue.empty () + && state == ST_PLAYING + && pl->ob->speed_left > 0.f) { - /* 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 != NULL && pl->ob->speed_left < 0) - { - return; - } - - i = SockList_ReadPacket (ns->fd, &ns->inbuf, MAXSOCKBUF - 1); + command &cmd = cmd_queue.front (); - if (i < 0) - { -#ifdef ESRV_DEBUG - LOG (llevDebug, "HandleClient: Read error on connection player %s\n", (pl ? pl->ob->name : "None")); -#endif - /* Caller will take care of cleaning this up */ - ns->status = Ns_Dead; - return; - } - /* Still dont have a full packet */ - if (i == 0) - return; - -// //D//TODO//temporarily log long commands -// if (ns->inbuf.len >= 40 && pl && pl->ob) -// LOG (llevDebug, "HandleClient: long comamnd from <%s,%s> %d<%s>\n", pl->ob->name, ns->host, ns->inbuf.len, ns->inbuf.buf + 2); - - /* 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. - */ - data = (char *) strchr ((char *) ns->inbuf.buf + 2, ' '); - if (data) + if (cmd.stamp + MAX_QUEUE_BACKLOG < NOW) { - *data = '\0'; - data++; - len = ns->inbuf.len - (data - (char *) ns->inbuf.buf); + reset_state (); + send_packet_printf ("drawinfo %d ignoring delayed commands.", NDI_RED); } else - len = 0; + execute (cmd.handler, cmd.data, cmd.datalen); - ns->inbuf.buf[ns->inbuf.len] = '\0'; /* Terminate buffer - useful for string data */ - for (i = 0; nscommands[i].cmdname != NULL; i++) - { - if (strcmp ((char *) ns->inbuf.buf + 2, nscommands[i].cmdname) == 0) - { - nscommands[i].cmdproc ((char *) data, len, ns); - ns->inbuf.len = 0; - return; //D// not doing this causes random memory corruption - goto next_packet; - } - } - /* 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 (i = 0; plcommands[i].cmdname != NULL; i++) - { - if (strcmp ((char *) ns->inbuf.buf + 2, plcommands[i].cmdname) == 0) - { - if (pl->state == ST_PLAYING || !(plcommands[i].flag & 1)) - plcommands[i].cmdproc ((char *) data, len, pl); - ns->inbuf.len = 0; - //D// not doing this causes random memory corruption - if (plcommands[i].flag & 2) - 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.buf + 2); - next_packet: - ; + sfree (cmd.data, cmd.datalen + 1); + cmd_queue.pop_front (); + return true; } + else + return false; } +void +client::tick () +{ + if (!pl || destroyed ()) + return; -/***************************************************************************** - * - * Low level socket looping - select calls and watchdog udp packet - * sending. - * - ******************************************************************************/ - -#ifdef WATCHDOG + pl->dirty = true; -/** - * 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 - */ + /* Update the players stats once per tick. More efficient than + * sending them whenever they change, and probably just as useful + */ + esrv_update_stats (pl); -void -watchdog (void) -{ - static int fd = -1; - static struct sockaddr_in insock; + sint32 weight = pl->ob->client_weight (); - if (fd == -1) + if (last_weight != weight) { - 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"); + pl->ob->update_stats (); + esrv_update_item (UPD_WEIGHT, pl->ob, pl->ob); } - sendto (fd, (void *) &fd, 1, 0, (struct sockaddr *) &insock, sizeof (insock)); -} -#endif -void -flush_sockets (void) -{ - player *pl; + draw_client_map (pl); - for (pl = first_player; pl != NULL; pl = pl->next) - if (pl->socket.status != Ns_Dead) - Socket_Flush (&pl->socket); -} + if (update_look) + esrv_draw_look (pl); -/** - * This checks the sockets for input and exceptions, does the right thing. - * - * A bit of this code is grabbed out of socket.c - * There are 2 lists we need to look through - init_sockets is a list - * - */ -void -doeric_server (void) -{ - int i, pollret; - fd_set tmp_read, tmp_exceptions, tmp_write; - struct sockaddr_in addr; - socklen_t addrlen = sizeof (struct sockaddr); - player *pl, *next; - -#ifdef CS_LOGSTATS - if ((time (NULL) - cst_lst.time_start) >= CS_LOGTIME) - write_cs_stats (); -#endif + mapinfo_queue_run (); - FD_ZERO (&tmp_read); - FD_ZERO (&tmp_write); - FD_ZERO (&tmp_exceptions); +#if HAVE_TCP_INFO + // check time of last ack, and, if too old, kill connection + socklen_t len = sizeof (tcpi); - for (i = 0; i < socket_info.allocated_sockets; i++) + if (!getsockopt (fd, IPPROTO_TCP, TCP_INFO, &tcpi, &len) && len == sizeof (tcpi)) { - if (init_sockets[i].status == Ns_Dead) - { - free_newsocket (&init_sockets[i]); - init_sockets[i].status = Ns_Avail; - socket_info.nconns--; - } - else if (init_sockets[i].status != Ns_Avail) - { - FD_SET ((uint32) init_sockets[i].fd, &tmp_read); - FD_SET ((uint32) init_sockets[i].fd, &tmp_write); - FD_SET ((uint32) init_sockets[i].fd, &tmp_exceptions); - } - } + if (tcpi.tcpi_snd_mss) + mss = tcpi.tcpi_snd_mss; - /* Go through the players. Let the loop set the next pl value, - * since we may remove some - */ - for (pl = first_player; pl != NULL;) - { - if (pl->socket.status == Ns_Dead) - { - player *npl = pl->next; +#if 0 + fprintf (stderr, "uack %d ack %d lost %d ret %d fack %d sst %d cwnd %d mss %d pmtu %d advmss %d EXC %d\n", + tcpi.tcpi_unacked, + tcpi.tcpi_sacked, + tcpi.tcpi_lost, + tcpi.tcpi_retrans, + tcpi.tcpi_fackets, + tcpi.tcpi_snd_ssthresh, tcpi.tcpi_snd_cwnd, tcpi.tcpi_advmss, tcpi.tcpi_pmtu, tcpi.tcpi_advmss, + + tcpi.tcpi_snd_cwnd - (tcpi.tcpi_unacked - tcpi.tcpi_sacked)); +#endif - save_player (pl->ob, 0); - if (!QUERY_FLAG (pl->ob, FLAG_REMOVED)) - { - terminate_all_pets (pl->ob); - remove_ob (pl->ob); - } - leave (pl, 1); - final_free_player (pl); - pl = npl; - } - else - { - FD_SET ((uint32) pl->socket.fd, &tmp_read); - FD_SET ((uint32) pl->socket.fd, &tmp_write); - FD_SET ((uint32) pl->socket.fd, &tmp_exceptions); - pl = pl->next; + // fast-time-out a player by checking for missign acks + // do this only when player is active + if (pl && pl->active + && tcpi.tcpi_last_ack_recv > int (SOCKET_TIMEOUT * 1000)) + { + send_msg (NDI_RED | NDI_REPLY, "connection-timeout", "safety disconnect due to tcp/ip timeout (no packets received)"); + write_outputbuffer (); + + LOG (llevDebug, "connection on fd %d closed due to ack timeout (%u/%u/%u)\n", fd, + (unsigned)tcpi.tcpi_last_ack_recv, (unsigned)tcpi.tcpi_last_data_sent, (unsigned)tcpi.tcpi_unacked); + destroy (); } } +#endif - /* Reset timeout each time, since some OS's will change the values on - * the return from select. - */ - socket_info.timeout.tv_sec = 0; - socket_info.timeout.tv_usec = 0; - - pollret = select (socket_info.max_filedescriptor, &tmp_read, &tmp_write, &tmp_exceptions, &socket_info.timeout); + rate_avail = min (max_rate + mss, rate_avail + max_rate); - if (pollret == -1) - { - LOG (llevError, "select failed: %s\n", strerror (errno)); - return; - } + int max_send = rate_avail; - /* We need to do some of the processing below regardless */ +#if HAVE_TCP_INFO + // further restrict the available bandwidth by the excess bandwidth available + max_send = min (max_send, (tcpi.tcpi_snd_cwnd - tcpi.tcpi_unacked + tcpi.tcpi_sacked) * mss); +#endif -/* if (!pollret) return;*/ + // round to next-lowest mss + max_send -= max_send % mss; - /* Following adds a new connection */ - if (pollret && FD_ISSET (init_sockets[0].fd, &tmp_read)) + if (ixface.empty ()) { - int newsocknum = 0; + // regularly send a new face when queue is empty + if (bg_scrub && !--bg_scrub) + while (scrub_idx < faces.size () - 1) + { + ++scrub_idx; -#ifdef ESRV_DEBUG - LOG (llevDebug, "doeric_server: New Connection\n"); -#endif - /* If this is the case, all sockets currently in used */ - if (socket_info.allocated_sockets <= socket_info.nconns) - { - init_sockets = (NewSocket *) realloc (init_sockets, sizeof (NewSocket) * (socket_info.nconns + 1)); - if (!init_sockets) - fatal (OUT_OF_MEMORY); - newsocknum = socket_info.allocated_sockets; - socket_info.allocated_sockets++; - init_sockets[newsocknum].faces_sent_len = nrofpixmaps; - init_sockets[newsocknum].faces_sent = (uint8 *) malloc (nrofpixmaps * sizeof (*init_sockets[newsocknum].faces_sent)); - if (!init_sockets[newsocknum].faces_sent) - fatal (OUT_OF_MEMORY); - init_sockets[newsocknum].status = Ns_Avail; - } - else - { - int j; + if (!faces_sent [scrub_idx]) + if (faceinfo *f = face_info (scrub_idx)) + if (f->type == FT_FACE || f->type == FT_SOUND) // only scrub faces and sounds for now + { + send_face (scrub_idx, -120); + flush_fx (); + + bg_scrub = 1; // send up to one fx per tick, unless an image was requested + break; + } + } + } + else + { + bg_scrub = BG_SCRUB_RATE; - for (j = 1; j < socket_info.allocated_sockets; j++) - if (init_sockets[j].status == Ns_Avail) - { - newsocknum = j; - break; - } - } - init_sockets[newsocknum].fd = accept (init_sockets[0].fd, (struct sockaddr *) &addr, &addrlen); - if (init_sockets[newsocknum].fd == -1) - { - LOG (llevError, "accept failed: %s\n", strerror (errno)); - } - else + for (;;) { - char buf[MAX_BUF]; - long ip; - NewSocket *ns; + int avail = max_send - outputbuffer_len (); - ns = &init_sockets[newsocknum]; + if (avail <= 0) + break; - ip = ntohl (addr.sin_addr.s_addr); - sprintf (buf, "%ld.%ld.%ld.%ld", (ip >> 24) & 255, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255); + ixsend &ix = ixface.back (); - if (checkbanned (NULL, buf)) + if (facedata *d = face_data (ix.idx, faceset)) { - LOG (llevInfo, "Banned host tried to connect: [%s]\n", buf); - close (init_sockets[newsocknum].fd); - init_sockets[newsocknum].fd = -1; - } - else - { - InitConnection (ns, buf); - socket_info.nconns++; - } - } - } + // estimate the packet header overhead "ix " + idx + (new)ofs + int pktlen = 3 + ber32::encoded_size (ix.idx) + ber32::encoded_size (ix.ofs); + int chunk = min (avail - packet::hdrlen, MAXSOCKBUF) - pktlen; + + // only transfer something if the amount of data transferred + // has a healthy relation to the header overhead + if (chunk < 64) + break; - /* Check for any exceptions/input on the sockets */ - if (pollret) - for (i = 1; i < socket_info.allocated_sockets; i++) - { - if (init_sockets[i].status == Ns_Avail) - continue; - if (FD_ISSET (init_sockets[i].fd, &tmp_exceptions)) - { - free_newsocket (&init_sockets[i]); - init_sockets[i].status = Ns_Avail; - socket_info.nconns--; - continue; - } - if (FD_ISSET (init_sockets[i].fd, &tmp_read)) - { - HandleClient (&init_sockets[i], NULL); - } - if (FD_ISSET (init_sockets[i].fd, &tmp_write)) - { - init_sockets[i].can_write = 1; - } - } + chunk = min (chunk, (int)ix.ofs); - /* This does roughly the same thing, but for the players now */ - for (pl = first_player; pl != NULL; pl = next) - { + ix.ofs -= chunk; - next = pl->next; - if (pl->socket.status == Ns_Dead) - continue; + //fprintf (stderr, "i%dx %6d: %5d+%4d (%4d)\n", fxix, ix.idx,ix.ofs,chunk, ixface.size());//D - if (FD_ISSET (pl->socket.fd, &tmp_write)) - { - if (!pl->socket.can_write) - { -#if 0 - LOG (llevDebug, "Player %s socket now write enabled\n", pl->ob->name); -#endif - pl->socket.can_write = 1; - write_socket_buffer (&pl->socket); - } - /* if we get an error on the write_socket buffer, no reason to - * continue on this socket. - */ - if (pl->socket.status == Ns_Dead) - continue; - } - else - pl->socket.can_write = 0; + packet sl ("ix"); - if (FD_ISSET (pl->socket.fd, &tmp_exceptions)) - { - save_player (pl->ob, 0); - if (!QUERY_FLAG (pl->ob, FLAG_REMOVED)) - { - terminate_all_pets (pl->ob); - remove_ob (pl->ob); - } - leave (pl, 1); - final_free_player (pl); - } - else - { - HandleClient (&pl->socket, pl); - /* If the player has left the game, then the socket status - * will be set to this be the leave function. We don't - * need to call leave again, as it has already been called - * once. - */ - if (pl->socket.status == Ns_Dead) - { - save_player (pl->ob, 0); - if (!QUERY_FLAG (pl->ob, FLAG_REMOVED)) - { - terminate_all_pets (pl->ob); - remove_ob (pl->ob); - } - leave (pl, 1); - final_free_player (pl); + sl << ber32 (ix.idx) + << ber32 (ix.ofs) + << data (d->data.data () + ix.ofs, chunk); + + send_packet (sl); } else + ix.ofs = 0; + + if (!ix.ofs) { + ixface.pop_back (); - /* 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); - if (pl->last_weight != WEIGHT (pl->ob)) - 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)); - } - /* draw_client_map does sanity checking that map is - * valid, so don't do it here. - */ - draw_client_map (pl->ob); - if (pl->socket.update_look) - esrv_draw_look (pl->ob); + if (ixface.empty ()) + break; } } } + + rate_avail -= outputbuffer_len (); } + +void +client::flush_sockets (void) +{ + for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) + (*i)->flush (); +} + +void +client::clock (void) +{ + for (sockvec::iterator i = clients.begin (); i != clients.end (); ++i) + (*i)->tick (); + + // give them all the same chances + flush_sockets (); + + //TODO: should not be done here, either + for (unsigned i = 0; i < clients.size (); ++i) + clients[i]->refcnt_chk (); +} +