From c1534075d3f45a11f9a123622fa02051c891d4bd Mon Sep 17 00:00:00 2001 From: hilnius Date: Sat, 13 Jul 2013 23:12:53 +0000 Subject: [PATCH] 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 --- doc/protocols.xls | Bin 10752 -> 11776 bytes sources.cmake | 8 +++++ src/main.cpp | 10 +++--- src/main_loop.cpp | 8 +++-- src/network/client_network_manager.cpp | 10 ++++-- src/network/client_network_manager.hpp | 10 +++--- src/network/network_manager.cpp | 8 ++--- src/network/network_manager.hpp | 23 ++++++------- src/network/protocol.hpp | 2 ++ src/network/protocol_manager.cpp | 12 +++---- src/network/protocol_manager.hpp | 6 ++-- src/network/server_network_manager.cpp | 16 ++++----- src/network/server_network_manager.hpp | 16 ++++----- src/network/singleton.hpp | 8 ++--- src/network/stk_host.cpp | 4 +-- src/network/stk_host.hpp | 44 ++++++++++++------------- src/network/stk_peer.cpp | 4 +-- src/network/stk_peer.hpp | 4 +-- 18 files changed, 107 insertions(+), 86 deletions(-) diff --git a/doc/protocols.xls b/doc/protocols.xls index cbdf99308e8eb1ed36ca20cee93e3aa2bb87a2d9..a426d9ea251d52fc7dad55538d1af06c49813c48 100644 GIT binary patch delta 1283 zcmZuuO;giQ6un75=ybqBAu=ju9+oL$TPbc81;6lP1Ii9vg!D-x=}UP@VCp!A(VcFb z>CbQlaj7%9ap}q*;l@9pqxIf2El|nKo4kAOdH39N@7~(p<9C&zF^IllfRX)$-%2in z00!{4-EPwvjqV!#_gCL52k|Q@_4`n3Ver+!kK{9i9|T_qA13?}fJd$_-DSPbSETLQ zhHWJP*6b}V;s9>5!I83J=}j)n4wpO3r4$NMirG(DojZ=c$wC&fhnd2_9vog}8%<^l&bV&Wm}}N~3F*RRo{-$POnt*@mKfjRvdQKv z6;?Mb%XGM5i>h;Jo62LcMl>2K>`_hEM&r8UNzPoo;U6L_7P9j_Hcd%`R8(_1WM8MqNBL85Vn&P3QA~G{@tCu6ajzK>SMeI_8+r4B0qdQSVgYUYWgi=rNsMpRt z5jH_fIrLc$WtXE`<y6F4@pSaUEr^Gvqo zX<`p&4FpQtY(B;l#LD`gfeXl7CmhSjBq_R?SCo&DQEs!0*jtXt8x;>IgUojT5l|q@ zz|X)e!oUvXGBSt&Av^o#DM~qvlMg6aZc-^^W;C2!q^`~0zs)7N?i}M}MHZRO2h_JP TF$zpB)oNy{lGyx9i;WooH=HvG diff --git a/sources.cmake b/sources.cmake index 3d63266e8..64ff8b3ad 100644 --- a/sources.cmake +++ b/sources.cmake @@ -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 diff --git a/src/main.cpp b/src/main.cpp index b562193aa..8393a46af 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -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(); + NetworkManager::getInstance(); NetworkManager::getInstance()->run(); if (NetworkManager::getInstance()->isServer()) { diff --git a/src/main_loop.cpp b/src/main_loop.cpp index c025bb55a..0e1b0409c 100644 --- a/src/main_loop.cpp +++ b/src/main_loop.cpp @@ -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()->isRunning()) + NetworkWorld::getInstance()->update(dt); + else + World::getWorld()->updateWorld(dt); } // updateRace //----------------------------------------------------------------------------- diff --git a/src/network/client_network_manager.cpp b/src/network/client_network_manager.cpp index ba1dbb39c..81f33ddce 100644 --- a/src/network/client_network_manager.cpp +++ b/src/network/client_network_manager.cpp @@ -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(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() diff --git a/src/network/client_network_manager.hpp b/src/network/client_network_manager.hpp index 8c162a367..1c62d4eb9 100644 --- a/src/network/client_network_manager.hpp +++ b/src/network/client_network_manager.hpp @@ -29,19 +29,19 @@ class ClientNetworkManager : public NetworkManager { return Singleton::getInstance(); } - + 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; }; diff --git a/src/network/network_manager.cpp b/src/network/network_manager.cpp index 9b22b5710..dba65b39c 100644 --- a/src/network/network_manager.cpp +++ b/src/network/network_manager.cpp @@ -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); } } } diff --git a/src/network/network_manager.hpp b/src/network/network_manager.hpp index cc13ae665..2869be6fc 100644 --- a/src/network/network_manager.hpp +++ b/src/network/network_manager.hpp @@ -34,17 +34,17 @@ class NetworkManager : public Singleton { friend class Singleton; 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 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 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 m_peers; STKHost* m_localhost; bool m_playing_online; GameSetup* m_game_setup; - + TransportAddress m_public_address; PlayerLogin m_player_login; }; diff --git a/src/network/protocol.hpp b/src/network/protocol.hpp index 2c0d0298e..5015e5089 100644 --- a/src/network/protocol.hpp +++ b/src/network/protocol.hpp @@ -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,//!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) diff --git a/src/network/protocol_manager.hpp b/src/network/protocol_manager.hpp index 1345ebb4c..7f97b82ff 100644 --- a/src/network/protocol_manager.hpp +++ b/src/network/protocol_manager.hpp @@ -104,15 +104,15 @@ class ProtocolManager : public Singleton /*! * \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. diff --git a/src/network/server_network_manager.cpp b/src/network/server_network_manager.cpp index 774bb84ad..9b1fe8b52 100644 --- a/src/network/server_network_manager.cpp +++ b/src/network/server_network_manager.cpp @@ -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); } diff --git a/src/network/server_network_manager.hpp b/src/network/server_network_manager.hpp index aadc8c7e5..2c2dc4990 100644 --- a/src/network/server_network_manager.hpp +++ b/src/network/server_network_manager.hpp @@ -30,21 +30,21 @@ class ServerNetworkManager : public NetworkManager { return Singleton::getInstance(); } - + 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 diff --git a/src/network/singleton.hpp b/src/network/singleton.hpp index 65cf27dd4..616b0fac6 100644 --- a/src/network/singleton.hpp +++ b/src/network/singleton.hpp @@ -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 (m_singleton)); if (result == NULL) Log::debug("Singleton", "THE SINGLETON HAS NOT BEEN REALOCATED, IT IS NOT OF THE REQUESTED TYPE."); diff --git a/src/network/stk_host.cpp b/src/network/stk_host.cpp index 50c39b375..6012881eb 100644 --- a/src/network/stk_host.cpp +++ b/src/network/stk_host.cpp @@ -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); } diff --git a/src/network/stk_host.hpp b/src/network/stk_host.hpp index bd2e1ee23..5cf3eac7f 100644 --- a/src/network/stk_host.hpp +++ b/src/network/stk_host.hpp @@ -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 diff --git a/src/network/stk_peer.cpp b/src/network/stk_peer.cpp index 4aa26e1dd..76c0d4f0f 100644 --- a/src/network/stk_peer.cpp +++ b/src/network/stk_peer.cpp @@ -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); } diff --git a/src/network/stk_peer.hpp b/src/network/stk_peer.hpp index d91f291cb..10caa0273 100644 --- a/src/network/stk_peer.hpp +++ b/src/network/stk_peer.hpp @@ -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; }