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.9 by root, Wed Dec 13 18:08:02 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>
79#endif 80#endif
80} 81}
81 82
82/*********************************************************************** 83/***********************************************************************
83 * 84 *
84 * SockList functions/utilities 85 * packet functions/utilities
85 * 86 *
86 **********************************************************************/ 87 **********************************************************************/
87 88
88SockList &SockList::operator <<(const data8 &v) 89packet &packet::operator <<(const data &v)
89{ 90{
90 *this << uint8 (v.len); 91 if (v.len)
91 92 {
92 memcpy (buf + len, v.data, v.len); 93 memcpy (buf + len, v.ptr, v.len);
93 len += v.len; 94 len += v.len;
95 }
94 96
95 return *this; 97 return *this;
96} 98}
97 99
100packet &packet::operator <<(const data8 &v)
101{
102 unsigned int len = min (v.len, 0x00FF);
103 return *this << uint8 (len) << data (v.ptr, len);
104}
105
98SockList &SockList::operator <<(const data16 &v) 106packet &packet::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
108/* Basically does the reverse of SockList_AddInt, but on 112packet &packet::operator <<(const char *v)
109 * strings instead. Same for the GetShort, but for 16 bits. 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/******************************************************************************
110 */ 129 *
130 * Start of read routines.
131 *
132 ******************************************************************************/
133
111int 134int
112GetInt_String (unsigned char *data) 135NewSocket::read_packet ()
113{ 136{
114 return ((data[0] << 24) + (data[1] << 16) + (data[2] << 8) + data[3]); 137 for (;;)
115}
116
117short
118GetShort_String (unsigned char *data)
119{
120 return ((data[0] << 8) + data[1]);
121}
122
123/******************************************************************************
124 *
125 * Start of read routines.
126 *
127 ******************************************************************************/
128
129/**
130 * This reads from fd and puts the data in sl. We return true if we think
131 * we have a full packet, 0 if we have a partial packet. The only processing
132 * we do is remove the intial size value. len (As passed) is the size of the
133 * buffer allocated in the socklist. We make the assumption the buffer is
134 * at least 2 bytes long.
135 */
136
137int
138SockList_ReadPacket (int fd, SockList * sl, int len)
139{
140 int stat, toread;
141
142 /* Sanity check - shouldn't happen */
143 if (sl->len < 0)
144 {
145 abort ();
146 } 138 {
147 /* We already have a partial packet */ 139 if (inbuf_len >= 2)
148 if (sl->len < 2)
149 {
150 do
151 {
152 stat = read (fd, sl->buf + sl->len, 2 - sl->len);
153 } 140 {
154 while ((stat == -1) && (errno == EINTR)); 141 unsigned int pkt_len = (inbuf [0] << 8) | inbuf [1];
155 142
156 if (stat < 0) 143 if (inbuf_len >= 2 + pkt_len)
144 return pkt_len + 2;
157 { 145 }
158 /* In non blocking mode, EAGAIN is set when there is no 146
159 * data available. 147 int amount = sizeof (inbuf) - inbuf_len;
160 */ 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 {
161 if (errno != EAGAIN && errno != EWOULDBLOCK) 164 if (errno != EAGAIN && errno != EINTR)
162 { 165 {
163 LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno)); 166 LOG (llevError, "read error: %s", strerror (errno));
167 return -1;
164 } 168 }
165 return 0; /*Error */
166 }
167 if (stat == 0)
168 return -1;
169 sl->len += stat;
170#ifdef CS_LOGSTATS
171 cst_tot.ibytes += stat;
172 cst_lst.ibytes += stat;
173#endif
174 if (stat < 2)
175 return 0; /* Still don't have a full packet */
176 }
177 /* Figure out how much more data we need to read. Add 2 from the
178 * end of this - size header information is not included.
179 */
180 toread = 2 + (sl->buf[0] << 8) + sl->buf[1] - sl->len;
181 if ((toread + sl->len) >= len)
182 {
183 LOG (llevError, "SockList_ReadPacket: Want to read more bytes than will fit in buffer (%d>=%d).\n", toread + sl->len, len);
184 /* Quick hack in case for 'oldsocketmode' input. If we are
185 * closing the socket anyways, then reading this extra 100 bytes
186 * shouldn't hurt.
187 */
188 read (fd, sl->buf + 2, 100);
189 169
190 /* return error so the socket is closed */
191 return -1;
192 }
193 do
194 {
195 do
196 {
197 stat = read (fd, sl->buf + sl->len, toread);
198 }
199 while ((stat < 0) && (errno == EINTR));
200 if (stat < 0)
201 {
202
203 if (errno != EAGAIN && errno != EWOULDBLOCK)
204 {
205 LOG (llevDebug, "ReadPacket got error %s, returning 0\n", strerror (errno));
206 }
207 return 0; /*Error */
208 }
209 if (stat == 0)
210 return -1;
211 sl->len += stat;
212#ifdef CS_LOGSTATS
213 cst_tot.ibytes += stat;
214 cst_lst.ibytes += stat;
215#endif
216 toread -= stat;
217 if (toread == 0)
218 return 1;
219 if (toread < 0)
220 {
221 LOG (llevError, "SockList_ReadPacket: Read more bytes than desired.\n");
222 return 1; 170 return 0;
223 } 171 }
172
173 inbuf_len += amount;
174
175 cst_tot.ibytes += amount;
176 cst_lst.ibytes += amount;
224 } 177 }
225 while (toread > 0); 178}
226 return 0; 179
180void
181NewSocket::skip_packet (int len)
182{
183 inbuf_len -= len;
184 memmove (inbuf, inbuf + len, inbuf_len);
227} 185}
228 186
229/******************************************************************************* 187/*******************************************************************************
230 * 188 *
231 * Start of write related routines. 189 * Start of write related routines.
238 * 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
239 * data, and len is the number of bytes to add. 197 * data, and len is the number of bytes to add.
240 */ 198 */
241 199
242static void 200static void
243add_to_buffer (NewSocket * ns, char *buf, int len) 201add_to_buffer (NewSocket *ns, char *buf, int len)
244{ 202{
245 int avail, end; 203 int avail, end;
246 204
247 if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE) 205 if ((len + ns->outputbuffer.len) > SOCKETBUFSIZE)
248 { 206 {
402 cst_lst.obytes += amt; 360 cst_lst.obytes += amt;
403#endif 361#endif
404 } 362 }
405} 363}
406 364
407
408/** 365/**
409 * 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
410 * shortcut function. 367 * shortcut function.
411 */ 368 */
412void
413cs_write_string (NewSocket * ns, const char *buf, int len)
414{
415 SockList sl;
416 369
417 sl.len = len; 370void
418 sl.buf = (unsigned char *) buf; 371NewSocket::send_packet (packet &sl)
372{
419 Send_With_Handling (ns, &sl); 373 Send_With_Handling (this, &sl);
420} 374}
421 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}
422 390
423/** 391/**
424 * Calls Write_To_Socket to send data to the client. 392 * Calls Write_To_Socket to send data to the client.
425 * 393 *
426 * 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
427 *, and we prepend the length information. 395 *, and we prepend the length information.
428 */ 396 */
429void 397void
430Send_With_Handling (NewSocket * ns, SockList * msg) 398Send_With_Handling (NewSocket *ns, packet *msg)
431{ 399{
432 unsigned char sbuf[4]; 400 unsigned char sbuf[4];
433 401
434 if (ns->status == Ns_Dead || !msg) 402 if (ns->status == Ns_Dead || !msg)
435 return; 403 return;
446 sbuf[1] = ((uint32) (msg->len)) & 0xFF; 414 sbuf[1] = ((uint32) (msg->len)) & 0xFF;
447 if (ns->status != Ns_Old) 415 if (ns->status != Ns_Old)
448 Write_To_Socket (ns, (char *) sbuf, 2); 416 Write_To_Socket (ns, (char *) sbuf, 2);
449 Write_To_Socket (ns, (char *) msg->buf, msg->len); 417 Write_To_Socket (ns, (char *) msg->buf, msg->len);
450} 418}
451
452/**
453 * Takes a string of data, and writes it out to the socket. A very handy
454 * shortcut function.
455 */
456void
457Write_String_To_Socket (NewSocket * ns, char *buf, int len)
458{
459 SockList sl;
460
461 sl.len = len;
462 sl.buf = (unsigned char *) buf;
463 Send_With_Handling (ns, &sl);
464}
465
466 419
467/****************************************************************************** 420/******************************************************************************
468 * 421 *
469 * statistics logging functions. 422 * statistics logging functions.
470 * 423 *

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines