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.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>
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);
109 return *this << uint16 (len) << data (v.ptr, len);
110}
101 111
102 memcpy (buf + len, v.data, v.len); 112SockList &SockList::operator <<(const char *v)
103 len += v.len; 113{
114 return *this << data (v, strlen (v ? v : 0));
115}
104 116
105 return *this; 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);
106} 126}
107 127
108/* Basically does the reverse of SockList_AddInt, but on 128/* Basically does the reverse of SockList_AddInt, but on
109 * strings instead. Same for the GetShort, but for 16 bits. 129 * strings instead. Same for the GetShort, but for 16 bits.
110 */ 130 */
133 * 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
134 * at least 2 bytes long. 154 * at least 2 bytes long.
135 */ 155 */
136 156
137int 157int
138SockList_ReadPacket (int fd, SockList * sl, int len) 158SockList_ReadPacket (int fd, SockList *sl, int len)
139{ 159{
140 int stat, toread; 160 int stat, toread;
141 161
142 /* Sanity check - shouldn't happen */ 162 /* Sanity check - shouldn't happen */
143 if (sl->len < 0) 163 if (sl->len < 0)
408/** 428/**
409 * 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
410 * shortcut function. 430 * shortcut function.
411 */ 431 */
412void 432void
413cs_write_string (NewSocket * ns, const char *buf, int len) 433cs_write_string (NewSocket *ns, const char *buf, int len)
414{ 434{
415 SockList sl; 435 SockList sl;
416 436
417 sl.len = len; 437 sl.len = len;
418 sl.buf = (unsigned char *) buf; 438 sl.buf = (unsigned char *) buf;
425 * 445 *
426 * 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
427 *, and we prepend the length information. 447 *, and we prepend the length information.
428 */ 448 */
429void 449void
430Send_With_Handling (NewSocket * ns, SockList * msg) 450Send_With_Handling (NewSocket *ns, SockList *msg)
431{ 451{
432 unsigned char sbuf[4]; 452 unsigned char sbuf[4];
433 453
434 if (ns->status == Ns_Dead || !msg) 454 if (ns->status == Ns_Dead || !msg)
435 return; 455 return;
452/** 472/**
453 * 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
454 * shortcut function. 474 * shortcut function.
455 */ 475 */
456void 476void
457Write_String_To_Socket (NewSocket * ns, char *buf, int len) 477Write_String_To_Socket (NewSocket *ns, char *buf, int len)
458{ 478{
459 SockList sl; 479 SockList sl;
460 480
461 sl.len = len; 481 sl.len = len;
462 sl.buf = (unsigned char *) buf; 482 sl.buf = (unsigned char *) buf;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines