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.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 do
163 {
164 stat = read (fd, sl->buf + sl->len, 2 - sl->len);
165 } 140 {
166 while ((stat == -1) && (errno == EINTR)); 141 unsigned int pkt_len = (inbuf [0] << 8) | inbuf [1];
167 142
168 if (stat < 0) 143 if (inbuf_len >= 2 + pkt_len)
144 return pkt_len + 2;
169 { 145 }
170 /* In non blocking mode, EAGAIN is set when there is no 146
171 * data available. 147 int amount = sizeof (inbuf) - inbuf_len;
172 */ 148
149 if (amount <= 0)
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 {
173 if (errno != EAGAIN && errno != EWOULDBLOCK) 164 if (errno != EAGAIN && errno != EINTR)
174 { 165 {
175 LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); 166 LOG (llevError, "read error: %s", strerror (errno));
167 return -1;
176 } 168 }
177 return 0; /*Error */
178 }
179 if (stat == 0)
180 return -1;
181 sl->len += stat;
182#ifdef CS_LOGSTATS
183 cst_tot.ibytes += stat;
184 cst_lst.ibytes += stat;
185#endif
186 if (stat < 2)
187 return 0; /* Still don't have a full packet */
188 }
189 /* Figure out how much more data we need to read. Add 2 from the
190 * end of this - size header information is not included.
191 */
192 toread = 2 + (sl->buf[0] << 8) + sl->buf[1] - sl->len;
193 if ((toread + sl->len) >= len)
194 {
195 LOG (llevError, "SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n", toread + sl->len, len);
196 /* Quick hack in case for 'oldsocketmode' input. If we are
197 * closing the socket anyways, then reading this extra 100 bytes
198 * shouldn't hurt.
199 */
200 read (fd, sl->buf + 2, 100);
201 169
202 /* return error so the socket is closed */
203 return -1;
204 }
205 do
206 {
207 do
208 {
209 stat = read (fd, sl->buf + sl->len, toread);
210 }
211 while ((stat < 0) && (errno == EINTR));
212 if (stat < 0)
213 {
214
215 if (errno != EAGAIN && errno != EWOULDBLOCK)
216 {
217 LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno));
218 }
219 return 0; /*Error */
220 }
221 if (stat == 0)
222 return -1;
223 sl->len += stat;
224#ifdef CS_LOGSTATS
225 cst_tot.ibytes += stat;
226 cst_lst.ibytes += stat;
227#endif
228 toread -= stat;
229 if (toread == 0)
230 return 1;
231 if (toread < 0)
232 {
233 LOG (llevError, "SockList_ReadPacket: Read more bytes than desired.\n");
234 return 1; 170 return 0;
235 } 171 }
172
173 inbuf_len += amount;
174
175 cst_tot.ibytes += amount;
176 cst_lst.ibytes += amount;
236 } 177 }
237 while (toread > 0); 178}
238 return 0; 179
180void
181NewSocket::skip_packet (int len)
182{
183 inbuf_len -= len;
184 memmove (inbuf, inbuf + len, inbuf_len);
239} 185}
240 186
241/******************************************************************************* 187/*******************************************************************************
242 * 188 *
243 * Start of write related routines. 189 * Start of write related routines.
250 * 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
251 * data, and len is the number of bytes to add. 197 * data, and len is the number of bytes to add.
252 */ 198 */
253 199
254static void 200static void
255add_to_buffer (NewSocket * ns, char *buf, int len) 201add_to_buffer (NewSocket *ns, char *buf, int len)
256{ 202{
257 int avail, end; 203 int avail, end;
258 204
259 if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE) 205 if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE)
260 { 206 {
414 cst_lst.obytes += amt; 360 cst_lst.obytes += amt;
415#endif 361#endif
416 } 362 }
417} 363}
418 364
419
420/** 365/**
421 * 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
422 * shortcut function. 367 * shortcut function.
423 */ 368 */
424void
425cs_write_string (NewSocket * ns, const char *buf, int len)
426{
427 SockList sl;
428 369
429 sl.len = len; 370void
430 sl.buf = (unsigned char *) buf; 371NewSocket::send_packet (packet &sl)
372{
431 Send_With_Handling (ns, &sl); 373 Send_With_Handling (this, &sl);
432} 374}
433 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}
434 390
435/** 391/**
436 * Calls Write_To_Socket to send data to the client. 392 * Calls Write_To_Socket to send data to the client.
437 * 393 *
438 * 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
439 *, and we prepend the length information. 395 *, and we prepend the length information.
440 */ 396 */
441void 397void
442Send_With_Handling (NewSocket * ns, SockList * msg) 398Send_With_Handling (NewSocket *ns, packet *msg)
443{ 399{
444 unsigned char sbuf[4]; 400 unsigned char sbuf[4];
445 401
446 if (ns->status == Ns_Dead || !msg) 402 if (ns->status == Ns_Dead || !msg)
447 return; 403 return;
458 sbuf[1] = ((uint32) (msg->len)) & 0xFF; 414 sbuf[1] = ((uint32) (msg->len)) & 0xFF;
459 if (ns->status != Ns_Old) 415 if (ns->status != Ns_Old)
460 Write_To_Socket (ns, (char *) sbuf, 2); 416 Write_To_Socket (ns, (char *) sbuf, 2);
461 Write_To_Socket (ns, (char *) msg->buf, msg->len); 417 Write_To_Socket (ns, (char *) msg->buf, msg->len);
462} 418}
463
464/**
465 * Takes a string of data, and writes it out to the socket. A very handy
466 * shortcut function.
467 */
468void
469Write_String_To_Socket (NewSocket * ns, char *buf, int len)
470{
471 SockList sl;
472
473 sl.len = len;
474 sl.buf = (unsigned char *) buf;
475 Send_With_Handling (ns, &sl);
476}
477
478 419
479/****************************************************************************** 420/******************************************************************************
480 * 421 *
481 * statistics logging functions. 422 * statistics logging functions.
482 * 423 *
509 cst_lst.obytes = 0; 450 cst_lst.obytes = 0;
510 cst_lst.max_conn = socket_info.nconns; 451 cst_lst.max_conn = socket_info.nconns;
511 cst_lst.time_start = now; 452 cst_lst.time_start = now;
512} 453}
513#endif 454#endif
455

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines