魔域服务端源码
发布网友
发布时间:2022-05-24 18:30
我来回答
共2个回答
热心网友
时间:2023-10-25 06:21
发一段给你吧。这一段是关于Socket的,呵呵,多学学。
bool CSocketKernel::Create(IMessagePort* pPort)
{
m_idNpcSocket = SOCKET_NONE;
m_pMsgPort = pPort;
m_pMsgPort->Open();
// TODO: 请在此添加初始化代码
m_pListen = m_cListen.GetInterface();
if(!ServerSocketInit())
return false;
if(!m_pListen->Listen(SERVER_LISTEN_PORT, SOCKET_SENDBUFSIZE))
return false;
return true;// return false : 创建失败,程序关闭。
}
bool CSocketKernel::ProcessMsg(OBJID idPacket, void* buf, int nType, int nFrom)
{
// TODO: 请在此添加消息处理代码
switch(idPacket)
{
caseSOCKET_CLOSESOCKET:
{
SOCKET_IDidSocket= *(int*)buf;
if(idSocket >= m_setSocket.size())
break;
LOGDEBUG("DEBUG:通讯核心收到关闭网络消息,SOCKET_ID[%u]", idSocket);
if(m_setSocket[idSocket])// readme.txt (1-7)
{
m_setSocket[idSocket]->Close(true);
m_setSocket[idSocket]= NULL;
CServerSocket::Destroy(m_setServerSocket[idSocket]);
m_setProcessID[idSocket]= PROCESS_NONE;
m_setNetBreakFlag[idSocket]= false;
if(m_idNpcSocket == idSocket)
{
LOGDUMP("m_idNpcSocket(%d) = SOCKET_NONE", m_idNpcSocket);
m_idNpcSocket = SOCKET_NONE;
char*pText = "NPC server broken connect.";
m_pMsgPort->Send(MSGPORT_SHELL, SHELL_PRINTTEXT, STRING_TYPE(pText), pText);
}
}
}
break;
caseSOCKET_SETPROCESSID:
{
const CHANGE_USERDATA*pPack = (CHANGE_USERDATA*)buf;
if(pPack->idSocket >= 0 && pPack->idSocket < m_setSocket.size()
&& m_setSocket[pPack->idSocket]
&& !IsNetBreakFlag(pPack->idSocket) )// 网络正常
m_setProcessID[pPack->idSocket] = pPack->nData;
else
m_pMsgPort->Send(nFrom, KERNEL_CLOSEKERNEL, VARTYPE_INT, &pPack->idSocket);// readme.txt (1-6)
}
break;
caseSOCKET_SENDCLIENTMSG:
{
const SENDCLIENTMSG_PACKET0* pPack = (SENDCLIENTMSG_PACKET0*)buf;
if(pPack->idSocket >= m_setSocket.size())
break;
if(m_setSocket[pPack->idSocket])
{
char buf[MAX_PACKETSIZE];
intnNetPacketSize = UnitePacket(buf, MAX_PACKETSIZE, pPack->idPacket, pPack->buf, pPack->nSize);
ASSERT(nNetPacketSize);
if(nNetPacketSize)
{
#ifdefPALED_DEBUG
OBJIDidPacket = *((unsigned short*)buf + 1);
热心网友
时间:2023-10-25 06:21
太长了,只是把Socket的发一小段给你,Thread部分的无法发布,说重复字符太多了。