Example #1
0
bool C4Network2ClientList::BroadcastMsgToClients(const C4NetIOPacket &rPkt)
{
	// Send a msg to all clients, including clients that are not connected to
	// this computer (will get forwarded by host).

	C4PacketFwd Fwd; Fwd.SetListType(true);
	// lock
	pIO->BeginBroadcast(false);
	// select connections for broadcast
	for (C4Network2Client *pClient = pFirst; pClient; pClient = pClient->getNext())
		if (!pClient->isHost())
			if (pClient->isConnected())
			{
				pClient->getMsgConn()->SetBroadcastTarget(true);
				Fwd.AddClient(pClient->getID());
			}
	// broadcast
	bool fSuccess = pIO->Broadcast(rPkt);
	// unlock
	pIO->EndBroadcast();
	// clients: send forward request to host
	if (!fHost)
	{
		Fwd.SetData(rPkt);
		fSuccess &= SendMsgToHost(MkC4NetIOPacket(PID_FwdReq, Fwd));
	}
	return fSuccess;
}
void C4Network2Players::HandlePacket(char cStatus, const C4PacketBase *pPacket, C4Network2IOConnection *pConn)
{
	if (!pConn) return;

	// find associated client
	C4Network2Client *pClient = ::Network.Clients.GetClient(pConn);
	if (!pClient) pClient = ::Network.Clients.GetClientByID(pConn->getClientID());

#define GETPKT(type, name) \
    assert(pPacket); const type &name = \
     static_cast<const type &>(*pPacket);

	// player join request?
	if (cStatus == PID_PlayerInfoUpdReq)
	{
		GETPKT(C4PacketPlayerInfoUpdRequest, pkPlrInfo);
		// this packet is sent to the host only, and thus cannot have been sent from the host
		if (!::Network.isHost()) return;
		// handle this packet
		HandlePlayerInfoUpdRequest(&pkPlrInfo.Info, false);
	}
	else if (cStatus == PID_LeagueRoundResults)
	{
		GETPKT(C4PacketLeagueRoundResults, pkLeagueInfo);
		// accepted from the host only
		if (!pClient || !pClient->isHost()) return;
		// process
		Game.RoundResults.EvaluateLeague(pkLeagueInfo.sResultsString.getData(), pkLeagueInfo.fSuccess, pkLeagueInfo.Players);
	}

#undef GETPKT
}