📄 cnserverclient.cpp
字号:
//---------------------------------------------------------------------------
#pragma hdrstop
#include "CNServerClient.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
///////////////////////////CNServerClientThrad//////////////////////////
CNServerClientThread::~CNServerClientThread()
{
// If we have a client object then delete it
if (m_client != NULL)
delete m_client;
}
BOOL CNServerClientThread::Init(CNServerClient *client, CNServer *server, VSocket *socket)
{
// Save the server pointer and window handle
m_server = server;
m_socket = socket;
m_client = client;
// Start the thread
start();
return TRUE;
}
VInt32 CNServerClientThread::AnalyzeParams(VString Buff,WORD BuffLen)
{
//分析参数串
WORD ParamCount = 0;
WORD Len = 0;
memset(ParamBuf,0,MAX_PARAM_COUNT * (MAX_PARAM_LENGTH + 1));
for(register i = 0; i < BuffLen; i++)
{
if( Buff[i] == ';')
{
ParamBuf[ParamCount][Len] = '\0';
ParamCount ++;
Len = 0;
}
else
{
ParamBuf[ParamCount][Len ++] = Buff[i];
}
}
ParamBuf[ParamCount][Len] = '\0';
return ParamCount + 1;
}
void CNServerClientThread::run(void *arg)
{
BOOL connected = TRUE;
TCommandMessage CmdMsg; //命令
char Buff[MAX_PARAM_LENGTH + 1];
WORD ParamCount = 0;
m_socket->WriteLn("+OK:");
// MessageBox(NULL,"客户确定连接!","服务器",MB_OK);
while (connected) //每个客户连接的主循环
{
//等待一个命令
if (!m_socket->ReadExact((char *)&CmdMsg,sizeof(CmdMsg)))
{
connected = FALSE;
break;
}
if( CmdMsg.ParamStrLength > 0 && CmdMsg.ParamStrLength < MAX_PARAM_LENGTH) //如果有参数
{
memset(Buff,0,MAX_PARAM_LENGTH + 1);
if( !m_socket->ReadExact(Buff,CmdMsg.ParamStrLength))//读入参数
{
connected = FALSE;
break;
}
//分解参数
ParamCount = AnalyzeParams(Buff,CmdMsg.ParamStrLength);
}
else ParamCount = 0;
Cmd_DoCommand(CmdMsg.CommandType,ParamCount,ParamBuf,m_socket);
}
// MessageBox(NULL,"客户正常退出","服务器",MB_OK);
// Remove the client from the server, just in case!
m_server->RemoveClient(m_client->GetClientId());
}
///////////////////////////////CNServerClient//////////////////////////////
CNServerClient::CNServerClient()
{
m_socket = NULL;
}
CNServerClient::~CNServerClient()
{
// If we have a socket then kill it
if (m_socket != NULL)
{
delete m_socket;
m_socket = NULL;
}
}
BOOL CNServerClient::Init(CNServer *server,
VSocket *socket,
CNServerClientId newid)
{
// Save the server id;
m_server = server;
// Save the socket
m_socket = socket;
// Save the name of the connecting client
char *name = m_socket->GetPeerName();
if (name != 0)
m_client_name = strdup(name);
else
m_client_name = strdup("<unknown>");
// Save the client id
m_id = newid;
// Spawn the child thread here
m_thread = new CNServerClientThread;
if (m_thread == NULL)
return FALSE;
return ((CNServerClientThread *)m_thread)->Init(this, m_server, m_socket);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -