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.8 by pippijn, Mon Dec 11 19:46:47 2006 UTC vs.
Revision 1.11 by root, Wed Dec 13 21:27:09 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 * SockList functions/utilities
87 * 86 *
88 **********************************************************************/ 87 **********************************************************************/
89 88
90void 89SockList &SockList::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 100SockList &SockList::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); 104}
112 sl->buf[sl->len++] = (char) ((data >> 32) & 0xff);
113 105
114 sl->buf[sl->len++] = (char) ((data >> 24) & 0xff); 106SockList &SockList::operator <<(const data16 &v)
115 sl->buf[sl->len++] = (char) ((data >> 16) & 0xff); 107{
116 sl->buf[sl->len++] = (char) ((data >> 8) & 0xff); 108 unsigned int len = min (v.len, 0xFFFF);
117 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);
118} 126}
119 127
120/* Basically does the reverse of SockList_AddInt, but on 128/* Basically does the reverse of SockList_AddInt, but on
121 * strings instead. Same for the GetShort, but for 16 bits. 129 * strings instead. Same for the GetShort, but for 16 bits.
122 */ 130 */
145 * 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
146 * at least 2 bytes long. 154 * at least 2 bytes long.
147 */ 155 */
148 156
149int 157int
150SockList_ReadPacket (int fd, SockList * sl, int len) 158SockList_ReadPacket (int fd, SockList *sl, int len)
151{ 159{
152 int stat, toread; 160 int stat, toread;
153 161
154 /* Sanity check - shouldn't happen */ 162 /* Sanity check - shouldn't happen */
155 if (sl->len < 0) 163 if (sl->len < 0)
420/** 428/**
421 * 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
422 * shortcut function. 430 * shortcut function.
423 */ 431 */
424void 432void
425cs_write_string (NewSocket * ns, const char *buf, int len) 433cs_write_string (NewSocket *ns, const char *buf, int len)
426{ 434{
427 SockList sl; 435 SockList sl;
428 436
429 sl.len = len; 437 sl.len = len;
430 sl.buf = (unsigned char *) buf; 438 sl.buf = (unsigned char *) buf;
437 * 445 *
438 * 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
439 *, and we prepend the length information. 447 *, and we prepend the length information.
440 */ 448 */
441void 449void
442Send_With_Handling (NewSocket * ns, SockList * msg) 450Send_With_Handling (NewSocket *ns, SockList *msg)
443{ 451{
444 unsigned char sbuf[4]; 452 unsigned char sbuf[4];
445 453
446 if (ns->status == Ns_Dead || !msg) 454 if (ns->status == Ns_Dead || !msg)
447 return; 455 return;
464/** 472/**
465 * 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
466 * shortcut function. 474 * shortcut function.
467 */ 475 */
468void 476void
469Write_String_To_Socket (NewSocket * ns, char *buf, int len) 477Write_String_To_Socket (NewSocket *ns, char *buf, int len)
470{ 478{
471 SockList sl; 479 SockList sl;
472 480
473 sl.len = len; 481 sl.len = len;
474 sl.buf = (unsigned char *) buf; 482 sl.buf = (unsigned char *) buf;
509 cst_lst.obytes = 0; 517 cst_lst.obytes = 0;
510 cst_lst.max_conn = socket_info.nconns; 518 cst_lst.max_conn = socket_info.nconns;
511 cst_lst.time_start = now; 519 cst_lst.time_start = now;
512} 520}
513#endif 521#endif
522

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines