adding a protocol to synchronize client/servers and a little bit of code to test it

git-svn-id: svn+ssh://svn.code.sf.net/p/supertuxkart/code/main/branches/hilnius@13208 178a84e3-b1eb-0310-8ba1-8eac791a3b58
This commit is contained in:
hilnius 2013-07-13 23:12:53 +00:00
parent ebc4c60e2c
commit c1534075d3
18 changed files with 107 additions and 86 deletions

Binary file not shown.

View File

@ -104,6 +104,7 @@ src/karts/controller/ai_base_controller.cpp
src/karts/controller/ai_properties.cpp
src/karts/controller/controller.cpp
src/karts/controller/end_controller.cpp
src/karts/controller/network_player_controller.cpp
src/karts/controller/player_controller.cpp
src/karts/controller/skidding_ai.cpp
src/karts/explosion_animation.cpp
@ -142,6 +143,7 @@ src/network/http_functions.cpp
src/network/network_interface.cpp
src/network/network_manager.cpp
src/network/network_string.cpp
src/network/network_world.cpp
src/network/protocol.cpp
src/network/protocol_manager.cpp
src/network/protocols/connect_to_peer.cpp
@ -156,8 +158,10 @@ src/network/protocols/ping_protocol.cpp
src/network/protocols/quick_join_protocol.cpp
src/network/protocols/request_connection.cpp
src/network/protocols/show_public_address.cpp
src/network/protocols/start_game_protocol.cpp
src/network/protocols/start_server.cpp
src/network/protocols/stop_server.cpp
src/network/protocols/synchronization_protocol.cpp
src/network/server_network_manager.cpp
src/network/stk_host.cpp
src/network/stk_peer.cpp
@ -378,6 +382,7 @@ src/karts/controller/ai_properties.hpp
src/karts/controller/controller.hpp
src/karts/controller/end_controller.hpp
src/karts/controller/kart_control.hpp
src/karts/controller/network_player_controller.hpp
src/karts/controller/player_controller.hpp
src/karts/controller/skidding_ai.hpp
src/karts/explosion_animation.hpp
@ -415,6 +420,7 @@ src/network/http_functions.hpp
src/network/network_interface.hpp
src/network/network_manager.hpp
src/network/network_string.hpp
src/network/network_world.hpp
src/network/protocol.hpp
src/network/protocol_manager.hpp
src/network/protocols/connect_to_peer.hpp
@ -429,8 +435,10 @@ src/network/protocols/ping_protocol.hpp
src/network/protocols/quick_join_protocol.hpp
src/network/protocols/request_connection.hpp
src/network/protocols/show_public_address.hpp
src/network/protocols/start_game_protocol.hpp
src/network/protocols/start_server.hpp
src/network/protocols/stop_server.hpp
src/network/protocols/synchronization_protocol.hpp
src/network/remote_kart_info.hpp
src/network/server_network_manager.hpp
src/network/singleton.hpp

View File

@ -606,7 +606,7 @@ int handleCmdLine(int argc, char **argv)
{
int n;
char s[1024];
bool try_login = false;
irr::core::stringw login, password;
@ -1104,7 +1104,7 @@ int handleCmdLine(int argc, char **argv)
UserConfigParams::m_sfx = false; // Disable sound effects
UserConfigParams::m_music = false;// and music when profiling
}
if (try_login)
{
irr::core::stringw s;
@ -1240,7 +1240,7 @@ void cleanSuperTuxKart()
if(news_manager) delete news_manager;
if(addons_manager) delete addons_manager;
NetworkManager::kill();
if(grand_prix_manager) delete grand_prix_manager;
if(highscore_manager) delete highscore_manager;
if(attachment_manager) delete attachment_manager;
@ -1382,10 +1382,10 @@ int main(int argc, char *argv[] )
//handleCmdLine() needs InitTuxkart() so it can't be called first
if(!handleCmdLine(argc, argv)) exit(0);
// load the network manager
// If the server has been created (--server option), this will do nothing (just a warning):
NetworkManager::getInstance<ClientNetworkManager>();
NetworkManager::getInstance<ClientNetworkManager>();
NetworkManager::getInstance()->run();
if (NetworkManager::getInstance()->isServer())
{

View File

@ -29,6 +29,7 @@
#include "input/wiimote_manager.hpp"
#include "modes/profile_world.hpp"
#include "modes/world.hpp"
#include "network/network_world.hpp"
#include "race/race_manager.hpp"
#include "states_screens/state_manager.hpp"
#include "utils/profiler.hpp"
@ -74,7 +75,7 @@ float MainLoop::getLimitedDt()
// When in menus, reduce FPS much, it's not necessary to push to the maximum for plain menus
const int max_fps = (StateManager::get()->throttleFPS() ? 35 : UserConfigParams::m_max_fps);
const int current_fps = (int)(1000.0f/dt);
if( current_fps > max_fps && !ProfileWorld::isNoGraphics())
if( current_fps > max_fps && !ProfileWorld::isProfileMode())
{
int wait_time = 1000/max_fps - 1000/current_fps;
if(wait_time < 1) wait_time = 1;
@ -95,7 +96,10 @@ void MainLoop::updateRace(float dt)
{
if(ProfileWorld::isProfileMode()) dt=1.0f/60.0f;
World::getWorld()->updateWorld(dt);
if (NetworkWorld::getInstance<NetworkWorld>()->isRunning())
NetworkWorld::getInstance<NetworkWorld>()->update(dt);
else
World::getWorld()->updateWorld(dt);
} // updateRace
//-----------------------------------------------------------------------------

View File

@ -36,6 +36,8 @@ void* waitInput(void* data)
std::string str = "";
bool stop = false;
int n = 0;
bool success = false;
uint32_t ping = 0;
while(!stop)
{
@ -64,6 +66,10 @@ void* waitInput(void* data)
ClientLobbyRoomProtocol* clrp = static_cast<ClientLobbyRoomProtocol*>(protocol);
clrp->requestKartSelection(str2);
}
else if (str == "synchronize")
{
ProtocolManager::getInstance()->requestStart(new SynchronizationProtocol(&ping, &success));
}
else if (NetworkManager::getInstance()->getPeers().size() > 0)
{
NetworkString msg;
@ -106,11 +112,11 @@ void ClientNetworkManager::run()
NetworkManager::run();
}
void ClientNetworkManager::sendPacket(const NetworkString& data)
void ClientNetworkManager::sendPacket(const NetworkString& data, bool reliable)
{
if (m_peers.size() > 1)
Log::warn("ClientNetworkManager", "Ambiguous send of data.\n");
m_peers[0]->sendPacket(data);
m_peers[0]->sendPacket(data, reliable);
}
STKPeer* ClientNetworkManager::getPeer()

View File

@ -29,19 +29,19 @@ class ClientNetworkManager : public NetworkManager
{
return Singleton<NetworkManager>::getInstance<ClientNetworkManager>();
}
virtual void run();
virtual void sendPacket(const NetworkString& data);
virtual void sendPacket(const NetworkString& data, bool reliable = true);
STKPeer* getPeer();
virtual bool isServer() { return false; }
void setConnected(bool value) { m_connected = value; }
bool isConnected() { return m_connected; }
protected:
ClientNetworkManager();
virtual ~ClientNetworkManager();
bool m_connected; //!< Is the user connected to a server
pthread_t* m_thread_keyboard;
};

View File

@ -113,21 +113,21 @@ void NetworkManager::notifyEvent(Event* event)
//-----------------------------------------------------------------------------
void NetworkManager::sendPacket(STKPeer* peer, const NetworkString& data)
void NetworkManager::sendPacket(STKPeer* peer, const NetworkString& data, bool reliable)
{
if (peer)
peer->sendPacket(data);
peer->sendPacket(data, reliable);
}
//-----------------------------------------------------------------------------
void NetworkManager::sendPacketExcept(STKPeer* peer, const NetworkString& data)
void NetworkManager::sendPacketExcept(STKPeer* peer, const NetworkString& data, bool reliable)
{
for (unsigned int i = 0; i < m_peers.size(); i++)
{
if (!m_peers[i]->isSamePeer(peer))
{
m_peers[i]->sendPacket(data);
m_peers[i]->sendPacket(data, reliable);
}
}
}

View File

@ -34,17 +34,17 @@ class NetworkManager : public Singleton<NetworkManager>
{
friend class Singleton<NetworkManager>;
public:
virtual void run();
virtual void run();
// network management functions
virtual bool connect(TransportAddress peer);
virtual void setManualSocketsMode(bool manual);
// message/packets related functions
virtual void notifyEvent(Event* event);
virtual void sendPacket(const NetworkString& data) = 0;
virtual void sendPacket(STKPeer* peer, const NetworkString& data);
virtual void sendPacketExcept(STKPeer* peer, const NetworkString& data);
virtual void sendPacket(const NetworkString& data, bool reliable = true) = 0;
virtual void sendPacket(STKPeer* peer, const NetworkString& data, bool reliable = true);
virtual void sendPacketExcept(STKPeer* peer, const NetworkString& data, bool reliable = true);
// Game related functions
virtual GameSetup* setupNewGame(); //!< Creates a new game setup and returns it
@ -53,29 +53,30 @@ class NetworkManager : public Singleton<NetworkManager>
void setLogin(std::string username, std::string password);
void setPublicAddress(TransportAddress addr);
void removePeer(STKPeer* peer);
// getters
virtual bool peerExists(TransportAddress peer);
virtual bool isConnectedTo(TransportAddress peer);
virtual bool isServer() = 0;
inline bool isClient() { return !isServer(); }
bool isPlayingOnline() { return m_playing_online; }
STKHost* getHost() { return m_localhost; }
std::vector<STKPeer*> getPeers() { return m_peers; }
unsigned int getPeerCount() { return m_peers.size(); }
TransportAddress getPublicAddress() { return m_public_address; }
GameSetup* getGameSetup() { return m_game_setup; }
protected:
NetworkManager();
virtual ~NetworkManager();
// protected members
std::vector<STKPeer*> m_peers;
STKHost* m_localhost;
bool m_playing_online;
GameSetup* m_game_setup;
TransportAddress m_public_address;
PlayerLogin m_player_login;
};

View File

@ -34,6 +34,8 @@ 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.
PROTOCOL_START_GAME = 3, //!< Protocol used when starting the game.
PROTOCOL_SYNCHRONIZATION = 4,//!<Protocol used to synchronize clocks.
PROTOCOL_SILENT = 0xffff //!< Used for protocols that do not subscribe to any network event.
};

View File

@ -88,27 +88,27 @@ void ProtocolManager::notifyEvent(Event* event)
pthread_mutex_unlock(&m_events_mutex);
}
void ProtocolManager::sendMessage(Protocol* sender, const NetworkString& message)
void ProtocolManager::sendMessage(Protocol* sender, const NetworkString& message, bool reliable)
{
NetworkString newMessage;
newMessage.ai8(sender->getProtocolType()); // add one byte to add protocol type
newMessage += message;
NetworkManager::getInstance()->sendPacket(newMessage);
NetworkManager::getInstance()->sendPacket(newMessage, reliable);
}
void ProtocolManager::sendMessage(Protocol* sender, STKPeer* peer, const NetworkString& message)
void ProtocolManager::sendMessage(Protocol* sender, STKPeer* peer, const NetworkString& message, bool reliable)
{
NetworkString newMessage;
newMessage.ai8(sender->getProtocolType()); // add one byte to add protocol type
newMessage += message;
NetworkManager::getInstance()->sendPacket(peer, newMessage);
NetworkManager::getInstance()->sendPacket(peer, newMessage, reliable);
}
void ProtocolManager::sendMessageExcept(Protocol* sender, STKPeer* peer, const NetworkString& message)
void ProtocolManager::sendMessageExcept(Protocol* sender, STKPeer* peer, const NetworkString& message, bool reliable)
{
NetworkString newMessage;
newMessage.ai8(sender->getProtocolType()); // add one byte to add protocol type
newMessage += message;
NetworkManager::getInstance()->sendPacketExcept(peer, newMessage);
NetworkManager::getInstance()->sendPacketExcept(peer, newMessage, reliable);
}
uint32_t ProtocolManager::requestStart(Protocol* protocol)

View File

@ -104,15 +104,15 @@ class ProtocolManager : public Singleton<ProtocolManager>
/*!
* \brief WILL BE COMMENTED LATER
*/
virtual void sendMessage(Protocol* sender, const NetworkString& message);
virtual void sendMessage(Protocol* sender, const NetworkString& message, bool reliable = true);
/*!
* \brief WILL BE COMMENTED LATER
*/
virtual void sendMessage(Protocol* sender, STKPeer* peer, const NetworkString& message);
virtual void sendMessage(Protocol* sender, STKPeer* peer, const NetworkString& message, bool reliable = true);
/*!
* \brief WILL BE COMMENTED LATER
*/
virtual void sendMessageExcept(Protocol* sender, STKPeer* peer, const NetworkString& message);
virtual void sendMessageExcept(Protocol* sender, STKPeer* peer, const NetworkString& message, bool reliable = true);
/*!
* \brief Asks the manager to start a protocol.

View File

@ -50,15 +50,15 @@ void* waitInput2(void* data)
ServerNetworkManager::getInstance()->kickAllPlayers();
}
}
uint32_t id = ProtocolManager::getInstance()->requestStart(new StopServer());
while(ProtocolManager::getInstance()->getProtocolState(id) != PROTOCOL_STATE_TERMINATED)
{
}
main_loop->abort();
exit(0);
return NULL;
}
@ -76,7 +76,7 @@ ServerNetworkManager::~ServerNetworkManager()
void ServerNetworkManager::run()
{
if (enet_initialize() != 0)
if (enet_initialize() != 0)
{
Log::error("ServerNetworkManager", "Could not initialize enet.\n");
return;
@ -84,11 +84,11 @@ void ServerNetworkManager::run()
m_localhost = new STKHost();
m_localhost->setupServer(STKHost::HOST_ANY, 7321, 16, 2, 0, 0);
m_localhost->startListening();
// listen keyboard console input
m_thread_keyboard = (pthread_t*)(malloc(sizeof(pthread_t)));
pthread_create(m_thread_keyboard, NULL, waitInput2, NULL);
NetworkManager::run();
}
@ -100,7 +100,7 @@ void ServerNetworkManager::kickAllPlayers()
}
}
void ServerNetworkManager::sendPacket(const NetworkString& data)
void ServerNetworkManager::sendPacket(const NetworkString& data, bool reliable)
{
m_localhost->broadcastPacket(data);
m_localhost->broadcastPacket(data, reliable);
}

View File

@ -30,21 +30,21 @@ class ServerNetworkManager : public NetworkManager
{
return Singleton<NetworkManager>::getInstance<ServerNetworkManager>();
}
virtual void run();
void kickAllPlayers();
virtual void sendPacket(const NetworkString& data);
virtual void sendPacket(const NetworkString& data, bool reliable = true);
virtual bool isServer() { return true; }
protected:
ServerNetworkManager();
virtual ~ServerNetworkManager();
pthread_t* m_thread_keyboard;
};
#endif // SERVER_NETWORK_MANAGER_HPP

View File

@ -26,9 +26,9 @@ class Singleton
{
protected:
Singleton () { m_singleton = NULL; }
virtual ~Singleton ()
{
Log::info("Singleton", "Destroyed singleton.");
virtual ~Singleton ()
{
Log::info("Singleton", "Destroyed singleton.");
}
public:
@ -37,7 +37,7 @@ class Singleton
{
if (m_singleton == NULL)
m_singleton = new S;
S* result = (dynamic_cast<S*> (m_singleton));
if (result == NULL)
Log::debug("Singleton", "THE SINGLETON HAS NOT BEEN REALOCATED, IT IS NOT OF THE REQUESTED TYPE.");

View File

@ -203,10 +203,10 @@ uint8_t* STKHost::receiveRawPacket(TransportAddress sender)
// ----------------------------------------------------------------------------
void STKHost::broadcastPacket(const NetworkString& data)
void STKHost::broadcastPacket(const NetworkString& data, bool reliable)
{
ENetPacket* packet = enet_packet_create(data.c_str(), data.size()+1,
ENET_PACKET_FLAG_RELIABLE);
(reliable ? ENET_PACKET_FLAG_RELIABLE : ENET_PACKET_FLAG_UNSEQUENCED));
enet_host_broadcast(m_host, 0, packet);
}

View File

@ -31,12 +31,12 @@
/*! \class STKHost
* \brief Represents the local host.
* This host is either a server host or a client host. A client host is in
* This host is either a server host or a client host. A client host is in
* charge of connecting to a server. A server opens a socket for incoming
* connections.
* connections.
* By default, this host will use ENet to exchange packets. It also defines an
* interface for ENet use. Nevertheless, this class can be used to send and/or
* receive packets whithout ENet adding its headers.
* receive packets whithout ENet adding its headers.
* This class is used by the Network Manager to send packets.
*/
class STKHost
@ -53,20 +53,20 @@ class STKHost
HOST_BROADCAST = 0xFFFFFFFF, //!< Defines the broadcast address.
PORT_ANY = 0 //!< Any port.
};
/*! \brief Constructor */
STKHost();
/*! \brief Destructor */
virtual ~STKHost();
/*! \brief Thread function checking if data is received.
* This function tries to get data from network low-level functions as
* often as possible. When something is received, it generates an
* often as possible. When something is received, it generates an
* event and passes it to the Network Manager.
* \param self : used to pass the ENet host to the function.
*/
static void* receive_data(void* self);
/*! \brief Setups the host as a server.
* \param address : The IPv4 address of incoming connections.
* \param port : The port on which the server listens.
@ -75,9 +75,9 @@ class STKHost
* \param max_incoming_bandwidth : The maximum incoming bandwidth.
* \param max_outgoing_bandwidth : The maximum outgoing bandwidth.
*/
void setupServer(uint32_t address, uint16_t port,
int peer_count, int channel_limit,
uint32_t max_incoming_bandwidth,
void setupServer(uint32_t address, uint16_t port,
int peer_count, int channel_limit,
uint32_t max_incoming_bandwidth,
uint32_t max_outgoing_bandwidth);
/*! \brief Setups the host as a client.
* In fact there is only one peer connected to this host.
@ -86,10 +86,10 @@ class STKHost
* \param max_incoming_bandwidth : The maximum incoming bandwidth.
* \param max_outgoing_bandwidth : The maximum outgoing bandwidth.
*/
void setupClient(int peer_count, int channel_limit,
uint32_t max_incoming_bandwidth,
void setupClient(int peer_count, int channel_limit,
uint32_t max_incoming_bandwidth,
uint32_t max_outgoing_bandwidth);
/*! \brief Starts the listening of events from ENet.
* Starts a thread that updates it as often as possible.
*/
@ -98,26 +98,26 @@ class STKHost
* Stops the thread that was receiving events.
*/
void stopListening();
/*! \brief Sends a packet whithout ENet adding its headers.
* This function is used in particular to achieve the STUN protocol.
* \param data : Data to send.
* \param length : Length of the sent data.
* \param dst : Destination of the packet.
*/
void sendRawPacket(uint8_t* data, int length,
void sendRawPacket(uint8_t* data, int length,
TransportAddress dst);
/*! \brief Receives a packet directly from the network interface.
* Receive a packet whithout ENet processing it.
* \return A string containing the data of the received packet.
*/
uint8_t* receiveRawPacket();
/*! \brief Receives a packet directly from the network interface and
/*! \brief Receives a packet directly from the network interface and
* filter its address.
* Receive a packet whithout ENet processing it. Checks that the
* sender of the packet is the one that corresponds to the sender
* Receive a packet whithout ENet processing it. Checks that the
* sender of the packet is the one that corresponds to the sender
* parameter. Does not check the port right now.
* \param sender : Transport address of the original sender of the
* \param sender : Transport address of the original sender of the
* wanted packet.
* \return A string containing the data of the received packet
* matching the sender's ip address.
@ -126,8 +126,8 @@ class STKHost
/*! \brief Broadcasts a packet to all peers.
* \param data : Data to send.
*/
void broadcastPacket(const NetworkString& data);
void broadcastPacket(const NetworkString& data, bool reliable = true);
/*! \brief Tells if a peer is known.
* \return True if the peer is known, false elseway.
*/
@ -139,7 +139,7 @@ class STKHost
protected:
ENetHost* m_host; //!< ENet host interfacing sockets.
pthread_t* m_listening_thread; //!< Thread listening network events.
};
#endif // STK_HOST_HPP

View File

@ -89,14 +89,14 @@ void STKPeer::disconnect()
//-----------------------------------------------------------------------------
void STKPeer::sendPacket(NetworkString const& data)
void STKPeer::sendPacket(NetworkString const& data, bool reliable)
{
Log::verbose("STKPeer", "sending packet of size %d to %i.%i.%i.%i:%i",
data.size(), (m_peer->address.host>>0)&0xff,
(m_peer->address.host>>8)&0xff,(m_peer->address.host>>16)&0xff,
(m_peer->address.host>>24)&0xff,m_peer->address.port);
ENetPacket* packet = enet_packet_create(data.c_str(), data.size()+1,
ENET_PACKET_FLAG_RELIABLE);
(reliable ? ENET_PACKET_FLAG_RELIABLE : ENET_PACKET_FLAG_UNSEQUENCED));
enet_peer_send(m_peer, 0, packet);
}

View File

@ -32,10 +32,10 @@ class STKPeer
STKPeer(const STKPeer& peer);
virtual ~STKPeer();
virtual void sendPacket(const NetworkString& data);
virtual void sendPacket(const NetworkString& data, bool reliable = true);
static bool connectToHost(STKHost* localhost, TransportAddress host, uint32_t channel_count, uint32_t data);
void disconnect();
void setClientServerToken(const uint32_t& token) { *m_client_server_token = token; *m_token_set = true; }
void unsetClientServerToken() { *m_token_set = false; }
void setPlayerProfile(NetworkPlayerProfile* profile) { *m_player_profile = profile; }