Skip to content

Commit

Permalink
Fixes to netw err handling, setting min netw timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
TwinFan committed Nov 4, 2023
1 parent 6ea6324 commit a0c87e1
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 23 deletions.
13 changes: 8 additions & 5 deletions Include/DataRefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ const bool DEF_SND_FMOD_INST = false; ///< Enforce using our own FMOD
const bool DEF_SND_FMOD_INST = true; ///< Enforce using our own FMOD instance instead of X-Plane's?
#endif
const int DEF_SUI_TRANSP = 0; ///< Settings UI: transaprent background?
const int MIN_NETW_TIMEOUT = 5; ///< [s] minimum network request timeout
const int DEF_NETW_TIMEOUT = 90; ///< [s] of network request timeout
const int DEF_MIN_NETW_TIMEOUT = 5; ///< [s] default minimum network request timeout
const int DEF_MAX_NETW_TIMEOUT = 5; ///< [s] default maximum network request timeout


constexpr int DEF_UI_FONT_SCALE = 100; ///< [%] Default font scaling
Expand Down Expand Up @@ -382,7 +382,8 @@ enum dataRefsLT {
DR_CFG_FD_LONG_REFRESH_INTVL,
DR_CFG_FD_BUF_PERIOD,
DR_CFG_FD_REDUCE_HEIGHT,
DR_CFG_NETW_TIMEOUT,
DR_CFG_MIN_NETW_TIMEOUT,
DR_CFG_MAX_NETW_TIMEOUT,
DR_CFG_LND_LIGHTS_TAXI,
DR_CFG_HIDE_BELOW_AGL,
DR_CFG_HIDE_TAXIING,
Expand Down Expand Up @@ -685,7 +686,8 @@ class DataRefs
int fdCurrRefrIntvl = DEF_FD_REFRESH_INTVL; ///< current value of how often to fetch new flight data
int fdBufPeriod = DEF_FD_BUF_PERIOD; ///< seconds to buffer before simulating aircraft
int fdReduceHeight = DEF_FD_REDUCE_HEIGHT; ///< [ft] reduce flight data usage when user aircraft is flying above this altitude
int netwTimeout = DEF_NETW_TIMEOUT; ///< [s] of network request timeout
int netwTimeoutMin = DEF_MIN_NETW_TIMEOUT; ///< [s] of min network request timeout
int netwTimeoutMax = DEF_MAX_NETW_TIMEOUT; ///< [s] of max network request timeout
int bLndLightsTaxi = false; // keep landing lights on while taxiing? (to be able to see the a/c as there is no taxi light functionality)
int hideBelowAGL = 0; // if positive: a/c visible only above this height AGL
int hideTaxiing = 0; // hide a/c while taxiing?
Expand Down Expand Up @@ -922,7 +924,8 @@ class DataRefs
inline int GetFdRefreshIntvl() const { return fdCurrRefrIntvl; }
inline int GetFdBufPeriod() const { return fdBufPeriod; }
inline int GetAcOutdatedIntvl() const { return 2 * GetFdBufPeriod(); }
inline int GetNetwTimeout() const { return netwTimeout; }
inline int GetNetwTimeoutMin() const { return netwTimeoutMin; }
inline int GetNetwTimeoutMax() const { return netwTimeoutMax; }
inline bool GetLndLightsTaxi() const { return bLndLightsTaxi != 0; }
inline int GetHideBelowAGL() const { return hideBelowAGL; }
inline bool GetHideTaxiing() const { return hideTaxiing != 0; }
Expand Down
14 changes: 10 additions & 4 deletions Src/DataRefs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -526,6 +526,7 @@ DataRefs::dataRefDefinitionT DATA_REFS_LT[CNT_DATAREFS_LT] = {
{"livetraffic/cfg/fd_long_refresh_intvl", DataRefs::LTGetInt, DataRefs::LTSetCfgValue, GET_VAR, true, true },
{"livetraffic/cfg/fd_buf_period", DataRefs::LTGetInt, DataRefs::LTSetCfgValue, GET_VAR, true, true },
{"livetraffic/cfg/fd_reduce_height", DataRefs::LTGetInt, DataRefs::LTSetCfgValue, GET_VAR, true, true },
{"livetraffic/cfg/network_timeout_min", DataRefs::LTGetInt, DataRefs::LTSetCfgValue, GET_VAR, true },
{"livetraffic/cfg/network_timeout", DataRefs::LTGetInt, DataRefs::LTSetCfgValue, GET_VAR, true },
{"livetraffic/cfg/lnd_lights_taxi", DataRefs::LTGetInt, DataRefs::LTSetCfgValue, GET_VAR, true },
{"livetraffic/cfg/hide_below_agl", DataRefs::LTGetInt, DataRefs::LTSetCfgValue, GET_VAR, true, true },
Expand Down Expand Up @@ -612,7 +613,8 @@ void* DataRefs::getVarAddr (dataRefsLT dr)
case DR_CFG_FD_LONG_REFRESH_INTVL: return &fdLongRefrIntvl;
case DR_CFG_FD_BUF_PERIOD: return &fdBufPeriod;
case DR_CFG_FD_REDUCE_HEIGHT: return &fdReduceHeight;
case DR_CFG_NETW_TIMEOUT: return &netwTimeout;
case DR_CFG_MIN_NETW_TIMEOUT: return &netwTimeoutMin;
case DR_CFG_MAX_NETW_TIMEOUT: return &netwTimeoutMax;
case DR_CFG_LND_LIGHTS_TAXI: return &bLndLightsTaxi;
case DR_CFG_HIDE_BELOW_AGL: return &hideBelowAGL;
case DR_CFG_HIDE_TAXIING: return &hideTaxiing;
Expand Down Expand Up @@ -1709,7 +1711,7 @@ bool DataRefs::SetCfgValue (void* p, int val)
fdBufPeriod < fdLongRefrIntvl || fdBufPeriod > 180 ||
fdReduceHeight < 1000 || fdReduceHeight > 100000||
fdSnapTaxiDist < 0 || fdSnapTaxiDist > 50 ||
netwTimeout < 10 ||
netwTimeoutMax < 5 || netwTimeoutMin > netwTimeoutMax ||
hideBelowAGL < 0 || hideBelowAGL > MDL_ALT_MAX ||
hideNearbyGnd < 0 || hideNearbyGnd > 500 ||
hideNearbyAir < 0 || hideNearbyAir > 5000 ||
Expand Down Expand Up @@ -1776,7 +1778,8 @@ void DataRefs::ResetAdvCfgToDefaults ()
fdLongRefrIntvl = DEF_FD_LONG_REFR_INTVL;
fdBufPeriod = DEF_FD_BUF_PERIOD;
fdReduceHeight = DEF_FD_REDUCE_HEIGHT;
netwTimeout = DEF_NETW_TIMEOUT;
netwTimeoutMin = DEF_MIN_NETW_TIMEOUT;
netwTimeoutMax = DEF_MAX_NETW_TIMEOUT;
contrailAltMin_ft = DEF_CONTR_ALT_MIN;
contrailAltMax_ft = DEF_CONTR_ALT_MAX;
contrailLifeTime = DEF_CONTR_LIFETIME;
Expand Down Expand Up @@ -2074,7 +2077,7 @@ bool DataRefs::LoadConfigFile()
conv = CFG_V31;
if (cfgFileVer < 30301) // < 3.3.1
conv = CFG_V331;
if (cfgFileVer < 30402) // < 3.4.2: Reset Force FMOD instance = 0
if (cfgFileVer < 30402) // < 3.4.2: Reset Force FMOD instance = 0, set network timeout to 5s
conv = CFG_V342;
}
}
Expand Down Expand Up @@ -2139,6 +2142,9 @@ bool DataRefs::LoadConfigFile()
// Re-implemented XP Sound, so we reset the "Force own FMOD Instance flag" because we believe in the XP implementation now ;-)
if (*i == DATA_REFS_LT[DR_CFG_SND_FORCE_FMOD_INSTANCE])
sVal = "0";
// With OpenSky API timeouts we set the max timeout to just 5s so we try more often
if (*i == DATA_REFS_LT[DR_CFG_MAX_NETW_TIMEOUT])
sVal = "5";
break;
}

Expand Down
2 changes: 1 addition & 1 deletion Src/LTADSBEx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -643,7 +643,7 @@ bool ADSBExchangeConnection::DoTestADSBExAPIKey (const std::string newKey)
// prepare the handle with the right options
readBuf.reserve(CURL_MAX_WRITE_SIZE);
curl_easy_setopt(pCurl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, dataRefs.GetNetwTimeout());
curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, dataRefs.GetNetwTimeoutMax());
curl_easy_setopt(pCurl, CURLOPT_ERRORBUFFER, curl_errtxt);
curl_easy_setopt(pCurl, CURLOPT_HEADERFUNCTION, testKeyTy == ADSBEX_KEY_RAPIDAPI ? ReceiveHeader : NULL);
curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, DoTestADSBExAPIKeyCB);
Expand Down
26 changes: 19 additions & 7 deletions Src/LTChannel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ std::ofstream LTOnlineChannel::outRaw;

LTOnlineChannel::LTOnlineChannel () :
pCurl(NULL),
nTimeout(dataRefs.GetNetwTimeout()),
nTimeout(dataRefs.GetNetwTimeoutMax()),
netData((char*)malloc(CURL_MAX_WRITE_SIZE)), // initial buffer allocation
netDataPos(0), netDataSize(CURL_MAX_WRITE_SIZE),
curl_errtxt{0}, httpResponse(HTTP_OK)
Expand Down Expand Up @@ -575,10 +575,20 @@ bool LTOnlineChannel::FetchAllData (const positionTy& pos)

// In case of timeout increase the channel's timeout and don't count it as an error
if (cc == CURLE_OPERATION_TIMEDOUT) {
LOG_MSG(logWARN, ERR_CURL_PERFORM, ChName(), cc, curl_errtxt);
nTimeout = std::min (nTimeout * 2,
dataRefs.GetNetwTimeout());
LOG_MSG(logWARN, "%s: Timeout increased to %ds", ChName(), nTimeout);
// But bother the user only with messages once every 3 minutes (this is due to OpenSky having been plagued with slow API responses running into timeouts)
static float tLastMsg = 0.0f; // time last message was logged
const float now = dataRefs.GetMiscNetwTime();
if (tLastMsg + 180.0f < now) {
LOG_MSG(logWARN, ERR_CURL_PERFORM, ChName(), cc, curl_errtxt);
tLastMsg = now;
}
// Increase the timeout
const int prevTimeout = nTimeout;
nTimeout = std::min (nTimeout * 2, // double the timeout
dataRefs.GetNetwTimeoutMax());
if (nTimeout > prevTimeout) {
LOG_MSG(logWARN, "%s: Timeout increased to %ds", ChName(), nTimeout);
}
} else {
SHOW_MSG(logERR, ERR_CURL_PERFORM, ChName(), cc, curl_errtxt);
IncErrCnt();
Expand All @@ -594,9 +604,11 @@ bool LTOnlineChannel::FetchAllData (const positionTy& pos)
// Based on actual time take set a new network timeout as twice the response time
{
const std::chrono::seconds d = std::chrono::duration_cast<std::chrono::seconds>(tEnd - tStart);
// In case of a reduction we reduce to no less than half the previous value,
// keep value between min/max network timeout
nTimeout = std::clamp<int> ((int)d.count() * 2,
std::max(MIN_NETW_TIMEOUT,nTimeout/2), // this means the in case of a reduction we reduce to now less than half the previous value
dataRefs.GetNetwTimeout());
std::max(dataRefs.GetNetwTimeoutMin(),nTimeout/2),
dataRefs.GetNetwTimeoutMax());
// LOG_MSG(logWARN, "%s: Timeout set to %ds", ChName(), nTimeout);
break;
}
Expand Down
2 changes: 1 addition & 1 deletion Src/LTOpenGlider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -926,7 +926,7 @@ static bool OGNAcListDoDownload ()
// prepare the handle with the right options
ho.readBuf.reserve(CURL_MAX_WRITE_SIZE);
curl_easy_setopt(pCurl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, dataRefs.GetNetwTimeout());
curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, dataRefs.GetNetwTimeoutMax());
curl_easy_setopt(pCurl, CURLOPT_ERRORBUFFER, curl_errtxt);
curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, OGNAcListNetwCB);
curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, &ho);
Expand Down
1 change: 0 additions & 1 deletion Src/LTOpenSky.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -503,7 +503,6 @@ bool OpenSkyAcMasterdata::FetchAllData (const positionTy& /*pos*/)
if (!info.empty())
vecAc.push_back(std::move(info));

IncErrCnt();
return !listMd.empty(); // return `true` if there is data to process, otherwise we wouldn't process what had been received before the error
}

Expand Down
2 changes: 1 addition & 1 deletion Src/LTVersion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,7 @@ bool FetchXPlaneOrgVersion ()
verXPlaneOrg = 0;
readBuf.reserve(CURL_MAX_WRITE_SIZE);
curl_easy_setopt(pCurl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, dataRefs.GetNetwTimeout());
curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, dataRefs.GetNetwTimeoutMax());
curl_easy_setopt(pCurl, CURLOPT_ERRORBUFFER, curl_errtxt);
curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, FetchVersionCB);
curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, &readBuf);
Expand Down
2 changes: 1 addition & 1 deletion Src/LTWeather.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ bool WeatherFetch (float _lat, float _lon, float _radius_nm)
// prepare the handle with the right options
readBuf.reserve(CURL_MAX_WRITE_SIZE);
curl_easy_setopt(pCurl, CURLOPT_NOSIGNAL, 1);
curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, dataRefs.GetNetwTimeout());
curl_easy_setopt(pCurl, CURLOPT_TIMEOUT, dataRefs.GetNetwTimeoutMax());
curl_easy_setopt(pCurl, CURLOPT_ERRORBUFFER, curl_errtxt);
curl_easy_setopt(pCurl, CURLOPT_WRITEFUNCTION, WeatherFetchCB);
curl_easy_setopt(pCurl, CURLOPT_WRITEDATA, &readBuf);
Expand Down
5 changes: 3 additions & 2 deletions Src/SettingsUI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -791,8 +791,9 @@ void LTSettingsUI::buildInterface()
ImGui::FilteredCfgNumber("Above height AGL of", sFilter, DR_CFG_FD_REDUCE_HEIGHT, 1000, 100000, 1000, "%d ft");
ImGui::FilteredCfgNumber("increase refresh to", sFilter, DR_CFG_FD_LONG_REFRESH_INTVL, 10, 180, 5, "%d s");
ImGui::FilteredCfgNumber("Buffering period", sFilter, DR_CFG_FD_BUF_PERIOD, 10, 180, 5, "%d s");
ImGui::FilteredCfgNumber("Max. Network timeout", sFilter, DR_CFG_NETW_TIMEOUT, 10, 180, 5, "%d s");

ImGui::FilteredCfgNumber("Min. Network timeout", sFilter, DR_CFG_MIN_NETW_TIMEOUT, 5, 180, 5, "%d s");
ImGui::FilteredCfgNumber("Max. Network timeout", sFilter, DR_CFG_MAX_NETW_TIMEOUT, 5, 180, 5, "%d s");

if (!*sFilter) ImGui::TreePop();
}

Expand Down
10 changes: 10 additions & 0 deletions docs/readme.html
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,16 @@ <h3>v3.4.2</h3>
<P>Change log:</P>

<ul>
<li>
In light of OpenSky's API timeout issues, some network error handling
is reworked. Introduced a new
<a href="https://twinfan.gitbook.io/livetraffic/setup/configuration/settings-advanced#aircraft-selection">Advanced Setting</a>
for <i>Min. Network Timeout</i>.
With this version, both <i>Min.</i> and <i>Max. Network timeout</i> are set to 5s.
This way LiveTraffic won't wait long for OpenSky not responding
but instead will retry again sooner.
<a href="https://forums.x-plane.org/index.php?/forums/topic/295468-opensky-request-timeouts/&do=findComment&comment=2627189">See here for recommendations.</a>
</li>
<li>
Reimplemented usage of XP's sound system when running in X-Plane 12.04 and later.
As I believe in this implementation, the
Expand Down

0 comments on commit a0c87e1

Please sign in to comment.