rhost.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. //
  19. // Filename: rhost.cpp
  20. // Project: wwnet
  21. // Author: Tom Spencer-Smith
  22. // Date: June 1998
  23. // Description: Data about a remote host
  24. //
  25. //-----------------------------------------------------------------------------
  26. #include "rhost.h" // I WANNA BE FIRST!
  27. #include <stdlib.h>
  28. #include <math.h>
  29. #include "systimer.h"
  30. #include "miscutil.h"
  31. #include "mathutil.h"
  32. #include "connect.h"
  33. #include "wwdebug.h"
  34. #include "packetmgr.h"
  35. bool cRemoteHost::AllowExtraModemBandwidthThrottling = true;
  36. int cRemoteHost::PriorityUpdateRate = 15;
  37. //
  38. // class defines
  39. //
  40. //-----------------------------------------------------------------------------
  41. cRemoteHost::cRemoteHost() :
  42. ReliablePacketSendId(0),
  43. UnreliablePacketSendId(0),
  44. ReliablePacketRcvId(0),
  45. UnreliablePacketRcvId(0),
  46. LastKeepaliveTimeMs(TIMEGETTIME()),
  47. IsFlowControlEnabled(cConnection::Is_Flow_Control_Enabled()),
  48. MustEvict(false),
  49. LastReliableSendId(-2), // dummy value
  50. LastUnreliableSendId(-2), // dummy value
  51. ResendTimeoutMs(cNetUtil::Get_Default_Resend_Timeout_Ms()),
  52. LastServiceCount(0),
  53. LastContactTime(0),
  54. TargetBps(0),
  55. MaximumBps(0),
  56. MaxInternalPingtimeMs(0),
  57. AverageInternalPingtimeMs(0),
  58. BandwidthMultiplier(1.0f),
  59. AverageObjectPriority(0.5f),
  60. IsLoading(false),
  61. ExpectPacketFlood(false),
  62. WasLoading(0),
  63. CreationTime(TIMEGETTIME()),
  64. TotalResends(0),
  65. PriorityUpdateCounter(0),
  66. ExtendedAveragePingTime(0),
  67. ExtendedAverageCount(0),
  68. LastAveragePingTime(0),
  69. IsOutgoingFlooded(false),
  70. TotalResentPacketsInQueue(0),
  71. NextOutgoingFloodActionTime(0),
  72. NumOutgoingFloods(0)
  73. {
  74. //WWDEBUG_SAY(("cRemoteHost::cRemoteHost\n"));
  75. ZeroMemory(&Address, sizeof(SOCKADDR_IN));
  76. if (IsFlowControlEnabled) {
  77. //ThresholdPriority = cNetUtil::Get_Initial_Threshold_Priority();
  78. //ThresholdPriority = 1.0;
  79. ThresholdPriority = 0.5;
  80. } else {
  81. ThresholdPriority = 0.0;
  82. }
  83. //ThresholdPriority = 0.0;
  84. TPIncrement = 0.01;//TSS2001d
  85. Init_Stats();
  86. }
  87. //-----------------------------------------------------------------------------
  88. void cRemoteHost::Init_Stats()
  89. {
  90. Stats.Init_Net_Stats();
  91. //
  92. // Possibly, pull back ResendTimeoutMs if we have had a chance to measure ping time yet.
  93. //
  94. Adjust_Resend_Timeout();
  95. //if (MaxInternalPingtimeMs && AverageInternalPingtimeMs) {
  96. // int candidate_resend_timeout_ms = min(3 * AverageInternalPingtimeMs,
  97. // (int) (1.1 * MaxInternalPingtimeMs));
  98. // if (candidate_resend_timeout_ms < ResendTimeoutMs) {
  99. // WWASSERT(candidate_resend_timeout_ms < 0xffff);
  100. // ResendTimeoutMs = candidate_resend_timeout_ms;
  101. // //WWDEBUG_SAY((">> ResendTimeoutMs for rhost %d = %d\n", Id, ResendTimeoutMs));
  102. // }
  103. //}
  104. //WWDEBUG_SAY(("ResendTimeoutMs for rhost %d = %d\n", Id, (unsigned long)ResendTimeoutMs));
  105. WWDEBUG_SAY(("ResendTimeoutMs for rhost = %d\n", (unsigned long)ResendTimeoutMs));
  106. TotalInternalPingtimeMs = 0;
  107. NumInternalPings = 0;
  108. AverageInternalPingtimeMs = 0;//-1;
  109. MinInternalPingtimeMs = 1000000;
  110. MaxInternalPingtimeMs = 0;
  111. }
  112. //-----------------------------------------------------------------------------
  113. void cRemoteHost::Set_Last_Service_Count(int service_count)
  114. {
  115. WWASSERT(service_count >= 0);
  116. LastServiceCount = service_count;
  117. }
  118. //-----------------------------------------------------------------------------
  119. void cRemoteHost::Compute_List_Max(int list_type)
  120. {
  121. WWASSERT(list_type >= 0 && list_type < 4);
  122. ListMax[list_type] = PacketList[list_type].Get_Count();
  123. }
  124. //-----------------------------------------------------------------------------
  125. int cRemoteHost::Get_List_Max(int list_type)
  126. {
  127. WWASSERT(list_type >= 0 && list_type < 4);
  128. return ListMax[list_type];
  129. }
  130. //-----------------------------------------------------------------------------
  131. void cRemoteHost::Set_List_Processing_Time(int list_type, int processing_time_ms)
  132. {
  133. WWASSERT(list_type >= 0 && list_type < 4);
  134. ListProcessingTime[list_type] = processing_time_ms;
  135. }
  136. //-----------------------------------------------------------------------------
  137. int cRemoteHost::Get_List_Processing_Time(int list_type)
  138. {
  139. WWASSERT(list_type >= 0 && list_type < 4);
  140. return ListProcessingTime[list_type];
  141. }
  142. //-----------------------------------------------------------------------------
  143. SOCKADDR_IN & cRemoteHost::Get_Address()
  144. {
  145. return Address;
  146. }
  147. //-----------------------------------------------------------------------------
  148. cRemoteHost::~cRemoteHost()
  149. {
  150. SLNode<cPacket> * objnode;
  151. cPacket * p_packet;
  152. for (int list_type = 0; list_type < 4; list_type++) {
  153. for (objnode = PacketList[list_type].Head(); objnode != NULL;) {
  154. p_packet = objnode->Data();
  155. WWASSERT(p_packet != NULL);
  156. objnode = objnode->Next();
  157. PacketList[list_type].Remove(p_packet);
  158. p_packet->Flush();
  159. delete p_packet;
  160. }
  161. }
  162. }
  163. //------------------------------------------------------------------------------------
  164. void cRemoteHost::Add_Packet(cPacket & packet, BYTE list_type)
  165. {
  166. WWASSERT(
  167. list_type == RELIABLE_SEND_LIST ||
  168. list_type == RELIABLE_RCV_LIST ||
  169. list_type == UNRELIABLE_SEND_LIST ||
  170. list_type == UNRELIABLE_RCV_LIST);
  171. WWASSERT(packet.Get_Id() >= 0);
  172. if (list_type == RELIABLE_SEND_LIST) {
  173. //
  174. // Test to make sure that additions to send list are in sequence.
  175. //
  176. if (LastReliableSendId != -2) {
  177. WWASSERT(packet.Get_Id() == LastReliableSendId + 1);
  178. }
  179. LastReliableSendId = packet.Get_Id();
  180. } else if (list_type == UNRELIABLE_SEND_LIST) {
  181. if (LastUnreliableSendId != -2) {
  182. WWASSERT(packet.Get_Id() == LastUnreliableSendId + 1);
  183. }
  184. LastUnreliableSendId = packet.Get_Id();
  185. }
  186. cPacket * p_packet = new cPacket;
  187. WWASSERT(p_packet != NULL);
  188. *p_packet = packet; // copy data
  189. if (list_type == RELIABLE_SEND_LIST || list_type == UNRELIABLE_SEND_LIST) {
  190. PacketList[list_type].Add_Tail(p_packet);
  191. } else {
  192. //
  193. // Locate insertion point according to packet id
  194. //
  195. SLNode<cPacket> * objnode;
  196. cPacket * obj = NULL;
  197. for (objnode = PacketList[list_type].Head(); objnode; objnode = objnode->Next()) {
  198. obj = objnode->Data();
  199. WWASSERT(obj != NULL);
  200. if (obj->Get_Id() > packet.Get_Id()) {
  201. break;
  202. }
  203. }
  204. //
  205. // Insert packet at designated point
  206. // TSS - this is inefficient: we have already parsed the
  207. // list once
  208. //
  209. if (objnode == NULL) {
  210. PacketList[list_type].Add_Tail(p_packet);
  211. } else {
  212. PacketList[list_type].Insert_Before(p_packet, obj);
  213. }
  214. }
  215. }
  216. //------------------------------------------------------------------------------------
  217. void cRemoteHost::Remove_Packet(int packet_id, BYTE list_type)
  218. {
  219. WWASSERT(packet_id >= 0);
  220. WWASSERT(
  221. list_type == RELIABLE_SEND_LIST ||
  222. list_type == RELIABLE_RCV_LIST ||
  223. list_type == UNRELIABLE_SEND_LIST ||
  224. list_type == UNRELIABLE_RCV_LIST);
  225. SLNode<cPacket> * objnode;
  226. for (objnode = PacketList[list_type].Head(); objnode != NULL;) {
  227. cPacket * p_packet = objnode->Data();
  228. WWASSERT(p_packet != NULL);
  229. objnode = objnode->Next();
  230. if (packet_id == p_packet->Get_Id()) {
  231. if (list_type == RELIABLE_SEND_LIST) {// &&
  232. // Packets that require a resend shouldn't count towards ping time calculations. It may be being removed because
  233. // the ACK to the first send just came in and if we just resent it then the ping time will look really low so we get
  234. // biased towards a low resend timeout value on connections of variable quality. ST - 12/7/2001 12:48PM
  235. if (p_packet->Get_Resend_Count() == 0 || NumInternalPings == 0) {
  236. unsigned long time = TIMEGETTIME();
  237. int ping_time = time - p_packet->Get_Send_Time();
  238. if (p_packet->Get_Resend_Count() != 0) {
  239. WWASSERT(NumInternalPings == 0);
  240. // If we are not getting any timing info at all then we need to do something. Use the first send time. It's going
  241. // to make it big but that should cut down on the resends and let us get better timing info.
  242. if (NumInternalPings == 0) {
  243. ping_time = TIMEGETTIME() - p_packet->Get_First_Send_Time();
  244. }
  245. }
  246. TotalInternalPingtimeMs += ping_time;
  247. NumInternalPings++;
  248. if (NumInternalPings > 0) {
  249. AverageInternalPingtimeMs = cMathUtil::Round(TotalInternalPingtimeMs / (double) NumInternalPings);
  250. } else {
  251. AverageInternalPingtimeMs = 0;
  252. }
  253. if (ping_time < MinInternalPingtimeMs) {
  254. MinInternalPingtimeMs = ping_time;
  255. }
  256. if (ping_time > MaxInternalPingtimeMs) {
  257. MaxInternalPingtimeMs = ping_time;
  258. #if (0)
  259. int candidate_resend_timeout_ms = (int) (MaxInternalPingtimeMs
  260. * 1.1);
  261. if (candidate_resend_timeout_ms > ResendTimeoutMs) {
  262. ResendTimeoutMs = candidate_resend_timeout_ms;
  263. //WWDEBUG_SAY((">> ResendTimeoutMs for rhost %d = %d\n", Id, ResendTimeoutMs));
  264. }
  265. #endif //(0)
  266. }
  267. }
  268. }
  269. //if (list_type == RELIABLE_SEND_LIST) {
  270. //WWDEBUG_SAY(("Removing packet %d from RELIABLE_SEND_LIST\n", packet_id));
  271. //}
  272. PacketList[list_type].Remove(p_packet);
  273. p_packet->Flush();
  274. delete p_packet;
  275. break;
  276. }
  277. }
  278. }
  279. //------------------------------------------------------------------------------------
  280. void cRemoteHost::Toggle_Flow_Control()
  281. {
  282. IsFlowControlEnabled = !IsFlowControlEnabled;
  283. if (IsFlowControlEnabled) {
  284. //ThresholdPriority = 1.0;
  285. ThresholdPriority = 0.5;
  286. } else {
  287. ThresholdPriority = 0.0;
  288. }
  289. //ThresholdPriority = 0.0;
  290. }
  291. //------------------------------------------------------------------------------------
  292. void cRemoteHost::Adjust_Flow_If_Necessary(float sample_time_ms)
  293. {
  294. if (!IsFlowControlEnabled) {
  295. return;
  296. }
  297. #ifdef OBSOLETE
  298. WWASSERT(TargetBps > 0);
  299. float sample_target_bits = sample_time_ms / 1000.0f * TargetBps;
  300. WWASSERT(sample_target_bits > 0);
  301. //TSS101401
  302. //float ratio = Stats.StatSnapshot[STAT_BitsSent] / sample_target_bits;
  303. float ratio = PacketManager.Get_Compressed_Bandwidth_Out(&Get_Address()) / sample_target_bits;
  304. if (ratio > MISCUTIL_EPSILON) {
  305. double last_tp = TPIncrement;
  306. if (ratio > 1)
  307. {
  308. TPIncrement = ::fabs(TPIncrement);
  309. }
  310. else
  311. {
  312. TPIncrement = -::fabs(TPIncrement);
  313. }
  314. if ((TPIncrement > 0 && last_tp > 0) ||
  315. (TPIncrement < 0 && last_tp < 0)) {
  316. if (::fabs(TPIncrement) < 0.025) {
  317. TPIncrement *= 1.5;
  318. }
  319. } else {
  320. if (::fabs(TPIncrement) > 0.0001) {
  321. TPIncrement *= 0.5;
  322. }
  323. }
  324. double new_tp = ThresholdPriority + TPIncrement;
  325. if (new_tp < 0)
  326. {
  327. new_tp = 0;
  328. }
  329. else if (new_tp > 1)
  330. {
  331. new_tp = 1;
  332. }
  333. ThresholdPriority = new_tp;
  334. /*
  335. if (Id == 1) {
  336. WWDEBUG_SAY(("ratio = %-7d / %-7d = %5.2f, TPIncrement = %9.7f, ThresholdPriority = %9.7f\n",
  337. Stats.StatSnapshot[STAT_BitsSent], (int) sample_target_bits, ratio, TPIncrement, ThresholdPriority));
  338. }
  339. /**/
  340. }
  341. #endif //OBSOLETE
  342. //
  343. // Do a similar calculation for the new bandwidth per object per client distribution method.
  344. //
  345. float actual = (float) PacketManager.Get_Compressed_Bandwidth_Out(&Get_Address());
  346. float desired = (float) TargetBps; //sample_time_ms / 1000.0f * TargetBps;
  347. //if (Id > 1) {
  348. //WWDEBUG_SAY(("actual = %f, desired = %f\n", actual, desired));
  349. //}
  350. if (ExpectPacketFlood) {
  351. BandwidthMultiplier = 0.5f; //1.5f;
  352. if (TIMEGETTIME() - FloodTimer > 8000) {
  353. ExpectPacketFlood = false;
  354. }
  355. } else {
  356. //
  357. // If we are sending way more than we know the remote host can receive then we need to send much less.
  358. //
  359. if (actual > desired) {
  360. BandwidthMultiplier = (BandwidthMultiplier * (desired / actual)) * 0.85f;
  361. } else {
  362. //
  363. // Another case we have to trap is a low bandwidth connection that has been accidentally flooded by having several
  364. // frames where we send more stuff than usual. It could also be a remote host that has incorrectly reported his
  365. // downstream bandwidth and can't receive as much as we think we can send to him. Or it could simply be some temporary
  366. // condition at either end of the connection or anywhere in between. If this happens we have to cut way back on sends
  367. // so that the connection can catch up.
  368. //
  369. if (AllowExtraModemBandwidthThrottling) {
  370. if (IsOutgoingFlooded) {
  371. //
  372. // We recently detected a outbound flooding condition. If we are still flooding then we have to take further action.
  373. //
  374. if (Is_Outgoing_Flooded()) {
  375. Dam_The_Flood();
  376. } else {
  377. IsOutgoingFlooded = false;
  378. }
  379. } else {
  380. //
  381. // See if we are flooding now.
  382. //
  383. if (Is_Outgoing_Flooded()) {
  384. IsOutgoingFlooded = true;
  385. //
  386. // Well, something is wrong. It's kind of hard to say exactly what the problem is so a remedy is only guesswork.
  387. // In the short term we can try reducing the BandwidthMultiplier to clamp down on non-guaranteed packets. If the
  388. // problem persists then we will have to reduce MaximumBps too.
  389. //
  390. NumOutgoingFloods++;
  391. NextOutgoingFloodActionTime = 0;
  392. Dam_The_Flood();
  393. }
  394. }
  395. }
  396. if (BandwidthMultiplier < 20.0f) {
  397. if (actual < (desired * 0.7f)) {
  398. /*
  399. ** Give bandwidth a quicker boost
  400. */
  401. BandwidthMultiplier += 0.5f * (1.0f - (actual / desired));
  402. if (BandwidthMultiplier > 20.0f) {
  403. BandwidthMultiplier = 20.0f;
  404. }
  405. } else {
  406. if (actual < (desired * 0.95f)) {
  407. /*
  408. ** Gradually increase bandwidth usage until we are close to max utilization.
  409. */
  410. BandwidthMultiplier += 0.1f * (1.0f - (actual / desired));
  411. if (BandwidthMultiplier > 20.0f) {
  412. BandwidthMultiplier = 20.0f;
  413. }
  414. }
  415. }
  416. }
  417. }
  418. }
  419. }
  420. //------------------------------------------------------------------------------------
  421. bool cRemoteHost::Is_Outgoing_Flooded(void)
  422. {
  423. //
  424. // Don't take action if the rhost is loading.
  425. //
  426. if (Was_Recently_Loading()) {
  427. return(false);
  428. }
  429. //
  430. // We can try to detect outgoing floods in two ways.
  431. //
  432. //
  433. // 1. Look for a spike in the ping times. If we are sending more than the remote host can receive then ping times will
  434. // quickly rise as each packet backs up at the receive end and thus gets ack'd later than usual.
  435. //
  436. if (ExtendedAverageCount) {
  437. // average ping time over the life of the connection to use as a base line.
  438. int average = (int)(ExtendedAveragePingTime / (unsigned)ExtendedAverageCount);
  439. if ((LastAveragePingTime > 1500 && MaximumBps < 100000) || LastAveragePingTime > 500 && LastAveragePingTime > average * 3) {
  440. //
  441. // Ping time is bigger than we expect. If we are still receiving stuff from this host then chances are we are
  442. // flooding him.
  443. //
  444. if (TIMEGETTIME() - LastContactTime < 1000) {
  445. if (!IsOutgoingFlooded) {
  446. WWDEBUG_SAY(("Detected abnormal ping times - assuming outbound connection to rhost %d is flooded\n", Id));
  447. WWDEBUG_SAY(("Normal average ping time = %d, last average ping time = %d\n", average, LastAveragePingTime));
  448. }
  449. return(true);
  450. }
  451. }
  452. }
  453. //
  454. // 2. An excessive number of packets in the out queue that are older than the average ping time. Since acks aren't
  455. // coming back we resend more and so exacerbate the problem.
  456. //
  457. int total_in_queue = PacketList[RELIABLE_SEND_LIST].Get_Count();
  458. // Let's say that if more than 90% of the packets in the queue have been resent then there is a problem.
  459. if (total_in_queue > 20 && (TotalResentPacketsInQueue*10) > (total_in_queue*9)) {
  460. //
  461. // More resends than we expect. If we are still receiving stuff from this host then chances are we are
  462. // flooding him.
  463. //
  464. if (TIMEGETTIME() - LastContactTime < 1000) {
  465. WWDEBUG_SAY(("Detected abnormally high number of resends - assuming outbound connection to rhost %d is flooded\n", Id));
  466. WWDEBUG_SAY(("Total packets in queue = %d, queue packets resent = %d\n", total_in_queue, TotalResentPacketsInQueue));
  467. return(true);
  468. }
  469. }
  470. return(false);
  471. }
  472. //------------------------------------------------------------------------------------
  473. void cRemoteHost::Dam_The_Flood(void)
  474. {
  475. WWASSERT(IsOutgoingFlooded);
  476. //
  477. // How long since we last took action?
  478. //
  479. bool action_time = (TIMEGETTIME() > NextOutgoingFloodActionTime) ? true : false;
  480. //
  481. // Try something else every now and then until things improve.
  482. //
  483. if (action_time) {
  484. //
  485. // Try reducing BandwidthMultiplier. This will be a temporary change and will be allowed to revert once the connection
  486. // recovers.
  487. //
  488. if (BandwidthMultiplier > 0.5f || NextOutgoingFloodActionTime == 0) {
  489. BandwidthMultiplier = BandwidthMultiplier / 2.0f;
  490. //
  491. // Give this a half second or so to take effect.
  492. //
  493. if (BandwidthMultiplier <= 0.5f) {
  494. //
  495. // Reducing MaximumBps is a last resort. Give it a little more time to recover.
  496. //
  497. NextOutgoingFloodActionTime = 2000 + TIMEGETTIME();
  498. } else {
  499. NextOutgoingFloodActionTime = 600 + TIMEGETTIME();
  500. }
  501. } else {
  502. //
  503. // Try reducing MaximumBps. This will be a permanent change. 30,000ish should be our minimum since the min requirement is
  504. // a 33600 modem.
  505. //
  506. if (MaximumBps > 30000) {
  507. #ifdef WWDEBUG
  508. int old_bps = MaximumBps;
  509. #endif //WWDEBUG
  510. if (MaximumBps > 100000) {
  511. MaximumBps = 100000;
  512. } else {
  513. if (MaximumBps > 56000) {
  514. MaximumBps = 56000;
  515. } else {
  516. //if (MaximumBps > 40000) {
  517. // MaximumBps = 40000;
  518. //} else {
  519. if (MaximumBps > 33600) {
  520. MaximumBps = 33600;
  521. } else {
  522. MaximumBps = 30000;
  523. }
  524. //}
  525. }
  526. }
  527. WWDEBUG_SAY(("Reduced MaximumBps for rhost %d from %d to %d\n", Id, old_bps, MaximumBps));
  528. /*
  529. ** Give this a few seconds to take effect before stepping down again.
  530. */
  531. NextOutgoingFloodActionTime = 5000 + TIMEGETTIME();
  532. }
  533. }
  534. }
  535. }
  536. //------------------------------------------------------------------------------------
  537. void cRemoteHost::Set_Flood(bool state)
  538. {
  539. ExpectPacketFlood = state;
  540. if (state) {
  541. FloodTimer = TIMEGETTIME();
  542. }
  543. }
  544. //------------------------------------------------------------------------------------
  545. void cRemoteHost::Set_Is_Loading(bool state)
  546. {
  547. if (IsLoading && !state) {
  548. WasLoading = TIMEGETTIME();
  549. }
  550. IsLoading = state;
  551. }
  552. //------------------------------------------------------------------------------------
  553. bool cRemoteHost::Was_Recently_Loading(unsigned long time)
  554. {
  555. if (IsLoading) {
  556. return(true);
  557. }
  558. if (time == 0) {
  559. time = TIMEGETTIME();
  560. }
  561. if (time - WasLoading < 1000 * 8) {
  562. return(true);
  563. }
  564. return(false);
  565. }
  566. //------------------------------------------------------------------------------------
  567. void cRemoteHost::Adjust_Resend_Timeout(void)
  568. {
  569. if (NumInternalPings > 0) {
  570. if (MaxInternalPingtimeMs && AverageInternalPingtimeMs) {
  571. int candidate_resend_timeout_ms = min(3 * AverageInternalPingtimeMs, (int) (1.3f * (float)MaxInternalPingtimeMs));
  572. // Make sure it stays inside a reasonable range.
  573. candidate_resend_timeout_ms = min(candidate_resend_timeout_ms, 3000);
  574. candidate_resend_timeout_ms = max(candidate_resend_timeout_ms, 333);
  575. ResendTimeoutMs = (ResendTimeoutMs + candidate_resend_timeout_ms) / 2;
  576. if (candidate_resend_timeout_ms < ResendTimeoutMs) {
  577. WWASSERT(candidate_resend_timeout_ms < 0xffff);
  578. ResendTimeoutMs = candidate_resend_timeout_ms;
  579. //WWDEBUG_SAY((">> ResendTimeoutMs for rhost %d = %d\n", Id, ResendTimeoutMs));
  580. }
  581. //WWDEBUG_SAY(("ResendTimeoutMs for rhost = %d\n", (unsigned long)ResendTimeoutMs));
  582. //
  583. // Keep track of the last average we calculated plus the average ping over the life of the connection.
  584. //
  585. ExtendedAveragePingTime += (unsigned) TotalInternalPingtimeMs;
  586. ExtendedAverageCount++;
  587. LastAveragePingTime = AverageInternalPingtimeMs;
  588. }
  589. TotalInternalPingtimeMs = 0;
  590. NumInternalPings = 0;
  591. AverageInternalPingtimeMs = 0;//-1;
  592. MinInternalPingtimeMs = 65535;
  593. MaxInternalPingtimeMs = 0;
  594. }
  595. }