#ifndef __NETWAKERDEFINE_H__
#define __NETWAKERDEFINE_H__
/************************************
NetWaker 資料定義資料　
Revision By: hsiang
Revised on 2004/10/13 
Comments: 
************************************/
#include "NetWakerPackageDef.h"
#pragma	warning (disable:4200)
#pragma warning (disable:4949)
#include <string>
#include <vector>
#include <set>
#include <map>
#include "MyTypeDef.h"

#pragma unmanaged

using namespace std;

//-----------------------------------------------------------------------------------
/*
#define UInt8	unsigned char	
#define UInt16	unsigned short
#define UInt32	unsigned long

#define Int8	char
#define Int16	short 
#define Int32	long
*/
//-----------------------------------------------------------------------------------
//登入失敗列舉
//-----------------------------------------------------------------------------------
enum	SYSLogFailedENUM
{	
    EM_NetLogFailed_OK					= 0 ,   //沒錯誤
    EM_NetLogFailed_PasswordError		,		//密碼錯誤
    EM_NetLogFailed_ConnectFail			,		//連線不成功
    EM_NetLogFailed_ConnectError		,		//連結位置錯誤 
    EM_NetLogFailed_NotFindError		,		//找不到帳號
	EM_NetLogFailed_ProxyNotExist		,		//Proxy不存在
    EM_NetLogFailed_SwitchDisconnect    ,		//未完成註冊及與 Switch 斷線
	EM_NetLogFailed_ProxyLoginFailed    ,		//未完成註冊及與 Proxy 連線失敗
	EM_NetLogFailed_UnknowError		    ,		//暫時未規類的錯誤

    EM_SYSLogFailedEM_FORCE_DWORD		= 0x7fffffff ,
};
//-----------------------------------------------------------------------------------
namespace   NetWakerPackage
{

    
    #define     _Sys_Package_Number_        0xfffffffe 

    #define     _Max_Package_Size_		    0xffff	//最大封包大小
    #define     _Max_Cli_Package_Size_	    4096	//最大封包大小
    #define     _Max_Proxy_Count_           256
    #define     _Max_GSrv_Count_            256
    #define     _Max_Cli_Count_             65535
    #define     _Sys_Max_CallbackCount_     256

    #define     _Max_GSrv_Name_Size_        32      //最大GSrv 名偁長度
    #define     _Max_CliAccount_Name_Size_  32      //最大Client Account 名偁長度
    #define     _Max_CliPassword_Name_Size_ 32      //最大Client Password 名偁長度

    //-----------------------------------------------------------------------------------
    //狀態列舉
    //-----------------------------------------------------------------------------------
    enum	SYSNetStatENUM
    {	
        EM_NetStat_Unknow     = 0       ,   
        EM_NetStat_Connect	= 10	    ,	//連線狀態
        EM_NetStat_Login                ,   //登入中	
		EM_NetStat_LoginSrvOK           ,   //與Switch注測好 也通知過Proxy Addr
        EM_NetStat_ACLoginOK            ,   //Account Agent 註冊成功 ，但是還未完成連線
        EM_NetStat_OnDestory	    	,	//準備結束連線
        EM_NetStat_Relogin  	    	,	//準備結束連線
        EM_SYSNetStatEM_FORCE_DWORD   = 0x7fffffff ,
    };
    //-----------------------------------------------------------------------------------
    //管理等級列舉
    //-----------------------------------------------------------------------------------
    enum	SYSNetManageENUM
    {	
        EM_Manage_Normal	= 0		,	//一般
        EM_Manage_GM		= 10	,	//GM
        EM_Manage_GC		= 20	,	//遊戲創造者
        EM_SYSNetManageEM_FORCE_DWORD   = 0x7fffffff ,
    };
    //-----------------------------------------------------------------------------------
    //Server 型別列舉
    //-----------------------------------------------------------------------------------
    enum	SYSGSrvTypeENUM
    {	
        EM_GSrvType_Passive		= 0	,	//被動型
        EM_GSrvType_Active		= 1	,	//主動型
        EM_SYSGSrvTypeEM_FORCE_DWORD   = 0x7fffffff ,
    };
    //-----------------------------------------------------------------------------------
    //	GSrv & Cli 端設定的事件CallBack
    //-----------------------------------------------------------------------------------
    class NewWakerEventListener
    { 
    public:
        //表示登入GateWay Server
        virtual void  OnLogin( string info ) = 0;

        virtual	void  OnLogout( ) = 0;

        //表示登入GateWay Server失敗
        virtual void  OnLoginFailed(SYSLogFailedENUM) = 0;

        //與某一個Server成立連線
        virtual void  OnGSrvConnected( int id , string GSrvName ) = 0;
        //與某一個Server斷線
        virtual void  OnGSrvDisconnected( int id ) = 0;
        //收到封包
        virtual bool  OnGSrvPacket( int id, int size , void* data ) = 0;	

        //收到Client 的封包
        virtual bool  OnCliPacket( int id, int size , void* data ){return false;};	

        //某個 Client 連線
        virtual void  OnClientConnected( int id , string accountname){};
        //某個 Client 斷線
        virtual void  OnClientDisconnected( int id ){};
        //Server 傳送過來的訊息 ( Proxy or Switch)
        virtual void  OnServerMsg( char* Msg){};

        virtual void  OnCliReloginByOther( int IPNum ){ };
    };
    
    //-----------------------------------------------------------------------------------
    //來源結構
    //-----------------------------------------------------------------------------------
    struct SYSNetSourceStruct
    {	
        union 
        {
            UInt16  Type;
            struct 
            {
                bool Client  : 1;
                bool GSrv    : 1;
                bool Proxy   : 1;
                bool Switch  : 1;
                bool AccountAgent : 1;
            };
        };
        SYSNetSourceStruct()
        {
            Type = 0;
        }
    };
    //-----------------------------------------------------------------------------------
    struct  PGSysNULL
    {
        PacketCommandEnum Command;

        PGSysNULL()
        {
            Command = EM_NOUSED;
        }
    };
    //---------------------------------------------------------------------------
    //封包基本型
    struct PacketBase
    {
        PacketCommandEnum	Command;
        char    Data[0];

    };
    //-----------------------------------------------------------------------------------
    // Switch 內 Proxy 的連線資料
    struct ProxyInfoStruct
    {
        unsigned int		ID;				//連線號碼
        unsigned int		IPnum;			//ip number
        int                 GSrvPort;       //給Server聯結的Port
        int                 ClientPort;     //給Client聯結的Port

        SYSNetStatENUM		Stat;			//狀態

        set<int>			GSrvConnect;	//與哪些Server有作連結
        set<int>			CliConnect;	    //與哪些Cli有作連結

        ProxyInfoStruct()	{	Init();	};
        void	Init();
    };	
    //-----------------------------------------------------------------------------------
    // Switch 內 GSrv 的連線資料
    struct GSrvInfoStruct
    {
        unsigned int		ID;				//連線號碼
        unsigned int		IPnum;			//ip number
        SYSGSrvTypeENUM     Type;           //GSrv的模式

        char                GSrvName[ _Max_GSrv_Name_Size_ ];   

        SYSNetStatENUM		Stat;			//狀態

        set<int>			ProxyConnect;	//GSrv 與哪些Proxy有作連結
        set<int>			CliConnect;	    //GSrv 與哪些Cli有作連結

        GSrvInfoStruct()	{	Init();	};
        void	Init();
    };
    //-----------------------------------------------------------------------------------
    // Switch 內 Client 的連線資料
    struct CliInfoStruct
    {
        unsigned int		ID;				//連線號碼
        unsigned int		IPnum;			//ip number
        int                 ProxyID;        //聯結的Proxy
        int                 SwitchNetID;    //聯結的Switch
        
        string              Account;
        string              Password;

        SYSNetStatENUM		Stat;			//狀態

        set<int>			GSrvConnect;	//Cli 與那些 GSrv 有連線

        int                 LastCheckTime;
		int					TempScID;		//處理暫時排程資料()

        CliInfoStruct()	{	Init();	};
        void	Init();
    };
    //-----------------------------------------------------------------------------------
    // GSrv 內 Client 的連線資料
    struct GSrv_CliInfoStruct
    {
        unsigned int		ID;				//連線號碼
        unsigned int		IPnum;			//ip number
        int                 ProxyID;        //聯結的Proxy

        string              Account;

        GSrv_CliInfoStruct()	{	Init();	};
        void	Init();
    };
    //-----------------------------------------------------------------------------------
    //封包權限資料 與 來源限制 封包大小等資訊 

    struct	PGBaseInfo
    {
        SYSNetSourceStruct		Type;			//來源限制
        SYSNetManageENUM	    ManageLV;		//權限

        unsigned int			PGSize;	

        PGBaseInfo()
        {
            ManageLV	= EM_Manage_Normal;
            PGSize		= _Max_Package_Size_;
        };
    };
    //-----------------------------------------------------------------------------------
    //快速搜尋 A -> B 與 B -> A
    //A 與 B 都在 10000 以內   
    class Map_NetIDtoIDClass
    {
    #define _Max_Number_    10000
        vector< int > _IDToNetList;
        vector< int > _NetToIDList;
    public:
        //設定  NetID 關聯 ID
        bool    Set( int NetID , int ID );
        bool    Cls( int NetID , int ID );
        int     GetID( int NetID );
        int     GetNetID( int ID );
    };
    //---------------------------------------------------------------------------
}

#pragma managed
#endif //__NETWAKERDEFINE_H__