--- deliantra/server/socket/loop.C 2006/12/14 00:01:37 1.11 +++ deliantra/server/socket/loop.C 2006/12/14 21:46:34 1.15 @@ -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; @@ -169,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); } /** @@ -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. @@ -278,53 +277,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 (); } /** @@ -338,43 +296,28 @@ 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; + 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 +328,61 @@ 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); - /* 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) - { - 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 + if (s->status == Ns_Dead) { - int j; - - for (j = 1; j < socket_info.allocated_sockets; j++) - if (init_sockets[j].status == Ns_Avail) - { - newsocknum = j; - break; - } + client_sockets.erase (i); + delete s; } - - 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; - - ns = &init_sockets[newsocknum]; + if (s->fd > maxfd) maxfd = s->fd; - ip = ntohl (addr.sin_addr.s_addr); - sprintf (buf, "%ld.%ld.%ld.%ld", (ip >> 24) & 255, (ip >> 16) & 255, (ip >> 8) & 255, ip & 255); + FD_SET (s->fd, &tmp_read); - 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, 0, 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); - } - - /* 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; + /* We need to do some of the processing below regardless */ - 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); + /* 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 (!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 +400,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); } }