#include	"CliNetWaker.h"
#pragma unmanaged
/*
NetListener         CliNetWaker::_Listener;
CliNetWakerClass* 	CliNetWaker::_Net          = NULL;
int	                CliNetWaker::_ExitFlag     = false;
int                 CliNetWaker::_ProcTime     = 10;

//------------------------------------------------------------------------------------------
//紀錄登入事件函式的資料
vector< OnLoginFunctionBase >			CliNetWaker::_LoginFunctionList;
vector< OnLogoutFunctionBase >			CliNetWaker::_LogoutFunctionList;
vector< OnLoginFailFunctionBase >		CliNetWaker::_LoginFailFunctionList;

vector< OnSrvConnectFunctionBase >		CliNetWaker::_SrvConnectFunctionList;
vector< OnSrvDisconnectFunctionBase >	CliNetWaker::_SrvDisconnectFunctionList;
vector< OnSrvPacketFunctionBase >		CliNetWaker::_SrvPacketFunctionList;

vector< OnSrvMsgFunctionBase >			CliNetWaker::_SrvMsgFunctionList;
*/
//------------------------------------------------------------------------------------------

void	NetListener::OnLogin				( string info )
{
    _Parent->_OnLogin( info );
}
void	NetListener::OnLogout			    ( )
{
    _Parent->_OnLogout( );
}
void	NetListener::OnLoginFailed		    ( SYSLogFailedENUM type )
{
    _Parent->_OnLoginFailed( type );
}

void    NetListener::OnGSrvConnected        ( int id , string GSrvName)
{
    _Parent->_OnGSrvConnected( id , GSrvName );
}
void    NetListener::OnGSrvDisconnected     ( int id )
{
    _Parent->_OnGSrvDisconnected( id );
}
bool    NetListener::OnGSrvPacket           ( int id, int size , void* data )	
{
    return _Parent->_OnGSrvPacket( id , size , (PacketBase*)data );
}

void	NetListener::OnServerMsg			( char* Msg)
{
    _Parent->_OnServerMsg( Msg );
}

void    NetListener::OnCliReloginByOther ( int IPNum )
{
    _Parent->_OnCliReloginByOther( IPNum );
}
//------------------------------------------------------------------------------------------
void	CliNetWaker::_OnLogin				( string info )
{
    TRACE1( "\n_OnLogin : info=%s 登入" , info.c_str() );
    for( UInt32 i = 0 ; i < _LoginFunctionList.size() ; i++ )
    {
        _LoginFunctionList[i]( info );
    }

}
void	CliNetWaker::_OnLogout				( )
{
    TRACE0( "\n_OnLogout  登出" );
    for( UInt32 i = 0 ; i < _LogoutFunctionList.size() ; i++ )
    {
        _LogoutFunctionList[i]( );
    }
}
void	CliNetWaker::_OnLoginFailed		( SYSLogFailedENUM type )
{
    TRACE0( "\n_OnLoginFailed  登入失敗" );
    for( UInt32 i = 0 ; i < _LoginFailFunctionList.size() ; i++ )
    {
        _LoginFailFunctionList[i]( type );
    }
}

void	CliNetWaker::_OnGSrvConnected	    ( int id , string GSrvName )
{
    TRACE2( "\n_OnGSrvConnected  有GSrv成立聯線 id=%d GSrvName=%s" , id , GSrvName.c_str() );

    for( UInt32 i = 0 ; i < _SrvConnectFunctionList.size() ; i++ )
    {
        _SrvConnectFunctionList[i]( id , GSrvName );
    }
}
void	CliNetWaker::_OnGSrvDisconnected	( int id )
{
    TRACE1( "\n_OnGSrvDisconnected  有GSrv斷線 id=%d " , id );
    for( UInt32 i = 0 ; i < _SrvDisconnectFunctionList.size() ; i++ )
    {
        _SrvDisconnectFunctionList[i]( id );
    }
}
bool	CliNetWaker::_OnGSrvPacket			( int id , int size , PacketBase* data )
{
//    TRACE0( "\n_OnGSrvPacket " ); 

    PGSysNULL*    PG = (PGSysNULL*)data;

    if( (unsigned)data->Command >= _SrvPacketFunctionList.size() )
    {
        TRACE1("\n_OnSrvPacket 封包號碼過大?? ( %d )", PG->Command );
        return false;
    }
    OnSrvPacketFunctionBase PGFunc;

    PGFunc = _SrvPacketFunctionList[ data->Command ];

    if( PGFunc == NULL )
    {
        TRACE1("\n_OnSrvPacket 此封包尚未註冊?? ( %d )", PG->Command );
        return false;
    }
    PGFunc( id , size , data );

    return true;
}

void	CliNetWaker::_OnServerMsg			( char*	Msg )
{
    TRACE1( "\n_OnServerMsg  Msg = %s " , Msg );
}
//------------------------------------------------------------------------------------------


//------------------------------------------------------------------------------------------
//GateWay 資料交換處理
bool	CliNetWaker::Process( )
{
    _Net->Process( );
    return !_ExitFlag;
}
//------------------------------------------------------------------------------------------
//連到 Switch Server
void CliNetWaker::Connect( char* SwitchIP , int SwitchPort , char* Account , char* Password )
{
    //設定 GSrv 的名稱
    
    _Net->ConnectSwitch( SwitchIP , SwitchPort , Account , Password );

}
//------------------------------------------------------------------------------------------
CliNetWaker::CliNetWaker( )
{

    _ExitFlag     = false;
    _ProcTime     = 10;
    _Listener     = new NetListener( this );

    _Net = new CliNetWakerClass;
    _Net->SetListener( _Listener );
    
}
//------------------------------------------------------------------------------------------
CliNetWaker::~CliNetWaker( )
{
}
//------------------------------------------------------------------------------------------
//把自己Logout
void CliNetWaker::Logout( )
{
    _Net->Logout();
}
//***********************************************************************************
//登錄事件的函式 (ret true 為成功 false 為失敗)
bool    CliNetWaker::RegOnLoginFunction			( OnLoginFunctionBase Func )
{
    _LoginFunctionList.push_back( Func );
    return true;
}
bool    CliNetWaker::RegOnLogoutFunction			( OnLogoutFunctionBase Func )
{
    _LogoutFunctionList.push_back( Func );
    return true;
}
bool    CliNetWaker::RegOnLoginFailFunction		( OnLoginFailFunctionBase Func ) 
{
    _LoginFailFunctionList.push_back( Func );
    return true;
}
bool    CliNetWaker::RegOnSrvConnectFunction		( OnSrvConnectFunctionBase Func )
{
    _SrvConnectFunctionList.push_back( Func );
    return true;
}
bool    CliNetWaker::RegOnSrvDisConnectFunction	( OnSrvDisconnectFunctionBase Func )
{
    _SrvDisconnectFunctionList.push_back( Func );
    return true;
}
bool    CliNetWaker::RegOnSrvMsgFunction		(  OnSrvMsgFunctionBase Func)
{
    _SrvMsgFunctionList.push_back( Func );
    return true;
}
//已登入過的封包id不可重覆登入
bool    CliNetWaker::RegOnSrvPacketFunction		( int PGid , OnSrvPacketFunctionBase Func )
{
    if( PGid < 0  )
        return false;

    while(  (int)_SrvPacketFunctionList.size() <= PGid )
        _SrvPacketFunctionList.push_back( NULL );

    if( _SrvPacketFunctionList[ PGid ] != NULL )
    {
        TRACE1("\n **警告**RegOnSrvPacketFunction PGId=%d 重復註冊 " , PGid );
    }
    _SrvPacketFunctionList[ PGid ] = Func;

    return true;
}
//------------------------------------------------------------------------------------------
//送封包給Server or Client
bool    CliNetWaker::SendToSrv( int SrvID , int Size , void*	Buf )	
{
    return _Net->SendToGSrv( SrvID , Size , Buf );
}
//------------------------------------------------------------------------------------------
bool    CliNetWaker::RegOnCliReloginByOther		( OnCliReloginByOtherFunctionBase Func )
{
    _CliReloginByOtherFunctionList.push_back( Func );
    return true;
}
//------------------------------------------------------------------------------------------
void	CliNetWaker::_OnCliReloginByOther				( int IPNum )
{
    Print( Def_PrintLV1 , "_OnCliReloginByOther" , "其他玩家登入此帳號" );
    for( UInt32 i = 0 ; i < _CliReloginByOtherFunctionList.size() ; i++ )
    {
        _CliReloginByOtherFunctionList[i]( IPNum );
    }
}
//------------------------------------------------------------------------------------------
#pragma managed