From d9cebc3991cd6d42a3e8cc7bef443122c09cab91 Mon Sep 17 00:00:00 2001 From: Benau Date: Fri, 27 Jul 2018 21:47:07 +0800 Subject: [PATCH] Try unreliable state in network --- src/network/protocols/game_protocol.cpp | 39 ++++++++++--------------- src/network/protocols/game_protocol.hpp | 3 +- src/network/rewind_manager.cpp | 2 +- src/network/stk_peer.cpp | 4 ++- 4 files changed, 20 insertions(+), 28 deletions(-) diff --git a/src/network/protocols/game_protocol.cpp b/src/network/protocols/game_protocol.cpp index 8eabd1ddd..2081b6db4 100644 --- a/src/network/protocols/game_protocol.cpp +++ b/src/network/protocols/game_protocol.cpp @@ -341,33 +341,25 @@ void GameProtocol::addState(BareNetworkString *buffer) // ---------------------------------------------------------------------------- /** Called by a server to finalize the current state, which add updated * names of rewinder using to the beginning of state buffer - * \param prev_rewinder List of previous rewinder using. * \param cur_rewinder List of current rewinder using. */ -void GameProtocol::finalizeState(std::vector& prev_rewinder, - std::vector& cur_rewinder) +void GameProtocol::finalizeState(std::vector& cur_rewinder) { assert(NetworkConfig::get()->isServer()); auto& buffer = m_data_to_send->getBuffer(); auto pos = buffer.begin() + 1/*protocol type*/ + 1 /*gp event type*/+ 4/*time*/; - if (prev_rewinder != cur_rewinder) + m_data_to_send->reset(); + std::vector names; + names.push_back((uint8_t)cur_rewinder.size()); + for (std::string& name : cur_rewinder) { - m_data_to_send->reset(); - std::vector names; - names.push_back((uint8_t)cur_rewinder.size()); - for (std::string& name : cur_rewinder) - { - names.push_back((uint8_t)name.size()); - std::vector rewinder(name.begin(), name.end()); - names.insert(names.end(), rewinder.begin(), rewinder.end()); - } - buffer.insert(pos, names.begin(), names.end()); - std::swap(prev_rewinder, cur_rewinder); + names.push_back((uint8_t)name.size()); + std::vector rewinder(name.begin(), name.end()); + names.insert(names.end(), rewinder.begin(), rewinder.end()); } - else - buffer.insert(pos, 0); + buffer.insert(pos, names.begin(), names.end()); } // finalizeState // ---------------------------------------------------------------------------- @@ -377,7 +369,7 @@ void GameProtocol::finalizeState(std::vector& prev_rewinder, void GameProtocol::sendState() { assert(NetworkConfig::get()->isServer()); - sendMessageToPeers(m_data_to_send, /*reliable*/true); + sendMessageToPeers(m_data_to_send, /*reliable*/false); } // sendState // ---------------------------------------------------------------------------- @@ -394,16 +386,15 @@ void GameProtocol::handleState(Event *event) int ticks = data.getUInt32(); // Check for updated rewinder using - unsigned new_rewinder_size = data.getUInt8(); - std::vector new_rewinder_using; - for (unsigned i = 0; i < new_rewinder_size; i++) + unsigned rewinder_size = data.getUInt8(); + std::vector rewinder_using; + for (unsigned i = 0; i < rewinder_size; i++) { std::string name; data.decodeString(&name); - new_rewinder_using.push_back(name); + rewinder_using.push_back(name); } - if (!new_rewinder_using.empty()) - RewindManager::get()->setRewinderUsing(new_rewinder_using); + RewindManager::get()->setRewinderUsing(rewinder_using); // Now copy the state data (without ticks etc) to a new // string, so it can be reset to the beginning easily diff --git a/src/network/protocols/game_protocol.hpp b/src/network/protocols/game_protocol.hpp index 1d2ca11e1..a4b2fbd4b 100644 --- a/src/network/protocols/game_protocol.hpp +++ b/src/network/protocols/game_protocol.hpp @@ -88,8 +88,7 @@ public: void startNewState(); void addState(BareNetworkString *buffer); void sendState(); - void finalizeState(std::vector& prev_rewinder, - std::vector& cur_rewinder); + void finalizeState(std::vector& cur_rewinder); void adjustTimeForClient(STKPeer *peer, int ticks); void sendItemEventConfirmation(int ticks); diff --git a/src/network/rewind_manager.cpp b/src/network/rewind_manager.cpp index c881f99b9..b554e0cfb 100644 --- a/src/network/rewind_manager.cpp +++ b/src/network/rewind_manager.cpp @@ -165,7 +165,7 @@ void RewindManager::saveState() } delete buffer; // buffer can be freed } - gp->finalizeState(m_current_rewinder_using, rewinder_using); + gp->finalizeState(rewinder_using); PROFILER_POP_CPU_MARKER(); } // saveState diff --git a/src/network/stk_peer.cpp b/src/network/stk_peer.cpp index db6b4c5f1..8cf9c4967 100644 --- a/src/network/stk_peer.cpp +++ b/src/network/stk_peer.cpp @@ -102,7 +102,9 @@ void STKPeer::sendPacket(NetworkString *data, bool reliable, bool encrypted) { packet = enet_packet_create(data->getData(), data->getTotalSize(), (reliable ? - ENET_PACKET_FLAG_RELIABLE : ENET_PACKET_FLAG_UNSEQUENCED)); + ENET_PACKET_FLAG_RELIABLE : + (ENET_PACKET_FLAG_UNSEQUENCED | + ENET_PACKET_FLAG_UNRELIABLE_FRAGMENT))); } if (packet)