PingProfile.cpp 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. *
  20. * FILE
  21. * $Archive: /Commando/Code/WWOnline/PingProfile.cpp $
  22. *
  23. * DESCRIPTION
  24. *
  25. * PROGRAMMER
  26. * $Author: Denzil_l $
  27. *
  28. * VERSION INFO
  29. * $Revision: 8 $
  30. * $Modtime: 1/11/02 5:45p $
  31. *
  32. ******************************************************************************/
  33. #include "always.h"
  34. #include "PingProfile.h"
  35. #include <WWOnline\WOLSession.h>
  36. #include <WWOnline\WOLServer.h>
  37. #include <WWOnline\WOLString.h>
  38. #include <WWLib\WideString.h>
  39. #include <WWDebug\WWDebug.h>
  40. #include <limits.h>
  41. #include <math.h>
  42. using namespace WWOnline;
  43. static PingProfile gPingProfile;
  44. /******************************************************************************
  45. *
  46. * NAME
  47. * RecalculatePingProfile
  48. *
  49. * DESCRIPTION
  50. * Calculate ping profile based on the current session ping times.
  51. *
  52. * INPUTS
  53. * Session - WWOnline session
  54. *
  55. * RESULT
  56. * True if ping times are valid
  57. *
  58. ******************************************************************************/
  59. bool RecalculatePingProfile(const RefPtr<Session>& session)
  60. {
  61. memset(gPingProfile.Pings, 0xFF, sizeof(gPingProfile.Pings));
  62. if (session.IsValid())
  63. {
  64. const PingServerList& pingers = session->GetPingServerList();
  65. // The ping profile holds up to eight pings.
  66. unsigned int count = min<unsigned int>(8, pingers.size());
  67. // If there aren't any ping servers then fail.
  68. if (count > 0)
  69. {
  70. for (unsigned int index = 0; index < count; index++)
  71. {
  72. int pingTime = pingers[index]->GetPingTime();
  73. // If a ping time is invalid then fail.
  74. if (pingTime < 0)
  75. {
  76. return false;
  77. }
  78. // Clamp pings time to a maximum of 1000 ms
  79. pingTime = min<int>(pingTime, 1000);
  80. // Scale ping time to from 0-1000 to 0-255
  81. gPingProfile.Pings[index] = (((unsigned long)pingTime * 255) / 1000);
  82. }
  83. return true;
  84. }
  85. }
  86. return false;
  87. }
  88. /******************************************************************************
  89. *
  90. * NAME
  91. * GetLocalPingProfile
  92. *
  93. * DESCRIPTION
  94. * Get the ping profile for this client.
  95. *
  96. * INPUTS
  97. * NONE
  98. *
  99. * RESULT
  100. * Profile - Ping profile to for current session.
  101. *
  102. ******************************************************************************/
  103. const PingProfile& GetLocalPingProfile(void)
  104. {
  105. return gPingProfile;
  106. }
  107. /******************************************************************************
  108. *
  109. * NAME
  110. * ComparePingProfile
  111. *
  112. * DESCRIPTION
  113. * Calcualte the best time between two ping profiles.
  114. *
  115. * INPUTS
  116. * Profile1 - Ping profile
  117. * Profile2 - Ping profile
  118. *
  119. * RESULT
  120. * Weight - Smallest time between two profiles.
  121. *
  122. ******************************************************************************/
  123. long ComparePingProfile(const PingProfile& ping1, const PingProfile& ping2)
  124. {
  125. long minWeight = LONG_MAX;
  126. for (int index = 0; index < 8; index++)
  127. {
  128. long a = ping1.Pings[index];
  129. long b = ping2.Pings[index];
  130. long weight = (a * a) + (b * b);
  131. minWeight = min<long>(weight, minWeight);
  132. }
  133. return minWeight;
  134. }
  135. /******************************************************************************
  136. *
  137. * NAME
  138. * EncodePingProfile
  139. *
  140. * DESCRIPTION
  141. * Encode the ping profile into a clear text string that can be transmited
  142. * via chat.
  143. *
  144. * INPUTS
  145. * Pings - Ping profile to encode.
  146. * Buffer - Buffer to encode pings into.
  147. *
  148. * RESULT
  149. * Length - Number of characters used to encode the profile.
  150. *
  151. ******************************************************************************/
  152. int EncodePingProfile(const PingProfile& pings, char* buffer)
  153. {
  154. assert(buffer != NULL);
  155. if (buffer == NULL)
  156. {
  157. return 0;
  158. }
  159. char temp[18];
  160. int count = sprintf(temp, "%02X%02X%02X%02X%02X%02X%02X%02X",
  161. pings.Pings[0], pings.Pings[1], pings.Pings[2], pings.Pings[3],
  162. pings.Pings[4], pings.Pings[5], pings.Pings[6], pings.Pings[7]);
  163. strcat(buffer, temp);
  164. return count;
  165. }
  166. /******************************************************************************
  167. *
  168. * NAME
  169. * DecodePingProfile
  170. *
  171. * DESCRIPTION
  172. * Decode the ping profile from clear text string.
  173. *
  174. * INPUTS
  175. * String - Encode ping profile.
  176. * Pings - Ping profile structure.
  177. *
  178. * RESULT
  179. * NONE
  180. *
  181. ******************************************************************************/
  182. void DecodePingProfile(const char* buffer, PingProfile& pings)
  183. {
  184. if (buffer)
  185. {
  186. unsigned long ping[8];
  187. sscanf(buffer, "%02X%02X%02X%02X%02X%02X%02X%02X",
  188. &ping[0], &ping[1], &ping[2], &ping[3], &ping[4], &ping[5], &ping[6], &ping[7]);
  189. pings.Pings[0] = ping[0];
  190. pings.Pings[1] = ping[1];
  191. pings.Pings[2] = ping[2];
  192. pings.Pings[3] = ping[3];
  193. pings.Pings[4] = ping[4];
  194. pings.Pings[5] = ping[5];
  195. pings.Pings[6] = ping[6];
  196. pings.Pings[7] = ping[7];
  197. }
  198. else
  199. {
  200. memset(&pings, 0xFF, sizeof(pings));
  201. }
  202. }
  203. /******************************************************************************
  204. *
  205. * NAME
  206. * PingProfileWait::Create
  207. *
  208. * DESCRIPTION
  209. * Create a wait condition to obtain an update ping profile.
  210. *
  211. * INPUTS
  212. * Session - WWOnline session
  213. *
  214. * RESULT
  215. * Wait - Wait condition to process.
  216. *
  217. ******************************************************************************/
  218. RefPtr<PingProfileWait> PingProfileWait::Create(void)
  219. {
  220. return new PingProfileWait;
  221. }
  222. /******************************************************************************
  223. *
  224. * NAME
  225. * PingProfileWait::PingProfileWait
  226. *
  227. * DESCRIPTION
  228. * Constructor
  229. *
  230. * INPUTS
  231. *
  232. * RESULT
  233. *
  234. ******************************************************************************/
  235. PingProfileWait::PingProfileWait() :
  236. SingleWait(WOLSTRING("WOL_PINGPROFILEREQUEST")),
  237. mCount(0)
  238. {
  239. WWDEBUG_SAY(("WOL: PingProfileWait Instantiated\n"));
  240. mWOLSession = Session::GetInstance(false);
  241. }
  242. /******************************************************************************
  243. *
  244. * NAME
  245. * PingProfileWait::~PingProfileWait
  246. *
  247. * DESCRIPTION
  248. * Destructor
  249. *
  250. * INPUTS
  251. * NONE
  252. *
  253. * RESULT
  254. * NONE
  255. *
  256. ******************************************************************************/
  257. PingProfileWait::~PingProfileWait()
  258. {
  259. WWDEBUG_SAY(("WOL: PingProfileWait Destroyed\n"));
  260. }
  261. /******************************************************************************
  262. *
  263. * NAME
  264. * PingProfileWait::WaitBeginning
  265. *
  266. * DESCRIPTION
  267. * Begin wait condition
  268. *
  269. * INPUTS
  270. * NONE
  271. *
  272. * RESULT
  273. * NONE
  274. *
  275. ******************************************************************************/
  276. void PingProfileWait::WaitBeginning(void)
  277. {
  278. WWDEBUG_SAY(("WOL: PingProfileWait Beginning\n"));
  279. if (mWOLSession.IsValid() == false)
  280. {
  281. EndWait(Error, WOLSTRING("WOL_NOTINITIALIZED"));
  282. return;
  283. }
  284. const PingServerList& pingers = mWOLSession->GetPingServerList();
  285. // Handle up to eight servers
  286. mCount = min<unsigned int>(8, pingers.size());
  287. if (mCount == 0)
  288. {
  289. EndWait(Error, WOLSTRING("WOL_NOPINGSERVER"));
  290. return;
  291. }
  292. Observer<RawPing>::NotifyMe(*mWOLSession);
  293. bool pingsValid = true;
  294. for (unsigned int index = 0; index < mCount; index++)
  295. {
  296. int pingTime = pingers[index]->GetPingTime();
  297. // If a ping time is invalid then request it.
  298. if (pingTime == -1)
  299. {
  300. pingsValid = false;
  301. const char* address = pingers[index]->GetHostAddress();
  302. mWOLSession->RequestPing(address);
  303. }
  304. }
  305. if (pingsValid)
  306. {
  307. RecalculatePingProfile(mWOLSession);
  308. EndWait(ConditionMet, WOLSTRING("WOL_PINGPROFILERECEIVED"));
  309. }
  310. }
  311. /******************************************************************************
  312. *
  313. * NAME
  314. * PingProfileWait::HandleNotification(RawPing)
  315. *
  316. * DESCRIPTION
  317. *
  318. * INPUTS
  319. *
  320. * RESULT
  321. * NONE
  322. *
  323. ******************************************************************************/
  324. void PingProfileWait::HandleNotification(RawPing& ping)
  325. {
  326. if ((mEndResult == Waiting) && (mCount > 0))
  327. {
  328. const char* address = ping.GetHostAddress();
  329. const PingServerList& pingers = mWOLSession->GetPingServerList();
  330. for (unsigned int index = 0; index < pingers.size(); index++)
  331. {
  332. const char* pinger = pingers[index]->GetHostAddress();
  333. if (stricmp(address, pinger) == 0)
  334. {
  335. WWDEBUG_SAY(("WOL: PingProfileWait received ping for '%s'\n", pingers[index]->GetData().name));
  336. mCount--;
  337. break;
  338. }
  339. }
  340. if (mCount == 0)
  341. {
  342. RecalculatePingProfile(mWOLSession);
  343. EndWait(ConditionMet, WOLSTRING("WOL_PINGPROFILERECEIVED"));
  344. }
  345. }
  346. }