Code Search for Developers
 
 
  

ComsMessageSender.cpp from Scorched 3D at Krugle


Show ComsMessageSender.cpp syntax highlighted

////////////////////////////////////////////////////////////////////////////////
//    Scorched3D (c) 2000-2003
//
//    This file is part of Scorched3D.
//
//    Scorched3D is free software; you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation; either version 2 of the License, or
//    (at your option) any later version.
//
//    Scorched3D is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with Scorched3D; if not, write to the Free Software
//    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
////////////////////////////////////////////////////////////////////////////////

#include <coms/ComsMessageSender.h>
#include <coms/ComsMessageHandler.h>
#include <net/NetBuffer.h>
#include <net/NetInterface.h>
#include <common/Defines.h>
#include <common/Logger.h>
#include <tank/TankContainer.h>
#include <tank/TankState.h>
#ifndef S3D_SERVER
#include <client/ScorchedClient.h>
#endif
#include <server/ScorchedServer.h>
#include <set>
#include <zlib.h>

bool ComsMessageSender::formMessage(ComsMessage &message)
{
	// Write the message and its type to the buffer
	NetBufferDefault::defaultBuffer.reset();
	if (!message.writeTypeMessage(NetBufferDefault::defaultBuffer))
	{
		Logger::log( "ERROR: ComsMessageSender - Failed to write message type");
		return false;
	}
	if (!message.writeMessage(NetBufferDefault::defaultBuffer))
	{
		Logger::log( "ERROR: ComsMessageSender - Failed to write message");
		return false;
	}

	// Compress the message
	NetBufferDefault::defaultBuffer.compressBuffer();

	return true;
}

#ifndef S3D_SERVER
bool ComsMessageSender::sendToServer(
	ComsMessage &message, unsigned int flags)
{
	if (!ScorchedClient::instance()->getContext().netInterface ||
		!ScorchedClient::instance()->getNetInterface().started()) return false;
	if (!formMessage(message)) return false;

	if (ScorchedClient::instance()->getComsMessageHandler().getMessageLogging())
	{
		Logger::log(formatString("Client::send(%s, %u)", 
			message.getMessageType(),
			NetBufferDefault::defaultBuffer.getBufferUsed()));
	}	
	ScorchedClient::instance()->getNetInterface().sendMessageServer(
		NetBufferDefault::defaultBuffer, flags);
	return true;
}
#endif

bool ComsMessageSender::sendToSingleClient(ComsMessage &message,
	unsigned int destination, unsigned int flags)
{
	if (destination == 0) return true;
	if (!ScorchedServer::instance()->getContext().netInterface ||
		!ScorchedServer::instance()->getNetInterface().started())
	{
		Logger::log( "ERROR: ComsMessageSender::sendToSingleClient - Server not started");
		return false;
	}

	if (!formMessage(message)) return false;

	if (ScorchedServer::instance()->getComsMessageHandler().getMessageLogging())
	{
		Logger::log(formatString("Server::send(%s, %u, %u)", 
			message.getMessageType(),
			destination,
			NetBufferDefault::defaultBuffer.getBufferUsed()));
	}	
	ScorchedServer::instance()->getNetInterface().sendMessageDest(
		NetBufferDefault::defaultBuffer, destination, flags);
	return true;
}

bool ComsMessageSender::sendToAllConnectedClients(
	ComsMessage &message, unsigned int flags)
{
	std::map<unsigned int, Tank *>::iterator itor;
	std::map<unsigned int, Tank *> tanks = 
		ScorchedServer::instance()->getTankContainer().getPlayingTanks();
	if (tanks.empty()) return true;

	// Serialize the same message once for all client
	if (!formMessage(message)) return false;

	// Used to ensure we only send messages to each
	// destination once
	std::set<unsigned int> destinations;
	destinations.insert(0); // Make sure we don't send to dest 0
	std::set<unsigned int>::iterator findItor;
	for (itor = tanks.begin();
		itor != tanks.end();
		itor++)
	{
		Tank *tank = (*itor).second;

		unsigned int destination = tank->getDestinationId();
		findItor = destinations.find(destination);
		if (findItor == destinations.end())
		{
			destinations.insert(destination);

			if (ScorchedServer::instance()->getComsMessageHandler().getMessageLogging())
			{
				Logger::log(formatString("Server::send(%s, %u, %u)", 
					message.getMessageType(),
					destination,
					NetBufferDefault::defaultBuffer.getBufferUsed()));
			}	
			if (!ScorchedServer::instance()->getContext().netInterface ||
				!ScorchedServer::instance()->getNetInterface().started())
			{
				Logger::log( "ERROR: ComsMessageSender::sendToAllConnectedClients - Server not started");
				return false;
			}
			ScorchedServer::instance()->getNetInterface().sendMessageDest(
				NetBufferDefault::defaultBuffer, destination, flags);
		}
	}

	return true;
}

bool ComsMessageSender::sendToAllPlayingClients(
	ComsMessage &message, unsigned int flags)
{
	std::map<unsigned int, Tank *>::iterator itor;
	std::map<unsigned int, Tank *> tanks = 
		ScorchedServer::instance()->getTankContainer().getPlayingTanks();
	if (tanks.empty()) return true;

	// Serialize the same message once for all client
	if (!formMessage(message)) return false;

	// Used to ensure we only send messages to each
	// destination once
	std::set<unsigned int> destinations;
	destinations.insert(0); // Make sure we don't send to dest 0
	std::set<unsigned int>::iterator findItor;
	for (itor = tanks.begin();
		itor != tanks.end();
		itor++)
	{
		Tank *tank = (*itor).second;
		if (tank->getState().getState() != TankState::sPending &&
			tank->getState().getState() != TankState::sLoading &&
			tank->getState().getState() != TankState::sInitializing)
		{
			unsigned int destination = tank->getDestinationId();
			findItor = destinations.find(destination);
			if (findItor == destinations.end())
			{
				destinations.insert(destination);

				if (ScorchedServer::instance()->getComsMessageHandler().getMessageLogging())
				{
					Logger::log(formatString("Server::send(%s, %u, %u)", 
						message.getMessageType(),
						destination,
						NetBufferDefault::defaultBuffer.getBufferUsed()));
				}	
				if (!ScorchedServer::instance()->getContext().netInterface ||
					!ScorchedServer::instance()->getNetInterface().started())
				{
					Logger::log( "ERROR: ComsMessageSender::sendToAllPlayingClients - Server not started");
					return false;
				}
				ScorchedServer::instance()->getNetInterface().sendMessageDest(
					NetBufferDefault::defaultBuffer, destination, flags);
			}
		}
	}

	return true;
}




See more files for this project here

Scorched 3D

Scorched3D is a 3D remake of the popular 2D artillery game Scorched Earth.\r\nScorched3D can be played against the computer, other players and remotely across the internet or LAN.

Project homepage: http://sourceforge.net/projects/scorched3d
Programming language(s): C,C++,XML
License: gpl2

  ComsAddPlayerMessage.cpp
  ComsAddPlayerMessage.h
  ComsAdminMessage.cpp
  ComsAdminMessage.h
  ComsBuyAccessoryMessage.cpp
  ComsBuyAccessoryMessage.h
  ComsChannelMessage.cpp
  ComsChannelMessage.h
  ComsChannelTextMessage.cpp
  ComsChannelTextMessage.h
  ComsConnectAcceptMessage.cpp
  ComsConnectAcceptMessage.h
  ComsConnectMessage.cpp
  ComsConnectMessage.h
  ComsConnectRejectMessage.cpp
  ComsConnectRejectMessage.h
  ComsDefenseMessage.cpp
  ComsDefenseMessage.h
  ComsFileAkMessage.cpp
  ComsFileAkMessage.h
  ComsFileMessage.cpp
  ComsFileMessage.h
  ComsGameStateMessage.cpp
  ComsGameStateMessage.h
  ComsGameStoppedMessage.cpp
  ComsGameStoppedMessage.h
  ComsGiftMoneyMessage.cpp
  ComsGiftMoneyMessage.h
  ComsHaveModFilesMessage.cpp
  ComsHaveModFilesMessage.h
  ComsHeightMapMessage.cpp
  ComsHeightMapMessage.h
  ComsInitializeMessage.cpp
  ComsInitializeMessage.h
  ComsKeepAliveMessage.cpp
  ComsKeepAliveMessage.h
  ComsLevelMessage.cpp
  ComsLevelMessage.h
  ComsLinesMessage.cpp
  ComsLinesMessage.h
  ComsMessage.cpp
  ComsMessage.h
  ComsMessageHandler.cpp
  ComsMessageHandler.h
  ComsMessageSender.cpp
  ComsMessageSender.h
  ComsNewGameMessage.cpp
  ComsNewGameMessage.h
  ComsPlayMovesMessage.cpp
  ComsPlayMovesMessage.h
  ComsPlayedMoveMessage.cpp
  ComsPlayedMoveMessage.h
  ComsPlayerAimMessage.cpp
  ComsPlayerAimMessage.h
  ComsPlayerReadyMessage.cpp
  ComsPlayerReadyMessage.h
  ComsPlayerStateMessage.cpp
  ComsPlayerStateMessage.h
  ComsPlayerStatusMessage.cpp
  ComsPlayerStatusMessage.h
  ComsRmPlayerMessage.cpp
  ComsRmPlayerMessage.h
  ComsScoreMessage.cpp
  ComsScoreMessage.h
  ComsStartGameMessage.cpp
  ComsStartGameMessage.h
  ComsSyncCheckMessage.cpp
  ComsSyncCheckMessage.h
  ComsTargetStateMessage.cpp
  ComsTargetStateMessage.h
  ComsTimerStartMessage.cpp
  ComsTimerStartMessage.h