예제 #1
0
bool NFNetModule::SendMsgPBToAllClient(const uint16_t nMsgID, const google::protobuf::Message& xData)
{
    NFMsg::MsgBase xMsg;
    if (!xData.SerializeToString(xMsg.mutable_msg_data()))
    {
		std::ostringstream stream;
		stream << " SendMsgPBToAllClient";
		stream << " Failed For Serialize of MsgData, MessageID " << nMsgID;
		m_pLogModule->LogError(stream, __FUNCTION__, __LINE__);

        return false;
    }

    std::string strMsg;
    if (!xMsg.SerializeToString(&strMsg))
    {
		std::ostringstream stream;
		stream << " SendMsgPBToAllClient";
		stream << " Failed For Serialize of MsgBase, MessageID " << nMsgID;
		m_pLogModule->LogError(stream, __FUNCTION__, __LINE__);

        return false;
    }

    return SendMsgToAllClientWithOutHead(nMsgID, strMsg);
}
예제 #2
0
bool NFNetModule::SendMsgPB(const uint16_t nMsgID, const google::protobuf::Message& xData, const NFSOCK nSockIndex)
{
	NFMsg::MsgBase xMsg;
	if (!xData.SerializeToString(xMsg.mutable_msg_data()))
	{
		std::ostringstream stream;
		stream << " SendMsgPB Message to  " << nSockIndex;
		stream << " Failed For Serialize of MsgData, MessageID " << nMsgID;
		m_pLogModule->LogError(stream, __FUNCTION__, __LINE__);

		return false;
	}

	NFMsg::Ident* pPlayerID = xMsg.mutable_player_id();
	*pPlayerID = NFToPB(NFGUID());

	std::string strMsg;
	if (!xMsg.SerializeToString(&strMsg))
	{
		std::ostringstream stream;
		stream << " SendMsgPB Message to  " << nSockIndex;
		stream << " Failed For Serialize of MsgBase, MessageID " << nMsgID;
		m_pLogModule->LogError(stream, __FUNCTION__, __LINE__);

		return false;
	}

	SendMsgWithOutHead(nMsgID, strMsg, nSockIndex);

	return true;
}
int NFCProxyServerNet_ServerModule::Transpond(const int nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen)
{
    NFMsg::MsgBase xMsg;
    if (!xMsg.ParseFromArray(msg, nLen))
    {
        char szData[MAX_PATH] = { 0 };
        sprintf(szData, "Parse Message Failed from Packet to MsgBase, MessageID: %d\n", nMsgID);

        return false;
    }

    //broadcast many palyer
    for (int i = 0; i < xMsg.player_client_list_size(); ++i)
    {
        NF_SHARE_PTR<int> pFD = mxClientIdent.GetElement(NFINetModule::PBToNF(xMsg.player_client_list(i)));
        if (pFD)
        {
            if (xMsg.has_hash_ident())
            {
                NetObject* pNetObject = m_pNetModule->GetNet()->GetNetObject(*pFD);
                if (pNetObject)
                {
                    pNetObject->SetHashIdentID(NFINetModule::PBToNF(xMsg.hash_ident()));
                }
            }

			m_pNetModule->GetNet()->SendMsgWithOutHead(nMsgID, msg, nLen, *pFD);
        }
    }

    //send message to one player
    if (xMsg.player_client_list_size() <= 0)
    {
        //playerid==ClientID;

        NF_SHARE_PTR<int> pFD = mxClientIdent.GetElement(NFINetModule::PBToNF(xMsg.player_id()));
        if (pFD)
        {
            if (xMsg.has_hash_ident())
            {
                NetObject* pNetObject = m_pNetModule->GetNet()->GetNetObject(*pFD);
                if (pNetObject)
                {
                    pNetObject->SetHashIdentID(NFINetModule::PBToNF(xMsg.hash_ident()));
                }
            }

			m_pNetModule->GetNet()->SendMsgWithOutHead(nMsgID, msg, nLen, *pFD);
        }
    }

    return true;
}
예제 #4
0
bool NFNetModule::SendMsgPB(const uint16_t nMsgID, const google::protobuf::Message& xData, const NFSOCK nSockIndex, const std::vector<NFGUID>* pClientIDList)
{
    if (!m_pNet)
    {
		std::ostringstream stream;
		stream << " m_pNet SendMsgPB faailed fd " << nSockIndex;
		stream << " Failed For Serialize of MsgBase, MessageID " << nMsgID;
		m_pLogModule->LogError(stream, __FUNCTION__, __LINE__);

        return false;
    }

    NFMsg::MsgBase xMsg;
    if (!xData.SerializeToString(xMsg.mutable_msg_data()))
    {
		std::ostringstream stream;
		stream << " SendMsgPB faailed fd " << nSockIndex;
		stream << " Failed For Serialize of MsgBase, MessageID " << nMsgID;
		m_pLogModule->LogError(stream, __FUNCTION__, __LINE__);

        return false;
    }


    NFMsg::Ident* pPlayerID = xMsg.mutable_player_id();
    *pPlayerID = NFToPB(NFGUID());
    if (pClientIDList)
    {
        for (int i = 0; i < pClientIDList->size(); ++i)
        {
            const NFGUID& ClientID = (*pClientIDList)[i];

            NFMsg::Ident* pData = xMsg.add_player_client_list();
            if (pData)
            {
                *pData = NFToPB(ClientID);
            }
        }
    }

    std::string strMsg;
    if (!xMsg.SerializeToString(&strMsg))
    {
		std::ostringstream stream;
		stream << " SendMsgPB faailed fd " << nSockIndex;
		stream << " Failed For Serialize of MsgBase, MessageID " << nMsgID;
		m_pLogModule->LogError(stream, __FUNCTION__, __LINE__);

        return false;
    }

    return SendMsgWithOutHead(nMsgID, strMsg, nSockIndex);
}
예제 #5
0
void NFCNetLogic::SendToServerByPB(const uint16_t nMsgID, google::protobuf::Message& xData)
{
	if(m_bSocketReady)
	{
		g_pNetClientModule->SendToAllServerByPB(nMsgID, xData);
	}
	else
	{
        NFMsg::MsgBase xMsg;
        xData.SerializeToString(xMsg.mutable_msg_data());
        NFMsg::Ident* pPlayerID = xMsg.mutable_player_id();
        *pPlayerID = NFINetModule::NFToPB(NFGUID());

        std::string strMsg;
        xMsg.SerializeToString(&strMsg);
		m_listDelayMsg.push_back(std::make_pair((uint16_t)nMsgID, strMsg));
	}
}
void NFCProxyServerNet_ServerModule::OnOtherMessage(const int nSockIndex, const int nMsgID, const char * msg, const uint32_t nLen)
{
	NFMsg::MsgBase xMsg;
	if (!xMsg.ParseFromArray(msg, nLen))
	{
		char szData[MAX_PATH] = { 0 };
		sprintf(szData, "Parse Message Failed from Packet to MsgBase, MessageID: %d\n", nMsgID);

		return;
	}

	NetObject* pNetObject = m_pNetModule->GetNet()->GetNetObject(nSockIndex);
	if (!pNetObject || pNetObject->GetConnectKeyState() <= 0 || pNetObject->GetGameID() <= 0)
	{
		//state error
		return;
	}
	if (xMsg.has_hash_ident())
	{
		//special for distributed
		if (!pNetObject->GetHashIdentID().IsNull())
		{
			NFCMachineNode xNode;
			if (mxConsistentHash.GetSuitNode(pNetObject->GetHashIdentID().ToString(), xNode))
			{
				m_pProxyServerToGameModule->GetClusterModule()->SendByServerID(xNode.GetDataID(), nMsgID, msg, nLen);
			}
		}
		else
		{
			NFGUID xHashIdent = NFINetModule::PBToNF(xMsg.hash_ident());

			NFCMachineNode xNode;
			if (mxConsistentHash.GetSuitNode(xHashIdent.ToString(), xNode))
			{
				m_pProxyServerToGameModule->GetClusterModule()->SendByServerID(xNode.GetDataID(), nMsgID, msg, nLen);
			}
		}
	}
	else
	{
		m_pProxyServerToGameModule->GetClusterModule()->SendByServerID(pNetObject->GetGameID(), nMsgID, msg, nLen);
	}
}
void NFCProxyServerNet_ServerModule::OnReqEnterGameServer(const int nSockIndex, const int nMsgID, const char* msg, const uint32_t nLen)
{
    //在没有正式进入游戏之前,nPlayerID都是FD
    NFGUID nPlayerID;
    NFMsg::ReqEnterGameServer xData;
    if (!m_pNetModule->ReceivePB(nSockIndex, nMsgID, msg, nLen, xData, nPlayerID))
    {
        return;
    }

    NF_SHARE_PTR<ConnectData> pServerData = m_pProxyServerToGameModule->GetClusterModule()->GetServerNetInfo(xData.game_id());
    if (pServerData && ConnectDataState::NORMAL == pServerData->eState)
    {
        //数据匹配
        NetObject* pNetObject = m_pNetModule->GetNet()->GetNetObject(nSockIndex);
        if (pNetObject
            && pNetObject->GetConnectKeyState() > 0
            && pNetObject->GetGameID() == xData.game_id()
            && pNetObject->GetAccount() == xData.account()
            && !xData.name().empty()
            && !xData.account().empty())
        {
            NFMsg::MsgBase xMsg;
            if (!xData.SerializeToString(xMsg.mutable_msg_data()))
            {
                return;
            }

            //playerid在进入游戏之前都是FD,其他时候是真实的ID
            xMsg.mutable_player_id()->CopyFrom(NFINetModule::NFToPB(pNetObject->GetClientID()));

            std::string strMsg;
            if (!xMsg.SerializeToString(&strMsg))
            {
                return;
            }

			m_pProxyServerToGameModule->GetClusterModule()->SendByServerID(pNetObject->GetGameID(), NFMsg::EGameMsgID::EGMI_REQ_ENTER_GAME, strMsg);
        }
    }
}
예제 #8
0
bool NFNetModule::SendMsg(const uint16_t nMsgID, const std::string & xData, const NFSOCK nSockIndex, const NFGUID id)
{
	NFMsg::MsgBase xMsg;
	xMsg.set_msg_data(xData.data(), xData.length());

	NFMsg::Ident* pPlayerID = xMsg.mutable_player_id();
	*pPlayerID = NFToPB(id);

	std::string strMsg;
	if (!xMsg.SerializeToString(&strMsg))
	{
		std::ostringstream stream;
		stream << " SendMsgPB Message to  " << nSockIndex;
		stream << " Failed For Serialize of MsgBase, MessageID " << nMsgID;
		m_pLogModule->LogError(stream, __FUNCTION__, __LINE__);

		return false;
	}

	return SendMsgWithOutHead(nMsgID, strMsg, nSockIndex);
}
void NFCProxyServerNet_ServerModule::OnClientDisconnect(const int nAddress)
{
    NetObject* pNetObject = m_pNetModule->GetNet()->GetNetObject(nAddress);
    if (pNetObject)
    {
        int nGameID = pNetObject->GetGameID();
        if (nGameID > 0)
        {
            if (!pNetObject->GetUserID().IsNull())
            {
                //掉线
                NFMsg::ReqLeaveGameServer xData;

                NFMsg::MsgBase xMsg;
                //playerid主要是网关转发消息的时候做识别使用,其他使用不使用
                *xMsg.mutable_player_id() = NFINetModule::NFToPB(pNetObject->GetUserID());

                if (!xData.SerializeToString(xMsg.mutable_msg_data()))
                {
                    return;
                }

                std::string strMsg;
                if (!xMsg.SerializeToString(&strMsg))
                {
                    return;
                }

				m_pProxyServerToGameModule->GetClusterModule()->SendByServerID(nGameID, NFMsg::EGameMsgID::EGMI_REQ_LEAVE_GAME, strMsg);
            }
        }

        NFGUID xClientIdent = pNetObject->GetClientID();
        mxClientIdent.RemoveElement(xClientIdent);
    }
}