#ifndef __SAT_CLIENT__
#define __SAT_CLIENT__

#include "SocketHandle.h"
#include "SocketClientImpl.h"
//#include "SATProtocol.h"
#include "SATData.h"

#pragma once
class CSATClient:public ISocketClientHandler
{
	CSATClient(void);
	typedef SocketClientImpl<ISocketClientHandler> CSocketClient;
	friend CSocketClient;
protected:
	// ISocketServerHandler
	virtual void OnThreadBegin(CSocketHandle* pSH);
	virtual void OnThreadExit(CSocketHandle* pSH);
	virtual void OnDataReceived(CSocketHandle* pSH, const BYTE* pbData, DWORD dwCount, const SockAddrIn& addr);
	virtual void OnConnectionDropped(CSocketHandle* pSH);
	virtual void OnConnectionError(CSocketHandle* pSH, DWORD dwError);

public:
	static CSATClient* GetInstance()
	{
		static CSATClient* pInstance = NULL;
		if (pInstance == NULL)
		{
			pInstance = new CSATClient;
		}

		return pInstance;
	}

	~CSATClient(void);

private:
	BOOL m_bSocket;			// 是否连接成功;
	int m_nSockType;		// UDP 或 TCP
	int m_nMode;			// IP4 或 IP6
	std::string lastData;
	CSocketClient m_SocketClient;
	bool OnSend(const byte* pData, int nLen, BOOL bShowGif = TRUE);
	bool OnReceive(const byte* pData, int nLen);
	bool OnClose();
	bool OnConnect();
	void TaskProcess(SATPROTO::Package *pak);

public:
	// 启动TCP客户端连接实例;
	BOOL Start();
	// 停止TCP客户端连接;
	void Stop();
	// 多播（Multicasting）;
	bool SetupMCAST();
	// 获取本机地址;
	void GetAddress(const SockAddrIn& addrIn, CString& rString) const;

	// 登录;
	bool TCPLogin(std::string strUserName, std::string strPassword, std::string strActuator, BOOL bLogin = TRUE, BOOL bShowGif = TRUE);
	// 登出;
	bool TCPLogout(std::string strUserName, std::string strPassword, std::string strActuator, BOOL bShowGif = TRUE);

	// 添加设备;
	bool TCPAddDevice(std::string strDevice, bool bDel = false, BOOL bShowGif = TRUE);
	// 删除设备;
	bool TCPDelDevice(std::string strDevice, BOOL bShowGif = TRUE);
	// 查询设备;
	bool TCPQueryDevices(BOOL bShowGif = TRUE);
};

#endif // __SAT_CLIENT__