ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/lowlevel.c
Revision: 1.2
Committed: Sun May 21 23:46:39 2006 UTC (18 years ago) by root
Content type: text/plain
Branch: MAIN
Changes since 1.1: +17 -1 lines
Log Message:
Problem: server sends out 2 tcp packets per command (e.g. a 2-byte packet
+ a 10 byte packet for a small map1a command).

Solution: rewrite that thing

Workaround: on linux, uncork and cork again just before sleeping for the next
tick, that solves the problem without hackery or rewriting it. This cuts
the # of packets send by at least one half.

File Contents

# User Rev Content
1 root 1.1
2     /*
3     * static char *rcsid_sockets_c =
4 root 1.2 * "$Id$";
5 root 1.1 */
6    
7     /*
8     CrossFire, A Multiplayer game for X-windows
9    
10     Copyright (C) 1992 Frank Tore Johansen
11    
12     This program is free software; you can redistribute it and/or modify
13     it under the terms of the GNU General Public License as published by
14     the Free Software Foundation; either version 2 of the License, or
15     (at your option) any later version.
16    
17     This program is distributed in the hope that it will be useful,
18     but WITHOUT ANY WARRANTY; without even the implied warranty of
19     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20     GNU General Public License for more details.
21    
22     You should have received a copy of the GNU General Public License
23     along with this program; if not, write to the Free Software
24     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25    
26     The author can be reached via e-mail to mark@pyramid.com
27     */
28    
29     /**
30     * \file
31     * Low-level socket-related functions.
32     *
33     * \date 2003-12-02
34     *
35     * Contains some base functions that both the client and server
36     * can use. As such, depending what we are being compiled for will
37     * determine what we can include. the client is designed have
38     * CFCLIENT defined as part of its compile flags.
39     */
40    
41     #include <global.h>
42     #include <newclient.h>
43     #include <sproto.h>
44    
45 root 1.2 #ifdef __linux__
46     # include <sys/types.h>
47     # include <sys/socket.h>
48     # include <netinet/in.h>
49     # include <netinet/tcp.h>
50     #endif
51    
52     void Socket_Flush (NewSocket *ns)
53     {
54     #ifdef __linux__
55     int val;
56     val = 0; setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val));
57     val = 1; setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val));
58     #endif
59     }
60    
61 root 1.1 /***********************************************************************
62     *
63     * SockList functions/utilities
64     *
65     **********************************************************************/
66    
67     void SockList_Init(SockList *sl)
68     {
69     sl->len=0;
70     sl->buf=NULL;
71     }
72    
73     void SockList_AddChar(SockList *sl, char c)
74     {
75     sl->buf[sl->len]=c;
76     sl->len++;
77     }
78    
79     void SockList_AddShort(SockList *sl, uint16 data)
80     {
81     sl->buf[sl->len++]= (data>>8)&0xff;
82     sl->buf[sl->len++] = data & 0xff;
83     }
84    
85    
86     void SockList_AddInt(SockList *sl, uint32 data)
87     {
88     sl->buf[sl->len++]= (data>>24)&0xff;
89     sl->buf[sl->len++]= (data>>16)&0xff;
90     sl->buf[sl->len++]= (data>>8)&0xff;
91     sl->buf[sl->len++] = data & 0xff;
92     }
93    
94     void SockList_AddInt64(SockList *sl, uint64 data)
95     {
96     sl->buf[sl->len++]= ( char )( (data>>56)&0xff );
97     sl->buf[sl->len++]= ( char )( (data>>48)&0xff );
98     sl->buf[sl->len++]= ( char )( (data>>40)&0xff );
99     sl->buf[sl->len++]= ( char )( (data>>32)&0xff );
100    
101     sl->buf[sl->len++]= ( char )( (data>>24)&0xff );
102     sl->buf[sl->len++]= ( char )( (data>>16)&0xff );
103     sl->buf[sl->len++]= ( char )( (data>>8)&0xff );
104     sl->buf[sl->len++] =( char )( data & 0xff );
105     }
106    
107     /* Basically does the reverse of SockList_AddInt, but on
108     * strings instead. Same for the GetShort, but for 16 bits.
109     */
110     int GetInt_String(unsigned char *data)
111     {
112     return ((data[0]<<24) + (data[1]<<16) + (data[2]<<8) + data[3]);
113     }
114    
115     short GetShort_String(unsigned char *data) {
116     return ((data[0]<<8)+data[1]);
117     }
118    
119     /******************************************************************************
120     *
121     * Start of read routines.
122     *
123     ******************************************************************************/
124    
125     /**
126     * This reads from fd and puts the data in sl. We return true if we think
127     * we have a full packet, 0 if we have a partial packet. The only processing
128     * we do is remove the intial size value. len (As passed) is the size of the
129     * buffer allocated in the socklist. We make the assumption the buffer is
130     * at least 2 bytes long.
131     */
132    
133     int SockList_ReadPacket(int fd, SockList *sl, int len)
134     {
135     int stat,toread;
136     extern int errno;
137    
138     /* Sanity check - shouldn't happen */
139     if (sl->len < 0) {
140     abort();
141     }
142     /* We already have a partial packet */
143     if (sl->len<2) {
144     #ifdef WIN32 /* ***WIN32 SockList_ReadPacket: change read() to recv() */
145    
146     stat=recv(fd, sl->buf + sl->len, 2-sl->len,0);
147    
148     #else
149     do {
150     stat=read(fd, sl->buf + sl->len, 2-sl->len);
151     } while ((stat==-1) && (errno==EINTR));
152     #endif
153     if (stat<0) {
154     /* In non blocking mode, EAGAIN is set when there is no
155     * data available.
156     */
157     #ifdef WIN32 /* ***WIN32 SockList_ReadPacket: error handling for win32 */
158     if ((stat==-1) && WSAGetLastError() !=WSAEWOULDBLOCK) {
159     if(WSAGetLastError() == WSAECONNRESET)
160     LOG(llevDebug,"Connection closed by client\n");
161     else
162     {
163     LOG(llevDebug,"ReadPacket got error %d, returning 0\n",WSAGetLastError());
164     }
165     return -1; /* kick this user! */
166     }
167     #else
168     if (errno != EAGAIN && errno !=EWOULDBLOCK) {
169     LOG(llevDebug, "ReadPacket got error %s, returning 0\n", strerror_local(errno));
170     }
171     #endif
172     return 0; /*Error */
173     }
174     if (stat==0) return -1;
175     sl->len += stat;
176     #ifdef CS_LOGSTATS
177     cst_tot.ibytes += stat;
178     cst_lst.ibytes += stat;
179     #endif
180     if (stat<2) return 0; /* Still don't have a full packet */
181     }
182     /* Figure out how much more data we need to read. Add 2 from the
183     * end of this - size header information is not included.
184     */
185     toread = 2+(sl->buf[0] << 8) + sl->buf[1] - sl->len;
186     if ((toread + sl->len) >= len) {
187     LOG(llevError,"SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n",
188     toread + sl->len, len);
189     /* Quick hack in case for 'oldsocketmode' input. If we are
190     * closing the socket anyways, then reading this extra 100 bytes
191     * shouldn't hurt.
192     */
193     #ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */
194     recv(fd, sl->buf+2, 100, 0);
195     #else
196     read(fd, sl->buf+2, 100);
197     #endif /* end win32 */
198    
199     /* return error so the socket is closed */
200     return -1;
201     }
202     do {
203     #ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */
204     stat = recv(fd, sl->buf+ sl->len, toread, 0);
205     #else
206     do {
207     stat = read(fd, sl->buf+ sl->len, toread);
208     } while ((stat<0) && (errno==EINTR));
209     #endif
210     if (stat<0) {
211    
212     #ifdef WIN32 /* ***win32 SockList_ReadPacket: change error handling for win32 */
213     if ((stat==-1) && WSAGetLastError() !=WSAEWOULDBLOCK) {
214     if(WSAGetLastError() == WSAECONNRESET)
215     LOG(llevDebug,"Connection closed by client\n");
216     else
217     {
218     LOG(llevDebug,"ReadPacket got error %d, returning 0\n",WSAGetLastError());
219     }
220     return -1; /* kick this user! */
221     }
222     #else
223     if (errno != EAGAIN && errno !=EWOULDBLOCK) {
224     LOG(llevDebug, "ReadPacket got error %s, returning 0\n", strerror_local(errno));
225     }
226     #endif
227     return 0; /*Error */
228     }
229     if (stat==0) return -1;
230     sl->len += stat;
231     #ifdef CS_LOGSTATS
232     cst_tot.ibytes += stat;
233     cst_lst.ibytes += stat;
234     #endif
235     toread -= stat;
236     if (toread==0) return 1;
237     if (toread < 0) {
238     LOG(llevError,"SockList_ReadPacket: Read more bytes than desired.\n");
239     return 1;
240     }
241     } while (toread>0);
242     return 0;
243     }
244    
245     /*******************************************************************************
246     *
247     * Start of write related routines.
248     *
249     ******************************************************************************/
250    
251     /**
252     * Adds data to a socket buffer for whatever reason.
253     *
254     * ns is the socket we are adding the data to, buf is the start of the
255     * data, and len is the number of bytes to add.
256     */
257    
258     static void add_to_buffer(NewSocket *ns, unsigned char *buf, int len)
259     {
260     int avail, end;
261    
262     if ((len+ns->outputbuffer.len)>SOCKETBUFSIZE) {
263     LOG(llevDebug,"Socket on fd %d has overrun internal buffer - marking as dead\n",
264     ns->fd);
265     ns->status = Ns_Dead;
266     return;
267     }
268    
269     /* data + end is where we start putting the new data. The last byte
270     * currently in use is actually data + end -1
271     */
272    
273     end=ns->outputbuffer.start + ns->outputbuffer.len;
274     /* The buffer is already in a wrapped state, so adjust end */
275     if (end>=SOCKETBUFSIZE) end-=SOCKETBUFSIZE;
276     avail=SOCKETBUFSIZE - end;
277    
278     /* We can all fit it behind the current data without wrapping */
279     if (avail >=len ) {
280     memcpy(ns->outputbuffer.data + end, buf, len);
281     }
282     else {
283     memcpy(ns->outputbuffer.data + end, buf, avail);
284     memcpy(ns->outputbuffer.data, buf+avail, len-avail);
285     }
286     ns->outputbuffer.len += len;
287     #if 0
288     LOG(llevDebug,"Added %d to output buffer, total length now %d, start=%d\n", len,
289     ns->outputbuffer.len, ns->outputbuffer.start);
290     #endif
291     }
292    
293     /**
294     * Writes data to socket.
295     *
296     * When the socket is clear to write, and we have backlogged data, this
297     * is called to write it out.
298     */
299     void write_socket_buffer(NewSocket *ns)
300     {
301     int amt, max;
302    
303     if (ns->outputbuffer.len==0) {
304     LOG(llevDebug,"write_socket_buffer called when there is no data, fd=%d\n",
305     ns->fd);
306     return;
307     }
308    
309     do {
310     max = SOCKETBUFSIZE - ns->outputbuffer.start;
311     if (ns->outputbuffer.len<max) max = ns->outputbuffer.len;
312    
313     #ifdef WIN32 /* ***win32 write_socket_buffer: change write() to send() */
314     amt=send(ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max,0);
315     #else
316     do {
317     amt=write(ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max);
318     } while ((amt<0) && (errno==EINTR));
319     #endif
320    
321     if (amt < 0) { /* We got an error */
322    
323     #ifdef WIN32 /* ***win32 write_socket_buffer: change error handling */
324     if (amt == -1 && WSAGetLastError() !=WSAEWOULDBLOCK) {
325     LOG(llevError,"New socket write failed (wsb) (%d).\n", WSAGetLastError());
326     #else
327     if (errno !=EWOULDBLOCK) {
328     LOG(llevError,"New socket write failed (wsb) (%d: %s).\n",
329     errno, strerror_local(errno));
330     #endif
331     ns->status=Ns_Dead;
332     return;
333     }
334     else { /* EWOULDBLOCK */
335     /* can't write it, so store it away. */
336     ns->can_write=0;
337     return;
338     }
339     }
340     ns->outputbuffer.start += amt;
341     /* wrap back to start of buffer */
342     if (ns->outputbuffer.start==SOCKETBUFSIZE) ns->outputbuffer.start=0;
343     ns->outputbuffer.len -= amt;
344     #ifdef CS_LOGSTATS
345     cst_tot.obytes += amt;
346     cst_lst.obytes += amt;
347     #endif
348     } while (ns->outputbuffer.len>0);
349     }
350    
351     /**
352     * This writes data to the socket. - It is very low level -
353     * all we try and do is write out the data to the socket
354     * provided (ns). buf is the data to write, len is the number
355     * of bytes to write. IT doesn't return anything - rather, it
356     * updates the ns structure if we get an error.
357     */
358     void Write_To_Socket(NewSocket *ns, unsigned char *buf, int len)
359     {
360     int amt=0;
361     unsigned char *pos=buf;
362    
363     if (ns->status == Ns_Dead || !buf) {
364     LOG(llevDebug,"Write_To_Socket called with dead socket\n");
365     return;
366     }
367    
368     #ifndef __GNU__ /* This caused problems on Hurd */
369     if (!ns->can_write) {
370     add_to_buffer(ns, buf, len);
371     return;
372     }
373     #endif
374     /* If we manage to write more than we wanted, take it as a bonus */
375     while (len>0) {
376    
377     #ifdef WIN32 /* ***win32 Write_To_Socket: change write() to send() */
378     amt=send(ns->fd, pos, len,0);
379     #else
380     do {
381     amt=write(ns->fd, pos, len);
382     } while ((amt<0) && (errno==EINTR));
383     #endif
384    
385     if (amt < 0) { /* We got an error */
386     #ifdef WIN32 /* ***win32 Write_To_Socket: change error handling */
387     if (amt == -1 && WSAGetLastError() !=WSAEWOULDBLOCK) {
388     LOG(llevError,"New socket write failed WTS (%d).\n",WSAGetLastError());
389     #else
390     if (errno !=EWOULDBLOCK) {
391     LOG(llevError,"New socket write failed WTS (%d: %s).\n", /* ---WIN32 */
392     errno, strerror_local(errno));
393     #endif
394     ns->status=Ns_Dead;
395     return;
396     }
397     else { /* EWOULDBLOCK */
398     /* can't write it, so store it away. */
399     add_to_buffer(ns, pos, len);
400     ns->can_write=0;
401     return;
402     }
403     }
404     /* amt gets set to 0 above in blocking code, so we do this as
405     * an else if to make sure we don't reprocess it.
406     */
407     else if (amt==0) {
408     LOG(llevError,"Write_To_Socket: No data written out.\n");
409     }
410     len -= amt;
411     pos += amt;
412     #ifdef CS_LOGSTATS
413     cst_tot.obytes += amt;
414     cst_lst.obytes += amt;
415     #endif
416     }
417     }
418    
419    
420     /**
421     * Takes a string of data, and writes it out to the socket. A very handy
422     * shortcut function.
423     */
424     void cs_write_string(NewSocket *ns, const char *buf, int len)
425     {
426     SockList sl;
427    
428     sl.len = len;
429     sl.buf = (unsigned char*)buf;
430     Send_With_Handling(ns, &sl);
431     }
432    
433    
434     /**
435     * Calls Write_To_Socket to send data to the client.
436     *
437     * The only difference in this function is that we take a SockList
438     *, and we prepend the length information.
439     */
440     void Send_With_Handling(NewSocket *ns,SockList *msg)
441     {
442     unsigned char sbuf[4];
443    
444     if (ns->status == Ns_Dead || !msg)
445     return;
446    
447     if (msg->len >= MAXSOCKBUF) {
448     LOG(llevError,"Trying to send a buffer beyond properly size, len =%d\n",
449     msg->len);
450     /* Almost certainly we've overflowed a buffer, so quite now to make
451     * it easier to debug.
452     */
453     abort();
454     }
455     sbuf[0] = ((uint32)(msg->len) >> 8) & 0xFF;
456     sbuf[1] = ((uint32)(msg->len)) & 0xFF;
457     if (ns->status != Ns_Old)
458     Write_To_Socket(ns, sbuf, 2);
459     Write_To_Socket(ns, msg->buf, msg->len);
460     }
461    
462     /**
463     * Takes a string of data, and writes it out to the socket. A very handy
464     * shortcut function.
465     */
466     void Write_String_To_Socket(NewSocket *ns, char *buf, int len)
467     {
468     SockList sl;
469    
470     sl.len = len;
471     sl.buf = (uint8*)buf;
472     Send_With_Handling(ns, &sl);
473     }
474    
475    
476     /******************************************************************************
477     *
478     * statistics logging functions.
479     *
480     ******************************************************************************/
481    
482     #ifdef CS_LOGSTATS
483     /* cst_tot is for the life of the server, cst_last is for the last series of
484     * stats
485     */
486     CS_Stats cst_tot, cst_lst;
487    
488     /**
489     * Writes out the gathered stats. We clear cst_lst.
490     */
491     void write_cs_stats(void)
492     {
493     time_t now=time(NULL);
494    
495     /* If no connections recently, don't both to log anything */
496     if (cst_lst.ibytes==0 && cst_lst.obytes==0) return;
497    
498     /* CSSTAT is put in so scripts can easily find the line */
499     LOG(llevInfo, "CSSTAT: %.16s tot %d %d %d %d inc %d %d %d %d\n",
500     ctime(&now), cst_tot.ibytes, cst_tot.obytes, cst_tot.max_conn,
501     now - cst_tot.time_start, cst_lst.ibytes, cst_lst.obytes,
502     cst_lst.max_conn, now - cst_lst.time_start);
503     cst_lst.ibytes=0;
504     cst_lst.obytes=0;
505     cst_lst.max_conn=socket_info.nconns;
506     cst_lst.time_start=now;
507     }
508     #endif