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.2 by root, Fri Aug 18 02:06:58 2006 UTC vs.
Revision 1.3 by root, Tue Aug 29 08:01:38 2006 UTC

1 1
2/* 2/*
3 * static char *rcsid_sockets_c = 3 * static char *rcsid_sockets_c =
4 * "$Id: lowlevel.C,v 1.2 2006/08/18 02:06:58 root Exp $"; 4 * "$Id: lowlevel.C,v 1.3 2006/08/29 08:01:38 root Exp $";
5 */ 5 */
6 6
7/* 7/*
8 CrossFire, A Multiplayer game for X-windows 8 CrossFire, A Multiplayer game for X-windows
9 9
70 70
71 if (tcpi.tcpi_unacked 71 if (tcpi.tcpi_unacked
72 && SOCKET_TIMEOUT1 * TCP_HZ < diff && diff < 0x80000000UL // ack delayed for 20s 72 && SOCKET_TIMEOUT1 * TCP_HZ < diff && diff < 0x80000000UL // ack delayed for 20s
73 && SOCKET_TIMEOUT2 * TCP_HZ < tcpi.tcpi_last_data_sent) // no data sent for 10s 73 && SOCKET_TIMEOUT2 * TCP_HZ < tcpi.tcpi_last_data_sent) // no data sent for 10s
74 { 74 {
75 LOG (llevDebug, "Connection on fd %d closed due to ack timeout (%u/%u/%u)\n", ns->fd, 75 LOG (llevDebug, "Connection on fd %d closed due to ack timeout (%u/%u/%u)\n", ns->fd,
76 (unsigned)tcpi.tcpi_last_ack_recv, (unsigned)tcpi.tcpi_last_data_sent, (unsigned)tcpi.tcpi_unacked); 76 (unsigned)tcpi.tcpi_last_ack_recv, (unsigned)tcpi.tcpi_last_data_sent, (unsigned)tcpi.tcpi_unacked);
77 ns->status = Ns_Dead; 77 ns->status = Ns_Dead;
78 } 78 }
79 } 79 }
80 80
147{ 147{
148 int stat,toread; 148 int stat,toread;
149 149
150 /* Sanity check - shouldn't happen */ 150 /* Sanity check - shouldn't happen */
151 if (sl->len < 0) { 151 if (sl->len < 0) {
152 abort(); 152 abort();
153 } 153 }
154 /* We already have a partial packet */ 154 /* We already have a partial packet */
155 if (sl->len<2) { 155 if (sl->len<2) {
156#ifdef WIN32 /* ***WIN32 SockList_ReadPacket: change read() to recv() */ 156#ifdef WIN32 /* ***WIN32 SockList_ReadPacket: change read() to recv() */
157 157
158 stat=recv(fd, sl->buf + sl->len, 2-sl->len,0); 158 stat=recv(fd, sl->buf + sl->len, 2-sl->len,0);
159 159
160#else 160#else
161 do { 161 do {
162 stat=read(fd, sl->buf + sl->len, 2-sl->len); 162 stat=read(fd, sl->buf + sl->len, 2-sl->len);
163 } while ((stat==-1) && (errno==EINTR)); 163 } while ((stat==-1) && (errno==EINTR));
164#endif 164#endif
165 if (stat<0) { 165 if (stat<0) {
166 /* In non blocking mode, EAGAIN is set when there is no 166 /* In non blocking mode, EAGAIN is set when there is no
167 * data available. 167 * data available.
168 */ 168 */
169#ifdef WIN32 /* ***WIN32 SockList_ReadPacket: error handling for win32 */ 169#ifdef WIN32 /* ***WIN32 SockList_ReadPacket: error handling for win32 */
170 if ((stat==-1) && WSAGetLastError() !=WSAEWOULDBLOCK) { 170 if ((stat==-1) && WSAGetLastError() !=WSAEWOULDBLOCK) {
171 if(WSAGetLastError() == WSAECONNRESET) 171 if(WSAGetLastError() == WSAECONNRESET)
172 LOG(llevDebug,"Connection closed by client\n"); 172 LOG(llevDebug,"Connection closed by client\n");
173 else 173 else
174 { 174 {
175 LOG(llevDebug,"ReadPacket got error %d, returning 0\n",WSAGetLastError()); 175 LOG(llevDebug,"ReadPacket got error %d, returning 0\n",WSAGetLastError());
176 } 176 }
177 return -1; /* kick this user! */ 177 return -1; /* kick this user! */
178 } 178 }
179#else 179#else
180 if (errno != EAGAIN && errno !=EWOULDBLOCK) { 180 if (errno != EAGAIN && errno !=EWOULDBLOCK) {
181 LOG(llevDebug, "ReadPacket got error %s, returning 0\n", strerror_local(errno)); 181 LOG(llevDebug, "ReadPacket got error %s, returning 0\n", strerror_local(errno));
182 } 182 }
183#endif 183#endif
184 return 0; /*Error */ 184 return 0; /*Error */
185 } 185 }
186 if (stat==0) return -1; 186 if (stat==0) return -1;
187 sl->len += stat; 187 sl->len += stat;
188#ifdef CS_LOGSTATS 188#ifdef CS_LOGSTATS
189 cst_tot.ibytes += stat; 189 cst_tot.ibytes += stat;
190 cst_lst.ibytes += stat; 190 cst_lst.ibytes += stat;
191#endif 191#endif
192 if (stat<2) return 0; /* Still don't have a full packet */ 192 if (stat<2) return 0; /* Still don't have a full packet */
193 } 193 }
194 /* Figure out how much more data we need to read. Add 2 from the 194 /* Figure out how much more data we need to read. Add 2 from the
195 * end of this - size header information is not included. 195 * end of this - size header information is not included.
196 */ 196 */
197 toread = 2+(sl->buf[0] << 8) + sl->buf[1] - sl->len; 197 toread = 2+(sl->buf[0] << 8) + sl->buf[1] - sl->len;
198 if ((toread + sl->len) >= len) { 198 if ((toread + sl->len) >= len) {
199 LOG(llevError,"SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n", 199 LOG(llevError,"SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n",
200 toread + sl->len, len); 200 toread + sl->len, len);
201 /* Quick hack in case for 'oldsocketmode' input. If we are 201 /* Quick hack in case for 'oldsocketmode' input. If we are
202 * closing the socket anyways, then reading this extra 100 bytes 202 * closing the socket anyways, then reading this extra 100 bytes
203 * shouldn't hurt. 203 * shouldn't hurt.
204 */ 204 */
205#ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */ 205#ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */
206 recv(fd, sl->buf+2, 100, 0); 206 recv(fd, sl->buf+2, 100, 0);
207#else 207#else
208 read(fd, sl->buf+2, 100); 208 read(fd, sl->buf+2, 100);
209#endif /* end win32 */ 209#endif /* end win32 */
210 210
211 /* return error so the socket is closed */ 211 /* return error so the socket is closed */
212 return -1; 212 return -1;
213 } 213 }
214 do { 214 do {
215#ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */ 215#ifdef WIN32 /* ***win32 SockList_ReadPacket: change read() to recv() */
216 stat = recv(fd, sl->buf+ sl->len, toread, 0); 216 stat = recv(fd, sl->buf+ sl->len, toread, 0);
217#else 217#else
218 do { 218 do {
219 stat = read(fd, sl->buf+ sl->len, toread); 219 stat = read(fd, sl->buf+ sl->len, toread);
220 } while ((stat<0) && (errno==EINTR)); 220 } while ((stat<0) && (errno==EINTR));
221#endif 221#endif
222 if (stat<0) { 222 if (stat<0) {
223 223
224#ifdef WIN32 /* ***win32 SockList_ReadPacket: change error handling for win32 */ 224#ifdef WIN32 /* ***win32 SockList_ReadPacket: change error handling for win32 */
225 if ((stat==-1) && WSAGetLastError() !=WSAEWOULDBLOCK) { 225 if ((stat==-1) && WSAGetLastError() !=WSAEWOULDBLOCK) {
226 if(WSAGetLastError() == WSAECONNRESET) 226 if(WSAGetLastError() == WSAECONNRESET)
227 LOG(llevDebug,"Connection closed by client\n"); 227 LOG(llevDebug,"Connection closed by client\n");
228 else 228 else
229 { 229 {
230 LOG(llevDebug,"ReadPacket got error %d, returning 0\n",WSAGetLastError()); 230 LOG(llevDebug,"ReadPacket got error %d, returning 0\n",WSAGetLastError());
231 } 231 }
232 return -1; /* kick this user! */ 232 return -1; /* kick this user! */
233 } 233 }
234#else 234#else
235 if (errno != EAGAIN && errno !=EWOULDBLOCK) { 235 if (errno != EAGAIN && errno !=EWOULDBLOCK) {
236 LOG(llevDebug, "ReadPacket got error %s, returning 0\n", strerror_local(errno)); 236 LOG(llevDebug, "ReadPacket got error %s, returning 0\n", strerror_local(errno));
237 } 237 }
238#endif 238#endif
239 return 0; /*Error */ 239 return 0; /*Error */
240 } 240 }
241 if (stat==0) return -1; 241 if (stat==0) return -1;
242 sl->len += stat; 242 sl->len += stat;
243#ifdef CS_LOGSTATS 243#ifdef CS_LOGSTATS
244 cst_tot.ibytes += stat; 244 cst_tot.ibytes += stat;
245 cst_lst.ibytes += stat; 245 cst_lst.ibytes += stat;
246#endif 246#endif
247 toread -= stat; 247 toread -= stat;
248 if (toread==0) return 1; 248 if (toread==0) return 1;
249 if (toread < 0) { 249 if (toread < 0) {
250 LOG(llevError,"SockList_ReadPacket: Read more bytes than desired.\n"); 250 LOG(llevError,"SockList_ReadPacket: Read more bytes than desired.\n");
251 return 1; 251 return 1;
252 } 252 }
253 } while (toread>0); 253 } while (toread>0);
254 return 0; 254 return 0;
255} 255}
256 256
257/******************************************************************************* 257/*******************************************************************************
270static void add_to_buffer(NewSocket *ns, char *buf, int len) 270static void add_to_buffer(NewSocket *ns, char *buf, int len)
271{ 271{
272 int avail, end; 272 int avail, end;
273 273
274 if ((len+ns->outputbuffer.len)>SOCKETBUFSIZE) { 274 if ((len+ns->outputbuffer.len)>SOCKETBUFSIZE) {
275 LOG(llevDebug,"Socket on fd %d has overrun internal buffer - marking as dead\n", 275 LOG(llevDebug,"Socket on fd %d has overrun internal buffer - marking as dead\n",
276 ns->fd); 276 ns->fd);
277 ns->status = Ns_Dead; 277 ns->status = Ns_Dead;
278 return; 278 return;
279 } 279 }
280 280
281 /* data + end is where we start putting the new data. The last byte 281 /* data + end is where we start putting the new data. The last byte
282 * currently in use is actually data + end -1 282 * currently in use is actually data + end -1
283 */ 283 */
287 if (end>=SOCKETBUFSIZE) end-=SOCKETBUFSIZE; 287 if (end>=SOCKETBUFSIZE) end-=SOCKETBUFSIZE;
288 avail=SOCKETBUFSIZE - end; 288 avail=SOCKETBUFSIZE - end;
289 289
290 /* We can all fit it behind the current data without wrapping */ 290 /* We can all fit it behind the current data without wrapping */
291 if (avail >=len ) { 291 if (avail >=len ) {
292 memcpy(ns->outputbuffer.data + end, buf, len); 292 memcpy(ns->outputbuffer.data + end, buf, len);
293 } 293 }
294 else { 294 else {
295 memcpy(ns->outputbuffer.data + end, buf, avail); 295 memcpy(ns->outputbuffer.data + end, buf, avail);
296 memcpy(ns->outputbuffer.data, buf+avail, len-avail); 296 memcpy(ns->outputbuffer.data, buf+avail, len-avail);
297 } 297 }
298 ns->outputbuffer.len += len; 298 ns->outputbuffer.len += len;
299#if 0 299#if 0
300 LOG(llevDebug,"Added %d to output buffer, total length now %d, start=%d\n", len, 300 LOG(llevDebug,"Added %d to output buffer, total length now %d, start=%d\n", len,
301 ns->outputbuffer.len, ns->outputbuffer.start); 301 ns->outputbuffer.len, ns->outputbuffer.start);
302#endif 302#endif
303} 303}
304 304
305/** 305/**
306 * Writes data to socket. 306 * Writes data to socket.
311void write_socket_buffer(NewSocket *ns) 311void write_socket_buffer(NewSocket *ns)
312{ 312{
313 int amt, max; 313 int amt, max;
314 314
315 if (ns->outputbuffer.len==0) { 315 if (ns->outputbuffer.len==0) {
316 LOG(llevDebug,"write_socket_buffer called when there is no data, fd=%d\n", 316 LOG(llevDebug,"write_socket_buffer called when there is no data, fd=%d\n",
317 ns->fd); 317 ns->fd);
318 return; 318 return;
319 } 319 }
320 320
321 do { 321 do {
322 max = SOCKETBUFSIZE - ns->outputbuffer.start; 322 max = SOCKETBUFSIZE - ns->outputbuffer.start;
323 if (ns->outputbuffer.len<max) max = ns->outputbuffer.len; 323 if (ns->outputbuffer.len<max) max = ns->outputbuffer.len;
324 324
325#ifdef WIN32 /* ***win32 write_socket_buffer: change write() to send() */ 325#ifdef WIN32 /* ***win32 write_socket_buffer: change write() to send() */
326 amt=send(ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max,0); 326 amt=send(ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max,0);
327#else 327#else
328 do { 328 do {
329 amt=write(ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max); 329 amt=write(ns->fd, ns->outputbuffer.data + ns->outputbuffer.start, max);
330 } while ((amt<0) && (errno==EINTR)); 330 } while ((amt<0) && (errno==EINTR));
331#endif 331#endif
332 332
333 if (amt < 0) { /* We got an error */ 333 if (amt < 0) { /* We got an error */
334 334
335#ifdef WIN32 /* ***win32 write_socket_buffer: change error handling */ 335#ifdef WIN32 /* ***win32 write_socket_buffer: change error handling */
336 if (amt == -1 && WSAGetLastError() !=WSAEWOULDBLOCK) { 336 if (amt == -1 && WSAGetLastError() !=WSAEWOULDBLOCK) {
337 LOG(llevError,"New socket write failed (wsb) (%d).\n", WSAGetLastError()); 337 LOG(llevError,"New socket write failed (wsb) (%d).\n", WSAGetLastError());
338#else 338#else
339 if (errno !=EWOULDBLOCK) { 339 if (errno !=EWOULDBLOCK) {
340 LOG(llevError,"New socket write failed (wsb) (%d: %s).\n", 340 LOG(llevError,"New socket write failed (wsb) (%d: %s).\n",
341 errno, strerror_local(errno)); 341 errno, strerror_local(errno));
342#endif 342#endif
343 ns->status=Ns_Dead; 343 ns->status=Ns_Dead;
344 return; 344 return;
345 } 345 }
346 else { /* EWOULDBLOCK */ 346 else { /* EWOULDBLOCK */
347 /* can't write it, so store it away. */ 347 /* can't write it, so store it away. */
348 ns->can_write=0; 348 ns->can_write=0;
349 return; 349 return;
350 } 350 }
351 } 351 }
352 ns->outputbuffer.start += amt; 352 ns->outputbuffer.start += amt;
353 /* wrap back to start of buffer */ 353 /* wrap back to start of buffer */
354 if (ns->outputbuffer.start==SOCKETBUFSIZE) ns->outputbuffer.start=0; 354 if (ns->outputbuffer.start==SOCKETBUFSIZE) ns->outputbuffer.start=0;
355 ns->outputbuffer.len -= amt; 355 ns->outputbuffer.len -= amt;
356#ifdef CS_LOGSTATS 356#ifdef CS_LOGSTATS
357 cst_tot.obytes += amt; 357 cst_tot.obytes += amt;
358 cst_lst.obytes += amt; 358 cst_lst.obytes += amt;
359 #endif 359 #endif
360 } while (ns->outputbuffer.len>0); 360 } while (ns->outputbuffer.len>0);
361} 361}
362 362
363/** 363/**
371{ 371{
372 int amt=0; 372 int amt=0;
373 char *pos=buf; 373 char *pos=buf;
374 374
375 if (ns->status == Ns_Dead || !buf) { 375 if (ns->status == Ns_Dead || !buf) {
376 LOG(llevDebug,"Write_To_Socket called with dead socket\n"); 376 LOG(llevDebug,"Write_To_Socket called with dead socket\n");
377 return; 377 return;
378 } 378 }
379 379
380#ifndef __GNU__ /* This caused problems on Hurd */ 380#ifndef __GNU__ /* This caused problems on Hurd */
381 if (!ns->can_write) { 381 if (!ns->can_write) {
382 add_to_buffer(ns, buf, len); 382 add_to_buffer(ns, buf, len);
383 return; 383 return;
384 } 384 }
385#endif 385#endif
386 /* If we manage to write more than we wanted, take it as a bonus */ 386 /* If we manage to write more than we wanted, take it as a bonus */
387 while (len>0) { 387 while (len>0) {
388 388
389#ifdef WIN32 /* ***win32 Write_To_Socket: change write() to send() */ 389#ifdef WIN32 /* ***win32 Write_To_Socket: change write() to send() */
390 amt=send(ns->fd, pos, len,0); 390 amt=send(ns->fd, pos, len,0);
391#else 391#else
392 do { 392 do {
393 amt=write(ns->fd, pos, len); 393 amt=write(ns->fd, pos, len);
394 } while ((amt<0) && (errno==EINTR)); 394 } while ((amt<0) && (errno==EINTR));
395#endif 395#endif
396 396
397 if (amt < 0) { /* We got an error */ 397 if (amt < 0) { /* We got an error */
398#ifdef WIN32 /* ***win32 Write_To_Socket: change error handling */ 398#ifdef WIN32 /* ***win32 Write_To_Socket: change error handling */
399 if (amt == -1 && WSAGetLastError() !=WSAEWOULDBLOCK) { 399 if (amt == -1 && WSAGetLastError() !=WSAEWOULDBLOCK) {
400 LOG(llevError,"New socket write failed WTS (%d).\n",WSAGetLastError()); 400 LOG(llevError,"New socket write failed WTS (%d).\n",WSAGetLastError());
401#else 401#else
402 if (errno !=EWOULDBLOCK) { 402 if (errno !=EWOULDBLOCK) {
403 LOG(llevError,"New socket write failed WTS (%d: %s).\n", /* ---WIN32 */ 403 LOG(llevError,"New socket write failed WTS (%d: %s).\n", /* ---WIN32 */
404 errno, strerror_local(errno)); 404 errno, strerror_local(errno));
405#endif 405#endif
406 ns->status=Ns_Dead; 406 ns->status=Ns_Dead;
407 return; 407 return;
408 } 408 }
409 else { /* EWOULDBLOCK */ 409 else { /* EWOULDBLOCK */
410 /* can't write it, so store it away. */ 410 /* can't write it, so store it away. */
411 add_to_buffer(ns, pos, len); 411 add_to_buffer(ns, pos, len);
412 ns->can_write=0; 412 ns->can_write=0;
413 return; 413 return;
414 } 414 }
415 } 415 }
416 /* amt gets set to 0 above in blocking code, so we do this as 416 /* amt gets set to 0 above in blocking code, so we do this as
417 * an else if to make sure we don't reprocess it. 417 * an else if to make sure we don't reprocess it.
418 */ 418 */
419 else if (amt==0) { 419 else if (amt==0) {
420 LOG(llevError,"Write_To_Socket: No data written out.\n"); 420 LOG(llevError,"Write_To_Socket: No data written out.\n");
421 } 421 }
422 len -= amt; 422 len -= amt;
423 pos += amt; 423 pos += amt;
424#ifdef CS_LOGSTATS 424#ifdef CS_LOGSTATS
425 cst_tot.obytes += amt; 425 cst_tot.obytes += amt;
426 cst_lst.obytes += amt; 426 cst_lst.obytes += amt;
427 #endif 427 #endif
428 } 428 }
429} 429}
430 430
431 431
452void Send_With_Handling(NewSocket *ns,SockList *msg) 452void Send_With_Handling(NewSocket *ns,SockList *msg)
453{ 453{
454 unsigned char sbuf[4]; 454 unsigned char sbuf[4];
455 455
456 if (ns->status == Ns_Dead || !msg) 456 if (ns->status == Ns_Dead || !msg)
457 return; 457 return;
458 458
459 if (msg->len >= MAXSOCKBUF) { 459 if (msg->len >= MAXSOCKBUF) {
460 LOG(llevError,"Trying to send a buffer beyond properly size, len =%d\n", 460 LOG(llevError,"Trying to send a buffer beyond properly size, len =%d\n",
461 msg->len); 461 msg->len);
462 /* Almost certainly we've overflowed a buffer, so quite now to make 462 /* Almost certainly we've overflowed a buffer, so quite now to make
463 * it easier to debug. 463 * it easier to debug.
464 */ 464 */
465 abort(); 465 abort();
466 } 466 }
467 sbuf[0] = ((uint32)(msg->len) >> 8) & 0xFF; 467 sbuf[0] = ((uint32)(msg->len) >> 8) & 0xFF;
468 sbuf[1] = ((uint32)(msg->len)) & 0xFF; 468 sbuf[1] = ((uint32)(msg->len)) & 0xFF;
469 if (ns->status != Ns_Old) 469 if (ns->status != Ns_Old)
470 Write_To_Socket(ns, (char *) sbuf, 2); 470 Write_To_Socket(ns, (char *) sbuf, 2);
471 Write_To_Socket(ns, (char*)msg->buf, msg->len); 471 Write_To_Socket(ns, (char*)msg->buf, msg->len);
472} 472}
473 473
474/** 474/**
475 * Takes a string of data, and writes it out to the socket. A very handy 475 * Takes a string of data, and writes it out to the socket. A very handy
507 /* If no connections recently, don't both to log anything */ 507 /* If no connections recently, don't both to log anything */
508 if (cst_lst.ibytes==0 && cst_lst.obytes==0) return; 508 if (cst_lst.ibytes==0 && cst_lst.obytes==0) return;
509 509
510 /* CSSTAT is put in so scripts can easily find the line */ 510 /* CSSTAT is put in so scripts can easily find the line */
511 LOG(llevInfo, "CSSTAT: %.16s tot %d %d %d %d inc %d %d %d %d\n", 511 LOG(llevInfo, "CSSTAT: %.16s tot %d %d %d %d inc %d %d %d %d\n",
512 ctime(&now), cst_tot.ibytes, cst_tot.obytes, cst_tot.max_conn, 512 ctime(&now), cst_tot.ibytes, cst_tot.obytes, cst_tot.max_conn,
513 now - cst_tot.time_start, cst_lst.ibytes, cst_lst.obytes, 513 now - cst_tot.time_start, cst_lst.ibytes, cst_lst.obytes,
514 cst_lst.max_conn, now - cst_lst.time_start); 514 cst_lst.max_conn, now - cst_lst.time_start);
515 cst_lst.ibytes=0; 515 cst_lst.ibytes=0;
516 cst_lst.obytes=0; 516 cst_lst.obytes=0;
517 cst_lst.max_conn=socket_info.nconns; 517 cst_lst.max_conn=socket_info.nconns;
518 cst_lst.time_start=now; 518 cst_lst.time_start=now;
519} 519}

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines