Optimize network state saving

This commit is contained in:
Benau
2018-07-30 16:26:28 +08:00
parent a8434624a1
commit f49a092603
4 changed files with 39 additions and 29 deletions

View File

@@ -382,7 +382,7 @@ void GameProtocol::handleState(Event *event)
return;
assert(NetworkConfig::get()->isClient());
const NetworkString &data = event->data();
NetworkString &data = event->data();
int ticks = data.getUInt32();
// Check for updated rewinder using
@@ -394,16 +394,11 @@ void GameProtocol::handleState(Event *event)
data.decodeString(&name);
rewinder_using.push_back(name);
}
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
// when restoring the state:
BareNetworkString *bns = new BareNetworkString(data.getCurrentData(),
data.size());
// The memory for bns will be handled in the RewindInfoState object
RewindManager::get()->addNetworkState(bns, ticks);
RewindInfoState* ris = new RewindInfoState(ticks, data.getCurrentOffset(),
rewinder_using, data.getBuffer());
RewindManager::get()->addRewindInfo(ris);
} // handleState
// ----------------------------------------------------------------------------

View File

@@ -46,11 +46,25 @@ void RewindInfo::setTicks(int ticks)
} // setTicks
// ============================================================================
RewindInfoState::RewindInfoState(int ticks, BareNetworkString *buffer,
bool is_confirmed)
: RewindInfo(ticks, is_confirmed)
RewindInfoState::RewindInfoState(int ticks, int start_offset,
std::vector<std::string>& rewinder_using,
std::vector<uint8_t>& buffer)
: RewindInfo(ticks, true/*is_confirmed*/)
{
m_rewinder_using = RewindManager::get()->getRewinderUsing();
std::swap(m_rewinder_using, rewinder_using);
m_start_offset = start_offset;
m_buffer = new BareNetworkString();
std::swap(m_buffer->getBuffer(), buffer);
} // RewindInfoState
// ------------------------------------------------------------------------
/** Constructor used only in unit testing (without list of rewinder using).
*/
RewindInfoState::RewindInfoState(int ticks, BareNetworkString* buffer,
bool is_confirmed)
: RewindInfo(ticks, is_confirmed)
{
m_start_offset = 0;
m_buffer = buffer;
} // RewindInfoState
@@ -62,6 +76,7 @@ RewindInfoState::RewindInfoState(int ticks, BareNetworkString *buffer,
void RewindInfoState::restore()
{
m_buffer->reset();
m_buffer->skip(m_start_offset);
for (const std::string& name : m_rewinder_using)
{
const uint16_t data_size = m_buffer->getUInt16();

View File

@@ -27,6 +27,7 @@
#include <assert.h>
#include <functional>
#include <string>
#include <vector>
/** Used to store rewind information for a given time for all rewind
@@ -93,14 +94,22 @@ class RewindInfoState: public RewindInfo
private:
std::vector<std::string> m_rewinder_using;
int m_start_offset;
/** Pointer to the buffer which stores all states. */
BareNetworkString *m_buffer;
public:
RewindInfoState(int ticks, BareNetworkString *buffer,
bool is_confirmed);
virtual ~RewindInfoState() { delete m_buffer; };
virtual void restore();
public:
// ------------------------------------------------------------------------
RewindInfoState(int ticks, int start_offset,
std::vector<std::string>& rewinder_using,
std::vector<uint8_t>& buffer);
// ------------------------------------------------------------------------
RewindInfoState(int ticks, BareNetworkString *buffer, bool is_confirmed);
// ------------------------------------------------------------------------
virtual ~RewindInfoState() { delete m_buffer; }
// ------------------------------------------------------------------------
virtual void restore();
// ------------------------------------------------------------------------
/** Returns a pointer to the state buffer. */
BareNetworkString *getBuffer() const { return m_buffer; }

View File

@@ -92,8 +92,6 @@ private:
* rewind data in case of local races only. */
static bool m_enable_rewind_manager;
std::vector<std::string> m_current_rewinder_using;
std::map<int, std::vector<std::function<void()> > > m_local_state;
/** A list of all objects that can be rewound. */
@@ -200,19 +198,12 @@ public:
return m_rewind_queue.getLatestConfirmedState();
} // getLatestConfirmedState
// ------------------------------------------------------------------------
/* Used by client to update rewinder using. */
void setRewinderUsing(std::vector<std::string>& ru)
{ m_current_rewinder_using = std::move(ru); }
// ------------------------------------------------------------------------
/* Used by client to get a copied list of rewinder using to be used
in different thread safely. */
const std::vector<std::string>& getRewinderUsing() const
{ return m_current_rewinder_using; }
// ------------------------------------------------------------------------
bool useLocalEvent() const;
// ------------------------------------------------------------------------
void addRewindInfoEventFunction(RewindInfoEventFunction* rief)
{ m_pending_rief.push_back(rief); }
// ------------------------------------------------------------------------
void addRewindInfo(RewindInfo* ri) { m_rewind_queue.insertRewindInfo(ri); }
}; // RewindManager