ViewVC Help
View File | Revision Log | Show Annotations | Download File
/cvs/deliantra/server/socket/lowlevel.C
(Generate patch)

Comparing deliantra/server/socket/lowlevel.C (file contents):
Revision 1.5 by root, Sun Sep 10 13:43:33 2006 UTC vs.
Revision 1.11 by root, Wed Dec 13 21:27:09 2006 UTC

1
2/*
3 * static char *rcsid_sockets_c =
4 * "$Id: lowlevel.C,v 1.5 2006/09/10 13:43:33 root Exp $";
5 */
6
7/* 1/*
8 CrossFire, A Multiplayer game for X-windows 2 CrossFire, A Multiplayer game for X-windows
9 3
10 Copyright (C) 1992 Frank Tore Johansen 4 Copyright (C) 1992 Frank Tore Johansen
11 5
41using namespace std; 35using namespace std;
42 36
43#include <global.h> 37#include <global.h>
44#include <newclient.h> 38#include <newclient.h>
45#include <sproto.h> 39#include <sproto.h>
40#include <cstdarg>
46 41
47#ifdef __linux__ 42#ifdef __linux__
48# include <sys/types.h> 43# include <sys/types.h>
49# include <sys/socket.h> 44# include <sys/socket.h>
50# include <netinet/in.h> 45# include <netinet/in.h>
78 } 73 }
79 } 74 }
80 75
81 int val; 76 int val;
82 77
83 val = 0;
84 setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); 78 val = 0; setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val));
85 val = 1;
86 setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); 79 val = 1; setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val));
87#endif 80#endif
88} 81}
89 82
90/*********************************************************************** 83/***********************************************************************
91 * 84 *
92 * SockList functions/utilities 85 * SockList functions/utilities
93 * 86 *
94 **********************************************************************/ 87 **********************************************************************/
95 88
96void 89SockList &SockList::operator <<(const data &v)
97SockList_Init (SockList * sl)
98{ 90{
99 sl->len = 0; 91 if (v.len)
100 sl->buf = NULL; 92 {
101} 93 memcpy (buf + len, v.ptr, v.len);
94 len += v.len;
95 }
102 96
103void 97 return *this;
104SockList_AddInt (SockList * sl, uint32 data)
105{
106 sl->buf[sl->len++] = (data >> 24) & 0xff;
107 sl->buf[sl->len++] = (data >> 16) & 0xff;
108 sl->buf[sl->len++] = (data >> 8) & 0xff;
109 sl->buf[sl->len++] = data & 0xff;
110} 98}
111 99
112void 100SockList &SockList::operator <<(const data8 &v)
113SockList_AddInt64 (SockList * sl, uint64 data)
114{ 101{
115 sl->buf[sl->len++] = (char) ((data >> 56) & 0xff); 102 unsigned int len = min (v.len, 0x00FF);
116 sl->buf[sl->len++] = (char) ((data >> 48) & 0xff); 103 return *this << uint8 (len) << data (v.ptr, len);
117 sl->buf[sl->len++] = (char) ((data >> 40) & 0xff); 104}
118 sl->buf[sl->len++] = (char) ((data >> 32) & 0xff);
119 105
120 sl->buf[sl->len++] = (char) ((data >> 24) & 0xff); 106SockList &SockList::operator <<(const data16 &v)
121 sl->buf[sl->len++] = (char) ((data >> 16) & 0xff); 107{
122 sl->buf[sl->len++] = (char) ((data >> 8) & 0xff); 108 unsigned int len = min (v.len, 0xFFFF);
123 sl->buf[sl->len++] = (char) (data & 0xff); 109 return *this << uint16 (len) << data (v.ptr, len);
110}
111
112SockList &SockList::operator <<(const char *v)
113{
114 return *this << data (v, strlen (v ? v : 0));
115}
116
117void
118SockList::printf (const char *format, ...)
119{
120 va_list ap;
121 va_start (ap, format);
122
123 len += vsprintf ((char *)buf + len, format, ap);
124
125 va_end (ap);
124} 126}
125 127
126/* Basically does the reverse of SockList_AddInt, but on 128/* Basically does the reverse of SockList_AddInt, but on
127 * strings instead. Same for the GetShort, but for 16 bits. 129 * strings instead. Same for the GetShort, but for 16 bits.
128 */ 130 */
151 * buffer allocated in the socklist. We make the assumption the buffer is 153 * buffer allocated in the socklist. We make the assumption the buffer is
152 * at least 2 bytes long. 154 * at least 2 bytes long.
153 */ 155 */
154 156
155int 157int
156SockList_ReadPacket (int fd, SockList * sl, int len) 158SockList_ReadPacket (int fd, SockList *sl, int len)
157{ 159{
158 int stat, toread; 160 int stat, toread;
159 161
160 /* Sanity check - shouldn't happen */ 162 /* Sanity check - shouldn't happen */
161 if (sl->len < 0) 163 if (sl->len < 0)
163 abort (); 165 abort ();
164 } 166 }
165 /* We already have a partial packet */ 167 /* We already have a partial packet */
166 if (sl->len < 2) 168 if (sl->len < 2)
167 { 169 {
168#ifdef WIN32 /* ***WIN32 SockList_ReadPacket: change read() to recv() */
169
170 stat = recv (fd, sl->buf + sl->len, 2 - sl->len, 0);
171
172#else
173 do 170 do
174 { 171 {
175 stat = read (fd, sl->buf + sl->len, 2 - sl->len); 172 stat = read (fd, sl->buf + sl->len, 2 - sl->len);
176 } 173 }
177 while ((stat == -1) && (errno == EINTR)); 174 while ((stat == -1) && (errno == EINTR));
178#endif 175
179 if (stat < 0) 176 if (stat < 0)
180 { 177 {
181 /* In non blocking mode, EAGAIN is set when there is no 178 /* In non blocking mode, EAGAIN is set when there is no
182 * data available. 179 * data available.
183 */ 180 */
184#ifdef WIN32 /* ***WIN32 SockList_ReadPacket: error handling for win32 */
185 if ((stat == -1) && WSAGetLastError () != WSAEWOULDBLOCK)
186 {
187 if (WSAGetLastError () == WSAECONNRESET)
188 LOG (llevDebug, "Connection closed by client\n");
189 else
190 {
191 LOG (llevDebug, "ReadPacket got error %d, returning 0\n", WSAGetLastError ());
192 }
193 return -1; /* kick this user! */
194 }
195#else
196 if (errno != EAGAIN && errno != EWOULDBLOCK) 181 if (errno != EAGAIN && errno != EWOULDBLOCK)
197 { 182 {
198 LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); 183 LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno));
199 } 184 }
200#endif
201 return 0; /*Error */ 185 return 0; /*Error */
202 } 186 }
203 if (stat == 0) 187 if (stat == 0)
204 return -1; 188 return -1;
205 sl->len += stat; 189 sl->len += stat;
219 LOG (llevError, "SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n", toread + sl->len, len); 203 LOG (llevError, "SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n", toread + sl->len, len);
220 /* Quick hack in case for 'oldsocketmode' input. If we are 204 /* Quick hack in case for 'oldsocketmode' input. If we are
221 * closing the socket anyways, then reading this extra 100 bytes 205 * closing the socket anyways, then reading this extra 100 bytes
222 * shouldn't hurt. 206 * shouldn't hurt.
223 */ 207 */
224#ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */
225 recv (fd, sl->buf + 2, 100, 0);
226#else
227 read (fd, sl->buf + 2, 100); 208 read (fd, sl->buf + 2, 100);
228#endif /* end win32 */
229 209
230 /* return error so the socket is closed */ 210 /* return error so the socket is closed */
231 return -1; 211 return -1;
232 } 212 }
233 do 213 do
234 { 214 {
235#ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */
236 stat = recv (fd, sl->buf + sl->len, toread, 0);
237#else
238 do 215 do
239 { 216 {
240 stat = read (fd, sl->buf + sl->len, toread); 217 stat = read (fd, sl->buf + sl->len, toread);
241 } 218 }
242 while ((stat < 0) && (errno == EINTR)); 219 while ((stat < 0) && (errno == EINTR));
243#endif
244 if (stat < 0) 220 if (stat < 0)
245 { 221 {
246 222
247#ifdef WIN32 /* ***win32 SockList_ReadPacket: change error handling for win32 */
248 if ((stat == -1) && WSAGetLastError () != WSAEWOULDBLOCK)
249 {
250 if (WSAGetLastError () == WSAECONNRESET)
251 LOG (llevDebug, "Connection closed by client\n");
252 else
253 {
254 LOG (llevDebug, "ReadPacket got error %d, returning 0\n", WSAGetLastError ());
255 }
256 return -1; /* kick this user! */
257 }
258#else
259 if (errno != EAGAIN && errno != EWOULDBLOCK) 223 if (errno != EAGAIN && errno != EWOULDBLOCK)
260 { 224 {
261 LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); 225 LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno));
262 } 226 }
263#endif
264 return 0; /*Error */ 227 return 0; /*Error */
265 } 228 }
266 if (stat == 0) 229 if (stat == 0)
267 return -1; 230 return -1;
268 sl->len += stat; 231 sl->len += stat;
314 277
315 end = ns->outputbuffer.start + ns->outputbuffer.len; 278 end = ns->outputbuffer.start + ns->outputbuffer.len;
316 /* The buffer is already in a wrapped state, so adjust end */ 279 /* The buffer is already in a wrapped state, so adjust end */
317 if (end >= SOCKETBUFSIZE) 280 if (end >= SOCKETBUFSIZE)
318 end -= SOCKETBUFSIZE; 281 end -= SOCKETBUFSIZE;
282
319 avail = SOCKETBUFSIZE - end; 283 avail = SOCKETBUFSIZE - end;
320 284
321 /* We can all fit it behind the current data without wrapping */ 285 /* We can all fit it behind the current data without wrapping */
322 if (avail >= len) 286 if (avail >= len)
323 {
324 memcpy (ns->outputbuffer.data + end, buf, len); 287 memcpy (ns->outputbuffer.data + end, buf, len);
325 }
326 else 288 else
327 { 289 {
328 memcpy (ns->outputbuffer.data + end, buf, avail); 290 memcpy (ns->outputbuffer.data + end, buf, avail);
329 memcpy (ns->outputbuffer.data, buf + avail, len - avail); 291 memcpy (ns->outputbuffer.data, buf + avail, len - avail);
330 } 292 }
293
331 ns->outputbuffer.len += len; 294 ns->outputbuffer.len += len;
332#if 0 295#if 0
333 LOG (llevDebug, "Added %d to output buffer, total length now %d, start=%d\n", len, ns->outputbuffer.len, ns->outputbuffer.start); 296 LOG (llevDebug, "Added %d to output buffer, total length now %d, start=%d\n", len, ns->outputbuffer.len, ns->outputbuffer.start);
334#endif 297#endif
335} 298}
355 { 318 {
356 max = SOCKETBUFSIZE - ns->outputbuffer.start; 319 max = SOCKETBUFSIZE - ns->outputbuffer.start;
357 if (ns->outputbuffer.len < max) 320 if (ns->outputbuffer.len < max)
358 max = ns->outputbuffer.len; 321 max = ns->outputbuffer.len;
359 322
360#ifdef WIN32 /* ***win32 write_socket_buffer: change write() to send() */
361 amt = send (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max, 0);
362#else
363 do 323 do
364 { 324 {
365 amt = write (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max); 325 amt = write (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max);
366 } 326 }
367 while ((amt < 0) && (errno == EINTR)); 327 while ((amt < 0) && (errno == EINTR));
368#endif
369 328
370 if (amt < 0) 329 if (amt < 0)
371 { /* We got an error */ 330 { /* We got an error */
372 331
373#ifdef WIN32 /* ***win32 write_socket_buffer: change error handling */
374 if (amt == -1 && WSAGetLastError () != WSAEWOULDBLOCK)
375 {
376 LOG (llevError, "New socket write failed (wsb) (%d).\n", WSAGetLastError ());
377#else
378 if (errno != EWOULDBLOCK) 332 if (errno != EWOULDBLOCK)
379 { 333 {
380 LOG (llevError, "New socket write failed (wsb) (%d: %s).\n", errno, strerror (errno)); 334 LOG (llevError, "New socket write failed (wsb) (%d: %s).\n", errno, strerror (errno));
381#endif
382 ns->status = Ns_Dead; 335 ns->status = Ns_Dead;
383 return; 336 return;
384 } 337 }
385 else 338 else
386 { /* EWOULDBLOCK */ 339 { /* EWOULDBLOCK */
430#endif 383#endif
431 /* If we manage to write more than we wanted, take it as a bonus */ 384 /* If we manage to write more than we wanted, take it as a bonus */
432 while (len > 0) 385 while (len > 0)
433 { 386 {
434 387
435#ifdef WIN32 /* ***win32 Write_To_Socket: change write() to send() */
436 amt = send (ns->fd, pos, len, 0);
437#else
438 do 388 do
439 { 389 {
440 amt = write (ns->fd, pos, len); 390 amt = write (ns->fd, pos, len);
441 } 391 }
442 while ((amt < 0) && (errno == EINTR)); 392 while ((amt < 0) && (errno == EINTR));
443#endif
444 393
445 if (amt < 0) 394 if (amt < 0)
446 { /* We got an error */ 395 { /* We got an error */
447#ifdef WIN32 /* ***win32 Write_To_Socket: change error handling */
448 if (amt == -1 && WSAGetLastError () != WSAEWOULDBLOCK)
449 {
450 LOG (llevError, "New socket write failed WTS (%d).\n", WSAGetLastError ());
451#else
452 if (errno != EWOULDBLOCK) 396 if (errno != EWOULDBLOCK)
453 { 397 {
454 LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ 398 LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */
455 errno, strerror (errno)); 399 errno, strerror (errno));
456#endif
457 ns->status = Ns_Dead; 400 ns->status = Ns_Dead;
458 return; 401 return;
459 } 402 }
460 else 403 else
461 { /* EWOULDBLOCK */ 404 { /* EWOULDBLOCK */
485/** 428/**
486 * Takes a string of data, and writes it out to the socket. A very handy 429 * Takes a string of data, and writes it out to the socket. A very handy
487 * shortcut function. 430 * shortcut function.
488 */ 431 */
489void 432void
490cs_write_string (NewSocket * ns, const char *buf, int len) 433cs_write_string (NewSocket *ns, const char *buf, int len)
491{ 434{
492 SockList sl; 435 SockList sl;
493 436
494 sl.len = len; 437 sl.len = len;
495 sl.buf = (unsigned char *) buf; 438 sl.buf = (unsigned char *) buf;
502 * 445 *
503 * The only difference in this function is that we take a SockList 446 * The only difference in this function is that we take a SockList
504 *, and we prepend the length information. 447 *, and we prepend the length information.
505 */ 448 */
506void 449void
507Send_With_Handling (NewSocket * ns, SockList * msg) 450Send_With_Handling (NewSocket *ns, SockList *msg)
508{ 451{
509 unsigned char sbuf[4]; 452 unsigned char sbuf[4];
510 453
511 if (ns->status == Ns_Dead || !msg) 454 if (ns->status == Ns_Dead || !msg)
512 return; 455 return;
529/** 472/**
530 * Takes a string of data, and writes it out to the socket. A very handy 473 * Takes a string of data, and writes it out to the socket. A very handy
531 * shortcut function. 474 * shortcut function.
532 */ 475 */
533void 476void
534Write_String_To_Socket (NewSocket * ns, char *buf, int len) 477Write_String_To_Socket (NewSocket *ns, char *buf, int len)
535{ 478{
536 SockList sl; 479 SockList sl;
537 480
538 sl.len = len; 481 sl.len = len;
539 sl.buf = (unsigned char *) buf; 482 sl.buf = (unsigned char *) buf;
574 cst_lst.obytes = 0; 517 cst_lst.obytes = 0;
575 cst_lst.max_conn = socket_info.nconns; 518 cst_lst.max_conn = socket_info.nconns;
576 cst_lst.time_start = now; 519 cst_lst.time_start = now;
577} 520}
578#endif 521#endif
522

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines