Blame common.h

dd2ea4
#ifndef COMMON_H
dd2ea4
#define COMMON_H
dd2ea4
dd2ea4
dd2ea4
enum {
dd2ea4
	PACKET_SIZE     = 1024,
dd2ea4
	PACKETS_COUNT   = 8*PACKET_SIZE,
dd2ea4
	TCP_BUFFER_SIZE = PACKET_SIZE*PACKETS_COUNT,
dd2ea4
};
dd2ea4
dd2ea4
enum : unsigned short {
dd2ea4
	CMD_SHIFT   = 12,
dd2ea4
	CMD_MASK    = (unsigned short)(0xffff << CMD_SHIFT),
dd2ea4
	SIZE_MASK   = (unsigned short)(~CMD_MASK),
dd2ea4
	
dd2ea4
	CMD_DATA    = 0,
dd2ea4
	CMD_TERM    = 1,
dd2ea4
	CMD_CONFIRM = 2,
dd2ea4
};
dd2ea4
dd2ea4
dd2ea4
dd2ea4
typedef unsigned long long Time;
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
	enum { HEADER_SIZE = ((Packet*)nullptr)->payload - nullptr; };
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
	
dd2ea4
	inline 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;
dd2ea4
		this->cmd = cmd;
dd2ea4
		this->size = size;
dd2ea4
	}
dd2ea4
};
dd2ea4
dd2ea4
dd2ea4
#endif