1
0
Fork 0

make the mp protocol send rate independant from time accel,

based on mp protocol clock.
This commit is contained in:
jano 2020-12-12 00:06:12 +01:00 committed by James Turner
parent a28bf28ee0
commit 4a4117b9ab
2 changed files with 22 additions and 31 deletions

View file

@ -36,6 +36,7 @@
#include <memory>
#include <simgear/debug/logstream.hxx>
#include <simgear/math/sg_random.h>
#include <simgear/misc/sg_dir.hxx>
#include <simgear/misc/stdint.hxx>
#include <simgear/misc/strutils.hxx>
@ -1106,13 +1107,14 @@ FGMultiplayMgr::init (void)
short txPort = fgGetInt("/sim/multiplay/txport", 5000);
string txAddress = fgGetString("/sim/multiplay/txhost");
int hz = fgGetInt("/sim/multiplay/tx-rate-hz", 10);
if (hz < 1) {
hz = 10;
int txRateHz = fgGetInt("/sim/multiplay/tx-rate-hz", 10);
if (txRateHz < 1) {
txRateHz = 10;
}
mDt = 1.0 / hz;
mTimeUntilSend = 0.0;
mDt = 1.0 / txRateHz;
mNextTransmitTime = sg_random() * mDt;
SG_LOG(SG_NETWORK, SG_DEBUG, "initial MP time: " << mNextTransmitTime);
mCallsign = fgGetString("/sim/multiplay/callsign");
fgGetNode("/sim/multiplay/callsign", true)->setAttribute(SGPropertyNode::PRESERVE, true);
@ -1960,9 +1962,13 @@ FGMultiplayMgr::update(double dt)
//////////////////////////////////////////////////
// Send if required
//////////////////////////////////////////////////
mTimeUntilSend -= dt;
if (mTimeUntilSend <= 0.0) {
Send();
//the mp protocol time is immune to pause, warp and time accel
const double mpTime = globals->get_subsystem<TimeManager>()->getMPProtocolClockSec();
// the mpTime is not monotonic (adjustable offset), going back in time will
// also trigger a send
if ((mpTime >= mNextTransmitTime) || (mpTime < (mNextTransmitTime - 2.0 * mDt))) {
Send(mpTime);
}
//////////////////////////////////////////////////
@ -2059,30 +2065,15 @@ void FGMultiplayMgr::ClearMotion()
}
}
void
FGMultiplayMgr::Send()
void FGMultiplayMgr::Send(double mpTime)
{
using namespace simgear;
findProperties();
// smooth the send rate, by adjusting based on the 'remainder' time, which
// is how -ve mTimeUntilSend is. Watch for large values and ignore them,
// however.
if ((mTimeUntilSend < 0.0) && (fabs(mTimeUntilSend) < mDt)) {
mTimeUntilSend = mDt + mTimeUntilSend;
}
else {
mTimeUntilSend = mDt;
}
// we are now using a time immune to pause, warp etc as timestamp.
double mp_time = globals->get_subsystem<TimeManager>()->getMPProtocolClockSec();
// static double lastTime = 0.0;
// SG_LOG(SG_GENERAL, SG_INFO, "actual mp dt=" << mp_time - lastTime);
// lastTime = mp_time;
// Try to stick to a grid with the sending rate as step,
// based on the MP protocol clock.
mNextTransmitTime += floor(1.0 + (mpTime - mNextTransmitTime) / mDt) * mDt;
FlightProperties ifce;
@ -2094,7 +2085,7 @@ FGMultiplayMgr::Send()
// note that the simulation time is updated before calling all the
// update methods. Thus it contains the time intervals *end* time.
// The FDM is already run, so the states belong to that time.
motionInfo.time = mp_time;
motionInfo.time = mpTime;
motionInfo.lag = mDt;
// These are for now converted from lat/lon/alt and euler angles.

View file

@ -108,7 +108,7 @@ private:
void findProperties();
void Send();
void Send(double currentMPTime);
void SendMyPosition(const FGExternalMotionData& motionInfo);
short get_scaled_short(double v, double scale);
@ -153,8 +153,8 @@ private:
MPPropertyListener* mListener;
double mDt; // reciprocal of /sim/multiplay/tx-rate-hz
double mTimeUntilSend;
double mNextTransmitTime = 0.0;
std::deque<std::shared_ptr<std::vector<char>>> mRecordMessageQueue;
std::deque<std::shared_ptr<std::vector<char>>> mReplayMessageQueue;
};