SuperTuxKart
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 "network/protocol_enum.hpp"
27 #include "utils/no_copy.hpp"
28 #include "utils/types.hpp"
29 
30 #include <memory>
31 #include <stddef.h>
32 
33 class Event;
34 class NetworkString;
35 class STKPeer;
36 
37 class Protocol;
38 
39 // ============================================================================*
44 {
45 public:
46  CallbackObject() {}
47  virtual ~CallbackObject() {}
48 
49  virtual void callback(Protocol *protocol) = 0;
50 }; // CallbackObject
51 
52 namespace ProtocolUtils
53 {
54  NetworkString* getNetworkString(ProtocolType type, size_t capacity = 16);
55 }
56 
57 // ============================================================================
66 class Protocol : public std::enable_shared_from_this<Protocol>,
67  public NoCopy
68 {
69 protected:
71  ProtocolType m_type;
72 
75 
78 public:
79  Protocol(ProtocolType type);
80  virtual ~Protocol();
81 
84  virtual void setup() = 0;
85  // ------------------------------------------------------------------------
86 
89  virtual void update(int ticks) = 0;
90 
93  virtual void asynchronousUpdate() = 0;
94 
96  bool checkDataSize(Event* event, unsigned int minimum_size);
97  void sendMessageToPeers(NetworkString *message, bool reliable = true);
99  bool reliable = true);
100  void sendToServer(NetworkString *message, bool reliable = true);
101  virtual void requestStart();
102  virtual void requestTerminate();
103  // ------------------------------------------------------------------------
107  virtual bool notifyEvent(Event* event) { return false; }
108  // ------------------------------------------------------------------------
113  virtual bool notifyEventAsynchronous(Event* event) { return false; }
114  // ------------------------------------------------------------------------
117  ProtocolType getProtocolType() const { return m_type; }
118  // ------------------------------------------------------------------------
121  // ------------------------------------------------------------------------
124  // ------------------------------------------------------------------------
126  virtual bool handleConnects() const { return m_handle_connections; }
127  // ------------------------------------------------------------------------
129  virtual bool handleDisconnects() const { return m_handle_disconnections; }
130 
131 }; // class Protocol
132 
133 #endif // PROTOCOL_HPP
Class that must be inherited to pass objects to protocols.
Definition: protocol.hpp:44
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:68
ProtocolType m_type
The type of the protocol.
Definition: protocol.hpp:71
virtual void asynchronousUpdate()=0
Called by the protocol listener as often as possible.
bool checkDataSize(Event *event, unsigned int minimum_size)
functions to check incoming data easily
Definition: protocol.cpp:65
virtual bool notifyEventAsynchronous(Event *event)
Notify a protocol matching the Event type of that event.
Definition: protocol.hpp:113
void setHandleConnections(bool b)
Sets if this protocol should receive connection events.
Definition: protocol.hpp:120
bool m_handle_disconnections
TRue if this protocol should recceiver disconnection events.
Definition: protocol.hpp:77
Protocol(ProtocolType type)
Constructor Sets the basic protocol parameters, as the callback object and the protocol type.
Definition: protocol.cpp:46
virtual void requestTerminate()
Submits a request to the ProtocolManager to terminate this protocol.
Definition: protocol.cpp:89
void sendMessageToPeers(NetworkString *message, bool reliable=true)
Sends a message to all validated peers in game, encrypt the message if needed.
Definition: protocol.cpp:101
bool m_handle_connections
True if this protocol should receive connection events.
Definition: protocol.hpp:74
void setHandleDisconnections(bool b)
Sets if this protocol should receive disconnection events.
Definition: protocol.hpp:123
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:107
virtual bool handleConnects() const
Return true if this protocol should be informed about connects.
Definition: protocol.hpp:126
void sendMessageToPeersInServer(NetworkString *message, bool reliable=true)
Sends a message to all validated peers in server, encrypt the message if needed.
Definition: protocol.cpp:112
ProtocolType getProtocolType() const
Method to get a protocol's type.
Definition: protocol.hpp:117
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:129
virtual ~Protocol()
Destructor.
Definition: protocol.cpp:56
void sendToServer(NetworkString *message, bool reliable=true)
Sends a message from a client to the server.
Definition: protocol.cpp:121
virtual void requestStart()
Starts a request in the protocol manager to start this protocol.
Definition: protocol.cpp:80
Represents a peer. This class is used to interface the ENetPeer structure.
Definition: stk_peer.hpp:76
Declares the general types that are used by the network.