--- deliantra/server/socket/loop.C 2006/09/14 22:34:05 1.7 +++ deliantra/server/socket/loop.C 2006/12/14 04:30:33 1.14 @@ -34,18 +34,14 @@ #include -#ifndef __CEXTRACT__ -# include -# include -#endif +#include +#include -#ifndef WIN32 /* ---win32 exclude unix headers */ -# include -# include -# include -# include -# include -#endif /* end win32 */ +#include +#include +#include +#include +#include #ifdef HAVE_UNISTD_H # include @@ -71,12 +67,13 @@ * we end up having 2 tables. */ -typedef void (*func_uint8_int_ns) (char *, int, NewSocket *); +typedef void (*func_uint8_int_ns) (char *, int, client_socket *); struct NsCmdMapping { const char *cmdname; func_uint8_int_ns cmdproc; + uint8 flag; }; typedef void (*func_uint8_int_pl) (char *, int, player *); @@ -113,22 +110,22 @@ {"mapredraw", MapRedrawCmd, 0}, /* Added: phil */ {"mapinfo", MapInfoCmd, 2}, /* CF+ */ {"ext", ExtCmd, 2}, /* CF+ */ - {NULL, NULL, 0} /* terminator */ + { 0 } /* terminator */ }; /** Face-related commands */ static struct NsCmdMapping nscommands[] = { - {"addme", AddMeCmd}, + {"addme", AddMeCmd, 2}, {"askface", SendFaceCmd}, /* Added: phil */ {"requestinfo", RequestInfo}, - {"setfacemode", SetFaceMode}, - {"setsound", SetSound}, - {"setup", SetUp}, - {"version", VersionCmd}, - {"toggleextendedinfos", ToggleExtendedInfos}, /*Added: tchize */ - {"toggleextendedtext", ToggleExtendedText}, /*Added: tchize */ + {"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) */ - {NULL, NULL} /* terminator (I, II & III) */ + { 0 } /* terminator (I, II & III) */ }; /** @@ -137,7 +134,7 @@ * that information. */ void -RequestInfo (char *buf, int len, NewSocket * ns) +RequestInfo (char *buf, int len, client_socket * ns) { char *params = NULL, *cp; @@ -162,6 +159,7 @@ params = cp + 1; break; } + if (!strcmp (buf, "image_info")) send_image_info (ns, params); else if (!strcmp (buf, "image_sums")) @@ -171,7 +169,7 @@ else if (!strcmp (buf, "spell_paths")) send_spell_paths (ns, params); else - Write_String_To_Socket (ns, bigbuf, len); + ns->send_packet (bigbuf, len); } /** @@ -185,68 +183,65 @@ */ void -HandleClient (NewSocket * ns, player *pl) +HandleClient (client_socket *ns, player *pl) { - 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--;) + for (int repeat = 16; repeat--;) { /* 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; - } + if (pl && pl->state == ST_PLAYING && pl->ob && pl->ob->speed_left < 0) + return; - i = SockList_ReadPacket (ns->fd, &ns->inbuf, MAXSOCKBUF - 1); + int pkt_len = ns->read_packet (); - if (i < 0) + if (pkt_len < 0) { -#ifdef ESRV_DEBUG - LOG (llevDebug, "HandleClient: Read error on connection player %s\n", (pl ? pl->ob->name : "None")); -#endif + LOG (llevError, "read error on player %s\n", &pl->ob->name); /* Caller will take care of cleaning this up */ ns->status = Ns_Dead; return; } - /* Still dont have a full packet */ - if (i == 0) + else if (pkt_len == 0) + /* Still dont have a full packet */ 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, ' '); + int datalen; + char *data = strchr ((char *)ns->inbuf + 2, ' '); + if (data) { - *data = '\0'; - data++; - len = ns->inbuf.len - (data - (char *) ns->inbuf.buf); + *data++ = 0; + datalen = pkt_len - (data - (char *)ns->inbuf); } else - len = 0; + { + data = (char *)ns->inbuf + 2; // better read garbage than segfault + datalen = 0; + } - ns->inbuf.buf[ns->inbuf.len] = '\0'; /* Terminate buffer - useful for string data */ - for (i = 0; nscommands[i].cmdname != NULL; i++) + ns->inbuf [pkt_len] = 0; /* Terminate buffer - useful for string data */ + + for (int i = 0; nscommands[i].cmdname; i++) { - if (strcmp ((char *) ns->inbuf.buf + 2, nscommands[i].cmdname) == 0) + if (!strcmp ((char *)ns->inbuf + 2, nscommands[i].cmdname)) { - nscommands[i].cmdproc ((char *) data, len, ns); - ns->inbuf.len = 0; - return; //D// not doing this causes random memory corruption - goto next_packet; + 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 @@ -255,24 +250,28 @@ * one they can't use, we still swallow it up. */ if (pl) - for (i = 0; plcommands[i].cmdname != NULL; i++) + for (int i = 0; plcommands[i].cmdname; i++) { - if (strcmp ((char *) ns->inbuf.buf + 2, plcommands[i].cmdname) == 0) + if (!strcmp ((char *)ns->inbuf + 2, plcommands[i].cmdname)) { if (pl->state == ST_PLAYING || !(plcommands[i].flag & 1)) - plcommands[i].cmdproc ((char *) data, len, pl); - ns->inbuf.len = 0; + plcommands[i].cmdproc ((char *) data, datalen, pl); + + ns->skip_packet (pkt_len); //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); + LOG (llevDebug, "Bad command from client (%s)\n", ns->inbuf + 2); + ns->skip_packet (pkt_len); next_packet: ; } @@ -323,12 +322,12 @@ player *pl; for (pl = first_player; pl != NULL; pl = pl->next) - if (pl->socket.status != Ns_Dead) - Socket_Flush (&pl->socket); + if (pl->socket->status != Ns_Dead) + Socket_Flush (pl->socket); } /** - * This checks the sockets for input and exceptions, does the right thing. + * This checks the sockets for input, 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 @@ -338,246 +337,124 @@ doeric_server (void) { int i, pollret; - fd_set tmp_read, tmp_exceptions, tmp_write; + fd_set tmp_read, tmp_write; 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 - FD_ZERO (&tmp_read); - FD_ZERO (&tmp_write); - FD_ZERO (&tmp_exceptions); - - for (i = 0; i < socket_info.allocated_sockets; i++) - { - 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); - } - } - /* Go through the players. Let the loop set the next pl value, * since we may remove some */ - for (pl = first_player; pl != NULL;) + //TODO: must be handled cleanly elsewhere + for (pl = first_player; pl; ) { - if (pl->socket.status == Ns_Dead) - { - player *npl = pl->next; + 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); - remove_ob (pl->ob); + pl->ob->remove (); } + 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; } - } - - /* 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); - if (pollret == -1) - { - LOG (llevError, "select failed: %s\n", strerror (errno)); - return; + pl = npl; } - /* We need to do some of the processing below regardless */ - -/* if (!pollret) return;*/ + FD_ZERO (&tmp_read); + FD_ZERO (&tmp_write); - /* Following adds a new connection */ - if (pollret && FD_ISSET (init_sockets[0].fd, &tmp_read)) + for (sockvec::iterator i = client_sockets.begin (); i != client_sockets.end (); ) { - int newsocknum = 0; + client_socket *s = *i; -#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) + if (s->status == Ns_Dead) { - 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; + client_sockets.erase (i); + delete s; } else { - int j; + if (s->fd > maxfd) maxfd = s->fd; - 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 - { - char buf[MAX_BUF]; - long ip; - NewSocket *ns; + FD_SET (s->fd, &tmp_read); - ns = &init_sockets[newsocknum]; + if (s->outputbuffer.len) + FD_SET (s->fd, &tmp_write); - ip = ntohl (addr.sin_addr.s_addr); - sprintf (buf, "%ld.%ld.%ld.%ld", (ip >> 24) & 255, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255); - - if (checkbanned (NULL, buf)) - { - 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++; - } + ++i; } } - /* 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; - } - } + struct timeval timeout; + + timeout.tv_sec = 0; + timeout.tv_usec = 0; + + pollret = select (maxfd + 1, + &tmp_read, &tmp_write, 0, + &timeout); - /* This does roughly the same thing, but for the players now */ - for (pl = first_player; pl != NULL; pl = next) + if (pollret == -1) { + LOG (llevError, "select failed: %s\n", strerror (errno)); + return; + } - next = pl->next; - if (pl->socket.status == Ns_Dead) - continue; + /* We need to do some of the processing below regardless */ - 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; + /* Check for any input on the sockets */ + for (sockvec::iterator i = client_sockets.begin (); i != client_sockets.end (); ++i) + { + client_socket *s = *i; + player *pl = s->pl; - if (FD_ISSET (pl->socket.fd, &tmp_exceptions)) + //TODO: writing should be independent of tick + if (FD_ISSET (s->fd, &tmp_write)) { - 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); + s->can_write = 1; + write_socket_buffer (s); } - else + + //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) { - 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. + /* Update the players stats once per tick. More efficient than + * sending them whenever they change, and probably just as useful */ - if (pl->socket.status == Ns_Dead) + esrv_update_stats (pl); + if (pl->last_weight != -1 && pl->last_weight != WEIGHT (pl->ob)) { - 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); + 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)); } - else - { - /* 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); - } + /* 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); } } } +