Files
Sun1602/Tools/SunUpdateManager/SunUpdateAgent/UpdateServerSession.cpp
T
2022-10-26 12:25:11 +08:00

90 lines
1.7 KiB
C++

#include "StdAfx.h"
#include "UpdateServerSession.h"
#include "SunUpdateAgent.h"
#include "PacketHandler.h"
#include <PacketStruct_SA.h>
UpdateServerSession::UpdateServerSession(void)
{
m_wConnectPort = 0;
m_bConnected = FALSE;
m_bConnector = FALSE;
m_bLogin = FALSE;
m_dwSessionIndex = 0;
}
UpdateServerSession::~UpdateServerSession(void)
{
}
VOID UpdateServerSession::SetAddr( const TCHAR* pszIP, WORD wPort )
{
if( !pszIP ) return;
m_strConnectIP = pszIP;
m_wConnectPort = wPort;
}
BOOL UpdateServerSession::TryToConnect()
{
if( IsConnected() ) return TRUE;
if( m_strConnectIP.empty() ) return FALSE;
DWORD dwIndex = SunUpdateAgent::Instance()->ConnectToServer( this, m_strConnectIP.c_str(), m_wConnectPort );
if( dwIndex != 0 )
{
m_dwSessionIndex = dwIndex;
return TRUE;
}
return FALSE;
}
VOID UpdateServerSession::OnRecv( BYTE *pMsg, WORD wSize )
{
PacketHandler::Instance()->ParserPacket_SA( this, (MSG_BASE*)pMsg, wSize );
}
VOID UpdateServerSession::OnConnect( BOOL bSuccess, DWORD dwNetworkIndex )
{
if( bSuccess )
{
SetConnected( TRUE );
SetIsConnector( TRUE );
MSGOUT( "[OnConnect] 업데이트 서버 접속 성공" );
SendServerType();
}
else
{
TryToConnect();
}
}
VOID UpdateServerSession::OnDisconnect()
{
SetConnected( FALSE );
MSGOUT( "[OnDisconnect] 서버 연결 해제" );
if( IsConnector() )
{
TryToConnect();
}
}
BOOL UpdateServerSession::SendPacket( MSG_BASE * pMsg, WORD wSize )
{
return Send( (BYTE*)pMsg, wSize );
}
VOID UpdateServerSession::SendServerType()
{
MSG_SERVER_TYPE msg;
msg.m_byCategory = SA_SERVER;
msg.m_byProtocol = SA_SERVER_TYPE_SYN;
msg.m_byServerType = UPDATE_AGENT;
SendPacket( &msg, sizeof(msg) );
}