Blame common.h

dd2ea4
#ifndef COMMON_H
dd2ea4
#define COMMON_H
dd2ea4
dd2ea4
460064
#include <cstdio></cstdio>
460064
460064
460064
#define HideDebugMSG(...) do {} while(0)
460064
460064
#ifdef NDEBUG
460064
#	define ShowDebugMSG(...) do {} while(0)
460064
#else
460064
#	define ShowDebugMSG(...) do { \
460064
		printf("DEBUG %16lld %s:%s:%d:", monotonicTime(), __FILE__, __func__, __LINE__); \
460064
		printf(" " __VA_ARGS__); \
460064
		printf("\n"); \
460064
		fflush(stdout); \
460064
	} while(0)
460064
#endif
460064
460064
460064
dd2ea4
enum {
0f653b
	PACKET_SIZE     = 470,
dd2ea4
	PACKETS_COUNT   = 8*PACKET_SIZE,
dd2ea4
	TCP_BUFFER_SIZE = PACKET_SIZE*PACKETS_COUNT,
937c1c
	CRYPT_BLOCK     = 128,
0f653b
	HASH_SIZE       = 32,
dd2ea4
};
dd2ea4
c47604
dd2ea4
enum : unsigned short {
dd2ea4
	CMD_SHIFT   = 12,
dd2ea4
	CMD_MASK    = (unsigned short)(0xffff << CMD_SHIFT),
dd2ea4
	SIZE_MASK   = (unsigned short)(~CMD_MASK),
dd2ea4
	
c47604
	CMD_DATA      = 0,
c47604
	CMD_CONFIRM   = 1,
c47604
	CMD_TERM      = 2,
c47604
	CMDMAX        = 3,
dd2ea4
};
dd2ea4
dd2ea4
dd2ea4
dd2ea4
typedef unsigned long long Time;
873e9f
Time monotonicTime();
873e9f
Time globalTime();
460064
const char* bitsToString(const void *data, int size);
dd2ea4
dd2ea4
dd2ea4
inline bool cycleLequal(unsigned int a, unsigned int b)
dd2ea4
	{ return (b - a) <= 0x7fffffff; }
dd2ea4
inline bool cycleLess(unsigned int a, unsigned int b)
dd2ea4
	{ return !cycleLequal(b, a); }
dd2ea4
inline bool timeLequal(Time a, Time b)
dd2ea4
	{ return (b - a) <= 0x7fffffffffffffffull; }
dd2ea4
inline bool timeLess(Time a, Time b)
dd2ea4
	{ return !timeLequal(b, a); }
dd2ea4
dd2ea4
dd2ea4
dd2ea4
class Address {
dd2ea4
public:
dd2ea4
	union {
dd2ea4
		struct { unsigned int ipUInt; };
dd2ea4
		struct { unsigned char ip[4]; };
dd2ea4
	};
dd2ea4
	unsigned short port;
dd2ea4
	inline Address(): ipUInt(), port() { }
dd2ea4
	inline bool operator<(const Address &other) const {
dd2ea4
		return ipUInt < other.ipUInt ? true
dd2ea4
		     : other.ipUInt < ipUInt ? false
dd2ea4
		     : port < other.port;
dd2ea4
	}
dd2ea4
	
dd2ea4
	void print() const;
dd2ea4
};
dd2ea4
dd2ea4
dd2ea4
class ConnId {
dd2ea4
public:
dd2ea4
	Address address;
dd2ea4
	unsigned int id;
dd2ea4
	inline ConnId(): id() { }
dd2ea4
	inline bool operator<(const ConnId &other) const {
dd2ea4
		return address < other.address ? true
dd2ea4
				: other.address < address ? false
dd2ea4
				: id < other.id;
dd2ea4
	}
dd2ea4
};
dd2ea4
dd2ea4
dd2ea4
struct __attribute__((__packed__)) Packet {
dd2ea4
	unsigned int connId;
dd2ea4
	unsigned int index;
dd2ea4
	unsigned short cmdSize;
dd2ea4
	unsigned char payload[PACKET_SIZE];
dd2ea4
	
dd2ea4
	
dd2ea4
	inline Packet():
dd2ea4
		connId(), index(), cmdSize(), payload() { }
dd2ea4
		
dd2ea4
	inline int getSize() const
dd2ea4
		{ return cmdSize & SIZE_MASK; }
dd2ea4
	inline int getCommand() const
dd2ea4
		{ return cmdSize >> CMD_SHIFT; }
dd2ea4
	
873e9f
	inline void init(
dd2ea4
		unsigned int connId,
dd2ea4
		unsigned int index,
dd2ea4
		unsigned short cmd,
dd2ea4
		unsigned short size )
dd2ea4
	{
dd2ea4
		this->connId = connId;
dd2ea4
		this->index = index;
873e9f
		this->cmdSize = (cmd << CMD_SHIFT) | size;
dd2ea4
	}
dd2ea4
};
dd2ea4
dd2ea4
c47604
enum {
c47604
	FULL_PACKET_SIZE  = sizeof(Packet),
c47604
	HEADER_SIZE       = FULL_PACKET_SIZE - PACKET_SIZE,
0f653b
	CRYPT_PACKET_SIZE = ((FULL_PACKET_SIZE + HASH_SIZE - 1)/CRYPT_BLOCK + 1)*CRYPT_BLOCK,
c47604
};
c47604
c47604
c47604
dd2ea4
#endif