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 #include "network/protocol.hpp"
20 
21 #include "network/event.hpp"
22 #include "network/network_string.hpp"
23 #include "network/protocol_manager.hpp"
24 #include "network/stk_host.hpp"
25 #include "network/stk_peer.hpp"
26 
27 /** \brief Constructor
28  *  Sets the basic protocol parameters, as the callback object and the
29  *  protocol type.
30  *  \param callback_object The callback object that will be used by the
31  *          protocol. Protocols that do not use callback objects must set
32  *          it to NULL.
33  *  \param type The type of the protocol.
34  */
Protocol(ProtocolType type)35 Protocol::Protocol(ProtocolType type)
36 {
37     m_type                  = type;
38     m_handle_connections    = false;
39     m_handle_disconnections = false;
40 }   // Protocol
41 
42 // ----------------------------------------------------------------------------
43 /** \brief Destructor.
44  */
~Protocol()45 Protocol::~Protocol()
46 {
47 }   // ~Protocol
48 
49 // ----------------------------------------------------------------------------
50 /** Returns a network string with the given type.
51  *  \capacity Default preallocated size for the message.
52  */
getNetworkString(size_t capacity) const53 NetworkString* Protocol::getNetworkString(size_t capacity) const
54 {
55     return new NetworkString(m_type, (int)capacity);
56 }   // getNetworkString
57 
58 // ----------------------------------------------------------------------------
59 /** Checks if the message has at least the specified size, and if not prints
60  *  a warning message including the message content.
61  *  \return True if the message is long enough, false otherwise.
62  */
checkDataSize(Event * event,unsigned int minimum_size)63 bool Protocol::checkDataSize(Event* event, unsigned int minimum_size)
64 {
65     const NetworkString &data = event->data();
66     if (data.size() < minimum_size)
67     {
68         Log::warn("Protocol", "Receiving a badly formatted message:");
69         Log::warn("Protocol", data.getLogMessage().c_str());
70         return false;
71     }
72     return true;
73 }   // checkDataSize
74 
75 // ----------------------------------------------------------------------------
76 /** Starts a request in the protocol manager to start this protocol.
77  */
requestStart()78 void Protocol::requestStart()
79 {
80     if (auto pm = ProtocolManager::lock())
81         pm->requestStart(shared_from_this());
82 }   // requestStart
83 
84 // ----------------------------------------------------------------------------
85 /** Submits a request to the ProtocolManager to terminate this protocol.
86  */
requestTerminate()87 void Protocol::requestTerminate()
88 {
89     if (auto pm = ProtocolManager::lock())
90         pm->requestTerminate(shared_from_this());
91 }   // requestTerminate
92 
93 // ----------------------------------------------------------------------------
94 /** Sends a message to all validated peers in game, encrypt the message if
95  *  needed. The message is composed of a 1-byte message (usually the message
96  *  type) followed by the actual message.
97  *  \param message The actual message content.
98 */
sendMessageToPeers(NetworkString * message,bool reliable)99 void Protocol::sendMessageToPeers(NetworkString *message, bool reliable)
100 {
101     STKHost::get()->sendPacketToAllPeers(message, reliable);
102 }   // sendMessageToPeers
103 
104 // ----------------------------------------------------------------------------
105 /** Sends a message to all validated peers in server, encrypt the message if
106  *  needed. The message is composed of a 1-byte message (usually the message
107  *  type) followed by the actual message.
108  *  \param message The actual message content.
109 */
sendMessageToPeersInServer(NetworkString * message,bool reliable)110 void Protocol::sendMessageToPeersInServer(NetworkString* message,
111                                           bool reliable)
112 {
113     STKHost::get()->sendPacketToAllPeersInServer(message, reliable);
114 }   // sendMessageToPeersInServer
115 
116 // ----------------------------------------------------------------------------
117 /** Sends a message from a client to the server.
118  */
sendToServer(NetworkString * message,bool reliable)119 void Protocol::sendToServer(NetworkString *message, bool reliable)
120 {
121     STKHost::get()->sendToServer(message, reliable);
122 }   // sendMessage
123