
#ifndef _MC_SOCK_MESSAGE_H_
#define _MC_SOCK_MESSAGE_H_

#include "messages.h"
#include <sys/epoll.h>
#include <sys/resource.h>

//data type
#ifndef _DEF_S8_
#define _DEF_S8_
typedef char S8;
#endif

#ifndef _DEF_U8_
#define _DEF_U8_
typedef unsigned char U8;
#endif

#ifndef _DEF_S16_
#define _DEF_S16_
typedef short S16;
#endif

#ifndef _DEF_U16_
#define _DEF_U16_
typedef unsigned short U16;
#endif

#ifndef _DEF_S32_
#define _DEF_S32_
typedef int S32;
#endif

#ifndef _DEF_U32_
#define _DEF_U32_
typedef unsigned int U32;
#endif


// buffer length
#define SOCK_MAX_READ_BUF_LEN	   32768
#define BUFF_MAX_READ_BUF_LEN	   32768
// magic code
#define VS						"VSTCPSOCK"
#define VS_MAGIC_LENGTH		   9
// socket error code
#define SOCK_MC_ERR_OK			  0x00
#define SOCK_MC_ERR_NULL_PTR		0x01
#define SOCK_MC_ERR_NO_INST		 0x02
#define SOCK_MC_ERR_FAIL_CREATE	 0x03
#define SOCK_MC_ERR_FAIL_SET_OPT	0x04
#define SOCK_MC_ERR_FAIL_BIND	   0x05
#define SOCK_MC_ERR_FAIL_LISTEN	 0x06
#define SOCK_MC_ERR_FAIL_CONNECT	0x07
#define SOCK_MC_ERR_FAIL_SEND_MSG   0x08
#define SOCK_MC_ERR_FAIL_GET_MSG	0x09
#define SOCK_MC_ERR_FAIL_START	  0x0a
#define SOCK_MC_ERR_FAIL_EXIT	   0x0a

class TSockHost
{
public:
	U32 IP;
	U16 port;
	TSockHost();
	~TSockHost();
	TSockHost(U32 nIp, U16 nPort);
	TSockHost(const TSockHost &host);
	void operator = (const TSockHost &host);
	int operator<(const TSockHost &host) const;
	int operator==(const TSockHost &host) const;
};

inline TSockHost::TSockHost() : IP(0), port(0)
{
}

inline TSockHost::~TSockHost()
{
}

inline TSockHost::TSockHost(U32 nIp, U16 nPort)
{
	IP = nIp;
	port = nPort;
}

inline TSockHost::TSockHost(const TSockHost &host)
{
	IP = host.IP;
	port = host.port;
}

inline void TSockHost::operator = (const TSockHost &host)
{
	IP = host.IP;
	port = host.port;
}
	
inline int TSockHost::operator<(const TSockHost& host) const
{
	if (IP == host.IP)
	{
		return (port < host.port);
	}
	else
	{
		return (IP < host.IP); 
	}
}

inline int TSockHost::operator==(const TSockHost& host) const
{
	return ((IP==host.IP) && (port==host.port));
}

/*
	U32 srcIP;
	U16 srcPort;
	U32 destIP;
	U16 destPort;
*/

typedef struct{
	U32 length;
	U32 srcIP;
	U32 destIP;
	U16 srcPort;
	U16 destPort;
	S32 nSockFd;
}sock_msg_info_t;

typedef struct{
	MSMsgHead_t head;
	S8  data[SOCK_MAX_READ_BUF_LEN];
}sock_msg_data_t;

typedef struct{
	sock_msg_info_t	info;
	sock_msg_data_t data;
}sock_data_t;

/***********************************************************/
/*			   define message type here				  */ 
/***********************************************************/

#define SOCK_MES_TYPE_HEARTBEAT			 0x0001
#define SOCK_MES_TYPE_REGISTER			  0x0002

#define SOCK_MES_TYPE_NVR_SMS			   0x0101
#define SOCK_MES_TYPE_CMS_VTDU			  0x0102
#define SOCK_MES_TYPE_CMS_NVR			   0x0103
#define SOCK_MES_TYPE_CMS_PU				0x0104

/***********************************************************/
/*			   define message struct here				  */ 
/***********************************************************/
typedef struct{
	S8 heartbeat[128];
} sock_mes_heartbeat_t;

typedef struct{
	U32 neID;
} sock_mes_register_t;

#define VS_TCPSERVER_EPOLL
const U64 MAX_TCPSERVER_EPOLL_SIZE = 10000; 


#endif

