1 // 2 // SuperTuxKart - a fun racing game with go-kart 3 // Copyright (C) 2013-2015 SuperTuxKart-Team 4 // 5 // This program is free software; you can redistribute it and/or 6 // modify it under the terms of the GNU General Public License 7 // as published by the Free Software Foundation; either version 3 8 // of the License, or (at your option) any later version. 9 // 10 // This program is distributed in the hope that it will be useful, 11 // but WITHOUT ANY WARRANTY; without even the implied warranty of 12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13 // GNU General Public License for more details. 14 // 15 // You should have received a copy of the GNU General Public License 16 // along with this program; if not, write to the Free Software 17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. 18 19 /*! \file protocol.hpp 20 * \brief Generic protocols declarations. 21 */ 22 23 #ifndef PROTOCOL_HPP 24 #define PROTOCOL_HPP 25 26 #include "utils/no_copy.hpp" 27 #include "utils/types.hpp" 28 29 #include <memory> 30 #include <stddef.h> 31 32 class Event; 33 class NetworkString; 34 class STKPeer; 35 36 37 /** \enum ProtocolType 38 * \brief The types that protocols can have. This is used to select which 39 * protocol receives which event. 40 * \ingroup network 41 */ 42 enum ProtocolType 43 { 44 PROTOCOL_NONE = 0x00, //!< No protocol type assigned. 45 PROTOCOL_CONNECTION = 0x01, //!< Protocol that deals with client-server connection. 46 PROTOCOL_LOBBY_ROOM = 0x02, //!< Protocol that is used during the lobby room phase. 47 PROTOCOL_GAME_EVENTS = 0x03, //!< Protocol to communicate the game events. 48 PROTOCOL_CONTROLLER_EVENTS = 0x04, //!< Protocol to transfer controller modifications 49 PROTOCOL_SILENT = 0x05, //!< Used for protocols that do not subscribe to any network event. 50 PROTOCOL_MAX , //!< Maximum number of different protocol types 51 PROTOCOL_SYNCHRONOUS = 0x80, //!< Flag, indicates synchronous delivery 52 }; // ProtocolType 53 54 // ---------------------------------------------------------------------------- 55 /** \enum ProtocolState 56 * \brief Defines the three states that a protocol can have. 57 */ 58 enum ProtocolState 59 { 60 PROTOCOL_STATE_INITIALISING, //!< The protocol is waiting to be started 61 PROTOCOL_STATE_RUNNING, //!< The protocol is being updated everytime. 62 PROTOCOL_STATE_PAUSED, //!< The protocol is paused. 63 PROTOCOL_STATE_TERMINATED //!< The protocol is terminated/does not exist. 64 }; // ProtocolState 65 66 class Protocol; 67 68 // ============================================================================* 69 /** \class CallbackObject 70 * \brief Class that must be inherited to pass objects to protocols. 71 */ 72 class CallbackObject 73 { 74 public: CallbackObject()75 CallbackObject() {} ~CallbackObject()76 virtual ~CallbackObject() {} 77 78 virtual void callback(Protocol *protocol) = 0; 79 }; // CallbackObject 80 81 // ============================================================================ 82 /** \class Protocol 83 * \brief Abstract class used to define the global protocol functions. 84 * A protocol is an entity that is started at a point, and that is updated 85 * by a thread. A protocol can be terminated by an other class, or it can 86 * terminate itself if has fulfilled its role. This class must be inherited 87 * to make any network job. 88 * \ingroup network 89 */ 90 class Protocol : public std::enable_shared_from_this<Protocol>, 91 public NoCopy 92 { 93 protected: 94 /** The type of the protocol. */ 95 ProtocolType m_type; 96 97 /** True if this protocol should receive connection events. */ 98 bool m_handle_connections; 99 100 /** TRue if this protocol should recceiver disconnection events. */ 101 bool m_handle_disconnections; 102 public: 103 Protocol(ProtocolType type); 104 virtual ~Protocol(); 105 106 /** \brief Called when the protocol is going to start. Must be re-defined 107 * by subclasses. */ 108 virtual void setup() = 0; 109 // ------------------------------------------------------------------------ 110 111 /** \brief Called by the protocol listener, synchronously with the main 112 * loop. Must be re-defined.*/ 113 virtual void update(int ticks) = 0; 114 115 /** \brief Called by the protocol listener as often as possible. 116 * Must be re-defined. */ 117 virtual void asynchronousUpdate() = 0; 118 119 /// functions to check incoming data easily 120 NetworkString* getNetworkString(size_t capacity = 16) const; 121 bool checkDataSize(Event* event, unsigned int minimum_size); 122 void sendMessageToPeers(NetworkString *message, bool reliable = true); 123 void sendMessageToPeersInServer(NetworkString *message, 124 bool reliable = true); 125 void sendToServer(NetworkString *message, bool reliable = true); 126 virtual void requestStart(); 127 virtual void requestTerminate(); 128 // ------------------------------------------------------------------------ 129 /** \brief Notify a protocol matching the Event type of that event. 130 * \param event : Pointer to the event. 131 * \return True if the event has been treated, false otherwise. */ notifyEvent(Event * event)132 virtual bool notifyEvent(Event* event) { return false; } 133 // ------------------------------------------------------------------------ 134 /** \brief Notify a protocol matching the Event type of that event. 135 * This update is done asynchronously : 136 * \param event : Pointer to the event. 137 * \return True if the event has been treated, false otherwise */ notifyEventAsynchronous(Event * event)138 virtual bool notifyEventAsynchronous(Event* event) { return false; } 139 // ------------------------------------------------------------------------ 140 /** \brief Method to get a protocol's type. 141 * \return The protocol type. */ getProtocolType() const142 ProtocolType getProtocolType() const { return m_type; } 143 // ------------------------------------------------------------------------ 144 /** Sets if this protocol should receive connection events. */ setHandleConnections(bool b)145 void setHandleConnections(bool b) { m_handle_connections = b; } 146 // ------------------------------------------------------------------------ 147 /** Sets if this protocol should receive disconnection events. */ setHandleDisconnections(bool b)148 void setHandleDisconnections(bool b) { m_handle_disconnections = b; } 149 // ------------------------------------------------------------------------ 150 /** Return true if this protocol should be informed about connects. */ handleConnects() const151 virtual bool handleConnects() const { return m_handle_connections; } 152 // ------------------------------------------------------------------------ 153 /** Return true if this protocol should be informed about disconnects. */ handleDisconnects() const154 virtual bool handleDisconnects() const { return m_handle_disconnections; } 155 156 }; // class Protocol 157 158 #endif // PROTOCOL_HPP 159