SuperTuxKart
Loading...
Searching...
No Matches
protocol.hpp
Go to the documentation of this file.
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
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
32class Event;
33class NetworkString;
34class STKPeer;
35
36
43{
52}; // ProtocolType
53
54// ----------------------------------------------------------------------------
59{
64}; // ProtocolState
65
66class Protocol;
67
68// ============================================================================*
73{
74public:
76 virtual ~CallbackObject() {}
77
78 virtual void callback(Protocol *protocol) = 0;
79}; // CallbackObject
80
81// ============================================================================
90class Protocol : public std::enable_shared_from_this<Protocol>,
91 public NoCopy
92{
93protected:
96
99
102public:
104 virtual ~Protocol();
105
108 virtual void setup() = 0;
109 // ------------------------------------------------------------------------
110
113 virtual void update(int ticks) = 0;
114
117 virtual void asynchronousUpdate() = 0;
118
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);
124 bool reliable = true);
125 void sendToServer(NetworkString *message, bool reliable = true);
126 virtual void requestStart();
127 virtual void requestTerminate();
128 // ------------------------------------------------------------------------
132 virtual bool notifyEvent(Event* event) { return false; }
133 // ------------------------------------------------------------------------
138 virtual bool notifyEventAsynchronous(Event* event) { return false; }
139 // ------------------------------------------------------------------------
143 // ------------------------------------------------------------------------
146 // ------------------------------------------------------------------------
149 // ------------------------------------------------------------------------
151 virtual bool handleConnects() const { return m_handle_connections; }
152 // ------------------------------------------------------------------------
154 virtual bool handleDisconnects() const { return m_handle_disconnections; }
155
156}; // class Protocol
157
158#endif // PROTOCOL_HPP
Class that must be inherited to pass objects to protocols.
Definition: protocol.hpp:73
Class representing an event that need to pass trough the system. This is used to remove ENet dependen...
Definition: event.hpp:73
A new implementation of NetworkString, which has a fixed format: Byte 0: The type of the message,...
Definition: network_string.hpp:422
Utility class, you can inherit from this class to disallow the assignment operator and copy construct...
Definition: no_copy.hpp:26
Abstract class used to define the global protocol functions.
Definition: protocol.hpp:92
ProtocolType m_type
The type of the protocol.
Definition: protocol.hpp:95
virtual void asynchronousUpdate()=0
Called by the protocol listener as often as possible.
bool checkDataSize(Event *event, unsigned int minimum_size)
Checks if the message has at least the specified size, and if not prints a warning message including ...
Definition: protocol.cpp:63
virtual bool notifyEventAsynchronous(Event *event)
Notify a protocol matching the Event type of that event.
Definition: protocol.hpp:138
void setHandleConnections(bool b)
Sets if this protocol should receive connection events.
Definition: protocol.hpp:145
bool m_handle_disconnections
TRue if this protocol should recceiver disconnection events.
Definition: protocol.hpp:101
virtual void requestTerminate()
Submits a request to the ProtocolManager to terminate this protocol.
Definition: protocol.cpp:87
void sendMessageToPeers(NetworkString *message, bool reliable=true)
Sends a message to all validated peers in game, encrypt the message if needed.
Definition: protocol.cpp:99
bool m_handle_connections
True if this protocol should receive connection events.
Definition: protocol.hpp:98
void setHandleDisconnections(bool b)
Sets if this protocol should receive disconnection events.
Definition: protocol.hpp:148
virtual void setup()=0
Called when the protocol is going to start.
virtual bool notifyEvent(Event *event)
Notify a protocol matching the Event type of that event.
Definition: protocol.hpp:132
virtual bool handleConnects() const
Return true if this protocol should be informed about connects.
Definition: protocol.hpp:151
void sendMessageToPeersInServer(NetworkString *message, bool reliable=true)
Sends a message to all validated peers in server, encrypt the message if needed.
Definition: protocol.cpp:110
ProtocolType getProtocolType() const
Method to get a protocol's type.
Definition: protocol.hpp:142
virtual void update(int ticks)=0
Called by the protocol listener, synchronously with the main loop.
virtual bool handleDisconnects() const
Return true if this protocol should be informed about disconnects.
Definition: protocol.hpp:154
virtual ~Protocol()
Destructor.
Definition: protocol.cpp:45
NetworkString * getNetworkString(size_t capacity=16) const
functions to check incoming data easily
Definition: protocol.cpp:53
void sendToServer(NetworkString *message, bool reliable=true)
Sends a message from a client to the server.
Definition: protocol.cpp:119
virtual void requestStart()
Starts a request in the protocol manager to start this protocol.
Definition: protocol.cpp:78
Represents a peer. This class is used to interface the ENetPeer structure.
Definition: stk_peer.hpp:76
ProtocolType
The types that protocols can have.
Definition: protocol.hpp:43
@ PROTOCOL_CONNECTION
Protocol that deals with client-server connection.
Definition: protocol.hpp:45
@ PROTOCOL_CONTROLLER_EVENTS
Protocol to transfer controller modifications.
Definition: protocol.hpp:48
@ PROTOCOL_MAX
Maximum number of different protocol types.
Definition: protocol.hpp:50
@ PROTOCOL_LOBBY_ROOM
Protocol that is used during the lobby room phase.
Definition: protocol.hpp:46
@ PROTOCOL_SYNCHRONOUS
Flag, indicates synchronous delivery.
Definition: protocol.hpp:51
@ PROTOCOL_GAME_EVENTS
Protocol to communicate the game events.
Definition: protocol.hpp:47
@ PROTOCOL_NONE
No protocol type assigned.
Definition: protocol.hpp:44
@ PROTOCOL_SILENT
Used for protocols that do not subscribe to any network event.
Definition: protocol.hpp:49
ProtocolState
Defines the three states that a protocol can have.
Definition: protocol.hpp:59
@ PROTOCOL_STATE_RUNNING
The protocol is being updated everytime.
Definition: protocol.hpp:61
@ PROTOCOL_STATE_TERMINATED
The protocol is terminated/does not exist.
Definition: protocol.hpp:63
@ PROTOCOL_STATE_INITIALISING
The protocol is waiting to be started.
Definition: protocol.hpp:60
@ PROTOCOL_STATE_PAUSED
The protocol is paused.
Definition: protocol.hpp:62
Declares the general types that are used by the network.