/*************************************************************
/* Copyright (C), 2014-2020, lyfz. Co., Ltd.
/* 文件名:		IServerImpl.h 
/* 作者:		Jeff.w			 
/* 创建日期:	2014-08-07
/* 版本号:		V1.0
/* 描述:		核心层底,客户端与服务端通信接口
/* 其它:  
/* 主要类模块: 
/* 历史修改记录: 
/*		作者           时间              版本           描述
/*	    Jeff.w         14/08/07          1.0	      创建这个模块
***************************************************************/

#pragma once

#include <afxwin.h>
#include <atlbase.h>
//#pragma warning(push)
//#pragma warning(disable:4995)
#include <list>
//#pragma warning(pop)
#include "CritSection.h"
#include "SocketHandle.h"
#include "SocketServerImpl.h"
#if defined(SOCKHANDLE_USE_OVERLAPPED)
#include "AsyncSocketServerImpl.h"
#endif

#include "Global.h"

// 宏定义:计算数据包总长度;
#define CALCDATALEN(x) (x+sizeof(STProtocolheader)+sizeof(STProtocoltail))

// 使用通信这一块,完全分离出来;
namespace ServerSocketImpl
{
	typedef std::list<SOCKET> CSocketList;
	class IServerImpl :public CDialog,public ISocketServerHandler
	{
#if defined(SOCKHANDLE_USE_OVERLAPPED)
		typedef ASocketServerImpl<ISocketServerHandler> CSocketServer;
#else
		typedef SocketServerImpl<ISocketServerHandler> CSocketServer;
#endif

		friend CSocketServer;

	public:
		IServerImpl();
		~IServerImpl();

		virtual void OnThreadBegin(CSocketHandle* pSH);
		virtual void OnThreadExit(CSocketHandle* pSH);
		virtual void OnAddConnection(CSocketHandle* pSH, SOCKET newSocket);
		virtual void OnDataReceived(CSocketHandle* pSH, const BYTE* pbData, DWORD dwCount, const SockAddrIn& addr);
		virtual void OnConnectionFailure(CSocketHandle* pSH, SOCKET newSocket);
		virtual void OnConnectionDropped(CSocketHandle* pSH);
		virtual void OnConnectionError(CSocketHandle* pSH, DWORD dwError);

	protected:
		CString m_strPort;
		int m_nPort;
		int m_nMode;
		int m_nSockType;

		CSocketServer m_SocketServer;

		void _cdecl AppendText(LPCTSTR lpszFormat, ...);
		void GetAddress(const SockAddrIn& addrIn, CString& rString) const;
		bool GetDestination(SockAddrIn& addrIn) const;
		bool SetupMCAST();

		// Jeff.2010年的处理方式;
		void ProcessData(CSocketHandle &sockHandle, const BYTE* pData, DWORD nDataLen);			// Jeff.数据包有效长度包含包头;
		int  OnCmdProcess(CSocketHandle &sockHandle, void *pData);

		// Jeff.2014年
		void ManipulationData(IN CSocketHandle &sockHandle, IN const BYTE* pData, IN DWORD nDataLen);	// Jeff.数据包有效长度不包含包头;
		int  OnCmdManipulation(IN CSocketHandle &sockHandle, IN void *pData);

		// Jeff.最新研究出来的处理方案;
		// 对异常包需要计数处理,当1分钟内出现多少异常包时,需要重启服务函数;
		unsigned int VerityIntegrityPacket(IN void *pIntegrityPacket,unsigned int nPacketSize);
		void ToprocessRecivebuf(IN CSocketHandle &sockHandle, IN const BYTE* pReceivebuf, IN DWORD dwReceiveSize);
		int  OnIntegrityPacket(IN CSocketHandle &sockHandle, IN void *pIntegrityPacket);

	public:
		int m_nSocketIndex;
		CRITICAL_SECTION m_csProcessData;

		BOOL Initialize();
		void Start(IN LPCTSTR strPort,IN const int &nMode);
		void Stop();
		void Send();
		void SendAll(CSocketHandle &sockHandle, unsigned char *pMsg, int nLength);

	};

	extern IServerImpl* g_pServerSocket[20];
};
