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.7 by root, Sun Nov 26 19:48:50 2006 UTC vs.
Revision 1.14 by root, Thu Dec 14 01:21:58 2006 UTC

35using namespace std; 35using namespace std;
36 36
37#include <global.h> 37#include <global.h>
38#include <newclient.h> 38#include <newclient.h>
39#include <sproto.h> 39#include <sproto.h>
40#include <cstdarg>
40 41
41#ifdef __linux__ 42#ifdef __linux__
42# include <sys/types.h> 43# include <sys/types.h>
43# include <sys/socket.h> 44# include <sys/socket.h>
44# include <netinet/in.h> 45# include <netinet/in.h>
72 } 73 }
73 } 74 }
74 75
75 int val; 76 int val;
76 77
77 val = 0;
78 setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); 78 val = 0; setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val));
79 val = 1;
80 setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val)); 79 val = 1; setsockopt (ns->fd, IPPROTO_TCP, TCP_CORK, &val, sizeof (val));
81#endif 80#endif
82} 81}
83 82
84/*********************************************************************** 83/***********************************************************************
85 * 84 *
86 * SockList functions/utilities 85 * packet functions/utilities
87 * 86 *
88 **********************************************************************/ 87 **********************************************************************/
89 88
90void 89packet &packet::operator <<(const data &v)
91SockList_Init (SockList * sl)
92{ 90{
93 sl->len = 0; 91 if (v.len)
94 sl->buf = NULL; 92 {
95} 93 memcpy (buf + len, v.ptr, v.len);
94 len += v.len;
95 }
96 96
97void 97 return *this;
98SockList_AddInt (SockList * sl, uint32 data)
99{
100 sl->buf[sl->len++] = (data >> 24) & 0xff;
101 sl->buf[sl->len++] = (data >> 16) & 0xff;
102 sl->buf[sl->len++] = (data >> 8) & 0xff;
103 sl->buf[sl->len++] = data & 0xff;
104} 98}
105 99
106void 100packet &packet::operator <<(const data8 &v)
107SockList_AddInt64 (SockList * sl, uint64 data)
108{ 101{
109 sl->buf[sl->len++] = (char) ((data >> 56) & 0xff); 102 unsigned int len = min (v.len, 0x00FF);
110 sl->buf[sl->len++] = (char) ((data >> 48) & 0xff); 103 return *this << uint8 (len) << data (v.ptr, len);
111 sl->buf[sl->len++] = (char) ((data >> 40) & 0xff);
112 sl->buf[sl->len++] = (char) ((data >> 32) & 0xff);
113
114 sl->buf[sl->len++] = (char) ((data >> 24) & 0xff);
115 sl->buf[sl->len++] = (char) ((data >> 16) & 0xff);
116 sl->buf[sl->len++] = (char) ((data >> 8) & 0xff);
117 sl->buf[sl->len++] = (char) (data & 0xff);
118} 104}
119 105
120/* Basically does the reverse of SockList_AddInt, but on 106packet &packet::operator <<(const data16 &v)
121 * 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/******************************************************************************
122 */ 129 *
130 * Start of read routines.
131 *
132 ******************************************************************************/
133
123int 134int
124GetInt_String (unsigned char *data) 135NewSocket::read_packet ()
125{ 136{
126 return ((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]); 137 for (;;)
127}
128
129short
130GetShort_String (unsigned char *data)
131{
132 return ((data[0] << 8) + data[1]);
133}
134
135/******************************************************************************
136 *
137 * Start of read routines.
138 *
139 ******************************************************************************/
140
141/**
142 * This reads from fd and puts the data in sl. We return true if we think
143 * we have a full packet, 0 if we have a partial packet. The only processing
144 * we do is remove the intial size value. len (As passed) is the size of the
145 * buffer allocated in the socklist. We make the assumption the buffer is
146 * at least 2 bytes long.
147 */
148
149int
150SockList_ReadPacket (int fd, SockList * sl, int len)
151{
152 int stat, toread;
153
154 /* Sanity check - shouldn't happen */
155 if (sl->len < 0)
156 {
157 abort ();
158 } 138 {
159 /* We already have a partial packet */ 139 if (inbuf_len >= 2)
160 if (sl->len < 2)
161 {
162#ifdef WIN32 /* ***WIN32 SockList_ReadPacket: change read() to recv() */
163
164 stat = recv (fd, sl->buf + sl->len, 2 - sl->len, 0);
165
166#else
167 do
168 {
169 stat = read (fd, sl->buf + sl->len, 2 - sl->len);
170 } 140 {
171 while ((stat == -1) && (errno == EINTR)); 141 unsigned int pkt_len = (inbuf [0] << 8) | inbuf [1];
172#endif 142
173 if (stat < 0) 143 if (inbuf_len >= 2 + pkt_len)
144 return pkt_len + 2;
174 { 145 }
175 /* In non blocking mode, EAGAIN is set when there is no 146
176 * data available. 147 int amount = sizeof (inbuf) - inbuf_len;
177 */ 148
178#ifdef WIN32 /* ***WIN32 SockList_ReadPacket: error handling for win32 */ 149 if (amount <= 0)
179 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)
180 { 165 {
181 if (WSAGetLastError () == WSAECONNRESET) 166 LOG (llevError, "read error: %s", strerror (errno));
182 LOG (llevDebug, "Connection closed by client\n"); 167 return -1;
183 else
184 {
185 LOG (llevDebug, "ReadPacket got error %d, returning 0\n", WSAGetLastError ());
186 }
187 return -1; /* kick this user! */
188 } 168 }
189#else
190 if (errno != EAGAIN && errno != EWOULDBLOCK)
191 {
192 LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno));
193 }
194#endif
195 return 0; /*Error */
196 }
197 if (stat == 0)
198 return -1;
199 sl->len += stat;
200#ifdef CS_LOGSTATS
201 cst_tot.ibytes += stat;
202 cst_lst.ibytes += stat;
203#endif
204 if (stat < 2)
205 return 0; /* Still don't have a full packet */
206 }
207 /* Figure out how much more data we need to read. Add 2 from the
208 * end of this - size header information is not included.
209 */
210 toread = 2 + (sl->buf[0] << 8) + sl->buf[1] - sl->len;
211 if ((toread + sl->len) >= len)
212 {
213 LOG (llevError, "SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n", toread + sl->len, len);
214 /* Quick hack in case for 'oldsocketmode' input. If we are
215 * closing the socket anyways, then reading this extra 100 bytes
216 * shouldn't hurt.
217 */
218#ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */
219 recv (fd, sl->buf + 2, 100, 0);
220#else
221 read (fd, sl->buf + 2, 100);
222#endif /* end win32 */
223 169
224 /* return error so the socket is closed */
225 return -1;
226 }
227 do
228 {
229#ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */
230 stat = recv (fd, sl->buf + sl->len, toread, 0);
231#else
232 do
233 {
234 stat = read (fd, sl->buf + sl->len, toread);
235 }
236 while ((stat < 0) && (errno == EINTR));
237#endif
238 if (stat < 0)
239 {
240
241#ifdef WIN32 /* ***win32 SockList_ReadPacket: change error handling for win32 */
242 if ((stat == -1) && WSAGetLastError () != WSAEWOULDBLOCK)
243 {
244 if (WSAGetLastError () == WSAECONNRESET)
245 LOG (llevDebug, "Connection closed by client\n");
246 else
247 {
248 LOG (llevDebug, "ReadPacket got error %d, returning 0\n", WSAGetLastError ());
249 }
250 return -1; /* kick this user! */
251 }
252#else
253 if (errno != EAGAIN && errno != EWOULDBLOCK)
254 {
255 LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno));
256 }
257#endif
258 return 0; /*Error */
259 }
260 if (stat == 0)
261 return -1;
262 sl->len += stat;
263#ifdef CS_LOGSTATS
264 cst_tot.ibytes += stat;
265 cst_lst.ibytes += stat;
266#endif
267 toread -= stat;
268 if (toread == 0)
269 return 1;
270 if (toread < 0)
271 {
272 LOG (llevError, "SockList_ReadPacket: Read more bytes than desired.\n");
273 return 1; 170 return 0;
274 } 171 }
172
173 inbuf_len += amount;
174
175 cst_tot.ibytes += amount;
176 cst_lst.ibytes += amount;
275 } 177 }
276 while (toread > 0); 178}
277 return 0; 179
180void
181NewSocket::skip_packet (int len)
182{
183 inbuf_len -= len;
184 memmove (inbuf, inbuf + len, inbuf_len);
278} 185}
279 186
280/******************************************************************************* 187/*******************************************************************************
281 * 188 *
282 * Start of write related routines. 189 * Start of write related routines.
289 * 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
290 * data, and len is the number of bytes to add. 197 * data, and len is the number of bytes to add.
291 */ 198 */
292 199
293static void 200static void
294add_to_buffer (NewSocket * ns, char *buf, int len) 201add_to_buffer (NewSocket *ns, char *buf, int len)
295{ 202{
296 int avail, end; 203 int avail, end;
297 204
298 if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE) 205 if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE)
299 { 206 {
349 { 256 {
350 max = SOCKETBUFSIZE - ns->outputbuffer.start; 257 max = SOCKETBUFSIZE - ns->outputbuffer.start;
351 if (ns->outputbuffer.len < max) 258 if (ns->outputbuffer.len < max)
352 max = ns->outputbuffer.len; 259 max = ns->outputbuffer.len;
353 260
354#ifdef WIN32 /* ***win32 write_socket_buffer: change write() to send() */
355 amt = send (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max, 0);
356#else
357 do 261 do
358 { 262 {
359 amt = write (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max); 263 amt = write (ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max);
360 } 264 }
361 while ((amt < 0) && (errno == EINTR)); 265 while ((amt < 0) && (errno == EINTR));
362#endif
363 266
364 if (amt < 0) 267 if (amt < 0)
365 { /* We got an error */ 268 { /* We got an error */
366 269
367#ifdef WIN32 /* ***win32 write_socket_buffer: change error handling */
368 if (amt == -1 && WSAGetLastError () != WSAEWOULDBLOCK)
369 {
370 LOG (llevError, "New socket write failed (wsb) (%d).\n", WSAGetLastError ());
371#else
372 if (errno != EWOULDBLOCK) 270 if (errno != EWOULDBLOCK)
373 { 271 {
374 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));
375#endif
376 ns->status = Ns_Dead; 273 ns->status = Ns_Dead;
377 return; 274 return;
378 } 275 }
379 else 276 else
380 { /* EWOULDBLOCK */ 277 { /* EWOULDBLOCK */
424#endif 321#endif
425 /* 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 */
426 while (len > 0) 323 while (len > 0)
427 { 324 {
428 325
429#ifdef WIN32 /* ***win32 Write_To_Socket: change write() to send() */
430 amt = send (ns->fd, pos, len, 0);
431#else
432 do 326 do
433 { 327 {
434 amt = write (ns->fd, pos, len); 328 amt = write (ns->fd, pos, len);
435 } 329 }
436 while ((amt < 0) && (errno == EINTR)); 330 while ((amt < 0) && (errno == EINTR));
437#endif
438 331
439 if (amt < 0) 332 if (amt < 0)
440 { /* We got an error */ 333 { /* We got an error */
441#ifdef WIN32 /* ***win32 Write_To_Socket: change error handling */
442 if (amt == -1 && WSAGetLastError () != WSAEWOULDBLOCK)
443 {
444 LOG (llevError, "New socket write failed WTS (%d).\n", WSAGetLastError ());
445#else
446 if (errno != EWOULDBLOCK) 334 if (errno != EWOULDBLOCK)
447 { 335 {
448 LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ 336 LOG (llevError, "New socket write failed WTS (%d: %s).\n", /* ---WIN32 */
449 errno, strerror (errno)); 337 errno, strerror (errno));
450#endif
451 ns->status = Ns_Dead; 338 ns->status = Ns_Dead;
452 return; 339 return;
453 } 340 }
454 else 341 else
455 { /* EWOULDBLOCK */ 342 { /* EWOULDBLOCK */
473 cst_lst.obytes += amt; 360 cst_lst.obytes += amt;
474#endif 361#endif
475 } 362 }
476} 363}
477 364
478
479/** 365/**
480 * 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
481 * shortcut function. 367 * shortcut function.
482 */ 368 */
483void
484cs_write_string (NewSocket * ns, const char *buf, int len)
485{
486 SockList sl;
487 369
488 sl.len = len; 370void
489 sl.buf = (unsigned char *) buf; 371NewSocket::send_packet (packet &sl)
372{
490 Send_With_Handling (ns, &sl); 373 Send_With_Handling (this, &sl);
491} 374}
492 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}
493 390
494/** 391/**
495 * Calls Write_To_Socket to send data to the client. 392 * Calls Write_To_Socket to send data to the client.
496 * 393 *
497 * 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
498 *, and we prepend the length information. 395 *, and we prepend the length information.
499 */ 396 */
500void 397void
501Send_With_Handling (NewSocket * ns, SockList * msg) 398Send_With_Handling (NewSocket *ns, packet *msg)
502{ 399{
503 unsigned char sbuf[4]; 400 unsigned char sbuf[4];
504 401
505 if (ns->status == Ns_Dead || !msg) 402 if (ns->status == Ns_Dead || !msg)
506 return; 403 return;
517 sbuf[1] = ((uint32) (msg->len)) & 0xFF; 414 sbuf[1] = ((uint32) (msg->len)) & 0xFF;
518 if (ns->status != Ns_Old) 415 if (ns->status != Ns_Old)
519 Write_To_Socket (ns, (char *) sbuf, 2); 416 Write_To_Socket (ns, (char *) sbuf, 2);
520 Write_To_Socket (ns, (char *) msg->buf, msg->len); 417 Write_To_Socket (ns, (char *) msg->buf, msg->len);
521} 418}
522
523/**
524 * Takes a string of data, and writes it out to the socket. A very handy
525 * shortcut function.
526 */
527void
528Write_String_To_Socket (NewSocket * ns, char *buf, int len)
529{
530 SockList sl;
531
532 sl.len = len;
533 sl.buf = (unsigned char *) buf;
534 Send_With_Handling (ns, &sl);
535}
536
537 419
538/****************************************************************************** 420/******************************************************************************
539 * 421 *
540 * statistics logging functions. 422 * statistics logging functions.
541 * 423 *
568 cst_lst.obytes = 0; 450 cst_lst.obytes = 0;
569 cst_lst.max_conn = socket_info.nconns; 451 cst_lst.max_conn = socket_info.nconns;
570 cst_lst.time_start = now; 452 cst_lst.time_start = now;
571} 453}
572#endif 454#endif
455

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines