--- deliantra/server/socket/init.C 2006/12/14 20:39:54 1.17 +++ deliantra/server/socket/init.C 2006/12/27 05:32:26 1.32 @@ -32,34 +32,33 @@ * The reading of data is handled in ericserver.c */ - #include #include #include #include #include #include +#include +#include #include -#ifdef HAVE_UNISTD_H -# include -#endif +#include +#include -#ifdef HAVE_ARPA_INET_H -# include -#endif -#include +#include -sockvec client_sockets; +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. */ -client_socket::client_socket (int fd, const char *peername) +client::client (int fd, const char *peername) : fd (fd), host (strdup (peername)), - socket_ev (this, &client_socket::socket_cb) + socket_ev (this, &client::socket_cb), + cmd_ev (this, &client::cmd_cb), + cc_inv (this), cc_other (this) { { struct linger linger_opt; @@ -68,9 +67,42 @@ linger_opt.l_linger = 0; if (setsockopt (fd, SOL_SOCKET, SO_LINGER, &linger_opt, sizeof (struct linger))) - LOG (llevError, "Cannot setsockopt(SO_LINGER): %s\n", strerror (errno)); + LOG (llevError, "SO_LINGER: %s\n", strerror (errno)); + } + + { + int val = IPTOS_LOWDELAY; + + if (setsockopt (fd, IPPROTO_IP, IP_TOS, &val, sizeof (val))) + LOG (llevError, "IP_TOS: %s\n", strerror (errno)); + } + + { + int val = 1; + + if (setsockopt (fd, IPPROTO_TCP, TCP_NODELAY, &val, sizeof (val))) + LOG (llevError, "TCP_NODELAY: %s\n", strerror (errno)); } +#ifdef __linux__ + { + int val; + + // set some very aggressive keepalive parameters + val = 10; + if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPIDLE, &val, sizeof (val))) + LOG (llevError, "TCP_KEEPIDLE: %s\n", strerror (errno)); + + val = 5; + if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPCNT, &val, sizeof (val))) + LOG (llevError, "TCP_KEEPCNT: %s\n", strerror (errno)); + + val = 1; + if (setsockopt (fd, IPPROTO_TCP, TCP_KEEPINTVL, &val, sizeof (val))) + LOG (llevError, "TCP_KEEPINTVL: %s\n", strerror (errno)); + } +#endif + { int bufsize = 65535; /*Supposed absolute upper limit */ int oldbufsize; @@ -90,17 +122,14 @@ getsockopt (fd, SOL_SOCKET, SO_SNDBUF, (char *) &oldbufsize, (socklen_t *) & buflen); } + state = ST_SETUP; mapmode = Map0Cmd; darkness = 1; - status = Ns_Add; mapx = 11; mapy = 11; itemcmd = 1; /* Default is version item1 command */ - faces_sent_len = nrofpixmaps; - faces_sent = (uint8 *)calloc (nrofpixmaps, sizeof (*faces_sent)); - if (!faces_sent) - fatal (OUT_OF_MEMORY); + faces_sent = salloc0 (nrofpixmaps); /* 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 @@ -108,15 +137,23 @@ */ faces_sent[0] = NS_FACESENT_FACE; - socket_ev.fd (fd).poll (PE_W).start (); + // socket I/O is low priority + // we prefer to have the server run instead of handling client requests + socket_ev.prio (PE_PRIO_NORMAL + 1); + socket_ev.fd (fd); + socket_ev.poll (PE_R); + + // command handling is even lower priority + // that way no client can monopolise the server + cmd_ev.prio (PE_PRIO_NORMAL + 2); // initialisation done, kick it! + send_packet_printf ("version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO); + flush (); - { - packet sl; - sl.printf ("version %d %d %s\n", VERSION_CS, VERSION_SC, VERSION_INFO); - send_packet (sl); - } + // clients are externally referenced by the socket + refcnt_inc (); + clients.push_back (this); #if 0//TODO socket_info.nconns++; @@ -125,20 +162,42 @@ #endif } -client_socket::~client_socket () +client::~client () { - status = Ns_Dead; + sockvec::iterator i = find (clients.begin (), clients.end (), this); + if (i != clients.end ()) + clients.erase (i); + + sfree (faces_sent, nrofpixmaps); + free (stats.range); + free (stats.title); + free (host); +} + +void +client::do_destroy () +{ + attachable::do_destroy (); + + if (pl) + pl->disconnect (); + + if (fd >= 0) + close (fd); + + state = ST_DEAD; + + socket_ev.suspend (); + cmd_ev.suspend (); + + // socket now longer references us + refcnt_dec (); } /** This sets up the socket and reads all the image information into memory. */ void init_ericserver (void) { - struct sockaddr_in insock; - struct linger linger_opt; - - 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); @@ -162,55 +221,23 @@ free_socket_images (); } -/** - * 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 (client_socket *ns) -{ - if (ns->fd >= 0) - close (ns->fd); - - if (ns->stats.range) - free (ns->stats.range); - - if (ns->stats.title) - free (ns->stats.title); - - free (ns->host); - - ns->fd = -1; - ns->stats.range = 0; - ns->stats.title = 0; - ns->host = 0; -} - -/** Sends the 'goodbye' command to the player, and closes connection. */ -void -final_free_player (player *pl) -{ - pl->socket->send_packet ("goodbye"); - free_newsocket (pl->socket); - free_player (pl); -} - -bool -add_client_socket (int fd, const char *peername) +client * +client::create (int fd, const char *peername) { if (checkbanned (NULL, peername)) { LOG (llevInfo, "Banned host tried to connect: [%s]\n", peername); - return false; + return 0; } else { - client_socket *ns = new client_socket (dup (fd), peername); - client_sockets.push_back (ns); - return true; + client *ns = new client (dup (fd), peername); + + ns->instantiate (); // effectively a nop right now + + INVOKE_CLIENT (CONNECT, ns); + + return ns; } }