/****************************************************************************
** File name    :  HTTcpSocket.h
** Description  :  define common api for tcp socket
** Create date  :  2018.09.01
** Auther by    :  Liuyx
** Version info :  V1.0.01
** Copyright (C) 2002-2018 xi'an huatek, Inc Co., Ltd
** Update record:  
** DATE         AUTHER   DESC 
** -------------------------------------------------------------------------               
** 2018.09.01   Liuyx    first build
****************************************************************************/
#ifndef __HT_TCP_SOCKET_H
#define __HT_TCP_SOCKET_H

#include "HTPublic.h"
//#include <mstcpip.h>

static const int MAX_SEND_INFO = 512;    //最大发送包长度
static const int MAX_SBUFF_TCP = 2048;   //发送最大包长度
// static const int MAX_DATA_INFO = 1024;   //数据包做大长度

#ifdef _WIN32
#ifndef ECONNRESET
#define  ECONNRESET WSAECONNRESET        //errno= 104 = Connection reset by peer
#endif
#endif

#ifndef INVALID_HANDLE
#define INVALID_HANDLE        -1         // 无效句柄
#endif

/* socket listen number */
#define  LISTENQ              20
#define  MAX_TEMP_BUFFER_SIZE 2048

/* error code define */
#define  ErrOpenServer        -1000   /* Open Server Fail    */
#define  ErrSockParam         -1001   /* Socket Param error  */
#define  ErrSendFail          -1002   /* send data fail or socket exception*/
#define  ErrRecvTimeOut       -1003   /* recv data time out  */
#define  ErrRecvFail          -1004   /* recv data fail      */
#define  ErrOpenFile          -1005   /* open dest file fail */
#define  ErrSetSockOpt        -1006   /* set socket options  */
#define  ErrBindFail          -1007   /* set socket bind fail*/
#define  ErrGetPeerIP         -1008   /* Get PeerIP Fail  */
#define  ErrListenFail        -1009   /* socket listen error */
#define  ErrTimeStrFmt        -1020   /* Input time string error */
#define  ErrEintr             -1021   /* Interrupted system call,EINTR*/
#define  ErrException         -1022   /* socket exceptions close */


class TTcpSocket  
{
public:
	void  clear_tcp_buffer(int socket_fd, int nbytes);

	bool  tcpIsConnected(int sockid);
    
    // 根据sockid获取源地址和源端口
    int  getPeerInfo(int sockid, unsigned char *sip, unsigned short *sport);
    int  getLocalSocketInfo(int sockid, unsigned char *sip, unsigned short *sport);

	int  tcpRecvBuffer(int sockid, char* pBuffer,int nMaxLength, int timeout=6);
	int  tcpSendBuffer(int sockid, const char *pBuffer, int length);

	int  tcpAcceptSocket(int sockid);
	int  tcpCloseSocket(int sockfd);
	int  tcpOpenServer(const char *ip,unsigned short port);
    int  tcpSetlinger(int sockid);
    int  tcpSetNodelay(int sockid) ;
    int  tcpConnect(const char *pszIP, short port, int iTimeout);
    void tcpSetServerfd(int fd);
    void tcpSetSockID(int fd);
    int  tcpGetServerfd();
    int  tcpGetSockID();

	TTcpSocket();
	virtual ~TTcpSocket();

    //长连接测试函数
    //bool  tcpClient_Keep() ;
private:
    #ifndef _WIN32
    void SetSocketMode(int sockid,int iMode=O_NONBLOCK) ; //O_NONBLOCK=04000
    #else
    void SetSocketMode(int sockid,int iMode=04000) ; //O_NONBLOCK=04000
    #endif
private:
	unsigned short m_tcpPort;
	bool           m_tcpConnected;
    int            m_sockid;
    int            m_server_fd;
};

#endif // end __HT_TCP_SOCKET_H

