2013-06-29 18:57:18 -04:00
|
|
|
//
|
|
|
|
// SuperTuxKart - a fun racing game with go-kart
|
|
|
|
// Copyright (C) 2013 SuperTuxKart-Team
|
|
|
|
//
|
|
|
|
// This program 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 3
|
|
|
|
// of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program 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 this program; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
|
|
|
|
|
2013-09-07 14:49:48 -04:00
|
|
|
/*! \file protocol.hpp
|
|
|
|
* \brief Generic protocols declarations.
|
|
|
|
*/
|
|
|
|
|
2013-06-29 18:57:18 -04:00
|
|
|
#ifndef PROTOCOL_HPP
|
|
|
|
#define PROTOCOL_HPP
|
|
|
|
|
2013-07-10 18:46:23 -04:00
|
|
|
#include "network/event.hpp"
|
2013-07-04 09:19:32 -04:00
|
|
|
#include "network/types.hpp"
|
2013-07-08 21:22:58 -04:00
|
|
|
#include "utils/types.hpp"
|
2013-06-29 18:57:18 -04:00
|
|
|
|
2013-07-10 18:46:23 -04:00
|
|
|
class ProtocolManager;
|
|
|
|
|
2013-06-29 18:57:18 -04:00
|
|
|
/** \enum PROTOCOL_TYPE
|
|
|
|
* \brief The types that protocols can have. This is used to select which protocol receives which event.
|
|
|
|
* \ingroup network
|
|
|
|
*/
|
|
|
|
enum PROTOCOL_TYPE
|
|
|
|
{
|
|
|
|
PROTOCOL_NONE = 0, //!< No protocol type assigned.
|
|
|
|
PROTOCOL_CONNECTION = 1, //!< Protocol that deals with client-server connection.
|
|
|
|
PROTOCOL_LOBBY_ROOM = 2, //!< Protocol that is used during the lobby room phase.
|
2013-07-13 19:12:53 -04:00
|
|
|
PROTOCOL_START_GAME = 3, //!< Protocol used when starting the game.
|
|
|
|
PROTOCOL_SYNCHRONIZATION = 4,//!<Protocol used to synchronize clocks.
|
2013-07-16 10:45:48 -04:00
|
|
|
PROTOCOL_KART_UPDATE = 5, //!< Protocol to update karts position, rotation etc...
|
2013-07-20 13:32:23 -04:00
|
|
|
PROTOCOL_GAME_EVENTS = 6, //!< Protocol to communicate the game events.
|
|
|
|
PROTOCOL_CONTROLLER_EVENTS = 7,//!< Protocol to transfer controller modifications
|
2013-06-29 18:57:18 -04:00
|
|
|
PROTOCOL_SILENT = 0xffff //!< Used for protocols that do not subscribe to any network event.
|
|
|
|
};
|
|
|
|
|
|
|
|
/** \class Protocol
|
2013-07-10 18:46:23 -04:00
|
|
|
* \brief Abstract class used to define the global protocol functions.
|
2013-06-29 18:57:18 -04:00
|
|
|
* A protocol is an entity that is started at a point, and that is updated by a thread.
|
|
|
|
* A protocol can be terminated by an other class, or it can terminate itself if has fulfilled its role.
|
|
|
|
* This class must be inherited to make any network job.
|
|
|
|
* \ingroup network
|
|
|
|
*/
|
|
|
|
class Protocol
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
/*!
|
|
|
|
* \brief Constructor
|
|
|
|
*
|
|
|
|
* Sets the basic protocol parameters, as the callback object and the protocol type.
|
|
|
|
*
|
|
|
|
* \param callback_object The callback object that will be used by the protocol. Protocols that do not use callback objects must set it to NULL.
|
|
|
|
* \param type The type of the protocol.
|
|
|
|
*/
|
|
|
|
Protocol(CallbackObject* callback_object, PROTOCOL_TYPE type);
|
2013-08-01 15:49:57 -04:00
|
|
|
/*! \brief Destructor
|
2013-06-29 18:57:18 -04:00
|
|
|
*/
|
2013-07-10 18:46:23 -04:00
|
|
|
virtual ~Protocol();
|
|
|
|
|
2013-08-01 15:49:57 -04:00
|
|
|
/*! \brief Notify a protocol matching the Event type of that event.
|
|
|
|
* \param event : Pointer to the event.
|
|
|
|
* \return True if the event has been treated, false elseway
|
2013-06-29 18:57:18 -04:00
|
|
|
*/
|
2013-08-01 15:49:57 -04:00
|
|
|
virtual bool notifyEvent(Event* event) { return false; }
|
2013-07-10 18:46:23 -04:00
|
|
|
|
2013-08-01 15:49:57 -04:00
|
|
|
/*! \brief Notify a protocol matching the Event type of that event.
|
|
|
|
* This update is done asynchronously :
|
|
|
|
* \param event : Pointer to the event.
|
|
|
|
* \return True if the event has been treated, false elseway
|
|
|
|
*/
|
|
|
|
virtual bool notifyEventAsynchronous(Event* event) { return false; }
|
|
|
|
|
|
|
|
/*! \brief Set the protocol listener.
|
|
|
|
* \param listener : Pointer to the listener.
|
2013-06-29 18:57:18 -04:00
|
|
|
*/
|
|
|
|
void setListener(ProtocolManager* listener);
|
2013-07-10 18:46:23 -04:00
|
|
|
|
2013-08-01 15:49:57 -04:00
|
|
|
/*! \brief Called when the protocol is going to start. Must be re-defined by subclasses.
|
2013-06-29 18:57:18 -04:00
|
|
|
*/
|
|
|
|
virtual void setup() = 0;
|
2013-08-01 15:49:57 -04:00
|
|
|
/*! \brief Called when the protocol is paused (by an other entity or by itself).
|
|
|
|
* This function must be called by the subclasse's pause function if re-defined.
|
2013-06-29 18:57:18 -04:00
|
|
|
*/
|
|
|
|
virtual void pause();
|
2013-08-01 15:49:57 -04:00
|
|
|
/*! \brief Called when the protocol is unpaused.
|
|
|
|
* This function must be called by the subclasse's unpause function if re-defined.
|
2013-06-29 18:57:18 -04:00
|
|
|
*/
|
|
|
|
virtual void unpause();
|
2013-08-01 15:49:57 -04:00
|
|
|
/*! \brief Called by the protocol listener, synchronously with the main loop. Must be re-defined.
|
2013-06-29 18:57:18 -04:00
|
|
|
*/
|
|
|
|
virtual void update() = 0;
|
2013-08-01 15:49:57 -04:00
|
|
|
/*! \brief Called by the protocol listener as often as possible. Must be re-defined.
|
2013-07-15 10:31:14 -04:00
|
|
|
*/
|
|
|
|
virtual void asynchronousUpdate() = 0;
|
2013-08-01 15:49:57 -04:00
|
|
|
/*! \brief Called when the protocol is to be killed.
|
2013-07-07 15:49:51 -04:00
|
|
|
*/
|
|
|
|
virtual void kill();
|
2013-07-10 18:46:23 -04:00
|
|
|
|
2013-08-01 15:49:57 -04:00
|
|
|
/*! \brief Method to get a protocol's type.
|
|
|
|
* \return The protocol type.
|
2013-06-29 18:57:18 -04:00
|
|
|
*/
|
|
|
|
PROTOCOL_TYPE getProtocolType();
|
2013-09-22 16:40:14 -04:00
|
|
|
|
|
|
|
/// functions to check incoming data easily
|
|
|
|
bool checkDataSizeAndToken(Event* event, int minimum_size);
|
|
|
|
bool isByteCorrect(Event* event, int byte_nb, int value);
|
|
|
|
void sendMessageToPeersChangingToken(NetworkString prefix, NetworkString message);
|
|
|
|
|
|
|
|
|
2013-06-29 18:57:18 -04:00
|
|
|
protected:
|
|
|
|
ProtocolManager* m_listener; //!< The protocol listener
|
|
|
|
PROTOCOL_TYPE m_type; //!< The type of the protocol
|
|
|
|
CallbackObject* m_callback_object; //!< The callback object, if needed
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // PROTOCOL_HPP
|