--- deliantra/server/socket/loop.C 2006/12/14 01:12:35 1.12 +++ deliantra/server/socket/loop.C 2006/12/14 04:30:33 1.14 @@ -67,7 +67,7 @@ * 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 { @@ -134,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; @@ -183,7 +183,7 @@ */ void -HandleClient (NewSocket *ns, player *pl) +HandleClient (client_socket *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 @@ -266,7 +266,6 @@ } } - printf ("BP34n");//D /* 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. @@ -323,8 +322,8 @@ 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); } /** @@ -342,39 +341,24 @@ 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); - - 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 (init_sockets[i].fd, &tmp_read); - FD_SET (init_sockets[i].fd, &tmp_write); - } - } - /* 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)) @@ -385,162 +369,72 @@ 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); - 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, 0, - &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; - - 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; + if (s->fd > maxfd) maxfd = s->fd; - ns = &init_sockets[newsocknum]; + FD_SET (s->fd, &tmp_read); - 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 (s->outputbuffer.len) + FD_SET (s->fd, &tmp_write); - 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 input on the sockets */ - if (pollret) - for (i = 1; i < socket_info.allocated_sockets; i++) - { - if (init_sockets[i].status == Ns_Avail) - continue; + struct timeval timeout; - //TODO: disassociate handleclient from socket readin - if (init_sockets[i].inbuf_len || FD_ISSET (init_sockets[i].fd, &tmp_read)) - HandleClient (&init_sockets[i], NULL); + timeout.tv_sec = 0; + timeout.tv_usec = 0; - if (FD_ISSET (init_sockets[i].fd, &tmp_write)) - init_sockets[i].can_write = 1; - } + 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; - - 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); - } + /* We need to do some of the processing below regardless */ - /* 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; - 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) + //TODO: writing should be independent of tick + if (FD_ISSET (s->fd, &tmp_write)) { - save_player (pl->ob, 0); + s->can_write = 1; + write_socket_buffer (s); + } - if (!QUERY_FLAG (pl->ob, FLAG_REMOVED)) - { - terminate_all_pets (pl->ob); - pl->ob->remove (); - } + //TODO: disassociate handleclient from socket readin + if (s->inbuf_len || FD_ISSET (s->fd, &tmp_read)) + HandleClient (s, pl); - leave (pl, 1); - final_free_player (pl); - } - else + //TODO: should not be done here, either + if (s->status != Ns_Dead && pl) { /* Update the players stats once per tick. More efficient than * sending them whenever they change, and probably just as useful @@ -558,7 +452,7 @@ * valid, so don't do it here. */ draw_client_map (pl->ob); - if (pl->socket.update_look) + if (s->update_look) esrv_draw_look (pl->ob); } }