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/ai_properties.cpp
src/karts/controller/controller.cpp src/karts/controller/controller.cpp
src/karts/controller/end_controller.cpp src/karts/controller/end_controller.cpp
src/karts/controller/network_player_controller.cpp
src/karts/controller/player_controller.cpp src/karts/controller/player_controller.cpp
src/karts/controller/skidding_ai.cpp src/karts/controller/skidding_ai.cpp
src/karts/explosion_animation.cpp src/karts/explosion_animation.cpp
@ -142,6 +143,7 @@ src/network/http_functions.cpp
src/network/network_interface.cpp src/network/network_interface.cpp
src/network/network_manager.cpp src/network/network_manager.cpp
src/network/network_string.cpp src/network/network_string.cpp
src/network/network_world.cpp
src/network/protocol.cpp src/network/protocol.cpp
src/network/protocol_manager.cpp src/network/protocol_manager.cpp
src/network/protocols/connect_to_peer.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/quick_join_protocol.cpp
src/network/protocols/request_connection.cpp src/network/protocols/request_connection.cpp
src/network/protocols/show_public_address.cpp src/network/protocols/show_public_address.cpp
src/network/protocols/start_game_protocol.cpp
src/network/protocols/start_server.cpp src/network/protocols/start_server.cpp
src/network/protocols/stop_server.cpp src/network/protocols/stop_server.cpp
src/network/protocols/synchronization_protocol.cpp
src/network/server_network_manager.cpp src/network/server_network_manager.cpp
src/network/stk_host.cpp src/network/stk_host.cpp
src/network/stk_peer.cpp src/network/stk_peer.cpp
@ -378,6 +382,7 @@ src/karts/controller/ai_properties.hpp
src/karts/controller/controller.hpp src/karts/controller/controller.hpp
src/karts/controller/end_controller.hpp src/karts/controller/end_controller.hpp
src/karts/controller/kart_control.hpp src/karts/controller/kart_control.hpp
src/karts/controller/network_player_controller.hpp
src/karts/controller/player_controller.hpp src/karts/controller/player_controller.hpp
src/karts/controller/skidding_ai.hpp src/karts/controller/skidding_ai.hpp
src/karts/explosion_animation.hpp src/karts/explosion_animation.hpp
@ -415,6 +420,7 @@ src/network/http_functions.hpp
src/network/network_interface.hpp src/network/network_interface.hpp
src/network/network_manager.hpp src/network/network_manager.hpp
src/network/network_string.hpp src/network/network_string.hpp
src/network/network_world.hpp
src/network/protocol.hpp src/network/protocol.hpp
src/network/protocol_manager.hpp src/network/protocol_manager.hpp
src/network/protocols/connect_to_peer.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/quick_join_protocol.hpp
src/network/protocols/request_connection.hpp src/network/protocols/request_connection.hpp
src/network/protocols/show_public_address.hpp src/network/protocols/show_public_address.hpp
src/network/protocols/start_game_protocol.hpp
src/network/protocols/start_server.hpp src/network/protocols/start_server.hpp
src/network/protocols/stop_server.hpp src/network/protocols/stop_server.hpp
src/network/protocols/synchronization_protocol.hpp
src/network/remote_kart_info.hpp src/network/remote_kart_info.hpp
src/network/server_network_manager.hpp src/network/server_network_manager.hpp
src/network/singleton.hpp src/network/singleton.hpp

View File

@ -29,6 +29,7 @@
#include "input/wiimote_manager.hpp" #include "input/wiimote_manager.hpp"
#include "modes/profile_world.hpp" #include "modes/profile_world.hpp"
#include "modes/world.hpp" #include "modes/world.hpp"
#include "network/network_world.hpp"
#include "race/race_manager.hpp" #include "race/race_manager.hpp"
#include "states_screens/state_manager.hpp" #include "states_screens/state_manager.hpp"
#include "utils/profiler.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 // 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 max_fps = (StateManager::get()->throttleFPS() ? 35 : UserConfigParams::m_max_fps);
const int current_fps = (int)(1000.0f/dt); 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; int wait_time = 1000/max_fps - 1000/current_fps;
if(wait_time < 1) wait_time = 1; if(wait_time < 1) wait_time = 1;
@ -95,7 +96,10 @@ void MainLoop::updateRace(float dt)
{ {
if(ProfileWorld::isProfileMode()) dt=1.0f/60.0f; 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 } // updateRace
//----------------------------------------------------------------------------- //-----------------------------------------------------------------------------

View File

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

View File

@ -31,7 +31,7 @@ class ClientNetworkManager : public NetworkManager
} }
virtual void run(); virtual void run();
virtual void sendPacket(const NetworkString& data); virtual void sendPacket(const NetworkString& data, bool reliable = true);
STKPeer* getPeer(); STKPeer* getPeer();
virtual bool isServer() { return false; } virtual bool isServer() { return false; }

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) 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++) for (unsigned int i = 0; i < m_peers.size(); i++)
{ {
if (!m_peers[i]->isSamePeer(peer)) if (!m_peers[i]->isSamePeer(peer))
{ {
m_peers[i]->sendPacket(data); m_peers[i]->sendPacket(data, reliable);
} }
} }
} }

View File

@ -42,9 +42,9 @@ class NetworkManager : public Singleton<NetworkManager>
// message/packets related functions // message/packets related functions
virtual void notifyEvent(Event* event); virtual void notifyEvent(Event* event);
virtual void sendPacket(const NetworkString& data) = 0; virtual void sendPacket(const NetworkString& data, bool reliable = true) = 0;
virtual void sendPacket(STKPeer* peer, const NetworkString& data); virtual void sendPacket(STKPeer* peer, const NetworkString& data, bool reliable = true);
virtual void sendPacketExcept(STKPeer* peer, const NetworkString& data); virtual void sendPacketExcept(STKPeer* peer, const NetworkString& data, bool reliable = true);
// Game related functions // Game related functions
virtual GameSetup* setupNewGame(); //!< Creates a new game setup and returns it virtual GameSetup* setupNewGame(); //!< Creates a new game setup and returns it
@ -63,6 +63,7 @@ class NetworkManager : public Singleton<NetworkManager>
bool isPlayingOnline() { return m_playing_online; } bool isPlayingOnline() { return m_playing_online; }
STKHost* getHost() { return m_localhost; } STKHost* getHost() { return m_localhost; }
std::vector<STKPeer*> getPeers() { return m_peers; } std::vector<STKPeer*> getPeers() { return m_peers; }
unsigned int getPeerCount() { return m_peers.size(); }
TransportAddress getPublicAddress() { return m_public_address; } TransportAddress getPublicAddress() { return m_public_address; }
GameSetup* getGameSetup() { return m_game_setup; } GameSetup* getGameSetup() { return m_game_setup; }

View File

@ -34,6 +34,8 @@ enum PROTOCOL_TYPE
PROTOCOL_NONE = 0, //!< No protocol type assigned. PROTOCOL_NONE = 0, //!< No protocol type assigned.
PROTOCOL_CONNECTION = 1, //!< Protocol that deals with client-server connection. PROTOCOL_CONNECTION = 1, //!< Protocol that deals with client-server connection.
PROTOCOL_LOBBY_ROOM = 2, //!< Protocol that is used during the lobby room phase. 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. 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); 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; NetworkString newMessage;
newMessage.ai8(sender->getProtocolType()); // add one byte to add protocol type newMessage.ai8(sender->getProtocolType()); // add one byte to add protocol type
newMessage += message; 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; NetworkString newMessage;
newMessage.ai8(sender->getProtocolType()); // add one byte to add protocol type newMessage.ai8(sender->getProtocolType()); // add one byte to add protocol type
newMessage += message; 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; NetworkString newMessage;
newMessage.ai8(sender->getProtocolType()); // add one byte to add protocol type newMessage.ai8(sender->getProtocolType()); // add one byte to add protocol type
newMessage += message; newMessage += message;
NetworkManager::getInstance()->sendPacketExcept(peer, newMessage); NetworkManager::getInstance()->sendPacketExcept(peer, newMessage, reliable);
} }
uint32_t ProtocolManager::requestStart(Protocol* protocol) uint32_t ProtocolManager::requestStart(Protocol* protocol)

View File

@ -104,15 +104,15 @@ class ProtocolManager : public Singleton<ProtocolManager>
/*! /*!
* \brief WILL BE COMMENTED LATER * \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 * \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 * \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. * \brief Asks the manager to start a protocol.

View File

@ -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

@ -35,7 +35,7 @@ class ServerNetworkManager : public NetworkManager
void kickAllPlayers(); void kickAllPlayers();
virtual void sendPacket(const NetworkString& data); virtual void sendPacket(const NetworkString& data, bool reliable = true);
virtual bool isServer() { return true; } virtual bool isServer() { return true; }

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, 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); enet_host_broadcast(m_host, 0, packet);
} }

View File

@ -126,7 +126,7 @@ class STKHost
/*! \brief Broadcasts a packet to all peers. /*! \brief Broadcasts a packet to all peers.
* \param data : Data to send. * \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. /*! \brief Tells if a peer is known.
* \return True if the peer is known, false elseway. * \return True if the peer is known, false elseway.

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", Log::verbose("STKPeer", "sending packet of size %d to %i.%i.%i.%i:%i",
data.size(), (m_peer->address.host>>0)&0xff, data.size(), (m_peer->address.host>>0)&0xff,
(m_peer->address.host>>8)&0xff,(m_peer->address.host>>16)&0xff, (m_peer->address.host>>8)&0xff,(m_peer->address.host>>16)&0xff,
(m_peer->address.host>>24)&0xff,m_peer->address.port); (m_peer->address.host>>24)&0xff,m_peer->address.port);
ENetPacket* packet = enet_packet_create(data.c_str(), data.size()+1, 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); enet_peer_send(m_peer, 0, packet);
} }

View File

@ -32,7 +32,7 @@ class STKPeer
STKPeer(const STKPeer& peer); STKPeer(const STKPeer& peer);
virtual ~STKPeer(); 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); static bool connectToHost(STKHost* localhost, TransportAddress host, uint32_t channel_count, uint32_t data);
void disconnect(); void disconnect();