--- deliantra/server/socket/init.C 2006/08/30 16:30:37 1.6 +++ deliantra/server/socket/init.C 2007/06/09 23:18:17 1.48 @@ -1,31 +1,27 @@ /* - * static char *rcsid_init_c = - * "$Id: init.C,v 1.6 2006/08/30 16:30:37 root Exp $"; + * This file is part of Crossfire TRT, the Multiplayer Online Role Playing Game. + * + * Copyright (©) 2005,2006,2007 Marc Alexander Lehmann / Robin Redeker / the Crossfire TRT team + * Copyright (©) 2001,2007 Mark Wedel + * Copyright (©) 1992,2007 Frank Tore Johansen + * + * Crossfire TRT 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 Crossfire TRT; if not, write to the Free Software Foundation, Inc. 51 + * Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + * The authors can be reached via e-mail to */ -/* - CrossFire, A Multiplayer game for X-windows - - Copyright (C) 2001 Mark Wedel - 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 * Socket general functions @@ -37,259 +33,218 @@ * The reading of data is handled in ericserver.c */ - #include -#ifndef __CEXTRACT__ #include -#endif -#ifndef WIN32 /* ---win32 exclude include files */ #include #include #include #include +#include +#include #include -#endif /* win32 */ -#ifdef HAVE_UNISTD_H #include -#endif - -#ifdef HAVE_ARPA_INET_H #include -#endif -#include -Socket_Info socket_info; -NewSocket *init_sockets; +#include + +// use relatively small values, as we do not expect to receive much, +// and we do userspace write buffering +// 8kb limits throughput to roughly 66kb/s +#define SOCKET_RCVBUF 8192 +#define SOCKET_SNDBUF 16384 + +sockvec clients; /** * Initializes a connection. Really, it just sets up the data structure, * socket setup is handled elsewhere. We do send a version to the * client. */ -void InitConnection(NewSocket *ns, const char *from_ip) +client::client (int fd, const char *peername) +: fd (fd), host (strdup (peername)), + socket_ev (this, &client::socket_cb), + cmd_ev (this, &client::cmd_cb) { - SockList sl; - char buf[256]; - int bufsize=65535; /*Supposed absolute upper limit */ - int oldbufsize; - int buflen=sizeof(int); + refcnt_inc (); // the socket is an external reference -#ifdef WIN32 /* ***WIN32 SOCKET: init win32 non blocking socket */ - int temp = 1; + mss = 1500 - 52; // 1500 typical ethernet frame, 66 typical tcp header overhead - if(ioctlsocket(ns->fd, FIONBIO , &temp) == -1) - LOG(llevError,"InitConnection: Error on ioctlsocket.\n"); -#else - if (fcntl(ns->fd, F_SETFL, O_NONBLOCK)==-1) { - LOG(llevError,"InitConnection: Error on fcntl.\n"); - } -#endif /* end win32 */ + { + struct linger linger_opt; + + linger_opt.l_onoff = 0; + linger_opt.l_linger = 0; + + if (setsockopt (fd, SOL_SOCKET, SO_LINGER, &linger_opt, sizeof (struct linger))) + LOG (llevError, "SO_LINGER: %s\n", strerror (errno)); + } - if (getsockopt(ns->fd,SOL_SOCKET,SO_SNDBUF, (char*)&oldbufsize, (socklen_t*) &buflen)==-1) - oldbufsize=0; - if (oldbufsizefd,SOL_SOCKET,SO_SNDBUF, (char*)&bufsize, sizeof(&bufsize))) { - LOG(llevError,"InitConnection: setsockopt unable to set output buf size to %d\n", bufsize); - } - } - buflen=sizeof(oldbufsize); - getsockopt(ns->fd,SOL_SOCKET,SO_SNDBUF, (char*)&oldbufsize, (socklen_t*) &buflen); -#ifdef ESRV_DEBUG - LOG(llevDebug, "Socket buffer size now %d bytes\n", oldbufsize); + +#ifdef SO_SNDBUF + val = SOCKET_SNDBUF; + if (setsockopt (fd, SOL_SOCKET, SO_SNDBUF, &val, sizeof (val))) + LOG (llevError, "SO_SNDBUF: %s\n", strerror (errno)); #endif - ns->faceset = 0; - ns->facecache = 0; - ns->image2 = 0; - ns->sound = 0; - ns->exp64 = 0; - ns->monitor_spells = 0; - ns->mapmode = Map0Cmd; - ns->darkness = 1; - ns->status = Ns_Add; - ns->comment = NULL; - ns->old_mode = (Old_Mode) 0; - ns->mapx = 11; - ns->mapy = 11; - ns->newmapcmd= 0; - ns->itemcmd = 1; /* Default is version item1 command*/ - ns->ext_mapinfos=0; /*extendedmapinfo datas*/ - ns->EMI_smooth=0; - ns->look_position = 0; - ns->update_look = 0; - ns->has_readable_type = 0; - ns->supported_readables = 0; - ns->monitor_spells = 0; - ns->mapinfocmd = 0; - ns->extcmd = 0; - ns->extmap = 0; - - /* we should really do some checking here - if total clients overflows - * we need to do something more intelligent, because client id's will start - * duplicating (not likely in normal cases, but malicous attacks that - * just open and close connections could get this total up. - */ - ns->inbuf.len=0; - ns->inbuf.buf= (unsigned char*) malloc( sizeof( unsigned char ) * MAXSOCKBUF ); - /* Basic initialization. Needed because we do a check in - * HandleClient for oldsocketmode without checking the - * length of data. - */ - memset( ns->inbuf.buf, 0, sizeof( unsigned char ) * MAXSOCKBUF ); - memset(&ns->lastmap,0,sizeof(struct Map)); - memset(ns->faces_sent, 0, ns->faces_sent_len*sizeof(*ns->faces_sent)); - memset(&ns->anims_sent,0,sizeof(ns->anims_sent)); - memset(&ns->stats,0,sizeof(struct statsinfo)); - /* Do this so we don't send a face command for the client for - * this face. Face 0 is sent to the client to say clear - * face information. - */ - ns->faces_sent[0] = NS_FACESENT_FACE; - - ns->outputbuffer.start=0; - ns->outputbuffer.len=0; - ns->can_write=1; - ns->password_fails = 0; - - ns->current_map = 0; - ns->current_x = 0; - ns->current_y = 0; - ns->client[0] = 0; - ns->buggy_mapscroll = 0; - - ns->sent_scroll=0; - ns->host=strdup_local(from_ip); - sprintf((char*)buf, "version %d %d %s\n", VERSION_CS,VERSION_SC, VERSION_INFO); - sl.buf=(unsigned char*)buf; - sl.len=strlen((char*)buf); - Send_With_Handling(ns, &sl); -#ifdef CS_LOGSTATS - if (socket_info.nconns>cst_tot.max_conn) - cst_tot.max_conn = socket_info.nconns; - if (socket_info.nconns>cst_lst.max_conn) - cst_lst.max_conn = socket_info.nconns; +#ifdef IP_TOS + val = IPTOS_LOWDELAY; + if (setsockopt (fd, IPPROTO_IP, IP_TOS, &val, sizeof (val))) + LOG (llevError, "IP_TOS: %s\n", strerror (errno)); #endif -} +#ifdef TCP_NODELAY + val = 1; + if (setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val))) + LOG (llevError, "TCP_NODELAY: %s\n", strerror (errno)); +#endif -/** This sets up the socket and reads all the image information into memory. */ -void init_ericserver(void) -{ - struct sockaddr_in insock; - struct protoent *protox; - struct linger linger_opt; + // set some very aggressive keepalive parameters +#ifdef TCP_KEEPIDLE + val = 1; + if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof (val))) + LOG (llevError, "TCP_KEEPIDLE: %s\n", strerror (errno)); +#endif -#ifdef WIN32 /* ***win32 - we init a windows socket */ - WSADATA w; +#ifdef TCP_KEEPCNT + val = 3; + if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof (val))) + LOG (llevError, "TCP_KEEPCNT: %s\n", strerror (errno)); +#endif - socket_info.max_filedescriptor = 1; /* used in select, ignored in winsockets */ - WSAStartup (0x0101,&w); /* this setup all socket stuff */ - /* ill include no error tests here, winsocket 1.1 should always work */ - /* except some old win95 versions without tcp/ip stack */ -#else /* non windows */ - -#ifdef HAVE_SYSCONF - socket_info.max_filedescriptor = sysconf(_SC_OPEN_MAX); -#else -# ifdef HAVE_GETDTABLESIZE - socket_info.max_filedescriptor = getdtablesize(); -# else - "Unable to find usable function to get max filedescriptors"; -# endif +#ifdef TCP_KEEPINTVL + val = 1; + if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof (val))) + LOG (llevError, "TCP_KEEPINTVL: %s\n", strerror (errno)); #endif -#endif /* win32 */ - socket_info.timeout.tv_sec = 0; - socket_info.timeout.tv_usec = 0; - socket_info.nconns=0; - -#ifdef CS_LOGSTATS - memset(&cst_tot, 0, sizeof(CS_Stats)); - memset(&cst_lst, 0, sizeof(CS_Stats)); - cst_tot.time_start=time(NULL); - cst_lst.time_start=time(NULL); + // try to find the mss value in use +#ifdef TCP_MAXSEG + socklen_t sl = sizeof (val); + if (!getsockopt (fd, IPPROTO_TCP, TCP_MAXSEG, &val, &sl) && sl == sizeof (val)) + mss = val; #endif + } - LOG(llevDebug,"Initialize new client/server data\n"); - socket_info.nconns = 1; - init_sockets = (NewSocket *) malloc(sizeof(NewSocket)); - init_sockets[0].faces_sent = NULL; /* unused */ - socket_info.allocated_sockets=1; - - protox = getprotobyname("tcp"); - if (protox==NULL) { - LOG(llevError,"init_ericserver: Error getting protox\n"); - return; - } - init_sockets[0].fd = socket(PF_INET, SOCK_STREAM, protox->p_proto); - if (init_sockets[0].fd == -1) { - LOG(llevError, "Cannot create socket: %s\n", strerror(errno)); - exit(-1); - } - insock.sin_family = AF_INET; - insock.sin_port = htons(settings.csport); - insock.sin_addr.s_addr = htonl(INADDR_ANY); + { + int bufsize = 65535; /*Supposed absolute upper limit */ + int oldbufsize; + int buflen = sizeof (int); - linger_opt.l_onoff = 0; - linger_opt.l_linger = 0; - if(setsockopt(init_sockets[0].fd,SOL_SOCKET,SO_LINGER,(char *) &linger_opt, - sizeof(struct linger))) { - LOG(llevError, "Cannot setsockopt(SO_LINGER): %s\n", strerror(errno)); - } -/* Would be nice to have an autoconf check for this. It appears that - * these functions are both using the same calling syntax, just one - * of them needs extra valus passed. - */ -#if defined(__osf__) || defined(hpux) || defined(sgi) || defined(NeXT) || \ - defined(__sun__) || defined(__linux__) || defined(SVR4) || \ - defined(__FreeBSD__) || defined(__OpenBSD__) || \ - defined(WIN32) /* ---win32 add this here */ || \ - defined(__GNU__) /* HURD */ + if (fcntl (fd, F_SETFL, O_NONBLOCK) == -1) + LOG (llevError, "InitConnection: Error on fcntl.\n"); + + if (getsockopt (fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen) == -1) + oldbufsize = 0; + + if (oldbufsize < bufsize) + if (setsockopt (fd, SOL_SOCKET, SO_SNDBUF, (char *) &bufsize, sizeof (&bufsize))) + LOG (llevError, "InitConnection: setsockopt unable to set output buf size to %d\n", bufsize); + + buflen = sizeof (oldbufsize); + getsockopt (fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen); + } + + state = ST_SETUP; + mapmode = Map0Cmd; + darkness = 1; + mapx = 11; + mapy = 11; + itemcmd = 1; /* Default is version item1 command */ + max_rate = 100000 / (1000000 / MAX_TIME); // ~1mbit is assumed per default + + /* Do this so we don't send a face command for the client for + * this face. Face 0 is sent to the client to say clear + * face information. + */ + faces_sent[0] = true; + + socket_ev.prio (PE_PRIO_NORMAL); + socket_ev.fd (fd); + socket_ev.poll (PE_R); + + cmd_ev.prio (PE_PRIO_NORMAL); + + // initialisation done, kick it! + send_packet_printf ("version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO); + flush (); + + reset_stats (); + + clients.insert (this); +} + +client::~client () +{ + clients.erase (this); + + free (stats.range); + free (stats.title); + free (host); +} + +void +client::do_destroy () +{ + attachable::do_destroy (); + + if (pl) + pl->disconnect (); + + if (fd >= 0) { -#ifdef WIN32 - char tmp = 1; -#else - int tmp =1; -#endif + send_packet ("goodbye"); + flush (); - if(setsockopt(init_sockets[0].fd,SOL_SOCKET,SO_REUSEADDR, &tmp, sizeof(tmp))) { - LOG(llevError, "Cannot setsockopt(SO_REUSEADDR): %s\n", strerror(errno)); - } + close (fd); } -#else - if(setsockopt(init_sockets[0].fd,SOL_SOCKET,SO_REUSEADDR,(char *)NULL,0)) { - LOG(llevError, "Cannot setsockopt(SO_REUSEADDR): %s\n", strerror(errno)); - } -#endif - if (bind(init_sockets[0].fd,(struct sockaddr *)&insock,sizeof(insock)) == (-1)) { - LOG(llevError, "Cannot bind socket to port %d: %s\n", ntohs(insock.sin_port), strerror(errno)); -#ifdef WIN32 /* ***win32: close() -> closesocket() */ - shutdown(init_sockets[0].fd,SD_BOTH); - closesocket(init_sockets[0].fd); -#else - close(init_sockets[0].fd); -#endif /* win32 */ - exit(-1); - } - if (listen(init_sockets[0].fd,5) == (-1)) { - LOG(llevError, "Cannot listen on socket: %s\n", strerror(errno)); -#ifdef WIN32 /* ***win32: close() -> closesocket() */ - shutdown(init_sockets[0].fd,SD_BOTH); - closesocket(init_sockets[0].fd); -#else - close(init_sockets[0].fd); -#endif /* win32 */ - exit(-1); - } - init_sockets[0].status=Ns_Add; - read_client_images(); + state = ST_DEAD; + + socket_ev.suspend (); + cmd_ev.suspend (); + + refcnt_dec (); // socket no longer open } +void +client::reset_stats () +{ + /* we need to clear these to -1 and not zero - otherwise, + * if a player quits and starts a new character, we wont + * send new values to the client, as things like exp start + * at zero. + */ + for (int i = 0; i < NUM_SKILLS; i++) + last_skill_exp[i] = -1; + + for (int i = 0; i < NROFATTACKS; i++) + last_resist[i] = -1; + + last_weapon_sp = -1; + last_level = -1; + last_stats.exp = -1; + last_weight = (uint32) - 1; + last_flags = 0; + last_weight = 0; + last_weight_limit = 0; + last_path_attuned = 0; + last_path_repelled = 0; + last_path_denied = 0; + last_speed = 0; + last_flags = 0; + + static living zero_living; + last_stats = zero_living; +} /******************************************************************************* * @@ -298,48 +253,19 @@ ******************************************************************************/ /** Free's all the memory that ericserver allocates. */ -void free_all_newserver(void) -{ - LOG(llevDebug,"Freeing all new client/server information.\n"); - free_socket_images(); - free(init_sockets); -} - -/** - * Frees a socket. - * Basically, all we need to do here is free all data structures that - * might be associated with the socket. It is up to the caller to - * update the list - */ - -void free_newsocket(NewSocket *ns) +void +free_all_newserver (void) { -#ifdef WIN32 /* ***win32: closesocket in windows style */ - shutdown(ns->fd,SD_BOTH); - if (closesocket(ns->fd)) { -#else - if (close(ns->fd)) { -#endif /* win32 */ - -#ifdef ESRV_DEBUG - LOG(llevDebug,"Error closing socket %d\n", ns->fd); -#endif - } - if (ns->stats.range) - free(ns->stats.range); - if (ns->stats.title) - free(ns->stats.title); - if (ns->comment) - free(ns->comment); - free(ns->host); - free(ns->inbuf.buf); + LOG (llevDebug, "Freeing all new client/server information.\n"); + free_socket_images (); } -/** Sends the 'goodbye' command to the player, and closes connection. */ -void final_free_player(player *pl) +client * +client::create (int fd, const char *peername) { - cs_write_string(&pl->socket, "goodbye", 8); - free_newsocket(&pl->socket); - free_player(pl); + client *ns = new client (dup (fd), peername); + ns->instantiate (); // effectively a nop right now + INVOKE_CLIENT (CONNECT, ns); + return ns; }