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.14 by root, Thu Dec 14 01:21:58 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 * packet functions/utilities
93 * 86 *
94 **********************************************************************/ 87 **********************************************************************/
95 88
96void 89packet &packet::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 100packet &packet::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);
118 sl->buf[sl->len++] = (char) ((data >> 32) & 0xff);
119
120 sl->buf[sl->len++] = (char) ((data >> 24) & 0xff);
121 sl->buf[sl->len++] = (char) ((data >> 16) & 0xff);
122 sl->buf[sl->len++] = (char) ((data >> 8) & 0xff);
123 sl->buf[sl->len++] = (char) (data & 0xff);
124} 104}
125 105
126/* Basically does the reverse of SockList_AddInt, but on 106packet &packet::operator <<(const data16 &v)
127 * strings instead. Same for the GetShort, but for 16 bits. 107{
108 unsigned int len = min (v.len, 0xFFFF);
109 return *this << uint16 (len) << data (v.ptr, len);
110}
111
112packet &packet::operator <<(const char *v)
113{
114 return *this << data (v, strlen (v ? v : 0));
115}
116
117void
118packet::printf (const char *format, ...)
119{
120 va_list ap;
121 va_start (ap, format);
122
123 len += vsnprintf ((char *)buf + len, MAXSOCKBUF, format, ap);
124
125 va_end (ap);
126}
127
128/******************************************************************************
128 */ 129 *
130 * Start of read routines.
131 *
132 ******************************************************************************/
133
129int 134int
130GetInt_String (unsigned char *data) 135NewSocket::read_packet ()
131{ 136{
132 return ((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]); 137 for (;;)
133}
134
135short
136GetShort_String (unsigned char *data)
137{
138 return ((data[0] << 8) + data[1]);
139}
140
141/******************************************************************************
142 *
143 * Start of read routines.
144 *
145 ******************************************************************************/
146
147/**
148 * This reads from fd and puts the data in sl. We return true if we think
149 * we have a full packet, 0 if we have a partial packet. The only processing
150 * we do is remove the intial size value. len (As passed) is the size of the
151 * buffer allocated in the socklist. We make the assumption the buffer is
152 * at least 2 bytes long.
153 */
154
155int
156SockList_ReadPacket (int fd, SockList * sl, int len)
157{
158 int stat, toread;
159
160 /* Sanity check - shouldn't happen */
161 if (sl->len < 0)
162 {
163 abort ();
164 } 138 {
165 /* We already have a partial packet */ 139 if (inbuf_len >= 2)
166 if (sl->len < 2)
167 {
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
174 {
175 stat = read (fd, sl->buf + sl->len, 2 - sl->len);
176 } 140 {
177 while ((stat == -1) && (errno == EINTR)); 141 unsigned int pkt_len = (inbuf [0] << 8) | inbuf [1];
178#endif 142
179 if (stat < 0) 143 if (inbuf_len >= 2 + pkt_len)
144 return pkt_len + 2;
180 { 145 }
181 /* In non blocking mode, EAGAIN is set when there is no 146
182 * data available. 147 int amount = sizeof (inbuf) - inbuf_len;
183 */ 148
184#ifdef WIN32 /* ***WIN32 SockList_ReadPacket: error handling for win32 */ 149 if (amount <= 0)
185 if ((stat == -1) && WSAGetLastError () != WSAEWOULDBLOCK) 150 {
151 LOG (llevError, "packet too large");//TODO
152 return -1;
153 }
154
155 amount = read (fd, inbuf + inbuf_len, amount);
156
157 if (!amount)
158 {
159 status = Ns_Dead;
160 return -1;
161 }
162 else if (amount < 0)
163 {
164 if (errno != EAGAIN && errno != EINTR)
186 { 165 {
187 if (WSAGetLastError () == WSAECONNRESET) 166 LOG (llevError, "read error: %s", strerror (errno));
188 LOG (llevDebug, "Connection closed by client\n"); 167 return -1;
189 else
190 {
191 LOG (llevDebug, "ReadPacket got error %d, returning 0\n", WSAGetLastError ());
192 }
193 return -1; /* kick this user! */
194 } 168 }
195#else
196 if (errno != EAGAIN && errno != EWOULDBLOCK)
197 {
198 LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno));
199 }
200#endif
201 return 0; /*Error */
202 }
203 if (stat == 0)
204 return -1;
205 sl->len += stat;
206#ifdef CS_LOGSTATS
207 cst_tot.ibytes += stat;
208 cst_lst.ibytes += stat;
209#endif
210 if (stat < 2)
211 return 0; /* Still don't have a full packet */
212 }
213 /* Figure out how much more data we need to read. Add 2 from the
214 * end of this - size header information is not included.
215 */
216 toread = 2 + (sl->buf[0] << 8) + sl->buf[1] - sl->len;
217 if ((toread + sl->len) >= len)
218 {
219 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
221 * closing the socket anyways, then reading this extra 100 bytes
222 * shouldn't hurt.
223 */
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);
228#endif /* end win32 */
229 169
230 /* return error so the socket is closed */
231 return -1;
232 }
233 do
234 {
235#ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */
236 stat = recv (fd, sl->buf + sl->len, toread, 0);
237#else
238 do
239 {
240 stat = read (fd, sl->buf + sl->len, toread);
241 }
242 while ((stat < 0) && (errno == EINTR));
243#endif
244 if (stat < 0)
245 {
246
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)
260 {
261 LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno));
262 }
263#endif
264 return 0; /*Error */
265 }
266 if (stat == 0)
267 return -1;
268 sl->len += stat;
269#ifdef CS_LOGSTATS
270 cst_tot.ibytes += stat;
271 cst_lst.ibytes += stat;
272#endif
273 toread -= stat;
274 if (toread == 0)
275 return 1;
276 if (toread < 0)
277 {
278 LOG (llevError, "SockList_ReadPacket: Read more bytes than desired.\n");
279 return 1; 170 return 0;
280 } 171 }
172
173 inbuf_len += amount;
174
175 cst_tot.ibytes += amount;
176 cst_lst.ibytes += amount;
281 } 177 }
282 while (toread > 0); 178}
283 return 0; 179
180void
181NewSocket::skip_packet (int len)
182{
183 inbuf_len -= len;
184 memmove (inbuf, inbuf + len, inbuf_len);
284} 185}
285 186
286/******************************************************************************* 187/*******************************************************************************
287 * 188 *
288 * Start of write related routines. 189 * Start of write related routines.
295 * ns is the socket we are adding the data to, buf is the start of the 196 * ns is the socket we are adding the data to, buf is the start of the
296 * data, and len is the number of bytes to add. 197 * data, and len is the number of bytes to add.
297 */ 198 */
298 199
299static void 200static void
300add_to_buffer (NewSocket * ns, char *buf, int len) 201add_to_buffer (NewSocket *ns, char *buf, int len)
301{ 202{
302 int avail, end; 203 int avail, end;
303 204
304 if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE) 205 if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE)
305 { 206 {
314 215
315 end = ns->outputbuffer.start + ns->outputbuffer.len; 216 end = ns->outputbuffer.start + ns->outputbuffer.len;
316 /* The buffer is already in a wrapped state, so adjust end */ 217 /* The buffer is already in a wrapped state, so adjust end */
317 if (end >= SOCKETBUFSIZE) 218 if (end >= SOCKETBUFSIZE)
318 end -= SOCKETBUFSIZE; 219 end -= SOCKETBUFSIZE;
220
319 avail = SOCKETBUFSIZE - end; 221 avail = SOCKETBUFSIZE - end;
320 222
321 /* We can all fit it behind the current data without wrapping */ 223 /* We can all fit it behind the current data without wrapping */
322 if (avail >= len) 224 if (avail >= len)
323 {
324 memcpy (ns->outputbuffer.data + end, buf, len); 225 memcpy (ns->outputbuffer.data + end, buf, len);
325 }
326 else 226 else
327 { 227 {
328 memcpy (ns->outputbuffer.data + end, buf, avail); 228 memcpy (ns->outputbuffer.data + end, buf, avail);
329 memcpy (ns->outputbuffer.data, buf + avail, len - avail); 229 memcpy (ns->outputbuffer.data, buf + avail, len - avail);
330 } 230 }
231
331 ns->outputbuffer.len += len; 232 ns->outputbuffer.len += len;
332#if 0 233#if 0
333 LOG (llevDebug, "Added %d to output buffer, total length now %d, start=%d\n", len, ns->outputbuffer.len, ns->outputbuffer.start); 234 LOG (llevDebug, "Added %d to output buffer, total length now %d, start=%d\n", len, ns->outputbuffer.len, ns->outputbuffer.start);
334#endif 235#endif
335} 236}
355 { 256 {
356 max = SOCKETBUFSIZE - ns->outputbuffer.start; 257 max = SOCKETBUFSIZE - ns->outputbuffer.start;
357 if (ns->outputbuffer.len < max) 258 if (ns->outputbuffer.len < max)
358 max = ns->outputbuffer.len; 259 max = ns->outputbuffer.len;
359 260
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 261 do
364 { 262 {
365 amt = write (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max); 263 amt = write (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max);
366 } 264 }
367 while ((amt < 0) && (errno == EINTR)); 265 while ((amt < 0) && (errno == EINTR));
368#endif
369 266
370 if (amt < 0) 267 if (amt < 0)
371 { /* We got an error */ 268 { /* We got an error */
372 269
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) 270 if (errno != EWOULDBLOCK)
379 { 271 {
380 LOG (llevError, "New socket write failed (wsb) (%d: %s).\n", errno, strerror (errno)); 272 LOG (llevError, "New socket write failed (wsb) (%d: %s).\n", errno, strerror (errno));
381#endif
382 ns->status = Ns_Dead; 273 ns->status = Ns_Dead;
383 return; 274 return;
384 } 275 }
385 else 276 else
386 { /* EWOULDBLOCK */ 277 { /* EWOULDBLOCK */
430#endif 321#endif
431 /* If we manage to write more than we wanted, take it as a bonus */ 322 /* If we manage to write more than we wanted, take it as a bonus */
432 while (len > 0) 323 while (len > 0)
433 { 324 {
434 325
435#ifdef WIN32 /* ***win32 Write_To_Socket: change write() to send() */
436 amt = send (ns->fd, pos, len, 0);
437#else
438 do 326 do
439 { 327 {
440 amt = write (ns->fd, pos, len); 328 amt = write (ns->fd, pos, len);
441 } 329 }
442 while ((amt < 0) && (errno == EINTR)); 330 while ((amt < 0) && (errno == EINTR));
443#endif
444 331
445 if (amt < 0) 332 if (amt < 0)
446 { /* We got an error */ 333 { /* 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) 334 if (errno != EWOULDBLOCK)
453 { 335 {
454 LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ 336 LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */
455 errno, strerror (errno)); 337 errno, strerror (errno));
456#endif
457 ns->status = Ns_Dead; 338 ns->status = Ns_Dead;
458 return; 339 return;
459 } 340 }
460 else 341 else
461 { /* EWOULDBLOCK */ 342 { /* EWOULDBLOCK */
479 cst_lst.obytes += amt; 360 cst_lst.obytes += amt;
480#endif 361#endif
481 } 362 }
482} 363}
483 364
484
485/** 365/**
486 * Takes a string of data, and writes it out to the socket. A very handy 366 * Takes a string of data, and writes it out to the socket. A very handy
487 * shortcut function. 367 * shortcut function.
488 */ 368 */
489void
490cs_write_string (NewSocket * ns, const char *buf, int len)
491{
492 SockList sl;
493 369
494 sl.len = len; 370void
495 sl.buf = (unsigned char *) buf; 371NewSocket::send_packet (packet &sl)
372{
496 Send_With_Handling (ns, &sl); 373 Send_With_Handling (this, &sl);
497} 374}
498 375
376void
377NewSocket::send_packet (const char *buf, int len)
378{
379 packet sl;
380
381 sl << data (buf, len);
382 send_packet (sl);
383}
384
385void
386NewSocket::send_packet (const char *buf)
387{
388 send_packet (buf, strlen (buf));
389}
499 390
500/** 391/**
501 * Calls Write_To_Socket to send data to the client. 392 * Calls Write_To_Socket to send data to the client.
502 * 393 *
503 * The only difference in this function is that we take a SockList 394 * The only difference in this function is that we take a packet
504 *, and we prepend the length information. 395 *, and we prepend the length information.
505 */ 396 */
506void 397void
507Send_With_Handling (NewSocket * ns, SockList * msg) 398Send_With_Handling (NewSocket *ns, packet *msg)
508{ 399{
509 unsigned char sbuf[4]; 400 unsigned char sbuf[4];
510 401
511 if (ns->status == Ns_Dead || !msg) 402 if (ns->status == Ns_Dead || !msg)
512 return; 403 return;
523 sbuf[1] = ((uint32) (msg->len)) & 0xFF; 414 sbuf[1] = ((uint32) (msg->len)) & 0xFF;
524 if (ns->status != Ns_Old) 415 if (ns->status != Ns_Old)
525 Write_To_Socket (ns, (char *) sbuf, 2); 416 Write_To_Socket (ns, (char *) sbuf, 2);
526 Write_To_Socket (ns, (char *) msg->buf, msg->len); 417 Write_To_Socket (ns, (char *) msg->buf, msg->len);
527} 418}
528
529/**
530 * Takes a string of data, and writes it out to the socket. A very handy
531 * shortcut function.
532 */
533void
534Write_String_To_Socket (NewSocket * ns, char *buf, int len)
535{
536 SockList sl;
537
538 sl.len = len;
539 sl.buf = (unsigned char *) buf;
540 Send_With_Handling (ns, &sl);
541}
542
543 419
544/****************************************************************************** 420/******************************************************************************
545 * 421 *
546 * statistics logging functions. 422 * statistics logging functions.
547 * 423 *
574 cst_lst.obytes = 0; 450 cst_lst.obytes = 0;
575 cst_lst.max_conn = socket_info.nconns; 451 cst_lst.max_conn = socket_info.nconns;
576 cst_lst.time_start = now; 452 cst_lst.time_start = now;
577} 453}
578#endif 454#endif
455

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines