📄 socketclient.cc
字号:
// SocketClient.cc: interface for the SocketLayer class.
/*/////////////////////////////////////////////////////////////////////////////
Socket层,封装了客户端基本功能
李亦
2006.06.21
/*//////////////////////////////////////////////////////////////////////////////
//#include "PlatForms.h"
//#include "server/net/IOTCPModule.h"
//#include "server/net/IOUDPModule.h"
//#include "server/net/AuthSocket.h"
//#include "server/net/AuthNull.h"
//#include "server/encrypt/Encryption.h"
//#include "server/encrypt/EncryptXORModule.h"
//#include "server/encrypt/EncryptDESHashModule.h"
#include "strhandle.h"
#include "server/cmd/CmdManager.h"
#include "server/SocketClient.h"
#include "sim/netConnection.h"
///////////////////////////////////////////////////////////////////////////////
//CS SDK
namespace CS
{
IMPLEMENT_CONOBJECT(SocketClient);
ConsoleMethod(SocketClient,attachConnection,bool,3,3,"obj.attachConnection(connection)")
{
SimObject* pObj = Sim::findObject(argv[2]);
if(pObj == NULL)
return false;
NetConnection *pConn = dynamic_cast<NetConnection *>(pObj);
object->AttachConnection(pConn);
return true;
}
ConsoleMethod(SocketClient,create,bool,3,3,"obj.create(conn)")
{
CSTR szAddr;
CSTR szNetMod;
CSTR szEncrypt;
CSTR szAuth;
if(!CallSameMethod(SocketClient,attachConnection))
return false;
szAddr = Con::getVariable("Pref::CS::Addr");
szNetMod = Con::getVariable("Pref::CS::IO");
szEncrypt = Con::getVariable("Pref::CS::Encrypt");
szAuth = Con::getVariable("Pref::CS::Auth");
//Con::getVariable("$Pref::CS::port");
return object->CreateClient(szAddr,szNetMod, szEncrypt, szAuth) == E_IOOK;
}
ConsoleMethod(SocketClient,disconnect,void,2,2,"obj.disconnect()")
{
object->Disconnect();
}
ConsoleMethod(SocketClient,run,void,2,2,"obj.run()")
{
object->RunClient();
}
ConsoleMethod(SocketClient,isConnected,bool,2,2,"obj.isConnected()")
{
return object->IsConnected();
}
ConsoleMethod(SocketClient,sendString,bool,3,3,"obj.sendString(str)")
{
return object->SendString(argv[2]);
}
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
SocketClient::SocketClient()
{
m_bIsConnected = FALSE;
m_pConnection = NULL;
m_bAutoQuery = FALSE;
m_pAuthSocket = NULL;
}
SocketClient::~SocketClient()
{
Disconnect();
}
void SocketClient::AttachConnection(NetConnection* pConn)
{
AssertWarn(pConn, "SocketClient::AttachConnection 需要指定 connection对象");
m_pConnection = pConn;
}
void SocketClient::OnCreateAuthSocket(CAuthSocket* pSocket)
{
m_pConnection->attachSocket(pSocket);
}
int SocketClient::CreateClient(CSTR szServerAddress, CSTR szNetMod, CSTR szEncryptionMod, CSTR szAuth,HWINDOW hParent)
{
AssertWarn(m_pConnection, "SocketClient::AttachConnection 需要指定 connection对象");
if(m_pConnection == NULL)
return E_IOFAILED;
m_bIsConnected = FALSE;
//m_NetCritSec.lock(true);
m_pAuthSocket = ConnectAuthSocket(NULL,0,hParent,
szServerAddress,
szNetMod,
szEncryptionMod,
szAuth);
//m_NetCritSec.unlock();
if(m_pAuthSocket == INVALID_AUTHPTR)
{
m_pConnection = NULL;
return E_IOFAILED;
}
m_uIdleTimeOut = Con::getIntVariable("Pref::CS::ClientIdle",CS_NET_IDLE);
m_bIsConnected = TRUE;
m_pAuthSocket->SetESTABLISHED();
return E_IOOK;
}
void SocketClient::run(S32 arg)
{
arg;
while(IsConnected())
{
HandleNetworkPoll();
Platform::sleep(CS_NET_IDLE);
}
}
void SocketClient::RunClient()
{
//DWORD dwThreadID;
//m_hServerListenThread = CreateThread(NULL,0,ServerListenThread,this,0,&dwThreadID);
//if(m_layerThread.start(Thread::PRI_LOWEST) )
if(!Parent::start(/*Thread::PRI_LOWEST*/) )
{
if(m_pAuthSocket)
{
//m_pAuthSocket->Close();
delete m_pAuthSocket;
m_pAuthSocket = NULL;
}
m_bIsConnected = FALSE;
}
}
void SocketClient::HandleNetworkPoll()
{
//BYTE *pBuffer;
//int nBufLen,
int nRet;
//AssertWarn(0,"需要重写逻辑");
//return;
// Check connected socket
for(;m_pAuthSocket->IsAlive();)
{
if(m_pAuthSocket->CheckSend(TRUE))
{
//m_NetCritSec.lock(true);
nRet = m_pAuthSocket->SendOutPacket();
//m_NetCritSec.unlock();
}
if(m_pAuthSocket->CheckRecv(TRUE))
{
//m_NetCritSec.lock(true);
m_pAuthSocket->BeginRecv();
m_pAuthSocket->EndRecv();
//m_NetCritSec.unlock();
}
Platform::sleep(m_uIdleTimeOut);
};
Disconnect();
}
BOOL SocketClient::Disconnect()
{
int nRet;
m_bIsConnected = FALSE;
if(m_pAuthSocket == NULL)
return TRUE;
////m_NetCritSec.lock(true);
//nRet = m_pAuthSocket->Close();
////m_NetCritSec.unlock();
//if(nRet != E_IOOK)
// return FALSE;
m_pAuthSocket->Close();
Platform::sleep(m_uIdleTimeOut);
delete m_pAuthSocket;
m_pAuthSocket = NULL;
return TRUE;
}
BOOL SocketClient::SendString(CSTR szData)
{
if(m_pAuthSocket == NULL)
return FALSE;
//INT nLen = dStrlen(szData) + 1;
BitStream stream(0,0);
if(m_pAuthSocket->BeginSend(&stream))
{
stream.writeString(szData);
m_pAuthSocket->EndSend(&stream);
}
return TRUE;
}
BOOL SocketClient::SendData(void *pBuffer, UINT nLength)
{
if(m_pAuthSocket == NULL)
return FALSE;
//m_NetCritSec.lock(true);
////EnterCriticalSection(&m_NetCritSec);
//int nRet = m_pCommand->IssueAuthCommandRequest(m_pAuthSocket,
// CS_COMMAND_DATA,
// Platform::getRealMilliseconds(),
// nLength,
// NULL,
// (char *)pBuffer);
////LeaveCriticalSection(&m_NetCritSec);
//m_NetCritSec.unlock();
//if(nRet < 0)
// return FALSE;
return TRUE;
}
BOOL SocketClient::SendCommand(int command, int nArg1, char *svArg2, char *svArg3)
{
if(m_pAuthSocket == NULL)
return FALSE;
//EnterCriticalSection(&m_NetCritSec);
//m_NetCritSec.lock(true);
int nRet = m_pCommand->IssueAuthCommandRequest(m_pAuthSocket,
command,
Platform::getRealMilliseconds(),
nArg1,
svArg2,
svArg3);
//m_NetCritSec.unlock();
//LeaveCriticalSection(&m_NetCritSec);
if(nRet < 0)
return FALSE;
return TRUE;
}
};//namespace CS
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -