ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/loop.c
Revision: 1.1.1.1 (vendor branch)
Committed: Fri Feb 3 07:14:44 2006 UTC (18 years, 4 months ago) by root
Content type: text/plain
Branch: UPSTREAM
CVS Tags: UPSTREAM_2006_02_03
Changes since 1.1: +0 -0 lines
Log Message:
initial import

File Contents

# User Rev Content
1 root 1.1
2     /*
3     * static char *rcsid_loop_c =
4     * "$Id: loop.c,v 1.39 2006/01/30 17:00:34 cavesomething Exp $";
5     */
6    
7     /*
8     CrossFire, A Multiplayer game for X-windows
9    
10     Copyright (C) 2002-2003 Mark Wedel & The Crossfire Development Team
11     Copyright (C) 1992 Frank Tore Johansen
12    
13     This program is free software; you can redistribute it and/or modify
14     it under the terms of the GNU General Public License as published by
15     the Free Software Foundation; either version 2 of the License, or
16     (at your option) any later version.
17    
18     This program is distributed in the hope that it will be useful,
19     but WITHOUT ANY WARRANTY; without even the implied warranty of
20     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21     GNU General Public License for more details.
22    
23     You should have received a copy of the GNU General Public License
24     along with this program; if not, write to the Free Software
25     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26    
27     The author can be reached via e-mail to crossfire-devel@real-time.com
28     */
29    
30     /**
31     * \file
32     * Main client/server loops.
33     *
34     * \date 2003-12-02
35     *
36     * loop.c mainly deals with initialization and higher level socket
37     * maintenance (checking for lost connections and if data has arrived.)
38     * The reading of data is handled in ericserver.c
39     */
40    
41    
42     #include <global.h>
43     #ifndef __CEXTRACT__
44     #include <sproto.h>
45     #include <sockproto.h>
46     #endif
47    
48     #ifndef WIN32 /* ---win32 exclude unix headers */
49     #include <sys/types.h>
50     #include <sys/time.h>
51     #include <sys/socket.h>
52     #include <netinet/in.h>
53     #include <netdb.h>
54     #endif /* end win32 */
55    
56     #ifdef HAVE_UNISTD_H
57     #include <unistd.h>
58     #endif
59    
60     #ifdef HAVE_ARPA_INET_H
61     #include <arpa/inet.h>
62     #endif
63    
64     #include <loader.h>
65     #include <newserver.h>
66    
67     /*****************************************************************************
68     * Start of command dispatch area.
69     * The commands here are protocol commands.
70     ****************************************************************************/
71    
72     /* Either keep this near the start or end of the file so it is
73     * at least reasonablye easy to find.
74     * There are really 2 commands - those which are sent/received
75     * before player joins, and those happen after the player has joined.
76     * As such, we have function types that might be called, so
77     * we end up having 2 tables.
78     */
79    
80     typedef void (*func_uint8_int_ns) (char*, int, NewSocket *);
81    
82     struct NsCmdMapping {
83     char *cmdname;
84     func_uint8_int_ns cmdproc;
85     };
86    
87     typedef void (*func_uint8_int_pl)(char*, int, player *);
88     struct PlCmdMapping {
89     char *cmdname;
90     func_uint8_int_pl cmdproc;
91     uint8 flag;
92     };
93    
94     /**
95     * Dispatch table for the server.
96     *
97     * CmdMapping is the dispatch table for the server, used in HandleClient,
98     * which gets called when the client has input. All commands called here
99     * use the same parameter form (char* data, int len, int clientnum.
100     * We do implicit casts, because the data that is being passed is
101     * unsigned (pretty much needs to be for binary data), however, most
102     * of these treat it only as strings, so it makes things easier
103     * to cast it here instead of a bunch of times in the function itself.
104     * flag is 1 if the player must be in the playing state to issue the
105     * command, 0 if they can issue it at any time.
106     */
107     static struct PlCmdMapping plcommands[] = {
108     { "examine", ExamineCmd, 1},
109     { "apply", ApplyCmd, 1},
110     { "move", MoveCmd, 1},
111     { "reply", ReplyCmd, 0},
112     { "command", PlayerCmd, 1},
113     { "ncom", (func_uint8_int_pl)NewPlayerCmd, 1},
114     { "lookat", LookAt, 1},
115     { "lock", (func_uint8_int_pl)LockItem, 1},
116     { "mark", (func_uint8_int_pl)MarkItem, 1},
117     { "mapredraw", MapRedrawCmd, 0}, /* Added: phil */
118     { NULL, NULL, 0} /* terminator */
119     };
120    
121     /** Face-related commands */
122     static struct NsCmdMapping nscommands[] = {
123     { "addme", AddMeCmd },
124     { "askface", SendFaceCmd}, /* Added: phil */
125     { "requestinfo", RequestInfo},
126     { "setfacemode", SetFaceMode},
127     { "setsound", SetSound},
128     { "setup", SetUp},
129     { "version", VersionCmd },
130     { "toggleextendedinfos", ToggleExtendedInfos}, /*Added: tchize*/
131     { "toggleextendedtext", ToggleExtendedText}, /*Added: tchize*/
132     { "asksmooth", AskSmooth}, /*Added: tchize (smoothing technologies)*/
133     { NULL, NULL} /* terminator (I, II & III)*/
134     };
135    
136     /**
137     * RequestInfo is sort of a meta command. There is some specific
138     * request of information, but we call other functions to provide
139     * that information.
140     */
141     void RequestInfo(char *buf, int len, NewSocket *ns)
142     {
143     char *params=NULL, *cp;
144     /* No match */
145     char bigbuf[MAX_BUF];
146     int slen;
147    
148     /* Set up replyinfo before we modify any of the buffers - this is used
149     * if we don't find a match.
150     */
151     strcpy(bigbuf,"replyinfo ");
152     slen = strlen(bigbuf);
153     safe_strcat(bigbuf, buf, &slen, MAX_BUF);
154    
155     /* find the first space, make it null, and update the
156     * params pointer.
157     */
158     for (cp = buf; *cp != '\0'; cp++)
159     if (*cp==' ') {
160     *cp = '\0';
161     params = cp + 1;
162     break;
163     }
164     if (!strcmp(buf, "image_info")) send_image_info(ns, params);
165     else if (!strcmp(buf,"image_sums")) send_image_sums(ns, params);
166     else if (!strcmp(buf,"skill_info")) send_skill_info(ns, params);
167     else if (!strcmp(buf,"spell_paths")) send_spell_paths(ns, params);
168     else Write_String_To_Socket(ns, bigbuf, len);
169     }
170    
171     /**
172     * Handles old socket format.
173     */
174     void Handle_Oldsocket(NewSocket *ns)
175     {
176     int stat,i;
177     CommFunc command;
178     char buf[MAX_BUF],*cp;
179     object ob;
180     player pl;
181    
182     /* This is not the most efficient block, but keeps the code simpler -
183     * we basically read a byte at a time until we get a newline, error,
184     * or no more characters to read.
185     */
186     do {
187     if (ns->inbuf.len >= MAXSOCKBUF-1) {
188     ns->status = Ns_Dead;
189     LOG(llevDebug, "Old input socket sent too much data without newline\n");
190     return;
191     }
192     #ifdef WIN32 /* ***win32: change oldsocket read() to recv() */
193     stat = recv(ns->fd, ns->inbuf.buf + ns->inbuf.len, 1,0);
194    
195     if (stat==-1 && WSAGetLastError() !=WSAEWOULDBLOCK) {
196     #else
197     do {
198     stat = read(ns->fd, ns->inbuf.buf + ns->inbuf.len, 1);
199     } while ((stat<0) && (errno == EINTR));
200    
201     if (stat<0 && errno != EAGAIN && errno !=EWOULDBLOCK) {
202     #endif
203     LOG(llevError, "Cannot read from socket: %s\n", strerror_local(errno));
204     ns->status = Ns_Dead;
205     return;
206     }
207     if (stat == 0) return;
208     } while (ns->inbuf.buf[ns->inbuf.len++]!='\n');
209    
210     ns->inbuf.buf[ns->inbuf.len]=0;
211    
212     cp = strchr(ns->inbuf.buf, ' ');
213     if (cp) {
214     /* Replace the space with a null, skip any more spaces */
215     *cp++=0;
216     while (isspace(*cp)) cp++;
217     }
218    
219     /* Strip off all spaces and control characters from end of line */
220     for (i=ns->inbuf.len-1; i>=0; i--) {
221     if (ns->inbuf.buf[i]<=32) ns->inbuf.buf[i]=0;
222     else break;
223     }
224     ns->inbuf.len=0; /* reset for next read */
225    
226     /* If just a return, don't do anything */
227     if (ns->inbuf.buf[0] == 0) return;
228     if (!strcasecmp(ns->inbuf.buf,"quit")) {
229     ns->status = Ns_Dead;
230     return;
231     }
232     if (!strcasecmp(ns->inbuf.buf, "listen")) {
233     if (cp) {
234     char *buf="Socket switched to listen mode\n";
235    
236     free(ns->comment);
237     ns->comment = strdup_local(cp);
238     ns->old_mode = Old_Listen;
239     cs_write_string(ns, buf, strlen(buf));
240     } else {
241     char *buf="Need to supply a comment/url to listen\n";
242     cs_write_string(ns, buf, strlen(buf));
243     }
244     return;
245     }
246     if (!strcasecmp(ns->inbuf.buf, "name")) {
247     char *cp1=NULL;
248     if (cp) cp1= strchr(cp, ' ');
249     if (cp1) {
250     *cp1++ = 0;
251     while (isspace(*cp1)) cp1++;
252     }
253     if (!cp || !cp1) {
254     char *buf="Need to provide a name/password to name\n";
255     cs_write_string(ns, buf, strlen(buf));
256     return;
257     }
258    
259     if (verify_player(cp, cp1)==0) {
260     char *buf="Welcome back\n";
261     free(ns->comment);
262     ns->comment = strdup_local(cp);
263     ns->old_mode = Old_Player;
264     cs_write_string(ns, buf, strlen(buf));
265     }
266     else if (verify_player(cp, cp1)==2) {
267     ns->password_fails++;
268     if (ns->password_fails >= MAX_PASSWORD_FAILURES) {
269     char *buf="You failed to log in too many times, you will now be kicked.\n";
270     LOG(llevInfo, "A player connecting from %s in oldsocketmode has been dropped for password failure\n",
271     ns->host);
272     cs_write_string(ns, buf, strlen(buf));
273     ns->status = Ns_Dead;
274     }
275     else {
276     char *buf="Could not login you in. Check your name and password.\n";
277     cs_write_string(ns, buf, strlen(buf));
278     }
279     }
280     else {
281     char *buf="Could not login you in. Check your name and password.\n";
282     cs_write_string(ns, buf, strlen(buf));
283     }
284     return;
285     }
286    
287     command = find_oldsocket_command(ns->inbuf.buf);
288     if (!command && ns->old_mode==Old_Player) {
289     command = find_oldsocket_command2(ns->inbuf.buf);
290     }
291     if (!command) {
292     snprintf(buf, sizeof(buf), "Could not find command: %s\n", ns->inbuf.buf);
293     cs_write_string(ns, buf, strlen(buf));
294     return;
295     }
296    
297     /* This is a bit of a hack, but works. Basically, we make some
298     * fake object and player pointers and give at it.
299     * This works as long as the functions we are calling don't need
300     * to do anything to the object structure (ie, they are only
301     * outputting information and not actually updating anything much.)
302     */
303     ob.contr = &pl;
304     pl.ob = &ob;
305     ob.type = PLAYER;
306     pl.listening = 10;
307     pl.socket = *ns;
308     pl.outputs_count = 1;
309     ob.name = ns->comment;
310    
311     command(&ob, cp);
312     }
313    
314    
315     /**
316     * Handle client input.
317     *
318     * HandleClient is actually not named really well - we only get here once
319     * there is input, so we don't do exception or other stuff here.
320     * sock is the output socket information. pl is the player associated
321     * with this socket, null if no player (one of the init_sockets for just
322     * starting a connection)
323     */
324    
325     void HandleClient(NewSocket *ns, player *pl)
326     {
327     int len=0,i;
328     unsigned char *data;
329    
330     /* Loop through this - maybe we have several complete packets here. */
331     while (1) {
332     /* If it is a player, and they don't have any speed left, we
333     * return, and will read in the data when they do have time.
334     */
335     if (pl && pl->state==ST_PLAYING && pl->ob != NULL && pl->ob->speed_left < 0) {
336     return;
337     }
338    
339     if (ns->status == Ns_Old) {
340     Handle_Oldsocket(ns);
341     return;
342     }
343     i=SockList_ReadPacket(ns->fd, &ns->inbuf, MAXSOCKBUF-1);
344     /* Special hack - let the user switch to old mode if in the Ns_Add
345     * phase. Don't demand they add in the special length bytes
346     */
347     if (ns->status == Ns_Add) {
348     if (!strncasecmp(ns->inbuf.buf,"oldsocketmode", 13)) {
349     ns->status = Ns_Old;
350     ns->inbuf.len=0;
351     cs_write_string(ns, "Switched to old socket mode\n", 28);
352     LOG(llevDebug,"Switched socket to old socket mode\n");
353     return;
354     }
355     }
356    
357     if (i<0) {
358     #ifdef ESRV_DEBUG
359     LOG(llevDebug,"HandleClient: Read error on connection player %s\n", (pl?pl->ob->name:"None"));
360     #endif
361     /* Caller will take care of cleaning this up */
362     ns->status =Ns_Dead;
363     return;
364     }
365     /* Still dont have a full packet */
366     if (i==0) return;
367    
368     /* First, break out beginning word. There are at least
369     * a few commands that do not have any paremeters. If
370     * we get such a command, don't worry about trying
371     * to break it up.
372     */
373     data = (unsigned char *)strchr((char*)ns->inbuf.buf +2, ' ');
374     if (data) {
375     *data='\0';
376     data++;
377     len = ns->inbuf.len - (data - ns->inbuf.buf);
378     }
379     else len=0;
380    
381     ns->inbuf.buf[ns->inbuf.len]='\0'; /* Terminate buffer - useful for string data */
382     for (i=0; nscommands[i].cmdname !=NULL; i++) {
383     if (strcmp((char*)ns->inbuf.buf+2,nscommands[i].cmdname)==0) {
384     nscommands[i].cmdproc((char*)data,len,ns);
385     ns->inbuf.len=0;
386     return;
387     }
388     }
389     /* Player must be in the playing state or the flag on the
390     * the command must be zero for the user to use the command -
391     * otherwise, a player cam save, be in the play_again state, and
392     * the map they were on getsswapped out, yet things that try to look
393     * at the map causes a crash. If the command is valid, but
394     * one they can't use, we still swallow it up.
395     */
396     if (pl) for (i=0; plcommands[i].cmdname !=NULL; i++) {
397     if (strcmp((char*)ns->inbuf.buf+2,plcommands[i].cmdname)==0) {
398     if (pl->state == ST_PLAYING || plcommands[i].flag == 0)
399     plcommands[i].cmdproc((char*)data,len,pl);
400     ns->inbuf.len=0;
401     return;
402     }
403     }
404     /* If we get here, we didn't find a valid command. Logging
405     * this might be questionable, because a broken client/malicious
406     * user could certainly send a whole bunch of invalid commands.
407     */
408     LOG(llevDebug,"Bad command from client (%s)\n",ns->inbuf.buf+2);
409     }
410     }
411    
412    
413     /*****************************************************************************
414     *
415     * Low level socket looping - select calls and watchdog udp packet
416     * sending.
417     *
418     ******************************************************************************/
419    
420     #ifdef WATCHDOG
421     /**
422     * Tell watchdog that we are still alive
423     *
424     * I put the function here since we should hopefully already be getting
425     * all the needed include files for socket support
426     */
427    
428     void watchdog(void)
429     {
430     static int fd=-1;
431     static struct sockaddr_in insock;
432    
433     if (fd==-1)
434     {
435     struct protoent *protoent;
436    
437     if ((protoent=getprotobyname("udp"))==NULL ||
438     (fd=socket(PF_INET, SOCK_DGRAM, protoent->p_proto))==-1)
439     {
440     return;
441     }
442     insock.sin_family=AF_INET;
443     insock.sin_port=htons((unsigned short)13325);
444     insock.sin_addr.s_addr=inet_addr("127.0.0.1");
445     }
446     sendto(fd,(void *)&fd,1,0,(struct sockaddr *)&insock,sizeof(insock));
447     }
448     #endif
449    
450     extern unsigned long todtick;
451    
452     /** Waits for new connection */
453     static void block_until_new_connection(void)
454     {
455    
456     struct timeval Timeout;
457     fd_set readfs;
458     int cycles;
459    
460     LOG(llevInfo, "Waiting for connections...\n");
461    
462     cycles=1;
463     do {
464     /* Every minutes is a bit often for updates - especially if nothing is going
465     * on. This slows it down to every 6 minutes.
466     */
467     cycles++;
468     if (cycles%2 == 0)
469     tick_the_clock();
470    
471     FD_ZERO(&readfs);
472     FD_SET((uint32)init_sockets[0].fd, &readfs);
473    
474     /* If fastclock is set, we need to seriously slow down the updates
475     * to the metaserver as well as watchdog. Do same for flush_old_maps() -
476     * that is time sensitive, so there is no good reason to call it 2000 times
477     * a second.
478     */
479     if (settings.fastclock > 0) {
480     #ifdef WATCHDOG
481     if (cycles % 120000 == 0) {
482     watchdog();
483     flush_old_maps();
484     }
485     #endif
486     if (cycles == 720000) {
487     metaserver_update();
488     cycles=1;
489     }
490     Timeout.tv_sec=0;
491     Timeout.tv_usec=50;
492     } else {
493     Timeout.tv_sec=60;
494     Timeout.tv_usec=0;
495     if (cycles == 7) {
496     metaserver_update();
497     cycles=1;
498     }
499     flush_old_maps();
500     }
501     }
502     while (select(socket_info.max_filedescriptor, &readfs, NULL, NULL, &Timeout)==0);
503    
504     reset_sleep(); /* Or the game would go too fast */
505     }
506    
507    
508     /**
509     * This checks the sockets for input and exceptions, does the right thing.
510     *
511     * A bit of this code is grabbed out of socket.c
512     * There are 2 lists we need to look through - init_sockets is a list
513     *
514     */
515     void doeric_server(void)
516     {
517     int i, pollret;
518     fd_set tmp_read, tmp_exceptions, tmp_write;
519     struct sockaddr_in addr;
520     socklen_t addrlen=sizeof(struct sockaddr);
521     player *pl, *next;
522    
523     #ifdef CS_LOGSTATS
524     if ((time(NULL)-cst_lst.time_start)>=CS_LOGTIME)
525     write_cs_stats();
526     #endif
527    
528     FD_ZERO(&tmp_read);
529     FD_ZERO(&tmp_write);
530     FD_ZERO(&tmp_exceptions);
531    
532     for(i=0;i<socket_info.allocated_sockets;i++) {
533     if (init_sockets[i].status == Ns_Dead) {
534     free_newsocket(&init_sockets[i]);
535     init_sockets[i].status = Ns_Avail;
536     socket_info.nconns--;
537     } else if (init_sockets[i].status != Ns_Avail){
538     FD_SET((uint32)init_sockets[i].fd, &tmp_read);
539     FD_SET((uint32)init_sockets[i].fd, &tmp_write);
540     FD_SET((uint32)init_sockets[i].fd, &tmp_exceptions);
541     }
542     }
543    
544     /* Go through the players. Let the loop set the next pl value,
545     * since we may remove some
546     */
547     for (pl=first_player; pl!=NULL; ) {
548     if (pl->socket.status == Ns_Dead) {
549     player *npl=pl->next;
550    
551     save_player(pl->ob, 0);
552     if(!QUERY_FLAG(pl->ob,FLAG_REMOVED)) {
553     terminate_all_pets(pl->ob);
554     remove_ob(pl->ob);
555     }
556     leave(pl,1);
557     final_free_player(pl);
558     pl=npl;
559     }
560     else {
561     FD_SET((uint32)pl->socket.fd, &tmp_read);
562     FD_SET((uint32)pl->socket.fd, &tmp_write);
563     FD_SET((uint32)pl->socket.fd, &tmp_exceptions);
564     pl=pl->next;
565     }
566     }
567    
568     if (socket_info.nconns==1 && first_player==NULL)
569     block_until_new_connection();
570    
571     /* Reset timeout each time, since some OS's will change the values on
572     * the return from select.
573     */
574     socket_info.timeout.tv_sec = 0;
575     socket_info.timeout.tv_usec = 0;
576    
577     pollret= select(socket_info.max_filedescriptor, &tmp_read, &tmp_write,
578     &tmp_exceptions, &socket_info.timeout);
579    
580     if (pollret==-1) {
581     LOG(llevError, "select failed: %s\n", strerror_local(errno));
582     return;
583     }
584    
585     /* We need to do some of the processing below regardless */
586     /* if (!pollret) return;*/
587    
588     /* Following adds a new connection */
589     if (pollret && FD_ISSET(init_sockets[0].fd, &tmp_read)) {
590     int newsocknum=0;
591    
592     #ifdef ESRV_DEBUG
593     LOG(llevDebug,"doeric_server: New Connection\n");
594     #endif
595     /* If this is the case, all sockets currently in used */
596     if (socket_info.allocated_sockets <= socket_info.nconns) {
597     init_sockets = realloc(init_sockets,sizeof(NewSocket)*(socket_info.nconns+1));
598     if (!init_sockets) fatal(OUT_OF_MEMORY);
599     newsocknum = socket_info.allocated_sockets;
600     socket_info.allocated_sockets++;
601     init_sockets[newsocknum].faces_sent_len = nrofpixmaps;
602     init_sockets[newsocknum].faces_sent = malloc(nrofpixmaps*sizeof(*init_sockets[newsocknum].faces_sent));
603     if (!init_sockets[newsocknum].faces_sent) fatal(OUT_OF_MEMORY);
604     init_sockets[newsocknum].status = Ns_Avail;
605     }
606     else {
607     int j;
608    
609     for (j=1; j<socket_info.allocated_sockets; j++)
610     if (init_sockets[j].status == Ns_Avail) {
611     newsocknum=j;
612     break;
613     }
614     }
615     init_sockets[newsocknum].fd=accept(init_sockets[0].fd, (struct sockaddr *)&addr, &addrlen);
616     if (init_sockets[newsocknum].fd==-1) {
617     LOG(llevError, "accept failed: %s\n", strerror_local(errno));
618     }
619     else {
620     char buf[MAX_BUF];
621     long ip;
622     NewSocket *ns;
623    
624     ns = &init_sockets[newsocknum];
625    
626     ip = ntohl(addr.sin_addr.s_addr);
627     sprintf(buf, "%ld.%ld.%ld.%ld", (ip>>24)&255, (ip>>16)&255, (ip>>8)&255, ip&255);
628    
629     if (checkbanned(NULL, buf)) {
630     LOG(llevInfo, "Banned host tried to connect: [%s]\n", buf);
631     close(init_sockets[newsocknum].fd);
632     init_sockets[newsocknum].fd = -1;
633     }
634     else {
635     InitConnection(ns, buf);
636     socket_info.nconns++;
637     }
638     }
639     }
640    
641     /* Check for any exceptions/input on the sockets */
642     if (pollret) for(i=1;i<socket_info.allocated_sockets;i++) {
643     if (init_sockets[i].status == Ns_Avail) continue;
644     if (FD_ISSET(init_sockets[i].fd,&tmp_exceptions)) {
645     free_newsocket(&init_sockets[i]);
646     init_sockets[i].status = Ns_Avail;
647     socket_info.nconns--;
648     continue;
649     }
650     if (FD_ISSET(init_sockets[i].fd, &tmp_read)) {
651     HandleClient(&init_sockets[i], NULL);
652     }
653     if (FD_ISSET(init_sockets[i].fd, &tmp_write)) {
654     init_sockets[i].can_write=1;
655     }
656     }
657    
658     /* This does roughly the same thing, but for the players now */
659     for (pl=first_player; pl!=NULL; pl=next) {
660    
661     next=pl->next;
662     if (pl->socket.status==Ns_Dead) continue;
663    
664     if (FD_ISSET(pl->socket.fd,&tmp_write)) {
665     if (!pl->socket.can_write) {
666     #if 0
667     LOG(llevDebug,"Player %s socket now write enabled\n", pl->ob->name);
668     #endif
669     pl->socket.can_write=1;
670     write_socket_buffer(&pl->socket);
671     }
672     /* if we get an error on the write_socket buffer, no reason to
673     * continue on this socket.
674     */
675     if (pl->socket.status==Ns_Dead) continue;
676     }
677     else pl->socket.can_write=0;
678    
679     if (FD_ISSET(pl->socket.fd,&tmp_exceptions)) {
680     save_player(pl->ob, 0);
681     if(!QUERY_FLAG(pl->ob,FLAG_REMOVED)) {
682     terminate_all_pets(pl->ob);
683     remove_ob(pl->ob);
684     }
685     leave(pl,1);
686     final_free_player(pl);
687     }
688     else {
689     HandleClient(&pl->socket, pl);
690     /* If the player has left the game, then the socket status
691     * will be set to this be the leave function. We don't
692     * need to call leave again, as it has already been called
693     * once.
694     */
695     if (pl->socket.status==Ns_Dead) {
696     save_player(pl->ob, 0);
697     if(!QUERY_FLAG(pl->ob,FLAG_REMOVED)) {
698     terminate_all_pets(pl->ob);
699     remove_ob(pl->ob);
700     }
701     leave(pl,1);
702     final_free_player(pl);
703     } else {
704    
705     /* Update the players stats once per tick. More efficient than
706     * sending them whenever they change, and probably just as useful
707     */
708     esrv_update_stats(pl);
709     if (pl->last_weight != -1 && pl->last_weight != WEIGHT(pl->ob)) {
710     esrv_update_item(UPD_WEIGHT, pl->ob, pl->ob);
711     if(pl->last_weight != WEIGHT(pl->ob))
712     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));
713     }
714     if (pl->ob->map && pl->ob->map->in_memory==MAP_IN_MEMORY)
715     draw_client_map(pl->ob);
716     if (pl->socket.update_look) esrv_draw_look(pl->ob);
717     }
718     }
719     }
720     }