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.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);
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 { 115}
112 int l = strlen (v);
113 memcpy (buf + len, v, l);
114 len += l;
115 }
116 116
117 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);
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;

Diff Legend

Removed lines
+ Added lines
< Changed lines
> Changed lines