--- deliantra/server/socket/loop.C 2006/08/13 17:16:06 1.1 +++ deliantra/server/socket/loop.C 2006/08/30 16:30:37 1.4 @@ -1,7 +1,7 @@ /* * static char *rcsid_loop_c = - * "$Id: loop.C,v 1.1 2006/08/13 17:16:06 elmex Exp $"; + * "$Id: loop.C,v 1.4 2006/08/30 16:30:37 root Exp $"; */ /* @@ -158,11 +158,11 @@ * params pointer. */ for (cp = buf; *cp != '\0'; cp++) - if (*cp==' ') { - *cp = '\0'; - params = cp + 1; - break; - } + 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); @@ -187,114 +187,114 @@ */ do { /* hack to disable old socket mode without creating too many conflicts */ - if (1 || ns->inbuf.len >= MAXSOCKBUF-1) { - ns->status = Ns_Dead; - LOG(llevDebug, "Old input socket sent too much data without newline\n"); - return; - } + if (1 || ns->inbuf.len >= MAXSOCKBUF-1) { + ns->status = Ns_Dead; + LOG(llevDebug, "Old input socket sent too much data without newline\n"); + return; + } #ifdef WIN32 /* ***win32: change oldsocket read() to recv() */ - stat = recv(ns->fd, ns->inbuf.buf + ns->inbuf.len, 1,0); + stat = recv(ns->fd, ns->inbuf.buf + ns->inbuf.len, 1,0); - if (stat==-1 && WSAGetLastError() !=WSAEWOULDBLOCK) { + if (stat==-1 && WSAGetLastError() !=WSAEWOULDBLOCK) { #else - do { - stat = read(ns->fd, ns->inbuf.buf + ns->inbuf.len, 1); - } while ((stat<0) && (errno == EINTR)); + do { + stat = read(ns->fd, ns->inbuf.buf + ns->inbuf.len, 1); + } while ((stat<0) && (errno == EINTR)); - if (stat<0 && errno != EAGAIN && errno !=EWOULDBLOCK) { + if (stat<0 && errno != EAGAIN && errno !=EWOULDBLOCK) { #endif - LOG(llevError, "Cannot read from socket: %s\n", strerror_local(errno)); - ns->status = Ns_Dead; - return; - } - if (stat == 0) return; + LOG(llevError, "Cannot read from socket: %s\n", strerror(errno)); + ns->status = Ns_Dead; + return; + } + if (stat == 0) return; } while (ns->inbuf.buf[ns->inbuf.len++]!='\n'); ns->inbuf.buf[ns->inbuf.len]=0; cp = strchr((const char*)ns->inbuf.buf, ' '); if (cp) { - /* Replace the space with a null, skip any more spaces */ - *cp++=0; - while (isspace(*cp)) cp++; + /* Replace the space with a null, skip any more spaces */ + *cp++=0; + while (isspace(*cp)) cp++; } /* Strip off all spaces and control characters from end of line */ for (i=ns->inbuf.len-1; i>=0; i--) { - if (ns->inbuf.buf[i]<=32) ns->inbuf.buf[i]=0; - else break; + if (ns->inbuf.buf[i]<=32) ns->inbuf.buf[i]=0; + else break; } ns->inbuf.len=0; /* reset for next read */ /* If just a return, don't do anything */ if (ns->inbuf.buf[0] == 0) return; if (!strcasecmp((const char*)ns->inbuf.buf,"quit")) { - ns->status = Ns_Dead; - return; + ns->status = Ns_Dead; + return; } if (!strcasecmp((const char*)ns->inbuf.buf, "listen")) { - if (cp) { - char *buf="Socket switched to listen mode\n"; + if (cp) { + char *buf="Socket switched to listen mode\n"; - free(ns->comment); - ns->comment = strdup_local(cp); - ns->old_mode = Old_Listen; - cs_write_string(ns, buf, strlen(buf)); - } else { - char *buf="Need to supply a comment/url to listen\n"; - cs_write_string(ns, buf, strlen(buf)); - } - return; + free(ns->comment); + ns->comment = strdup_local(cp); + ns->old_mode = Old_Listen; + cs_write_string(ns, buf, strlen(buf)); + } else { + char *buf="Need to supply a comment/url to listen\n"; + cs_write_string(ns, buf, strlen(buf)); + } + return; } if (!strcasecmp((const char*)ns->inbuf.buf, "name")) { - char *cp1=NULL; - if (cp) cp1= strchr(cp, ' '); - if (cp1) { - *cp1++ = 0; - while (isspace(*cp1)) cp1++; - } - if (!cp || !cp1) { - char *buf="Need to provide a name/password to name\n"; - cs_write_string(ns, buf, strlen(buf)); - return; - } - - if (verify_player(cp, cp1)==0) { - char *buf="Welcome back\n"; - free(ns->comment); - ns->comment = strdup_local(cp); - ns->old_mode = Old_Player; - cs_write_string(ns, buf, strlen(buf)); - } - else if (verify_player(cp, cp1)==2) { - ns->password_fails++; - if (ns->password_fails >= MAX_PASSWORD_FAILURES) { - char *buf="You failed to log in too many times, you will now be kicked.\n"; - LOG(llevInfo, "A player connecting from %s in oldsocketmode has been dropped for password failure\n", - ns->host); - cs_write_string(ns, buf, strlen(buf)); - ns->status = Ns_Dead; - } - else { - char *buf="Could not login you in. Check your name and password.\n"; - cs_write_string(ns, buf, strlen(buf)); - } - } - else { - char *buf="Could not login you in. Check your name and password.\n"; - cs_write_string(ns, buf, strlen(buf)); - } - return; + char *cp1=NULL; + if (cp) cp1= strchr(cp, ' '); + if (cp1) { + *cp1++ = 0; + while (isspace(*cp1)) cp1++; + } + if (!cp || !cp1) { + char *buf="Need to provide a name/password to name\n"; + cs_write_string(ns, buf, strlen(buf)); + return; + } + + if (verify_player(cp, cp1)==0) { + char *buf="Welcome back\n"; + free(ns->comment); + ns->comment = strdup_local(cp); + ns->old_mode = Old_Player; + cs_write_string(ns, buf, strlen(buf)); + } + else if (verify_player(cp, cp1)==2) { + ns->password_fails++; + if (ns->password_fails >= MAX_PASSWORD_FAILURES) { + char *buf="You failed to log in too many times, you will now be kicked.\n"; + LOG(llevInfo, "A player connecting from %s in oldsocketmode has been dropped for password failure\n", + ns->host); + cs_write_string(ns, buf, strlen(buf)); + ns->status = Ns_Dead; + } + else { + char *buf="Could not login you in. Check your name and password.\n"; + cs_write_string(ns, buf, strlen(buf)); + } + } + else { + char *buf="Could not login you in. Check your name and password.\n"; + cs_write_string(ns, buf, strlen(buf)); + } + return; } command = find_oldsocket_command((char*)ns->inbuf.buf); if (!command && ns->old_mode==Old_Player) { - command = find_oldsocket_command2((char*)ns->inbuf.buf); + command = find_oldsocket_command2((char*)ns->inbuf.buf); } if (!command) { - snprintf(buf, sizeof(buf), "Could not find command: %s\n", ns->inbuf.buf); - cs_write_string(ns, buf, strlen(buf)); - return; + snprintf(buf, sizeof(buf), "Could not find command: %s\n", ns->inbuf.buf); + cs_write_string(ns, buf, strlen(buf)); + return; } /* This is a bit of a hack, but works. Basically, we make some @@ -333,91 +333,91 @@ /* 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 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 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 (ns->status == Ns_Old) { - Handle_Oldsocket(ns); - return; - } - i=SockList_ReadPacket(ns->fd, &ns->inbuf, MAXSOCKBUF-1); - /* Special hack - let the user switch to old mode if in the Ns_Add - * phase. Don't demand they add in the special length bytes - */ - if (ns->status == Ns_Add) { - if (!strncasecmp((const char*)ns->inbuf.buf,"oldsocketmode", 13)) { - ns->status = Ns_Old; - ns->inbuf.len=0; - cs_write_string(ns, "Switched to old socket mode\n", 28); - LOG(llevDebug,"Switched socket to old socket mode\n"); - return; - } - } + if (pl && pl->state==ST_PLAYING && pl->ob != NULL && pl->ob->speed_left < 0) { + return; + } + + if (ns->status == Ns_Old) { + Handle_Oldsocket(ns); + return; + } + i=SockList_ReadPacket(ns->fd, &ns->inbuf, MAXSOCKBUF-1); + /* Special hack - let the user switch to old mode if in the Ns_Add + * phase. Don't demand they add in the special length bytes + */ + if (ns->status == Ns_Add) { + if (!strncasecmp((const char*)ns->inbuf.buf,"oldsocketmode", 13)) { + ns->status = Ns_Old; + ns->inbuf.len=0; + cs_write_string(ns, "Switched to old socket mode\n", 28); + LOG(llevDebug,"Switched socket to old socket mode\n"); + return; + } + } - if (i<0) { + if (i<0) { #ifdef ESRV_DEBUG - LOG(llevDebug,"HandleClient: Read error on connection player %s\n", (pl?pl->ob->name:"None")); + 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; + /* 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) { - *data='\0'; - data++; - len = ns->inbuf.len - (data - (char*)ns->inbuf.buf); - } - else len=0; - - 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; + /* 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) { + *data='\0'; + data++; + len = ns->inbuf.len - (data - (char*)ns->inbuf.buf); + } + else len=0; + + 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; + } + } + /* 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); + } + } + /* 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: ; } @@ -461,63 +461,6 @@ } #endif -extern unsigned long todtick; - -/** Waits for new connection */ -static void block_until_new_connection(void) -{ - - struct timeval Timeout; - fd_set readfs; - int cycles; - - LOG(llevInfo, "Waiting for connections...\n"); - - cycles=1; - do { - /* Every minutes is a bit often for updates - especially if nothing is going - * on. This slows it down to every 6 minutes. - */ - cycles++; - if (cycles%2 == 0) - tick_the_clock(); - - FD_ZERO(&readfs); - FD_SET((uint32)init_sockets[0].fd, &readfs); - - /* If fastclock is set, we need to seriously slow down the updates - * to the metaserver as well as watchdog. Do same for flush_old_maps() - - * that is time sensitive, so there is no good reason to call it 2000 times - * a second. - */ - if (settings.fastclock > 0) { -#ifdef WATCHDOG - if (cycles % 120000 == 0) { - watchdog(); - flush_old_maps(); - } -#endif - if (cycles == 720000) { - metaserver_update(); - cycles=1; - } - Timeout.tv_sec=0; - Timeout.tv_usec=50; - } else { - Timeout.tv_sec=60; - Timeout.tv_usec=0; - if (cycles == 7) { - metaserver_update(); - cycles=1; - } - flush_old_maps(); - } - } - while (select(socket_info.max_filedescriptor, &readfs, NULL, NULL, &Timeout)==0); - - reset_sleep(); /* Or the game would go too fast */ -} - void flush_sockets(void) { player *pl; @@ -544,7 +487,7 @@ #ifdef CS_LOGSTATS if ((time(NULL)-cst_lst.time_start)>=CS_LOGTIME) - write_cs_stats(); + write_cs_stats(); #endif FD_ZERO(&tmp_read); @@ -552,50 +495,41 @@ FD_ZERO(&tmp_exceptions); for(i=0;isocket.status == Ns_Dead) { - player *npl=pl->next; + if (pl->socket.status == Ns_Dead) { + player *npl=pl->next; - 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; - } + 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; + } } -#if 0 - // the event system and plugins require the server ti run at all times. - // since its primarily cf.schmorp.de we are interested in, and - // that is supposed to run all the time anyway, its globally disabled - // until we can use Event. - if (socket_info.nconns==1 && first_player==NULL) - block_until_new_connection(); -#endif - /* Reset timeout each time, since some OS's will change the values on * the return from select. */ @@ -603,11 +537,11 @@ socket_info.timeout.tv_usec = 0; pollret= select(socket_info.max_filedescriptor, &tmp_read, &tmp_write, - &tmp_exceptions, &socket_info.timeout); + &tmp_exceptions, &socket_info.timeout); if (pollret==-1) { - LOG(llevError, "select failed: %s\n", strerror_local(errno)); - return; + LOG(llevError, "select failed: %s\n", strerror(errno)); + return; } /* We need to do some of the processing below regardless */ @@ -615,136 +549,136 @@ /* Following adds a new connection */ if (pollret && FD_ISSET(init_sockets[0].fd, &tmp_read)) { - int newsocknum=0; + int newsocknum=0; #ifdef ESRV_DEBUG - LOG(llevDebug,"doeric_server: New Connection\n"); + 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; - - for (j=1; j>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++; - } - } + /* 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; + + for (j=1; j>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++; + } + } } /* Check for any exceptions/input on the sockets */ if (pollret) for(i=1;inext; - if (pl->socket.status==Ns_Dead) continue; + next=pl->next; + if (pl->socket.status==Ns_Dead) continue; - if (FD_ISSET(pl->socket.fd,&tmp_write)) { - if (!pl->socket.can_write) { + 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); + 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; - - 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); - } 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); - } - } + 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; + + 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); + } 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); + } + } } }