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.10 by root, Wed Dec 13 18:51:50 2006 UTC vs.
Revision 1.13 by root, Thu Dec 14 01:12:35 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>
83 * 84 *
84 * SockList functions/utilities 85 * SockList functions/utilities
85 * 86 *
86 **********************************************************************/ 87 **********************************************************************/
87 88
89SockList &SockList::operator <<(const data &v)
90{
91 if (v.len)
92 {
93 memcpy (buf + len, v.ptr, v.len);
94 len += v.len;
95 }
96
97 return *this;
98}
99
88SockList &SockList::operator <<(const data8 &v) 100SockList &SockList::operator <<(const data8 &v)
89{ 101{
90 *this << uint8 (v.len); 102 unsigned int len = min (v.len, 0x00FF);
91 103 return *this << uint8 (len) << data (v.ptr, len);
92 memcpy (buf + len, v.data, v.len);
93 len += v.len;
94
95 return *this;
96} 104}
97 105
98SockList &SockList::operator <<(const data16 &v) 106SockList &SockList::operator <<(const data16 &v)
99{ 107{
100 *this << uint16 (v.len); 108 unsigned int len = min (v.len, 0xFFFF);
101 109 return *this << uint16 (len) << data (v.ptr, len);
102 memcpy (buf + len, v.data, v.len);
103 len += v.len;
104
105 return *this;
106} 110}
107 111
108SockList &SockList::operator <<(const char *v) 112SockList &SockList::operator <<(const char *v)
109{ 113{
110 if (v) 114 return *this << data (v, strlen (v ? v : 0));
111 {
112 int l = strlen (v);
113 memcpy (buf + len, v, l);
114 len += l;
115 }
116
117 return *this;
118} 115}
119 116
120/* Basically does the reverse of SockList_AddInt, but on 117void
121 * strings instead. Same for the GetShort, but for 16 bits. 118SockList::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 (SockList &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 SockList sl (MAXSOCKBUF);
380
381 sl << data (buf, len);
382 send_packet (sl);
383 sl.free ();
384}
385
386void
387NewSocket::send_packet (const char *buf)
388{
389 send_packet (buf, strlen (buf));
390}
434 391
435/** 392/**
436 * Calls Write_To_Socket to send data to the client. 393 * Calls Write_To_Socket to send data to the client.
437 * 394 *
438 * The only difference in this function is that we take a SockList 395 * The only difference in this function is that we take a SockList
439 *, and we prepend the length information. 396 *, and we prepend the length information.
440 */ 397 */
441void 398void
442Send_With_Handling (NewSocket * ns, SockList * msg) 399Send_With_Handling (NewSocket *ns, SockList *msg)
443{ 400{
444 unsigned char sbuf[4]; 401 unsigned char sbuf[4];
445 402
446 if (ns->status == Ns_Dead || !msg) 403 if (ns->status == Ns_Dead || !msg)
447 return; 404 return;
458 sbuf[1] = ((uint32) (msg->len)) & 0xFF; 415 sbuf[1] = ((uint32) (msg->len)) & 0xFF;
459 if (ns->status != Ns_Old) 416 if (ns->status != Ns_Old)
460 Write_To_Socket (ns, (char *) sbuf, 2); 417 Write_To_Socket (ns, (char *) sbuf, 2);
461 Write_To_Socket (ns, (char *) msg->buf, msg->len); 418 Write_To_Socket (ns, (char *) msg->buf, msg->len);
462} 419}
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 420
479/****************************************************************************** 421/******************************************************************************
480 * 422 *
481 * statistics logging functions. 423 * statistics logging functions.
482 * 424 *

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines