add new protocol data type for IP addresses

(to avoid casting IPs to signed integer)

also mute single number debug messages for compound data structures like
string and key: no longer print a debug message for every char of a string
This commit is contained in:
ac-stef
2017-06-27 15:15:23 +02:00
parent f581506960
commit 0148c5b9a9
3 changed files with 52 additions and 8 deletions
+25 -2
View File
@@ -3,9 +3,9 @@
#include "cube.h"
#ifdef _DEBUG
bool protocoldbg = false;
bool protocoldbg = false, debugmute = false;
void protocoldebug(bool enable) { protocoldbg = enable; }
#define DEBUGCOND (protocoldbg)
#define DEBUGCOND (protocoldbg && !debugmute)
#endif
// all network traffic is in 32bit ints, which are then compressed using the following simple scheme (assumes that most values are small).
@@ -123,9 +123,11 @@ float getfloat(ucharbuf &p)
template<class T>
static inline void sendstring_(const char *text, T &p)
{
DEBUGCODE(debugmute = true);
const char *t = text;
if(t) { while(*t) putint(p, *t++); }
putint(p, 0);
DEBUGCODE(debugmute = false);
DEBUGVAR(text);
}
void sendstring(const char *t, ucharbuf &p) { sendstring_(t, p); }
@@ -134,6 +136,7 @@ void sendstring(const char *t, vector<uchar> &p) { sendstring_(t, p); }
void getstring(char *text, ucharbuf &p, int len)
{
DEBUGCODE(debugmute = true);
char *t = text;
do
{
@@ -142,9 +145,29 @@ void getstring(char *text, ucharbuf &p, int len)
*t = getint(p);
}
while(*t++);
DEBUGCODE(debugmute = false);
DEBUGVAR(text);
}
template<class T>
static inline void putip4_(T &p, enet_uint32 ip)
{
DEBUGCODE(string IP; iptoa(ip, IP));
DEBUGVAR(IP);
p.put(ip); p.put(ip>>8); p.put(ip>>16); p.put(ip>>24);
}
void putip4(ucharbuf &p, enet_uint32 ip) { putip4_(p, ip); }
void putip4(packetbuf &p, enet_uint32 ip) { putip4_(p, ip); }
void putip4(vector<uchar> &p, enet_uint32 ip) { putip4_(p, ip); }
enet_uint32 getip4(ucharbuf &p)
{
enet_uint32 ip = p.get() | (p.get()<<8) | (p.get()<<16) | (p.get()<<24);
DEBUGCODE(string IP; iptoa(ip, IP));
DEBUGVAR(IP);
return ip;
}
#define GZMSGBUFSIZE ((MAXGZMSGSIZE * 11) / 10)
static uchar *gzbuf = new uchar[GZMSGBUFSIZE]; // not thread-safe, but nesting is no problem
+4
View File
@@ -1059,6 +1059,10 @@ extern void sendstring(const char *t, ucharbuf &p);
extern void sendstring(const char *t, packetbuf &p);
extern void sendstring(const char *t, vector<uchar> &p);
extern void getstring(char *t, ucharbuf &p, int len = MAXTRANS);
extern void putip4(ucharbuf &p, enet_uint32 ip);
extern void putip4(packetbuf &p, enet_uint32 ip);
extern void putip4(vector<uchar> &p, enet_uint32 ip);
extern enet_uint32 getip4(ucharbuf &p);
extern void putgzbuf(vector<uchar> &d, vector<uchar> &s); // zips a vector into a stream, stored in another vector
extern ucharbuf *getgzbuf(ucharbuf &p); // fetch a gzipped buffer; needs to be freed properly later
extern void freegzbuf(ucharbuf *p); // free a ucharbuf created by getgzbuf()
+23 -6
View File
@@ -584,11 +584,11 @@ void sendf(int cn, int chan, const char *format, ...)
va_start(args, format);
while(*format) switch(*format++)
{
case 'x':
case 'x': // exclude client from getting this message
exclude = va_arg(args, int);
break;
case 'v':
case 'v': // integer array of variable length
{
int n = va_arg(args, int);
int *v = va_arg(args, int *);
@@ -596,20 +596,37 @@ void sendf(int cn, int chan, const char *format, ...)
break;
}
case 'i':
case 'k': // 32 bytes (uchar)
p.put(va_arg(args, uchar *), 32);
break;
case 'i': // one to nine integer arguments
{
int n = isdigit(*format) ? *format++-'0' : 1;
loopi(n) putint(p, va_arg(args, int));
break;
}
case 'a':
case 'a': // one to nine mostly positive integer arguments
{
int n = isdigit(*format) ? *format++-'0' : 1;
loopi(n) putaint(p, va_arg(args, int));
break;
}
case 's': sendstring(va_arg(args, const char *), p); break;
case 'm':
case 'u': // unsigned integer argument
putuint(p, va_arg(args, int));
break;
case 'I': // IPv4 address argument
putip4(p, va_arg(args, enet_uint32));
break;
case 's': // string
sendstring(va_arg(args, const char *), p);
break;
case 'm': // raw message block (unused)
{
int n = va_arg(args, int);
p.put(va_arg(args, uchar *), n);