#pragma once
#include "../HPSocket/Include/HPSocket/SocketInterface.h"
#include  <string>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

class CNetTcpClientListener : public CTcpClientListener
{
public:
	CNetTcpClientListener();
	~CNetTcpClientListener();

public:
	virtual EnHandleResult OnPrepareConnect(ITcpClient* pSender, CONNID dwConnID, SOCKET socket);
	virtual EnHandleResult OnSend(ITcpClient* pSender, CONNID dwConnID, const BYTE* pData, int iLength);
	virtual EnHandleResult OnReceive(ITcpClient* pSender, CONNID dwConnID, const BYTE* pData, int iLength);
	virtual EnHandleResult OnClose(ITcpClient* pSender, CONNID dwConnID, EnSocketOperation enOperation, int iErrorCode);
	virtual EnHandleResult OnConnect(ITcpClient* pSender, CONNID dwConnID);
	virtual EnHandleResult OnShutdown(ITcpServer* pSender);

	int OnReadHead(void* pData, int nLen);
	int SwapInt(int value);

	int HandlerPack(ITcpClient* pSender, CONNID dwConnID, const BYTE* pData, int iLength);
	long GetPackgeId() { return m_nPackgeId; };

private:
	BOOL			m_bMultiPacks;
	int				m_nLenTotal;
	int				m_nLenRecv;
	string			m_str;
	long  m_nPackgeId;
};
class CTcpClient
{
public:
	CTcpClient();
	~CTcpClient();
private:
	string m_strHostAddr;
	int    m_nPort;
	bool  m_bConnected;

	ITcpClient *m_pTcpClient;
	CNetTcpClientListener m_TcpClientListener;
public:
	void Init(string strHost, int nPort);
	bool IsConnected() { return m_bConnected; };
	void SendMsg(char* pBuf, int nLen);
	long GetPackgeId() {
		return m_TcpClientListener.GetPackgeId();
	}
};

