DragonNest/Common/NetworkLib/ClientSession.cpp
2024-12-20 16:56:44 +08:00

82 lines
No EOL
3.1 KiB
C++
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#include "StdAfx.h"
#include "ClientSession.h"
#ifdef _DEBUG
#define new new(_NORMAL_BLOCK,__FILE__,__LINE__)
#endif
CClientSession::CClientSession(void)
: m_Socket(INVALID_SOCKET), m_SessionType(0)
{
int nSize = 1024 * 32;
m_pRecvBuffer = new CBuffer(nSize);
m_pSendBuffer = new CBuffer(nSize);
}
CClientSession::CClientSession(int nSize)
: m_Socket(INVALID_SOCKET), m_SessionType(0)
{
m_pRecvBuffer = new CBuffer(nSize);
m_pSendBuffer = new CBuffer(nSize);
}
CClientSession::~CClientSession(void)
{
Clear();
SAFE_DELETE(m_pRecvBuffer);
SAFE_DELETE(m_pSendBuffer);
}
void CClientSession::OnDisconnect( bool bValidDisconnect )
{
/*
if( !bValidDisconnect ) {
if (m_pRecvBuffer) m_pRecvBuffer->Clear();
if (m_pSendBuffer) m_pSendBuffer->Clear();
}
*/
}
void CClientSession::Clear()
{
}
BOOL CClientSession::GetLocalIP(WCHAR* pIP)
{
if(!m_Socket) return FALSE;
char Name[256] = {0,};
gethostname(Name, sizeof(Name));
PHOSTENT host = gethostbyname(Name);
if (host)
{
if(MultiByteToWideChar(CP_ACP, 0, inet_ntoa(*(struct in_addr*)*host->h_addr_list), -1, pIP, 32) > 0)
return TRUE;
}
return FALSE;
}
USHORT CClientSession::GetLocalPort(void)
{
if (!m_Socket) return 0;
SOCKADDR_IN Addr;
ZeroMemory(&Addr, sizeof(Addr));
int AddrLength = sizeof(Addr);
if (getsockname(m_Socket, (sockaddr*) &Addr, &AddrLength) != SOCKET_ERROR)
return ntohs(Addr.sin_port);
return 0;
}
void CClientSession::ClearBuffer()
{
m_pRecvBuffer->Clear();
m_pSendBuffer->Clear(true);
}