FullyConnectedMesh2.cpp 44 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423
  1. /*
  2. * Copyright (c) 2014, Oculus VR, Inc.
  3. * All rights reserved.
  4. *
  5. * This source code is licensed under the BSD-style license found in the
  6. * LICENSE file in the root directory of this source tree. An additional grant
  7. * of patent rights can be found in the PATENTS file in the same directory.
  8. *
  9. */
  10. /// \file
  11. ///
  12. #include "NativeFeatureIncludes.h"
  13. #if _RAKNET_SUPPORT_FullyConnectedMesh2==1
  14. #include "FullyConnectedMesh2.h"
  15. #include "RakPeerInterface.h"
  16. #include "MessageIdentifiers.h"
  17. #include "RakAssert.h"
  18. #include "GetTime.h"
  19. #include "Rand.h"
  20. #include "DS_OrderedList.h"
  21. using namespace RakNet;
  22. int FCM2ParticipantComp( FullyConnectedMesh2::FCM2Participant * const &key, FullyConnectedMesh2::FCM2Participant * const &data )
  23. {
  24. if (key->fcm2Guid < data->fcm2Guid)
  25. return -1;
  26. if (key->fcm2Guid > data->fcm2Guid)
  27. return 1;
  28. return 0;
  29. }
  30. STATIC_FACTORY_DEFINITIONS(FullyConnectedMesh2,FullyConnectedMesh2);
  31. FullyConnectedMesh2::FullyConnectedMesh2()
  32. {
  33. startupTime=0;
  34. totalConnectionCount=0;
  35. ourFCMGuid=0;
  36. autoParticipateConnections=true;
  37. connectOnNewRemoteConnections=true;
  38. hostRakNetGuid=UNASSIGNED_RAKNET_GUID;
  39. }
  40. FullyConnectedMesh2::~FullyConnectedMesh2()
  41. {
  42. Clear();
  43. }
  44. RakNetGUID FullyConnectedMesh2::GetConnectedHost(void) const
  45. {
  46. if (ourFCMGuid==0)
  47. return UNASSIGNED_RAKNET_GUID;
  48. return hostRakNetGuid;
  49. }
  50. SystemAddress FullyConnectedMesh2::GetConnectedHostAddr(void) const
  51. {
  52. if (ourFCMGuid==0)
  53. return UNASSIGNED_SYSTEM_ADDRESS;
  54. return rakPeerInterface->GetSystemAddressFromGuid(hostRakNetGuid);
  55. }
  56. RakNetGUID FullyConnectedMesh2::GetHostSystem(void) const
  57. {
  58. if (ourFCMGuid==0)
  59. return rakPeerInterface->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS);
  60. return hostRakNetGuid;
  61. }
  62. bool FullyConnectedMesh2::IsHostSystem(void) const
  63. {
  64. return GetHostSystem()==rakPeerInterface->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS);
  65. }
  66. void FullyConnectedMesh2::GetHostOrder(DataStructures::List<RakNetGUID> &hostList)
  67. {
  68. hostList.Clear(true, _FILE_AND_LINE_);
  69. if (ourFCMGuid==0 || fcm2ParticipantList.Size()==0)
  70. {
  71. hostList.Push(rakPeerInterface->GetMyGUID(), _FILE_AND_LINE_);
  72. return;
  73. }
  74. FCM2Participant fcm2;
  75. fcm2.fcm2Guid=ourFCMGuid;
  76. fcm2.rakNetGuid=rakPeerInterface->GetMyGUID();
  77. DataStructures::OrderedList<FCM2Participant*, FCM2Participant*, FCM2ParticipantComp> olist;
  78. olist.Insert(&fcm2, &fcm2, true, _FILE_AND_LINE_);
  79. for (unsigned int i=0; i < fcm2ParticipantList.Size(); i++)
  80. olist.Insert(fcm2ParticipantList[i], fcm2ParticipantList[i], true, _FILE_AND_LINE_);
  81. for (unsigned int i=0; i < olist.Size(); i++)
  82. {
  83. hostList.Push(olist[i]->rakNetGuid, _FILE_AND_LINE_);
  84. }
  85. }
  86. bool FullyConnectedMesh2::IsConnectedHost(void) const
  87. {
  88. return GetConnectedHost()==rakPeerInterface->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS);
  89. }
  90. void FullyConnectedMesh2::SetAutoparticipateConnections(bool b)
  91. {
  92. autoParticipateConnections=b;
  93. }
  94. void FullyConnectedMesh2::ResetHostCalculation(void)
  95. {
  96. hostRakNetGuid=UNASSIGNED_RAKNET_GUID;
  97. startupTime=RakNet::GetTimeUS();
  98. totalConnectionCount=0;
  99. ourFCMGuid=0;
  100. for (unsigned int i=0; i < fcm2ParticipantList.Size(); i++)
  101. SendFCMGuidRequest(fcm2ParticipantList[i]->rakNetGuid);
  102. }
  103. // bool FullyConnectedMesh2::AddParticipantInternal( RakNetGUID rakNetGuid, FCM2Guid theirFCMGuid, BitStream *userContext )
  104. bool FullyConnectedMesh2::AddParticipantInternal( RakNetGUID rakNetGuid, FCM2Guid theirFCMGuid )
  105. {
  106. for (unsigned int i=0; i < fcm2ParticipantList.Size(); i++)
  107. {
  108. if (fcm2ParticipantList[i]->rakNetGuid==rakNetGuid)
  109. {
  110. if (theirFCMGuid!=0)
  111. fcm2ParticipantList[i]->fcm2Guid=theirFCMGuid;
  112. /*
  113. fcm2ParticipantList[i]->userContext.Reset();
  114. if (userContext)
  115. {
  116. userContext->ResetReadPointer();
  117. fcm2ParticipantList[i]->userContext.Write(userContext);
  118. }
  119. */
  120. return false;
  121. }
  122. }
  123. FCM2Participant *participant = RakNet::OP_NEW<FCM2Participant>(_FILE_AND_LINE_);
  124. participant->rakNetGuid=rakNetGuid;
  125. participant->fcm2Guid=theirFCMGuid;
  126. /*
  127. if (userContext)
  128. {
  129. userContext->ResetReadPointer();
  130. participant->userContext.Write(userContext);
  131. }
  132. */
  133. fcm2ParticipantList.Push(participant,_FILE_AND_LINE_);
  134. SendFCMGuidRequest(rakNetGuid);
  135. return true;
  136. }
  137. void FullyConnectedMesh2::AddParticipant( RakNetGUID rakNetGuid )
  138. {
  139. if (rakPeerInterface->GetConnectionState(rakPeerInterface->GetSystemAddressFromGuid(rakNetGuid))!=IS_CONNECTED)
  140. {
  141. #ifdef DEBUG_FCM2
  142. printf("AddParticipant to %s failed (not connected)\n", rakNetGuid.ToString());
  143. #endif
  144. return;
  145. }
  146. // Need to query other system for userdata before calling AddParticipantInternal
  147. // But maybe I can call with no data, and piggyback on ID_FCM2_REQUEST_FCMGUID
  148. //AddParticipantInternal(rakNetGuid,0,0);
  149. AddParticipantInternal(rakNetGuid,0);
  150. }
  151. void FullyConnectedMesh2::GetParticipantList(DataStructures::List<RakNetGUID> &participantList)
  152. {
  153. participantList.Clear(true, _FILE_AND_LINE_);
  154. unsigned int i;
  155. for (i=0; i < fcm2ParticipantList.Size(); i++)
  156. participantList.Push(fcm2ParticipantList[i]->rakNetGuid, _FILE_AND_LINE_);
  157. }
  158. bool FullyConnectedMesh2::HasParticipant(RakNetGUID participantGuid)
  159. {
  160. unsigned int i;
  161. for (i=0; i < fcm2ParticipantList.Size(); i++)
  162. {
  163. if (fcm2ParticipantList[i]->rakNetGuid==participantGuid)
  164. return true;
  165. }
  166. return false;
  167. }
  168. /*
  169. bool FullyConnectedMesh2::GetParticipantContext(RakNetGUID participantGuid, BitStream *userContext)
  170. {
  171. unsigned int i;
  172. for (i=0; i < fcm2ParticipantList.Size(); i++)
  173. {
  174. if (fcm2ParticipantList[i]->rakNetGuid==participantGuid)
  175. {
  176. if (fcm2ParticipantList[i]->userContext.GetNumberOfBitsUsed() > 0)
  177. {
  178. userContext->Write(fcm2ParticipantList[i]->userContext);
  179. fcm2ParticipantList[i]->userContext.ResetReadPointer();
  180. return true;
  181. }
  182. return false;
  183. }
  184. }
  185. return false;
  186. }
  187. void FullyConnectedMesh2::SetMyContext(BitStream *userContext)
  188. {
  189. if (userContext==0)
  190. {
  191. if (myContext.GetNumberOfBitsUsed()==0)
  192. return;
  193. myContext.Reset();
  194. }
  195. else
  196. {
  197. myContext.Write(userContext);
  198. userContext->ResetReadPointer();
  199. }
  200. RakNet::BitStream bsOut;
  201. bsOut.Write((MessageID)ID_FCM2_UPDATE_USER_CONTEXT);
  202. bsOut.Write(myContext);
  203. myContext.ResetReadPointer();
  204. unsigned int idx;
  205. for (idx=0; idx < fcm2ParticipantList.Size(); idx++)
  206. {
  207. rakPeerInterface->Send(&bsOut,HIGH_PRIORITY,RELIABLE_ORDERED,0,fcm2ParticipantList[idx]->rakNetGuid,false);
  208. }
  209. }
  210. */
  211. PluginReceiveResult FullyConnectedMesh2::OnReceive(Packet *packet)
  212. {
  213. switch (packet->data[0])
  214. {
  215. case ID_REMOTE_NEW_INCOMING_CONNECTION:
  216. {
  217. if (connectOnNewRemoteConnections)
  218. ConnectToRemoteNewIncomingConnections(packet);
  219. }
  220. break;
  221. case ID_FCM2_REQUEST_FCMGUID:
  222. OnRequestFCMGuid(packet);
  223. return RR_STOP_PROCESSING_AND_DEALLOCATE;
  224. //case ID_FCM2_UPDATE_USER_CONTEXT:
  225. // OnUpdateUserContext(packet);
  226. // return RR_STOP_PROCESSING_AND_DEALLOCATE;
  227. case ID_FCM2_RESPOND_CONNECTION_COUNT:
  228. OnRespondConnectionCount(packet);
  229. return RR_STOP_PROCESSING_AND_DEALLOCATE;
  230. case ID_FCM2_INFORM_FCMGUID:
  231. OnInformFCMGuid(packet);
  232. return RR_STOP_PROCESSING_AND_DEALLOCATE;
  233. case ID_FCM2_UPDATE_MIN_TOTAL_CONNECTION_COUNT:
  234. OnUpdateMinTotalConnectionCount(packet);
  235. return RR_STOP_PROCESSING_AND_DEALLOCATE;
  236. case ID_FCM2_NEW_HOST:
  237. if (packet->wasGeneratedLocally==false)
  238. return RR_STOP_PROCESSING_AND_DEALLOCATE;
  239. break;
  240. case ID_FCM2_VERIFIED_JOIN_START:
  241. return OnVerifiedJoinStart(packet);
  242. case ID_FCM2_VERIFIED_JOIN_CAPABLE:
  243. return OnVerifiedJoinCapable(packet);
  244. case ID_FCM2_VERIFIED_JOIN_FAILED:
  245. OnVerifiedJoinFailed(packet->guid, true);
  246. return RR_CONTINUE_PROCESSING;
  247. case ID_FCM2_VERIFIED_JOIN_ACCEPTED:
  248. if (packet->wasGeneratedLocally==false)
  249. OnVerifiedJoinAccepted(packet);
  250. return RR_CONTINUE_PROCESSING;
  251. case ID_FCM2_VERIFIED_JOIN_REJECTED:
  252. OnVerifiedJoinRejected(packet);
  253. return RR_CONTINUE_PROCESSING;
  254. case ID_NAT_TARGET_UNRESPONSIVE:
  255. case ID_NAT_TARGET_NOT_CONNECTED:
  256. case ID_NAT_CONNECTION_TO_TARGET_LOST:
  257. {
  258. RakNet::RakNetGUID g;
  259. RakNet::BitStream b(packet->data, packet->length, false);
  260. b.IgnoreBits(8); // Ignore the ID_...
  261. b.Read(g);
  262. UpdateVerifiedJoinInProgressMember(g, UNASSIGNED_RAKNET_GUID, JIPS_FAILED);
  263. return RR_CONTINUE_PROCESSING;
  264. }
  265. case ID_NAT_PUNCHTHROUGH_FAILED:
  266. UpdateVerifiedJoinInProgressMember(packet->guid, UNASSIGNED_RAKNET_GUID, JIPS_FAILED);
  267. return RR_CONTINUE_PROCESSING;
  268. }
  269. return RR_CONTINUE_PROCESSING;
  270. }
  271. void FullyConnectedMesh2::OnRakPeerStartup(void)
  272. {
  273. Clear();
  274. startupTime=RakNet::GetTimeUS();
  275. }
  276. void FullyConnectedMesh2::OnAttach(void)
  277. {
  278. Clear();
  279. // In case Startup() was called first
  280. if (rakPeerInterface->IsActive())
  281. startupTime=RakNet::GetTimeUS();
  282. }
  283. void FullyConnectedMesh2::OnRakPeerShutdown(void)
  284. {
  285. Clear();
  286. startupTime=0;
  287. }
  288. void FullyConnectedMesh2::OnClosedConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, PI2_LostConnectionReason lostConnectionReason )
  289. {
  290. (void) lostConnectionReason;
  291. (void) systemAddress;
  292. (void) rakNetGUID;
  293. unsigned int idx;
  294. idx=0;
  295. while (idx < joinsInProgress.Size())
  296. {
  297. if (joinsInProgress[idx]->requester==rakNetGUID)
  298. {
  299. Packet *p = AllocatePacketUnified(sizeof(MessageID)+sizeof(unsigned char));
  300. p->data[0]=ID_FCM2_VERIFIED_JOIN_FAILED;
  301. p->systemAddress=systemAddress;
  302. p->systemAddress.systemIndex=(SystemIndex)-1;
  303. p->guid=rakNetGUID;
  304. p->wasGeneratedLocally=true;
  305. rakPeerInterface->PushBackPacket(p, true);
  306. for (unsigned int j=0; j < joinsInProgress[idx]->vjipMembers.Size(); j++)
  307. {
  308. if ( joinsInProgress[idx]->vjipMembers[j].userData != 0)
  309. {
  310. RakNet::OP_DELETE(joinsInProgress[idx]->vjipMembers[j].userData, _FILE_AND_LINE_);
  311. }
  312. }
  313. RakNet::OP_DELETE(joinsInProgress[idx], _FILE_AND_LINE_);
  314. joinsInProgress.RemoveAtIndex(idx);
  315. }
  316. else
  317. {
  318. idx++;
  319. }
  320. }
  321. UpdateVerifiedJoinInProgressMember(rakNetGUID, UNASSIGNED_RAKNET_GUID, JIPS_FAILED);
  322. for (idx=0; idx < fcm2ParticipantList.Size(); idx++)
  323. {
  324. if (fcm2ParticipantList[idx]->rakNetGuid==rakNetGUID)
  325. {
  326. fcm2ParticipantList[idx]=fcm2ParticipantList[fcm2ParticipantList.Size()-1];
  327. #ifdef DEBUG_FCM2
  328. printf("Popping participant %s\n", fcm2ParticipantList[fcm2ParticipantList.Size()-1].rakNetGuid.ToString());
  329. #endif
  330. fcm2ParticipantList.Pop();
  331. if (rakNetGUID==hostRakNetGuid && ourFCMGuid!=0)
  332. {
  333. if (fcm2ParticipantList.Size()==0)
  334. {
  335. hostRakNetGuid=rakPeerInterface->GetMyGUID();
  336. hostFCM2Guid=ourFCMGuid;
  337. }
  338. else
  339. {
  340. CalculateHost(&hostRakNetGuid, &hostFCM2Guid);
  341. }
  342. PushNewHost(hostRakNetGuid, rakNetGUID);
  343. }
  344. return;
  345. }
  346. }
  347. }
  348. RakNet::TimeUS FullyConnectedMesh2::GetElapsedRuntime(void)
  349. {
  350. RakNet::TimeUS curTime=RakNet::GetTimeUS();
  351. if (curTime>startupTime)
  352. return curTime-startupTime;
  353. else
  354. return 0;
  355. }
  356. void FullyConnectedMesh2::OnNewConnection(const SystemAddress &systemAddress, RakNetGUID rakNetGUID, bool isIncoming)
  357. {
  358. (void) isIncoming;
  359. (void) rakNetGUID;
  360. (void) systemAddress;
  361. UpdateVerifiedJoinInProgressMember(rakNetGUID, rakNetGUID, JIPS_CONNECTED);
  362. if (autoParticipateConnections)
  363. AddParticipant(rakNetGUID);
  364. }
  365. void FullyConnectedMesh2::OnFailedConnectionAttempt(Packet *packet, PI2_FailedConnectionAttemptReason failedConnectionAttemptReason)
  366. {
  367. if (failedConnectionAttemptReason==FCAR_ALREADY_CONNECTED)
  368. {
  369. UpdateVerifiedJoinInProgressMember(packet->guid, packet->guid, JIPS_CONNECTED);
  370. }
  371. else
  372. {
  373. UpdateVerifiedJoinInProgressMember(packet->systemAddress, UNASSIGNED_RAKNET_GUID, JIPS_FAILED);
  374. }
  375. }
  376. void FullyConnectedMesh2::Clear(void)
  377. {
  378. for (unsigned int i=0; i < fcm2ParticipantList.Size(); i++)
  379. {
  380. delete fcm2ParticipantList[i];
  381. }
  382. fcm2ParticipantList.Clear(false, _FILE_AND_LINE_);
  383. for (unsigned int i=0; i < joinsInProgress.Size(); i++)
  384. {
  385. for (unsigned int j=0; j < joinsInProgress[i]->vjipMembers.Size(); j++)
  386. {
  387. if ( joinsInProgress[i]->vjipMembers[j].userData != 0)
  388. {
  389. RakNet::OP_DELETE(joinsInProgress[i]->vjipMembers[j].userData, _FILE_AND_LINE_);
  390. }
  391. }
  392. RakNet::OP_DELETE(joinsInProgress[i], _FILE_AND_LINE_);
  393. }
  394. joinsInProgress.Clear(true, _FILE_AND_LINE_);
  395. totalConnectionCount=0;
  396. ourFCMGuid=0;
  397. lastPushedHost=UNASSIGNED_RAKNET_GUID;
  398. }
  399. void FullyConnectedMesh2::PushNewHost(const RakNetGUID &guid, RakNetGUID oldHost)
  400. {
  401. Packet *p = AllocatePacketUnified(sizeof(MessageID)+sizeof(oldHost));
  402. RakNet::BitStream bs(p->data,p->length,false);
  403. bs.SetWriteOffset(0);
  404. bs.Write((MessageID)ID_FCM2_NEW_HOST);
  405. bs.Write(oldHost);
  406. p->systemAddress=rakPeerInterface->GetSystemAddressFromGuid(guid);
  407. p->systemAddress.systemIndex=(SystemIndex)-1;
  408. p->guid=guid;
  409. p->wasGeneratedLocally=true;
  410. rakPeerInterface->PushBackPacket(p, true);
  411. lastPushedHost=guid;
  412. }
  413. void FullyConnectedMesh2::SendFCMGuidRequest(RakNetGUID rakNetGuid)
  414. {
  415. if (rakNetGuid==rakPeerInterface->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS))
  416. return;
  417. RakNet::BitStream bsOut;
  418. bsOut.Write((MessageID)ID_FCM2_REQUEST_FCMGUID);
  419. if (ourFCMGuid==0)
  420. {
  421. bsOut.Write(false);
  422. bsOut.Write(GetElapsedRuntime());
  423. }
  424. else
  425. {
  426. bsOut.Write(true);
  427. bsOut.Write(totalConnectionCount);
  428. bsOut.Write(ourFCMGuid);
  429. }
  430. bsOut.Write(myContext);
  431. myContext.ResetReadPointer();
  432. rakPeerInterface->Send(&bsOut,HIGH_PRIORITY,RELIABLE_ORDERED,0,rakNetGuid,false);
  433. }
  434. void FullyConnectedMesh2::SendOurFCMGuid(SystemAddress addr)
  435. {
  436. RakNet::BitStream bsOut;
  437. bsOut.Write((MessageID)ID_FCM2_INFORM_FCMGUID);
  438. RakAssert(ourFCMGuid!=0); // Can't inform others of our FCM2Guid if it's unset!
  439. bsOut.Write(ourFCMGuid);
  440. bsOut.Write(totalConnectionCount);
  441. bsOut.Write(myContext);
  442. myContext.ResetReadPointer();
  443. rakPeerInterface->Send(&bsOut,HIGH_PRIORITY,RELIABLE_ORDERED,0,addr,false);
  444. }
  445. void FullyConnectedMesh2::SendConnectionCountResponse(SystemAddress addr, unsigned int responseTotalConnectionCount)
  446. {
  447. RakNet::BitStream bsOut;
  448. bsOut.Write((MessageID)ID_FCM2_RESPOND_CONNECTION_COUNT);
  449. bsOut.Write(responseTotalConnectionCount);
  450. //bsOut.Write(myContext);
  451. //myContext.ResetReadPointer();
  452. rakPeerInterface->Send(&bsOut,HIGH_PRIORITY,RELIABLE_ORDERED,0,addr,false);
  453. }
  454. void FullyConnectedMesh2::AssignOurFCMGuid(void)
  455. {
  456. // Only assigned once ever
  457. RakAssert(ourFCMGuid==0);
  458. unsigned int randomNumber = randomMT();
  459. randomNumber ^= (unsigned int) (RakNet::GetTimeUS() & 0xFFFFFFFF);
  460. randomNumber ^= (unsigned int) (rakPeerInterface->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS).g & 0xFFFFFFFF);
  461. ourFCMGuid |= randomNumber;
  462. uint64_t reponse64 = totalConnectionCount;
  463. ourFCMGuid |= reponse64<<32;
  464. }
  465. void FullyConnectedMesh2::CalculateHost(RakNetGUID *rakNetGuid, FCM2Guid *fcm2Guid)
  466. {
  467. // Can't calculate host without knowing our own
  468. RakAssert(ourFCMGuid!=0);
  469. // Can't calculate host without being connected to anyone else
  470. RakAssert(fcm2ParticipantList.Size()>0);
  471. // Return the lowest value of all FCM2Guid
  472. FCM2Guid lowestFCMGuid=ourFCMGuid;
  473. // SystemAddress associatedSystemAddress=UNASSIGNED_SYSTEM_ADDRESS;
  474. RakNetGUID associatedRakNetGuid=rakPeerInterface->GetGuidFromSystemAddress(UNASSIGNED_SYSTEM_ADDRESS);
  475. unsigned int idx;
  476. for (idx=0; idx < fcm2ParticipantList.Size(); idx++)
  477. {
  478. if (fcm2ParticipantList[idx]->fcm2Guid!=0 && fcm2ParticipantList[idx]->fcm2Guid<lowestFCMGuid)
  479. {
  480. lowestFCMGuid=fcm2ParticipantList[idx]->fcm2Guid;
  481. associatedRakNetGuid=fcm2ParticipantList[idx]->rakNetGuid;
  482. }
  483. }
  484. *rakNetGuid=associatedRakNetGuid;
  485. *fcm2Guid=lowestFCMGuid;
  486. }
  487. void FullyConnectedMesh2::OnRequestFCMGuid(Packet *packet)
  488. {
  489. RakNet::BitStream bsIn(packet->data,packet->length,false);
  490. bsIn.IgnoreBytes(sizeof(MessageID));
  491. bool hasRemoteFCMGuid=false;
  492. bsIn.Read(hasRemoteFCMGuid);
  493. RakNet::TimeUS senderElapsedRuntime=0;
  494. unsigned int remoteTotalConnectionCount=0;
  495. FCM2Guid theirFCMGuid=0;
  496. if (hasRemoteFCMGuid)
  497. {
  498. bsIn.Read(remoteTotalConnectionCount);
  499. bsIn.Read(theirFCMGuid);
  500. }
  501. else
  502. {
  503. bsIn.Read(senderElapsedRuntime);
  504. }
  505. /*
  506. BitStream remoteContext;
  507. bsIn.Read(remoteContext);
  508. AddParticipantInternal(packet->guid,theirFCMGuid, &remoteContext);
  509. */
  510. AddParticipantInternal(packet->guid,theirFCMGuid);
  511. if (ourFCMGuid==0)
  512. {
  513. if (hasRemoteFCMGuid==false)
  514. {
  515. // Nobody has a fcmGuid
  516. RakNet::TimeUS ourElapsedRuntime = GetElapsedRuntime();
  517. if (ourElapsedRuntime>senderElapsedRuntime)
  518. {
  519. // We are probably host
  520. SendConnectionCountResponse(packet->systemAddress, 2);
  521. }
  522. else
  523. {
  524. // They are probably host
  525. SendConnectionCountResponse(packet->systemAddress, 1);
  526. }
  527. }
  528. else
  529. {
  530. // They have a fcmGuid, we do not
  531. IncrementTotalConnectionCount(remoteTotalConnectionCount+1);
  532. AssignOurFCMGuid();
  533. unsigned int idx;
  534. for (idx=0; idx < fcm2ParticipantList.Size(); idx++)
  535. SendOurFCMGuid(rakPeerInterface->GetSystemAddressFromGuid(fcm2ParticipantList[idx]->rakNetGuid));
  536. }
  537. }
  538. else
  539. {
  540. if (hasRemoteFCMGuid==false)
  541. {
  542. // We have a fcmGuid they do not
  543. SendConnectionCountResponse(packet->systemAddress, totalConnectionCount+1);
  544. }
  545. else
  546. {
  547. // We both have fcmGuids
  548. IncrementTotalConnectionCount(remoteTotalConnectionCount);
  549. SendOurFCMGuid(packet->systemAddress);
  550. }
  551. }
  552. CalculateAndPushHost();
  553. }
  554. /*
  555. void FullyConnectedMesh2::OnUpdateUserContext(Packet *packet)
  556. {
  557. RakNet::BitStream bsIn(packet->data,packet->length,false);
  558. bsIn.IgnoreBytes(sizeof(MessageID));
  559. BitStream remoteContext;
  560. bsIn.Read(remoteContext);
  561. for (unsigned int i=0; i < fcm2ParticipantList.Size(); i++)
  562. {
  563. if (fcm2ParticipantList[i]->rakNetGuid==packet->guid)
  564. {
  565. fcm2ParticipantList[i]->userContext.Reset();
  566. remoteContext.Read(fcm2ParticipantList[i]->userContext);
  567. break;
  568. }
  569. }
  570. }
  571. */
  572. void FullyConnectedMesh2::OnRespondConnectionCount(Packet *packet)
  573. {
  574. RakNet::BitStream bsIn(packet->data,packet->length,false);
  575. bsIn.IgnoreBytes(sizeof(MessageID));
  576. unsigned int responseTotalConnectionCount;
  577. bsIn.Read(responseTotalConnectionCount);
  578. /*
  579. BitStream remoteContext;
  580. bsIn.Read(remoteContext);
  581. for (unsigned int i=0; i < fcm2ParticipantList.Size(); i++)
  582. {
  583. if (fcm2ParticipantList[i]->rakNetGuid==packet->guid)
  584. {
  585. fcm2ParticipantList[i]->userContext.Reset();
  586. remoteContext.Read(fcm2ParticipantList[i]->userContext);
  587. break;
  588. }
  589. }
  590. */
  591. IncrementTotalConnectionCount(responseTotalConnectionCount);
  592. bool wasAssigned;
  593. if (ourFCMGuid==0)
  594. {
  595. wasAssigned=true;
  596. AssignOurFCMGuid();
  597. }
  598. else
  599. wasAssigned=false;
  600. // 1 is returned to give us lower priority, but the actual minimum is 2
  601. IncrementTotalConnectionCount(2);
  602. if (wasAssigned==true)
  603. {
  604. unsigned int idx;
  605. for (idx=0; idx < fcm2ParticipantList.Size(); idx++)
  606. SendOurFCMGuid(rakPeerInterface->GetSystemAddressFromGuid(fcm2ParticipantList[idx]->rakNetGuid));
  607. CalculateAndPushHost();
  608. }
  609. }
  610. void FullyConnectedMesh2::OnInformFCMGuid(Packet *packet)
  611. {
  612. RakNet::BitStream bsIn(packet->data,packet->length,false);
  613. bsIn.IgnoreBytes(sizeof(MessageID));
  614. FCM2Guid theirFCMGuid;
  615. unsigned int theirTotalConnectionCount;
  616. bsIn.Read(theirFCMGuid);
  617. bsIn.Read(theirTotalConnectionCount);
  618. BitStream remoteContext;
  619. bsIn.Read(remoteContext);
  620. IncrementTotalConnectionCount(theirTotalConnectionCount);
  621. //if (AddParticipantInternal(packet->guid,theirFCMGuid, &remoteContext))
  622. if (AddParticipantInternal(packet->guid,theirFCMGuid))
  623. {
  624. // 1/19/2010 - Relay increased total connection count in case new participant only connects to part of the mesh
  625. unsigned int idx;
  626. RakNet::BitStream bsOut;
  627. bsOut.Write((MessageID)ID_FCM2_UPDATE_MIN_TOTAL_CONNECTION_COUNT);
  628. bsOut.Write(totalConnectionCount);
  629. for (idx=0; idx < fcm2ParticipantList.Size(); idx++)
  630. {
  631. if (packet->guid!=fcm2ParticipantList[idx]->rakNetGuid)
  632. rakPeerInterface->Send(&bsOut,HIGH_PRIORITY,RELIABLE_ORDERED,0,fcm2ParticipantList[idx]->rakNetGuid,false);
  633. }
  634. }
  635. if (ourFCMGuid==0)
  636. {
  637. AssignOurFCMGuid();
  638. unsigned int idx;
  639. for (idx=0; idx < fcm2ParticipantList.Size(); idx++)
  640. SendOurFCMGuid(rakPeerInterface->GetSystemAddressFromGuid(fcm2ParticipantList[idx]->rakNetGuid));
  641. }
  642. CalculateAndPushHost();
  643. }
  644. void FullyConnectedMesh2::OnUpdateMinTotalConnectionCount(Packet *packet)
  645. {
  646. RakNet::BitStream bsIn(packet->data,packet->length,false);
  647. bsIn.IgnoreBytes(sizeof(MessageID));
  648. unsigned int newMin;
  649. bsIn.Read(newMin);
  650. IncrementTotalConnectionCount(newMin);
  651. }
  652. void FullyConnectedMesh2::GetParticipantCount(unsigned int *participantListSize) const
  653. {
  654. *participantListSize=fcm2ParticipantList.Size();
  655. }
  656. unsigned int FullyConnectedMesh2::GetParticipantCount(void) const
  657. {
  658. return fcm2ParticipantList.Size();
  659. }
  660. void FullyConnectedMesh2::CalculateAndPushHost(void)
  661. {
  662. RakNetGUID newHostGuid;
  663. FCM2Guid newFcmGuid;
  664. if (ParticipantListComplete())
  665. {
  666. CalculateHost(&newHostGuid, &newFcmGuid);
  667. if (newHostGuid!=lastPushedHost)
  668. {
  669. hostRakNetGuid=newHostGuid;
  670. hostFCM2Guid=newFcmGuid;
  671. PushNewHost(hostRakNetGuid, lastPushedHost);
  672. }
  673. }
  674. }
  675. bool FullyConnectedMesh2::ParticipantListComplete(void)
  676. {
  677. for (unsigned int i=0; i < fcm2ParticipantList.Size(); i++)
  678. {
  679. if (fcm2ParticipantList[i]->fcm2Guid==0)
  680. return false;
  681. }
  682. return true;
  683. }
  684. void FullyConnectedMesh2::IncrementTotalConnectionCount(unsigned int i)
  685. {
  686. if (i>totalConnectionCount)
  687. {
  688. totalConnectionCount=i;
  689. // printf("totalConnectionCount=%i\n",i);
  690. }
  691. }
  692. void FullyConnectedMesh2::SetConnectOnNewRemoteConnection(bool attemptConnection, RakNet::RakString pw)
  693. {
  694. connectOnNewRemoteConnections=attemptConnection;
  695. connectionPassword=pw;
  696. }
  697. void FullyConnectedMesh2::ConnectToRemoteNewIncomingConnections(Packet *packet)
  698. {
  699. unsigned int count;
  700. RakNet::BitStream bsIn(packet->data, packet->length, false);
  701. bsIn.IgnoreBytes(sizeof(MessageID));
  702. bsIn.Read(count);
  703. SystemAddress remoteAddress;
  704. RakNetGUID remoteGuid;
  705. char str[64];
  706. for (unsigned int i=0; i < count; i++)
  707. {
  708. bsIn.Read(remoteAddress);
  709. bsIn.Read(remoteGuid);
  710. remoteAddress.ToString(false,str);
  711. rakPeerInterface->Connect(str,remoteAddress.GetPort(),connectionPassword.C_String(),(int) connectionPassword.GetLength());
  712. }
  713. }
  714. unsigned int FullyConnectedMesh2::GetTotalConnectionCount(void) const
  715. {
  716. return totalConnectionCount;
  717. }
  718. void FullyConnectedMesh2::StartVerifiedJoin(RakNetGUID client)
  719. {
  720. // Assert is because there is no point calling StartVerifiedJoin() if this client is already a participant
  721. RakAssert(HasParticipant(client)==false);
  722. RakAssert(client!=rakPeerInterface->GetMyGUID());
  723. BitStream bsOut;
  724. bsOut.Write((MessageID) ID_FCM2_VERIFIED_JOIN_START);
  725. bsOut.WriteCasted<unsigned short>(fcm2ParticipantList.Size());
  726. unsigned int i;
  727. for (i=0; i < fcm2ParticipantList.Size(); i++)
  728. {
  729. bsOut.Write(fcm2ParticipantList[i]->rakNetGuid);
  730. bsOut.Write(rakPeerInterface->GetSystemAddressFromGuid(fcm2ParticipantList[i]->rakNetGuid));
  731. BitStream vjsOut;
  732. //WriteVJSUserData(&vjsOut, fcm2ParticipantList[i]->rakNetGuid, &fcm2ParticipantList[i]->userContext );
  733. WriteVJSUserData(&vjsOut, fcm2ParticipantList[i]->rakNetGuid );
  734. bsOut.Write(vjsOut.GetNumberOfBitsUsed());
  735. bsOut.Write(&vjsOut);
  736. bsOut.AlignWriteToByteBoundary();
  737. }
  738. SendUnified(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, client, false);
  739. }
  740. void FullyConnectedMesh2::RespondOnVerifiedJoinCapable(Packet *packet, bool accept, BitStream *additionalData)
  741. {
  742. VerifiedJoinInProgress vjip;
  743. DecomposeJoinCapable(packet, &vjip);
  744. DataStructures::List<RakNetGUID> participatingMembersOnClientSucceeded;
  745. DataStructures::List<RakNetGUID> participatingMembersOnClientFailed;
  746. DataStructures::List<RakNetGUID> participatingMembersNotOnClient;
  747. DataStructures::List<RakNetGUID> clientMembersNotParticipatingSucceeded;
  748. DataStructures::List<RakNetGUID> clientMembersNotParticipatingFailed;
  749. CategorizeVJIP(&vjip,
  750. participatingMembersOnClientSucceeded,
  751. participatingMembersOnClientFailed,
  752. participatingMembersNotOnClient,
  753. clientMembersNotParticipatingSucceeded,
  754. clientMembersNotParticipatingFailed);
  755. if (participatingMembersNotOnClient.Size()>0)
  756. {
  757. BitStream bsOut;
  758. bsOut.Write((MessageID) ID_FCM2_VERIFIED_JOIN_START);
  759. bsOut.WriteCasted<unsigned short>(participatingMembersNotOnClient.Size());
  760. unsigned int i;
  761. for (i=0; i < participatingMembersNotOnClient.Size(); i++)
  762. {
  763. bsOut.Write(participatingMembersNotOnClient[i]);
  764. bsOut.Write(rakPeerInterface->GetSystemAddressFromGuid(participatingMembersNotOnClient[i]));
  765. bool written=false;
  766. for (unsigned int j=0; j < fcm2ParticipantList.Size(); j++)
  767. {
  768. if (fcm2ParticipantList[j]->rakNetGuid == participatingMembersNotOnClient[i])
  769. {
  770. written=true;
  771. BitStream vjsOut;
  772. //WriteVJSUserData(&vjsOut, fcm2ParticipantList[j]->rakNetGuid, &fcm2ParticipantList[j]->userContext );
  773. WriteVJSUserData(&vjsOut, fcm2ParticipantList[j]->rakNetGuid );
  774. bsOut.Write(vjsOut.GetNumberOfBitsUsed());
  775. bsOut.Write(&vjsOut);
  776. bsOut.AlignWriteToByteBoundary();
  777. break;
  778. }
  779. }
  780. RakAssert(written==true);
  781. }
  782. SendUnified(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, packet->guid, false);
  783. return;
  784. }
  785. RakAssert(participatingMembersOnClientFailed.Size()==0);
  786. RakAssert(participatingMembersNotOnClient.Size()==0);
  787. RakNet::BitStream bsOut;
  788. if (accept)
  789. {
  790. bsOut.Write((MessageID)ID_FCM2_VERIFIED_JOIN_ACCEPTED);
  791. bsOut.Write(packet->guid);
  792. // Tell client to disconnect from clientMembersNotParticipatingSucceeded
  793. bsOut.WriteCasted<unsigned short>(clientMembersNotParticipatingSucceeded.Size());
  794. for (unsigned int i=0; i < clientMembersNotParticipatingSucceeded.Size(); i++)
  795. bsOut.Write(clientMembersNotParticipatingSucceeded[i]);
  796. // Tell client to call AddParticipant() for participatingMembersOnClientSucceeded
  797. bsOut.WriteCasted<unsigned short>(participatingMembersOnClientSucceeded.Size());
  798. for (unsigned int i=0; i < participatingMembersOnClientSucceeded.Size(); i++)
  799. bsOut.Write(participatingMembersOnClientSucceeded[i]);
  800. if (additionalData)
  801. bsOut.Write(additionalData);
  802. for (unsigned int i=0; i < fcm2ParticipantList.Size(); i++)
  803. SendUnified(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, fcm2ParticipantList[i]->rakNetGuid, false);
  804. // Process immediately
  805. // This is so if another ID_FCM2_VERIFIED_JOIN_CAPABLE is buffered, it responds with ID_FCM2_VERIFIED_JOIN_START
  806. AddParticipant(packet->guid);
  807. Packet *p = AllocatePacketUnified(bsOut.GetNumberOfBytesUsed());
  808. memcpy(p->data, bsOut.GetData(), bsOut.GetNumberOfBytesUsed());
  809. p->systemAddress=packet->systemAddress;
  810. p->systemAddress.systemIndex=(SystemIndex)-1;
  811. p->guid=packet->guid;
  812. p->wasGeneratedLocally=true;
  813. rakPeerInterface->PushBackPacket(p, true);
  814. }
  815. else
  816. {
  817. // Tell client rejected, otherwise process the same as ID_FCM2_VERIFIED_JOIN_FAILED
  818. bsOut.Write((MessageID)ID_FCM2_VERIFIED_JOIN_REJECTED);
  819. if (additionalData)
  820. bsOut.Write(additionalData);
  821. }
  822. SendUnified(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, packet->guid, false);
  823. }
  824. void FullyConnectedMesh2::GetVerifiedJoinRequiredProcessingList(RakNetGUID host, DataStructures::List<SystemAddress> &addresses, DataStructures::List<RakNetGUID> &guids, DataStructures::List<BitStream*> &userData)
  825. {
  826. addresses.Clear(true, _FILE_AND_LINE_);
  827. guids.Clear(true, _FILE_AND_LINE_);
  828. unsigned int curIndex = GetJoinsInProgressIndex(host);
  829. if (curIndex!=(unsigned int) -1)
  830. {
  831. VerifiedJoinInProgress *vjip = joinsInProgress[curIndex];
  832. unsigned int j;
  833. for (j=0; j < vjip->vjipMembers.Size(); j++)
  834. {
  835. if (vjip->vjipMembers[j].joinInProgressState==JIPS_PROCESSING)
  836. {
  837. addresses.Push(vjip->vjipMembers[j].systemAddress, _FILE_AND_LINE_);
  838. guids.Push(vjip->vjipMembers[j].guid, _FILE_AND_LINE_);
  839. userData.Push(vjip->vjipMembers[j].userData, _FILE_AND_LINE_);
  840. }
  841. }
  842. }
  843. }
  844. void FullyConnectedMesh2::GetVerifiedJoinAcceptedAdditionalData(Packet *packet, bool *thisSystemAccepted, DataStructures::List<RakNetGUID> &systemsAccepted, BitStream *additionalData)
  845. {
  846. systemsAccepted.Clear(true, _FILE_AND_LINE_);
  847. RakNet::BitStream bsIn(packet->data, packet->length, false);
  848. bsIn.IgnoreBytes(sizeof(MessageID));
  849. RakNetGUID systemToAddGuid;
  850. bsIn.Read(systemToAddGuid);
  851. *thisSystemAccepted = systemToAddGuid == rakPeerInterface->GetMyGUID();
  852. unsigned short listSize;
  853. bsIn.Read(listSize);
  854. bsIn.IgnoreBytes(listSize*RakNetGUID::size());
  855. bsIn.Read(listSize);
  856. if (systemToAddGuid==rakPeerInterface->GetMyGUID())
  857. {
  858. for (unsigned short i=0; i < listSize; i++)
  859. {
  860. bsIn.Read(systemToAddGuid);
  861. systemsAccepted.Push(systemToAddGuid, _FILE_AND_LINE_);
  862. }
  863. systemsAccepted.Push(packet->guid, _FILE_AND_LINE_);
  864. }
  865. else
  866. {
  867. systemsAccepted.Push(systemToAddGuid, _FILE_AND_LINE_);
  868. bsIn.IgnoreBytes(listSize*RakNetGUID::size());
  869. }
  870. if (additionalData)
  871. {
  872. additionalData->Reset();
  873. additionalData->Write(bsIn);
  874. }
  875. }
  876. void FullyConnectedMesh2::GetVerifiedJoinRejectedAdditionalData(Packet *packet, BitStream *additionalData)
  877. {
  878. RakNet::BitStream bsIn(packet->data, packet->length, false);
  879. bsIn.IgnoreBytes(sizeof(MessageID));
  880. if (additionalData)
  881. {
  882. additionalData->Reset();
  883. additionalData->Write(bsIn);
  884. }
  885. }
  886. PluginReceiveResult FullyConnectedMesh2::OnVerifiedJoinStart(Packet *packet)
  887. {
  888. RakNet::BitStream bsIn(packet->data,packet->length,false);
  889. bsIn.IgnoreBytes(sizeof(MessageID));
  890. unsigned short listSize;
  891. bsIn.Read(listSize);
  892. unsigned int curIndex = GetJoinsInProgressIndex(packet->guid);
  893. if (curIndex!=(unsigned int) -1)
  894. {
  895. // Got update to existing list
  896. VerifiedJoinInProgress *vjip = joinsInProgress[curIndex];
  897. // if (vjip->sentResults==false)
  898. // {
  899. // // Got ID_FCM2_VERIFIED_JOIN_START twice before sending ID_FCM2_VERIFIED_JOIN_CAPABLE
  900. // RakAssert(vjip->sentResults!=false);
  901. // return RR_STOP_PROCESSING_AND_DEALLOCATE;
  902. // }
  903. for (unsigned int i=0; i < vjip->vjipMembers.Size(); i++)
  904. {
  905. vjip->vjipMembers[i].workingFlag=false;
  906. }
  907. // Server has updated list of participants
  908. for (unsigned short i=0; i < listSize; i++)
  909. {
  910. VerifiedJoinInProgressMember vjipm;
  911. ReadVerifiedJoinInProgressMember(&bsIn, &vjipm);
  912. unsigned int j;
  913. if (vjipm.guid!=UNASSIGNED_RAKNET_GUID)
  914. j = GetVerifiedJoinInProgressMemberIndex(vjipm.guid, vjip);
  915. else
  916. j = GetVerifiedJoinInProgressMemberIndex(vjipm.systemAddress, vjip);
  917. if (j==(unsigned int)-1)
  918. {
  919. // New
  920. vjipm.workingFlag=true;
  921. // 11/13/2013 - ReadVerifiedJoinInProgressMember already sets joinInProgressState
  922. // http://www.jenkinssoftware.com/forum/index.php?topic=5211.0
  923. // vjipm.joinInProgressState=JIPS_PROCESSING;
  924. vjip->vjipMembers.Push(vjipm, _FILE_AND_LINE_);
  925. // Allow resend of ID_FCM2_VERIFIED_JOIN_CAPABLE
  926. //vjip->sentResults=false;
  927. }
  928. else
  929. {
  930. vjip->vjipMembers[j].workingFlag=true;
  931. }
  932. }
  933. for (unsigned int i=0; i < vjip->vjipMembers.Size(); i++)
  934. {
  935. if (vjip->vjipMembers[i].workingFlag==false)
  936. vjip->vjipMembers[i].joinInProgressState=JIPS_UNNECESSARY;
  937. }
  938. if (ProcessVerifiedJoinInProgressIfCompleted(vjip))
  939. {
  940. // Completed
  941. return RR_STOP_PROCESSING_AND_DEALLOCATE;
  942. }
  943. // Else tell user about new list
  944. return RR_CONTINUE_PROCESSING;
  945. }
  946. VerifiedJoinInProgress *vjip = RakNet::OP_NEW<VerifiedJoinInProgress>(_FILE_AND_LINE_);
  947. vjip->requester=packet->guid;
  948. if (listSize==0)
  949. {
  950. //vjip->sentResults=true;
  951. // Send back result
  952. RakNet::BitStream bsOut;
  953. bsOut.Write((MessageID)ID_FCM2_VERIFIED_JOIN_CAPABLE);
  954. bsOut.WriteCasted<unsigned short>(0);
  955. WriteVJCUserData(&bsOut);
  956. SendUnified(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, packet->guid, false);
  957. //vjip->sentResults=true;
  958. joinsInProgress.Push(vjip, _FILE_AND_LINE_);
  959. return RR_STOP_PROCESSING_AND_DEALLOCATE;
  960. }
  961. //vjip->sentResults=false;
  962. for (unsigned short i=0; i < listSize; i++)
  963. {
  964. VerifiedJoinInProgressMember vjipm;
  965. ReadVerifiedJoinInProgressMember(&bsIn, &vjipm);
  966. vjip->vjipMembers.Push(vjipm, _FILE_AND_LINE_);
  967. }
  968. joinsInProgress.Push(vjip, _FILE_AND_LINE_);
  969. // 11/13/2013 - ReadVerifiedJoinInProgressMember may set JIPS_CONNECTED, so this may already be done
  970. // http://www.jenkinssoftware.com/forum/index.php?topic=5211.0
  971. if (ProcessVerifiedJoinInProgressIfCompleted(vjip))
  972. {
  973. // Completed
  974. return RR_STOP_PROCESSING_AND_DEALLOCATE;
  975. }
  976. return RR_CONTINUE_PROCESSING;
  977. }
  978. void FullyConnectedMesh2::SkipToVJCUserData(RakNet::BitStream *bsIn)
  979. {
  980. bsIn->IgnoreBytes(sizeof(MessageID));
  981. unsigned short listSize;
  982. bsIn->Read(listSize);
  983. for (unsigned short i=0; i < listSize; i++)
  984. {
  985. bsIn->IgnoreBytes(RakNetGUID::size());
  986. bsIn->IgnoreBytes(SystemAddress::size());
  987. bsIn->IgnoreBytes(sizeof(unsigned char));
  988. }
  989. }
  990. void FullyConnectedMesh2::DecomposeJoinCapable(Packet *packet, VerifiedJoinInProgress *vjip)
  991. {
  992. RakNet::BitStream bsIn(packet->data,packet->length,false);
  993. bsIn.IgnoreBytes(sizeof(MessageID));
  994. unsigned short listSize;
  995. bsIn.Read(listSize);
  996. for (unsigned short i=0; i < listSize; i++)
  997. {
  998. VerifiedJoinInProgressMember member;
  999. bsIn.Read(member.guid);
  1000. bsIn.Read(member.systemAddress);
  1001. bsIn.ReadCasted<unsigned char>(member.joinInProgressState);
  1002. member.userData = 0;
  1003. member.workingFlag=false;
  1004. vjip->vjipMembers.Push(member, _FILE_AND_LINE_);
  1005. }
  1006. }
  1007. PluginReceiveResult FullyConnectedMesh2::OnVerifiedJoinCapable(Packet *packet)
  1008. {
  1009. VerifiedJoinInProgress vjip;
  1010. DecomposeJoinCapable(packet, &vjip);
  1011. // If this assert hits, AddParticipant() was called on this system, or another system, which it should not have been.
  1012. RakAssert(HasParticipant(packet->guid)==false);
  1013. DataStructures::List<RakNetGUID> participatingMembersOnClientSucceeded;
  1014. DataStructures::List<RakNetGUID> participatingMembersOnClientFailed;
  1015. DataStructures::List<RakNetGUID> participatingMembersNotOnClient;
  1016. DataStructures::List<RakNetGUID> clientMembersNotParticipatingSucceeded;
  1017. DataStructures::List<RakNetGUID> clientMembersNotParticipatingFailed;
  1018. CategorizeVJIP(&vjip,
  1019. participatingMembersOnClientSucceeded,
  1020. participatingMembersOnClientFailed,
  1021. participatingMembersNotOnClient,
  1022. clientMembersNotParticipatingSucceeded,
  1023. clientMembersNotParticipatingFailed);
  1024. if (participatingMembersOnClientFailed.Size()>0)
  1025. {
  1026. // Send ID_FCM2_VERIFIED_JOIN_FAILED with GUIDs to disconnect
  1027. BitStream bsOut;
  1028. bsOut.Write((MessageID) ID_FCM2_VERIFIED_JOIN_FAILED);
  1029. SendUnified(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, packet->guid, false);
  1030. return RR_STOP_PROCESSING_AND_DEALLOCATE;
  1031. }
  1032. if (participatingMembersNotOnClient.Size()>0)
  1033. {
  1034. BitStream bsOut;
  1035. bsOut.Write((MessageID) ID_FCM2_VERIFIED_JOIN_START);
  1036. bsOut.WriteCasted<unsigned short>(participatingMembersNotOnClient.Size());
  1037. unsigned int i;
  1038. for (i=0; i < participatingMembersNotOnClient.Size(); i++)
  1039. {
  1040. bsOut.Write(participatingMembersNotOnClient[i]);
  1041. bsOut.Write(rakPeerInterface->GetSystemAddressFromGuid(participatingMembersNotOnClient[i]));
  1042. bool written=false;
  1043. for (unsigned int j=0; j < fcm2ParticipantList.Size(); j++)
  1044. {
  1045. if (fcm2ParticipantList[j]->rakNetGuid == participatingMembersNotOnClient[i])
  1046. {
  1047. written=true;
  1048. BitStream vjsOut;
  1049. //WriteVJSUserData(&vjsOut, fcm2ParticipantList[j]->rakNetGuid, &fcm2ParticipantList[j]->userContext );
  1050. WriteVJSUserData(&vjsOut, fcm2ParticipantList[j]->rakNetGuid );
  1051. bsOut.Write(vjsOut.GetNumberOfBitsUsed());
  1052. bsOut.Write(&vjsOut);
  1053. bsOut.AlignWriteToByteBoundary();
  1054. break;
  1055. }
  1056. }
  1057. RakAssert(written);
  1058. }
  1059. SendUnified(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, packet->guid, false);
  1060. return RR_STOP_PROCESSING_AND_DEALLOCATE;
  1061. }
  1062. // Let server decide if to accept or reject via RespondOnVerifiedJoinCapable
  1063. return RR_CONTINUE_PROCESSING;
  1064. }
  1065. void FullyConnectedMesh2::OnVerifiedJoinFailed(RakNetGUID hostGuid, bool callCloseConnection)
  1066. {
  1067. unsigned int curIndex = GetJoinsInProgressIndex(hostGuid);
  1068. if (curIndex==(unsigned int) -1)
  1069. return;
  1070. if (callCloseConnection)
  1071. {
  1072. VerifiedJoinInProgress *vjip = joinsInProgress[curIndex];
  1073. for (unsigned int j=0; j < vjip->vjipMembers.Size(); j++)
  1074. {
  1075. if (vjip->vjipMembers[j].joinInProgressState!=JIPS_FAILED)
  1076. {
  1077. rakPeerInterface->CloseConnection(vjip->vjipMembers[j].guid, true);
  1078. }
  1079. if (vjip->vjipMembers[j].userData != 0)
  1080. RakNet::OP_DELETE(vjip->vjipMembers[j].userData, _FILE_AND_LINE_);
  1081. }
  1082. }
  1083. for (unsigned int j=0; j < joinsInProgress[curIndex]->vjipMembers.Size(); j++)
  1084. {
  1085. if ( joinsInProgress[curIndex]->vjipMembers[j].userData != 0)
  1086. {
  1087. RakNet::OP_DELETE(joinsInProgress[curIndex]->vjipMembers[j].userData, _FILE_AND_LINE_);
  1088. }
  1089. }
  1090. // Clear joinsInProgress for packet->guid
  1091. RakNet::OP_DELETE(joinsInProgress[curIndex], _FILE_AND_LINE_);
  1092. joinsInProgress.RemoveAtIndex(curIndex);
  1093. }
  1094. void FullyConnectedMesh2::OnVerifiedJoinAccepted(Packet *packet)
  1095. {
  1096. RakNet::BitStream bsIn(packet->data,packet->length,false);
  1097. bsIn.IgnoreBytes(sizeof(MessageID));
  1098. RakNetGUID systemToAddGuid;
  1099. bsIn.Read(systemToAddGuid);
  1100. if (systemToAddGuid==rakPeerInterface->GetMyGUID())
  1101. {
  1102. // My own system
  1103. unsigned int curIndex = GetJoinsInProgressIndex(packet->guid);
  1104. if (curIndex==(unsigned int)-1)
  1105. return;
  1106. unsigned short listSize;
  1107. bsIn.Read(listSize);
  1108. for (unsigned short i=0; i < listSize; i++)
  1109. {
  1110. // List of clientMembersNotParticipatingSucceeded
  1111. RakNetGUID guid;
  1112. bsIn.Read(guid);
  1113. rakPeerInterface->CloseConnection(guid, true);
  1114. }
  1115. bsIn.Read(listSize);
  1116. for (unsigned short i=0; i < listSize; i++)
  1117. {
  1118. // List of participatingMembersOnClientSucceeded
  1119. RakNetGUID guid;
  1120. bsIn.Read(guid);
  1121. AddParticipant(guid);
  1122. }
  1123. AddParticipant(packet->guid);
  1124. for (unsigned int j=0; j < joinsInProgress[curIndex]->vjipMembers.Size(); j++)
  1125. {
  1126. if ( joinsInProgress[curIndex]->vjipMembers[j].userData != 0)
  1127. {
  1128. RakNet::OP_DELETE(joinsInProgress[curIndex]->vjipMembers[j].userData, _FILE_AND_LINE_);
  1129. }
  1130. }
  1131. // Clear joinsInProgress for packet->guid
  1132. RakNet::OP_DELETE(joinsInProgress[curIndex], _FILE_AND_LINE_);
  1133. joinsInProgress.RemoveAtIndex(curIndex);
  1134. }
  1135. else
  1136. {
  1137. // Another system
  1138. ConnectionState cs = rakPeerInterface->GetConnectionState(systemToAddGuid);
  1139. RakAssert(cs==IS_CONNECTED);
  1140. if (cs==IS_CONNECTED)
  1141. AddParticipant(systemToAddGuid);
  1142. }
  1143. }
  1144. void FullyConnectedMesh2::OnVerifiedJoinRejected(Packet *packet)
  1145. {
  1146. OnVerifiedJoinFailed(packet->guid, true);
  1147. }
  1148. unsigned int FullyConnectedMesh2::GetJoinsInProgressIndex(RakNetGUID requester) const
  1149. {
  1150. for (unsigned int i=0; i < joinsInProgress.Size(); i++)
  1151. {
  1152. if (joinsInProgress[i]->requester==requester)
  1153. return i;
  1154. }
  1155. return (unsigned int) -1;
  1156. }
  1157. void FullyConnectedMesh2::UpdateVerifiedJoinInProgressMember(const AddressOrGUID systemIdentifier, RakNetGUID guidToAssign, FullyConnectedMesh2::JoinInProgressState newState)
  1158. {
  1159. bool anythingChanged;
  1160. for (unsigned int i=0; i < joinsInProgress.Size(); i++)
  1161. {
  1162. VerifiedJoinInProgress *vjip = joinsInProgress[i];
  1163. //if (vjip->sentResults==true)
  1164. // continue;
  1165. anythingChanged=false;
  1166. unsigned int j;
  1167. j = GetVerifiedJoinInProgressMemberIndex(systemIdentifier, vjip);
  1168. if (j!=(unsigned int)-1)
  1169. {
  1170. if (vjip->vjipMembers[j].guid==UNASSIGNED_RAKNET_GUID && guidToAssign!=UNASSIGNED_RAKNET_GUID)
  1171. vjip->vjipMembers[j].guid = guidToAssign;
  1172. if (vjip->vjipMembers[j].joinInProgressState==JIPS_PROCESSING)
  1173. {
  1174. anythingChanged=true;
  1175. vjip->vjipMembers[j].joinInProgressState=newState;
  1176. }
  1177. }
  1178. if (anythingChanged)
  1179. {
  1180. ProcessVerifiedJoinInProgressIfCompleted(vjip);
  1181. }
  1182. }
  1183. }
  1184. bool FullyConnectedMesh2::ProcessVerifiedJoinInProgressIfCompleted(VerifiedJoinInProgress *vjip)
  1185. {
  1186. //if (vjip->sentResults)
  1187. // return true;
  1188. // If no systems in processing state, send results to server
  1189. // Return true if this was done
  1190. bool anyProcessing=false;
  1191. for (unsigned int i=0; i < vjip->vjipMembers.Size(); i++)
  1192. {
  1193. if (vjip->vjipMembers[i].joinInProgressState==JIPS_PROCESSING)
  1194. {
  1195. anyProcessing=true;
  1196. break;
  1197. }
  1198. }
  1199. if (anyProcessing==true)
  1200. return false;
  1201. // Send results to server
  1202. BitStream bsOut;
  1203. WriteVerifiedJoinCapable(&bsOut, vjip);
  1204. WriteVJCUserData(&bsOut);
  1205. SendUnified(&bsOut, HIGH_PRIORITY, RELIABLE_ORDERED, 0, vjip->requester, false);
  1206. //vjip->sentResults=true;
  1207. return true;
  1208. }
  1209. void FullyConnectedMesh2::WriteVerifiedJoinCapable(RakNet::BitStream *bsOut, VerifiedJoinInProgress *vjip)
  1210. {
  1211. bsOut->Write((MessageID) ID_FCM2_VERIFIED_JOIN_CAPABLE);
  1212. bsOut->WriteCasted<unsigned short>(vjip->vjipMembers.Size());
  1213. unsigned int i;
  1214. for (i=0; i < vjip->vjipMembers.Size(); i++)
  1215. {
  1216. bsOut->Write(vjip->vjipMembers[i].guid);
  1217. bsOut->Write(vjip->vjipMembers[i].systemAddress);
  1218. bsOut->WriteCasted<unsigned char>(vjip->vjipMembers[i].joinInProgressState);
  1219. }
  1220. }
  1221. void FullyConnectedMesh2::ReadVerifiedJoinInProgressMember(RakNet::BitStream *bsIn, VerifiedJoinInProgressMember *vjipm)
  1222. {
  1223. bsIn->Read(vjipm->guid);
  1224. bsIn->Read(vjipm->systemAddress);
  1225. ConnectionState cs = rakPeerInterface->GetConnectionState(vjipm->guid);
  1226. if (cs==IS_CONNECTED)
  1227. vjipm->joinInProgressState=JIPS_CONNECTED;
  1228. else if (cs==IS_DISCONNECTING || cs==IS_SILENTLY_DISCONNECTING)
  1229. vjipm->joinInProgressState=JIPS_FAILED;
  1230. else
  1231. vjipm->joinInProgressState=JIPS_PROCESSING;
  1232. BitSize_t vjsUserDataSize;
  1233. bsIn->Read(vjsUserDataSize);
  1234. if (vjsUserDataSize > 0)
  1235. {
  1236. vjipm->userData = RakNet::OP_NEW<BitStream>(_FILE_AND_LINE_);
  1237. bsIn->Read(vjipm->userData, vjsUserDataSize);
  1238. }
  1239. else
  1240. vjipm->userData = 0;
  1241. bsIn->AlignReadToByteBoundary();
  1242. }
  1243. unsigned int FullyConnectedMesh2::GetVerifiedJoinInProgressMemberIndex(const AddressOrGUID systemIdentifier, VerifiedJoinInProgress *vjip)
  1244. {
  1245. for (unsigned int j=0; j < vjip->vjipMembers.Size(); j++)
  1246. {
  1247. if ((systemIdentifier.rakNetGuid!=UNASSIGNED_RAKNET_GUID && vjip->vjipMembers[j].guid==systemIdentifier.rakNetGuid) ||
  1248. (systemIdentifier.systemAddress!=UNASSIGNED_SYSTEM_ADDRESS && vjip->vjipMembers[j].systemAddress==systemIdentifier.systemAddress))
  1249. {
  1250. return j;
  1251. }
  1252. }
  1253. return (unsigned int) -1;
  1254. }
  1255. void FullyConnectedMesh2::CategorizeVJIP(VerifiedJoinInProgress *vjip,
  1256. DataStructures::List<RakNetGUID> &participatingMembersOnClientSucceeded,
  1257. DataStructures::List<RakNetGUID> &participatingMembersOnClientFailed,
  1258. DataStructures::List<RakNetGUID> &participatingMembersNotOnClient,
  1259. DataStructures::List<RakNetGUID> &clientMembersNotParticipatingSucceeded,
  1260. DataStructures::List<RakNetGUID> &clientMembersNotParticipatingFailed)
  1261. {
  1262. for (unsigned int i=0; i < vjip->vjipMembers.Size(); i++)
  1263. vjip->vjipMembers[i].workingFlag=false;
  1264. for (unsigned int i=0; i < fcm2ParticipantList.Size(); i++)
  1265. {
  1266. unsigned int j = GetVerifiedJoinInProgressMemberIndex(fcm2ParticipantList[i]->rakNetGuid, vjip);
  1267. if (j==(unsigned int)-1)
  1268. {
  1269. participatingMembersNotOnClient.Push(fcm2ParticipantList[i]->rakNetGuid, _FILE_AND_LINE_);
  1270. }
  1271. else
  1272. {
  1273. if (vjip->vjipMembers[j].joinInProgressState==JIPS_FAILED)
  1274. participatingMembersOnClientFailed.Push(fcm2ParticipantList[i]->rakNetGuid, _FILE_AND_LINE_);
  1275. else
  1276. participatingMembersOnClientSucceeded.Push(fcm2ParticipantList[i]->rakNetGuid, _FILE_AND_LINE_);
  1277. vjip->vjipMembers[j].workingFlag=true;
  1278. }
  1279. }
  1280. for (unsigned int j=0; j < vjip->vjipMembers.Size(); j++)
  1281. {
  1282. if (vjip->vjipMembers[j].workingFlag==false)
  1283. {
  1284. if (vjip->vjipMembers[j].joinInProgressState==JIPS_FAILED)
  1285. clientMembersNotParticipatingFailed.Push(vjip->vjipMembers[j].guid, _FILE_AND_LINE_);
  1286. else
  1287. clientMembersNotParticipatingSucceeded.Push(vjip->vjipMembers[j].guid, _FILE_AND_LINE_);
  1288. }
  1289. }
  1290. }
  1291. #endif // _RAKNET_SUPPORT_*
粤ICP备19079148号