#include "CliNetWakerClass.h"
#pragma unmanaged
//---------------------------------------------------------------------------------
/*
void LSAPI LSwitch::OnConnected      ( LNETID NetID )
{
    TRACE0( "\nOnSwitchConnected" );
    Parent->_OnSwitchConnected( NetID );
}
void LSAPI LSwitch::OnDisconnected   ( LNETID NetID )
{
    TRACE0( "\nOnSwitchDisconnected" );
    Parent->_OnSwitchDisconnected( NetID );
}
void LSAPI LSwitch::OnPeerShutdown   ( LNETID NetID )
{
    TRACE0( "\nOnSwitchPeerShutdown" );
    Parent->_OnSwitchDisconnected( NetID );
}
bool LSAPI LSwitch::OnPacket         ( LNETID NetID, HBUFFER hIncomingData )
{
    
    return Parent->_OnSwitchPacket( NetID , hIncomingData );
}
void LSAPI LSwitch::OnConnectFailed  ( int NetID )
{
    TRACE0( "\nOnSwitchConnectFailed" );
    Parent->_OnSwitchConnectFailed( NetID );
}
*/
bool		LBSwitch::OnRecv			( DWORD dwNetID, DWORD dwPackSize, PVOID pPacketData )
{
    return _Parent->_OnSwitchPacket(  dwNetID, dwPackSize, pPacketData );
}
void		LBSwitch::OnConnectFailed	( DWORD dwFailedCode )
{
    TRACE0( "\nOnSwitchConnectFailed" );
    _Parent->_OnSwitchConnectFailed( dwFailedCode );
}
void		LBSwitch::OnConnect		( DWORD dwNetID )
{
    TRACE0( "\nOnSwitchConnected" );
    _Parent->_OnSwitchConnected( dwNetID );
}
void		LBSwitch::OnDisconnect	( DWORD dwNetID )
{
    TRACE0( "\nOnSwitchDisconnected" );
    _Parent->_OnSwitchDisconnected( dwNetID );
}
CEventObj*	LBSwitch::OnAccept		( DWORD dwNetID )
{
    return this;
}
//---------------------------------------------------------------------------------
/*
void LSAPI LProxy::OnConnected      ( LNETID NetID )
{
    TRACE0( "\n_OnProxyConnected" );
    Parent->_OnProxyConnected( NetID );
}
void LSAPI LProxy::OnDisconnected   ( LNETID NetID )
{
    TRACE0( "\n_OnProxyDisconnected" );
    Parent->_OnProxyDisconnected( NetID );
}
void LSAPI LProxy::OnPeerShutdown   ( LNETID NetID )
{
    TRACE0( "\n_OnProxyDisconnected" );
    Parent->_OnProxyDisconnected( NetID );
}
bool LSAPI LProxy::OnPacket         ( LNETID NetID, HBUFFER hIncomingData )
{

    return Parent->_OnProxyPacket( NetID , hIncomingData );
}
void LSAPI LProxy::OnConnectFailed  ( int NetID )
{
    TRACE0( "\n_OnProxyConnectFailed" );
    Parent->_OnProxyConnectFailed( NetID );
}
*/
bool		LBProxy::OnRecv			( DWORD dwNetID, DWORD dwPackSize, PVOID pPacketData )
{
    return _Parent->_OnProxyPacket( dwNetID , dwPackSize , pPacketData );
}
void		LBProxy::OnConnectFailed	( DWORD dwFailedCode )
{
    TRACE0( "\n_OnProxyConnectFailed" );
    _Parent->_OnProxyConnectFailed( dwFailedCode );
}
void		LBProxy::OnConnect		( DWORD dwNetID )
{
    TRACE0( "\n_OnProxyConnected" );
    _Parent->_OnProxyConnected( dwNetID );
}
void		LBProxy::OnDisconnect	( DWORD dwNetID )
{
    TRACE0( "\n_OnProxyDisconnected" );
    _Parent->_OnProxyDisconnected( dwNetID );
}
CEventObj*	LBProxy::OnAccept		( DWORD dwNetID )
{
    return this;
}
//---------------------------------------------------------------------------------
void	CliNetWakerClass::InitPGCallBack()
{

    int		i;
    PGBaseInfo	PGBase;
    //callback 函式陣列出始化
    for( i = 0 ; i <_Sys_Max_CallbackCount_ ; i++ )
    {
        _PGCallBack[i] = &CliNetWakerClass::_PGxUnKnoew;
        _PGInfo.push_back(PGBase);
    }

    //( Switch )
    _PGInfo[ EM_SysNet_Switch_Cli_LoginInfoRequest ].Type.Switch = true;
    _PGCallBack[ EM_SysNet_Switch_Cli_LoginInfoRequest ]        = &CliNetWakerClass::_PG_LoginInfoRequest;

    _PGInfo[ EM_SysNet_Switch_Cli_ConnectPorxyNotify ].Type.Switch = true;
    _PGCallBack[ EM_SysNet_Switch_Cli_ConnectPorxyNotify ]        = &CliNetWakerClass::_PG_LoginInProxyNotify;


    //( Proxy )
    _PGInfo[ EM_SysNet_Proxy_Cli_LoginInfoRequest ].Type.Proxy = true;
    _PGCallBack[ EM_SysNet_Proxy_Cli_LoginInfoRequest ]        = &CliNetWakerClass::_PG_LoginProxyRequest;

    _PGInfo[ EM_SysNet_Proxy_Cli_GSrvOnConnect ].Type.Proxy = true;
    _PGCallBack[ EM_SysNet_Proxy_Cli_GSrvOnConnect ]        = &CliNetWakerClass::_PGxGSrvOnConnect;

    _PGInfo[ EM_SysNet_Proxy_Cli_GSrvOnDisConnect ].Type.Proxy = true;
    _PGCallBack[ EM_SysNet_Proxy_Cli_GSrvOnDisConnect ]        = &CliNetWakerClass::_PGxGSrvOnDisonnect;

    _PGInfo[ EM_SysNet_Proxy_Cli_DataGSrvToCli ].Type.Proxy = true;
    _PGCallBack[ EM_SysNet_Proxy_Cli_DataGSrvToCli ]        = &CliNetWakerClass::_PGxDataGSrvToCli;

    _PGInfo[ EM_SysNet_Proxy_Cli_NotifyCliIDRelogin].Type.Proxy = true;
    _PGCallBack[ EM_SysNet_Proxy_Cli_NotifyCliIDRelogin ]       = &CliNetWakerClass::_PGxCliRelogin;


}

//----------------------------------------------------------------------------------
void     CliNetWakerClass::_PGxUnKnoew( int NetID,PGSysNULL* PG,int Size)
{
    TRACE2( "\n(Switch:%d)_PGxUnKnoew : Command = %d" , NetID ,  PG->Command );
}
//----------------------------------------------------------------------------------
bool    CliNetWakerClass::SendToSwitch( int Size , void* Data )
{
    _NetBSwitch->Send( _SwitchNetID , Size , Data , 0 );
    return true;
}
//-----------------------------------------------------------------------------------
bool    CliNetWakerClass::SendToProxy( int Size , void* Data )
{
    _NetBProxy->Send( _ProxyNetID , Size , Data , 0 );
    return true;
}
//-----------------------------------------------------------------------------------
CliNetWakerClass::CliNetWakerClass( )
{
    InitPGCallBack();

    _NetBSwitch       = CreateNetBridge();
    _LBSwitch         = NEW (LBSwitch(this));             //資料列舉Class
//    _LSwitch->Parent = this;

    _NetBProxy        = CreateNetBridge();
    _LBProxy          = NEW( LBProxy(this));              //資料列舉Class
//    _LProxy->Parent  = this;

    _SwitchNetID     = -1;
    _ExitFlag        = false;                   //結束旗標
    _IsReady         = false;                   //登入ok

}
//-----------------------------------------------------------------------------------
CliNetWakerClass::~CliNetWakerClass()
{
    DeleteNetBridge( _NetBSwitch );
    delete _LBSwitch;
    DeleteNetBridge( _NetBProxy );
    delete _LBProxy;
}
//-----------------------------------------------------------------------------------
void	CliNetWakerClass::ConnectSwitch( char* IP,int Port , char* Account , char* Password )
{
    Logout();

    strncpy( _Account  , Account  , sizeof( _Account  ) );
    strncpy( _Password , Password , sizeof( _Password ) );

    //sockaddr_in SwitchAddr;

    //MAKE_IP_ADDRESS( (sockaddr_in*)&SwitchAddr , IP , Port );
    int IPNum = ConvertIP( IP );
    _NetBSwitch->Connect( IPNum , Port , _LBSwitch );	

    TRACE4( "\nConnect to Switch Ip=%s Port=%d \n   Account=%s PWD=%s\n" , IP , Port , Account , Password );

}
//-----------------------------------------------------------------------------------
void	CliNetWakerClass::_ConnectProxy( UInt32 IP,int Port  )
{
    //sockaddr_in Addr;

    //MAKE_IP_ADDRESS( (sockaddr_in*)&Addr , IP , Port );    )
    _NetBProxy->Connect( IP , Port , _LBProxy );	

    TRACE2( "\nConnect to Proxy Ip=%d Port=%d \n" , IP , Port );

}
//-----------------------------------------------------------------------------------
//定時處理
bool	CliNetWakerClass::Process( )
{

    _NetBSwitch->Flush(  );
    _NetBProxy->Flush( );
    return true;
}
//-----------------------------------------------------------------------------------
//登出
bool    CliNetWakerClass::Logout( )
{
    _NetBProxy->Close( _ProxyNetID );
    _NetBSwitch->Close( _SwitchNetID );
    return true;
}
//-----------------------------------------------------------------------------------
//設定事件回呼函式
void    CliNetWakerClass::SetListener(  NewWakerEventListener*   Listener )
{
    _Listener = Listener;
}
//-----------------------------------------------------------------------------------
bool    CliNetWakerClass::SendToGSrv( int GSrvID , int Size , void* Data )
{
    if( Size > _Max_Cli_Package_Size_ )
    {
        TRACE0( "\n超過 Client 端封包送出的上限大小 Size=%s " );
        return false;
    }
    int PGSize = Size + sizeof(SysNet_Cli_Proxy_DataCliToGSrv);
    char* SendData = new char[ PGSize ];
    SysNet_Cli_Proxy_DataCliToGSrv* Send = (SysNet_Cli_Proxy_DataCliToGSrv*)SendData;
    Send->GSrvID        = GSrvID;
    Send->Size          = Size;
    Send->Command       = EM_SysNet_Cli_Proxy_DataCliToGSrv;
    memcpy( Send->Data , Data , Size );
    SendToProxy( PGSize , Send );
    
    delete SendData;
    return true;
}

#pragma managed