[手游新项目历程]第10天-角色登陆流程

管理容器
//References :
typedef std::map<std::string, int> clientMapT;
clientMapT clientMap;	
LogicalConnection* ClientFactoryImpl::AddLogicalConnection( LogicalConnection* pClient )
{
    std::string key(pClient->getKey());
    ScopedLock csLock(cs);
    //
    LogicalConnection* pRet = pClient;

    clientMapT::iterator it = clientMap.find(key);
    if(it!=clientMap.end())
    {
        pRet =  it->second;
    }
    else
    {
        clientMap[key] = pClient;
        nClientsCount++;
    }

    pRet->IncrementUsage();
    return pRet;
}

LogicalConnection* ClientFactoryImpl::FindLogicalConnection( const char* _key )
{
    std::string key(_key);
    //
    ScopedLock csLock(cs);

    clientMapT::iterator it = clientMap.find(key);
    if(it==clientMap.end())
        return NULL;

    LogicalConnection* pClient = it->second;

    pClient->IncrementUsage();
    return pClient;
}

	LogicalConnection* pRecipient = FindClient(request.GetArg1().c_str());
	if (pRecipient)
	{
		WebsocketDataMessage response(Routedcommunication);
		response.SetArguments(request.GetEncodedData());
		pRecipient->PushPacket(&response);
		ReturnClient(pRecipient);
	}
	
	std::map<Tint32, LogicalConnection*>	m_PlayerConnectionIdMap;	//错误的方式
	如果自己存一个连接的指针取出来发送回报错,
	
	std::map<Tint32, CLIENT_KEY>			m_PlayerConnectionIdMap;	//正确的方式	
	LogicalConnection* pRecipient = FindClient(CLIENT_KEY);
	只能存一个key的容器,通过key去自带的容器取连接来发送包


角色登陆流程

GAME_MSG_LOGIN_REQ					=		1001,					//登陆

client发送登陆包 -> webServer 产生链接ConIdid写进包->gate 

Tint32 NetGate::OnRecvHandle( t_ConnID ConnectionId,tagNetMsg *pMsg ) 消息派发

Tint32 NetClient::OnAcceptHandle( t_ConnID ConnectionId )

void GatePlayerMgr::AddPlayerConnIdMap( t_ConnID nConnId, GatePlayer* pGatePlayer )

TBool NetDBCache::PlayerRetLogin( t_ConnID ConnectionId,tagNetMsg &Msg )

void NetWorld::SendToWholeWorld(Tint32 msgID,char *buff, unsigned int buffLen , ePlayerFilter eFilter, Tint64 nFilterValue)
{
	Debug_Assert(buffLen < enMaxMsgDataBufferSize, );
	tagNetMsg msgToSend;
	memcpy(msgToSend.buff, buff, buffLen);
	msgToSend.BuffLen = buffLen;
	msgToSend.MsgID = msgID;
	SendToWholeWorld(msgToSend, eFilter, nFilterValue);
}

void NetWorld::SendToWholeWorld( tagNetMsg& msg,ePlayerFilter eFilter /*= ePlayerFilter_All*/, Tint64 nFilterValue /*= -1*/ )
{
	tagNetMsg MsgToSend;
	Stream stream(MsgToSend.buff, sizeof(MsgToSend.buff));
	stream.Write(eFilter);
	stream.Write(nFilterValue);
	stream.Write(msg);
	MsgToSend.MsgID = GAME_SERVER_MSG_BROCAST_WHOLE_WORLD;
	MsgToSend.BuffLen = stream.GetPos();
	SendSingle(MsgToSend.MsgID, MsgToSend.BuffLen, MsgToSend);
}

struct MsgLoginReq
{
	void Serialize(Stream& stream) 
	{
		unsigned short nLen = (unsigned short)m_Account.GetRealStrLen();
		stream.Write(nLen);
		stream.WriteBuffer(m_Account.GetName(),nLen);

		stream.Write(m_SerVerId);

		nLen = (unsigned short)m_Platform.GetRealStrLen();
 		stream.Write(nLen);
 		stream.WriteBuffer(m_Platform.GetName(),nLen);
	}

	bool DeSerialize(Stream& stream)
	{
		unsigned short nLen = 0;

		//玩家账号
		if (!stream.Read(nLen))
			return false;
		if(nLen >= MAX_ACCOUNT_LEN)
			return false;
		if (!stream.ReadBuffer(m_Account.GetName(), nLen))
			return false;
		//服务器id
		if (!stream.Read(m_SerVerId))
			return false;
		//平台标识
 		if (!stream.Read(nLen))
 			return false;
 		if(nLen >= MAX_PLATFORM_LEN)
 			return false;
 		if (!stream.ReadBuffer(m_Platform.GetName(), nLen))
 			return false;
		return true;
	}

	CommonAccountName m_Account;
	CommonString<255> m_Token;
	Tint32 m_SerVerId;
	CommonPlatform m_Platform;
};

TBool NetGate::PlayerLoginResService
DB_MSG_ACCOUNT_LOGIN,								//登录
DB_MSG_ACCOUNT_LOGIN_RET,							//登录返回
GAME_MSG_LOGIN_RES
void NetGate::SendToClientWithClientConnId
GAME_SERVER_MSG_DBCACHE_TO_GATE ,					//从DBCACHE发给GATE
TBool NetDBCache::PlayerRetLogin( t_ConnID ConnectionId,tagNetMsg &Msg )
原文地址:https://www.cnblogs.com/byfei/p/14104300.html