Connection.cpp 55 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602
  1. //
  2. // Copyright (c) 2008-2014 the Urho3D project.
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to deal
  6. // in the Software without restriction, including without limitation the rights
  7. // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. // copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  20. // THE SOFTWARE.
  21. //
  22. #include "Precompiled.h"
  23. #include "Component.h"
  24. #include "Connection.h"
  25. #include "File.h"
  26. #include "FileSystem.h"
  27. #include "Log.h"
  28. #include "MemoryBuffer.h"
  29. #include "Network.h"
  30. #include "NetworkEvents.h"
  31. #include "NetworkPriority.h"
  32. #include "PackageFile.h"
  33. #include "Profiler.h"
  34. #include "Protocol.h"
  35. #include "ResourceCache.h"
  36. #include "Scene.h"
  37. #include "SceneEvents.h"
  38. #include "SmoothedTransform.h"
  39. #include <kNet.h>
  40. #include "DebugNew.h"
  41. namespace Urho3D
  42. {
  43. static const int STATS_INTERVAL_MSEC = 2000;
  44. PackageDownload::PackageDownload() :
  45. totalFragments_(0),
  46. checksum_(0),
  47. initiated_(false)
  48. {
  49. }
  50. PackageUpload::PackageUpload() :
  51. fragment_(0),
  52. totalFragments_(0)
  53. {
  54. }
  55. Connection::Connection(Context* context, bool isClient, kNet::SharedPtr<kNet::MessageConnection> connection) :
  56. Object(context),
  57. connection_(connection),
  58. sendMode_(OPSM_NONE),
  59. isClient_(isClient),
  60. connectPending_(false),
  61. sceneLoaded_(false),
  62. logStatistics_(false)
  63. {
  64. sceneState_.connection_ = this;
  65. // Store address and port now for accurate logging (kNet may already have destroyed the socket on disconnection,
  66. // in which case we would log a zero address:port on disconnect)
  67. kNet::EndPoint endPoint = connection_->RemoteEndPoint();
  68. ///\todo Not IPv6-capable.
  69. address_ = Urho3D::ToString("%d.%d.%d.%d", endPoint.ip[0], endPoint.ip[1], endPoint.ip[2], endPoint.ip[3]);
  70. port_ = endPoint.port;
  71. }
  72. Connection::~Connection()
  73. {
  74. // Reset scene (remove possible owner references), as this connection is about to be destroyed
  75. SetScene(0);
  76. }
  77. void Connection::SendMessage(int msgID, bool reliable, bool inOrder, const VectorBuffer& msg, unsigned contentID)
  78. {
  79. SendMessage(msgID, reliable, inOrder, msg.GetData(), msg.GetSize(), contentID);
  80. }
  81. void Connection::SendMessage(int msgID, bool reliable, bool inOrder, const unsigned char* data, unsigned numBytes,
  82. unsigned contentID)
  83. {
  84. // Make sure not to use kNet internal message ID's
  85. if (msgID <= 0x4 || msgID >= 0x3ffffffe)
  86. {
  87. LOGERROR("Can not send message with reserved ID");
  88. return;
  89. }
  90. if (numBytes && !data)
  91. {
  92. LOGERROR("Null pointer supplied for network message data");
  93. return;
  94. }
  95. kNet::NetworkMessage *msg = connection_->StartNewMessage(msgID, numBytes);
  96. if (!msg)
  97. {
  98. LOGERROR("Can not start new network message");
  99. return;
  100. }
  101. msg->reliable = reliable;
  102. msg->inOrder = inOrder;
  103. msg->priority = 0;
  104. msg->contentID = contentID;
  105. if (numBytes)
  106. memcpy(msg->data, data, numBytes);
  107. connection_->EndAndQueueMessage(msg);
  108. }
  109. void Connection::SendRemoteEvent(StringHash eventType, bool inOrder, const VariantMap& eventData)
  110. {
  111. RemoteEvent queuedEvent;
  112. queuedEvent.senderID_ = 0;
  113. queuedEvent.eventType_ = eventType;
  114. queuedEvent.eventData_ = eventData;
  115. queuedEvent.inOrder_ = inOrder;
  116. remoteEvents_.Push(queuedEvent);
  117. }
  118. void Connection::SendRemoteEvent(Node* node, StringHash eventType, bool inOrder, const VariantMap& eventData)
  119. {
  120. if (!node)
  121. {
  122. LOGERROR("Null sender node for remote node event");
  123. return;
  124. }
  125. if (node->GetScene() != scene_)
  126. {
  127. LOGERROR("Sender node is not in the connection's scene, can not send remote node event");
  128. return;
  129. }
  130. if (node->GetID() >= FIRST_LOCAL_ID)
  131. {
  132. LOGERROR("Sender node has a local ID, can not send remote node event");
  133. return;
  134. }
  135. RemoteEvent queuedEvent;
  136. queuedEvent.senderID_ = node->GetID();
  137. queuedEvent.eventType_ = eventType;
  138. queuedEvent.eventData_ = eventData;
  139. queuedEvent.inOrder_ = inOrder;
  140. remoteEvents_.Push(queuedEvent);
  141. }
  142. void Connection::SetScene(Scene* newScene)
  143. {
  144. if (scene_)
  145. {
  146. // Remove replication states and owner references from the previous scene
  147. scene_->CleanupConnection(this);
  148. }
  149. scene_ = newScene;
  150. sceneLoaded_ = false;
  151. UnsubscribeFromEvent(E_ASYNCLOADFINISHED);
  152. if (!scene_)
  153. return;
  154. if (isClient_)
  155. {
  156. sceneState_.Clear();
  157. // When scene is assigned on the server, instruct the client to load it. This may require downloading packages
  158. const Vector<SharedPtr<PackageFile> >& packages = scene_->GetRequiredPackageFiles();
  159. unsigned numPackages = packages.Size();
  160. msg_.Clear();
  161. msg_.WriteString(scene_->GetFileName());
  162. msg_.WriteVLE(numPackages);
  163. for (unsigned i = 0; i < numPackages; ++i)
  164. {
  165. PackageFile* package = packages[i];
  166. msg_.WriteString(GetFileNameAndExtension(package->GetName()));
  167. msg_.WriteUInt(package->GetTotalSize());
  168. msg_.WriteUInt(package->GetChecksum());
  169. }
  170. SendMessage(MSG_LOADSCENE, true, true, msg_);
  171. }
  172. else
  173. {
  174. // Make sure there is no existing async loading
  175. scene_->StopAsyncLoading();
  176. SubscribeToEvent(scene_, E_ASYNCLOADFINISHED, HANDLER(Connection, HandleAsyncLoadFinished));
  177. }
  178. }
  179. void Connection::SetIdentity(const VariantMap& identity)
  180. {
  181. identity_ = identity;
  182. }
  183. void Connection::SetControls(const Controls& newControls)
  184. {
  185. controls_ = newControls;
  186. }
  187. void Connection::SetPosition(const Vector3& position)
  188. {
  189. position_ = position;
  190. if (sendMode_ == OPSM_NONE)
  191. sendMode_ = OPSM_POSITION;
  192. }
  193. void Connection::SetRotation(const Quaternion& rotation)
  194. {
  195. rotation_ = rotation;
  196. if (sendMode_ != OPSM_POSITION_ROTATION)
  197. sendMode_ = OPSM_POSITION_ROTATION;
  198. }
  199. void Connection::SetConnectPending(bool connectPending)
  200. {
  201. connectPending_ = connectPending;
  202. }
  203. void Connection::SetLogStatistics(bool enable)
  204. {
  205. logStatistics_ = enable;
  206. }
  207. void Connection::Disconnect(int waitMSec)
  208. {
  209. connection_->Disconnect(waitMSec);
  210. }
  211. void Connection::SendServerUpdate()
  212. {
  213. if (!scene_ || !sceneLoaded_)
  214. return;
  215. // Always check the root node (scene) first so that the scene-wide components get sent first,
  216. // and all other replicated nodes get added to the dirty set for sending the initial state
  217. unsigned sceneID = scene_->GetID();
  218. nodesToProcess_.Insert(sceneID);
  219. ProcessNode(sceneID);
  220. // Then go through all dirtied nodes
  221. nodesToProcess_.Insert(sceneState_.dirtyNodes_);
  222. nodesToProcess_.Erase(sceneID); // Do not process the root node twice
  223. while (nodesToProcess_.Size())
  224. {
  225. unsigned nodeID = nodesToProcess_.Front();
  226. ProcessNode(nodeID);
  227. }
  228. }
  229. void Connection::SendClientUpdate()
  230. {
  231. if (!scene_ || !sceneLoaded_)
  232. return;
  233. msg_.Clear();
  234. msg_.WriteUInt(controls_.buttons_);
  235. msg_.WriteFloat(controls_.yaw_);
  236. msg_.WriteFloat(controls_.pitch_);
  237. msg_.WriteVariantMap(controls_.extraData_);
  238. if (sendMode_ >= OPSM_POSITION)
  239. msg_.WriteVector3(position_);
  240. if (sendMode_ >= OPSM_POSITION_ROTATION)
  241. msg_.WritePackedQuaternion(rotation_);
  242. SendMessage(MSG_CONTROLS, false, false, msg_, CONTROLS_CONTENT_ID);
  243. }
  244. void Connection::SendRemoteEvents()
  245. {
  246. #ifdef URHO3D_LOGGING
  247. if (logStatistics_ && statsTimer_.GetMSec(false) > STATS_INTERVAL_MSEC)
  248. {
  249. statsTimer_.Reset();
  250. char statsBuffer[256];
  251. sprintf(statsBuffer, "RTT %.3f ms Pkt in %d Pkt out %d Data in %.3f KB/s Data out %.3f KB/s", connection_->RoundTripTime(), (int)connection_->PacketsInPerSec(),
  252. (int)connection_->PacketsOutPerSec(), connection_->BytesInPerSec() / 1000.0f, connection_->BytesOutPerSec() / 1000.0f);
  253. LOGINFO(statsBuffer);
  254. }
  255. #endif
  256. if (remoteEvents_.Empty())
  257. return;
  258. PROFILE(SendRemoteEvents);
  259. for (Vector<RemoteEvent>::ConstIterator i = remoteEvents_.Begin(); i != remoteEvents_.End(); ++i)
  260. {
  261. msg_.Clear();
  262. if (!i->senderID_)
  263. {
  264. msg_.WriteStringHash(i->eventType_);
  265. msg_.WriteVariantMap(i->eventData_);
  266. SendMessage(MSG_REMOTEEVENT, true, i->inOrder_, msg_);
  267. }
  268. else
  269. {
  270. msg_.WriteNetID(i->senderID_);
  271. msg_.WriteStringHash(i->eventType_);
  272. msg_.WriteVariantMap(i->eventData_);
  273. SendMessage(MSG_REMOTENODEEVENT, true, i->inOrder_, msg_);
  274. }
  275. }
  276. remoteEvents_.Clear();
  277. }
  278. void Connection::SendPackages()
  279. {
  280. while (!uploads_.Empty() && connection_->NumOutboundMessagesPending() < 1000)
  281. {
  282. unsigned char buffer[PACKAGE_FRAGMENT_SIZE];
  283. for (HashMap<StringHash, PackageUpload>::Iterator i = uploads_.Begin(); i != uploads_.End();)
  284. {
  285. HashMap<StringHash, PackageUpload>::Iterator current = i++;
  286. PackageUpload& upload = current->second_;
  287. unsigned fragmentSize = Min((int)(upload.file_->GetSize() - upload.file_->GetPosition()), (int)PACKAGE_FRAGMENT_SIZE);
  288. upload.file_->Read(buffer, fragmentSize);
  289. msg_.Clear();
  290. msg_.WriteStringHash(current->first_);
  291. msg_.WriteUInt(upload.fragment_++);
  292. msg_.Write(buffer, fragmentSize);
  293. SendMessage(MSG_PACKAGEDATA, true, false, msg_);
  294. // Check if upload finished
  295. if (upload.fragment_ == upload.totalFragments_)
  296. uploads_.Erase(current);
  297. }
  298. }
  299. }
  300. void Connection::ProcessPendingLatestData()
  301. {
  302. if (!scene_ || !sceneLoaded_)
  303. return;
  304. // Iterate through pending node data and see if we can find the nodes now
  305. for (HashMap<unsigned, PODVector<unsigned char> >::Iterator i = nodeLatestData_.Begin(); i != nodeLatestData_.End();)
  306. {
  307. HashMap<unsigned, PODVector<unsigned char> >::Iterator current = i++;
  308. Node* node = scene_->GetNode(current->first_);
  309. if (node)
  310. {
  311. MemoryBuffer msg(current->second_);
  312. msg.ReadNetID(); // Skip the node ID
  313. node->ReadLatestDataUpdate(msg);
  314. // ApplyAttributes() is deliberately skipped, as Node has no attributes that require late applying.
  315. // Furthermore it would propagate to components and child nodes, which is not desired in this case
  316. nodeLatestData_.Erase(current);
  317. }
  318. }
  319. // Iterate through pending component data and see if we can find the components now
  320. for (HashMap<unsigned, PODVector<unsigned char> >::Iterator i = componentLatestData_.Begin(); i != componentLatestData_.End();)
  321. {
  322. HashMap<unsigned, PODVector<unsigned char> >::Iterator current = i++;
  323. Component* component = scene_->GetComponent(current->first_);
  324. if (component)
  325. {
  326. MemoryBuffer msg(current->second_);
  327. msg.ReadNetID(); // Skip the component ID
  328. component->ReadLatestDataUpdate(msg);
  329. component->ApplyAttributes();
  330. componentLatestData_.Erase(current);
  331. }
  332. }
  333. }
  334. bool Connection::ProcessMessage(int msgID, MemoryBuffer &msg)
  335. {
  336. bool processed = true;
  337. switch (msgID)
  338. {
  339. case MSG_IDENTITY:
  340. ProcessIdentity(msgID, msg);
  341. break;
  342. case MSG_CONTROLS:
  343. ProcessControls(msgID, msg);
  344. break;
  345. case MSG_SCENELOADED:
  346. ProcessSceneLoaded(msgID, msg);
  347. break;
  348. case MSG_REQUESTPACKAGE:
  349. case MSG_PACKAGEDATA:
  350. ProcessPackageDownload(msgID, msg);
  351. break;
  352. case MSG_LOADSCENE:
  353. ProcessLoadScene(msgID, msg);
  354. break;
  355. case MSG_SCENECHECKSUMERROR:
  356. ProcessSceneChecksumError(msgID, msg);
  357. break;
  358. case MSG_CREATENODE:
  359. case MSG_NODEDELTAUPDATE:
  360. case MSG_NODELATESTDATA:
  361. case MSG_REMOVENODE:
  362. case MSG_CREATECOMPONENT:
  363. case MSG_COMPONENTDELTAUPDATE:
  364. case MSG_COMPONENTLATESTDATA:
  365. case MSG_REMOVECOMPONENT:
  366. ProcessSceneUpdate(msgID, msg);
  367. break;
  368. case MSG_REMOTEEVENT:
  369. case MSG_REMOTENODEEVENT:
  370. ProcessRemoteEvent(msgID, msg);
  371. break;
  372. case MSG_SYNCPACKAGES:
  373. ProcessSyncPackages(msgID, msg);
  374. break;
  375. case MSG_SYNCPACKAGESINFO:
  376. ProcessSyncPackagesInfo(msgID, msg);
  377. break;
  378. default:
  379. processed = false;
  380. break;
  381. }
  382. return processed;
  383. }
  384. void Connection::ProcessLoadScene(int msgID, MemoryBuffer& msg)
  385. {
  386. if (IsClient())
  387. {
  388. LOGWARNING("Received unexpected LoadScene message from client " + ToString());
  389. return;
  390. }
  391. if (!scene_)
  392. {
  393. LOGERROR("Can not handle LoadScene message without an assigned scene");
  394. return;
  395. }
  396. // Store the scene file name we need to eventually load
  397. sceneFileName_ = msg.ReadString();
  398. // Clear previous pending latest data and package downloads if any
  399. nodeLatestData_.Clear();
  400. componentLatestData_.Clear();
  401. downloads_.Clear();
  402. // In case we have joined other scenes in this session, remove first all downloaded package files from the resource system
  403. // to prevent resource conflicts
  404. const String& packageCacheDir = GetSubsystem<Network>()->GetPackageCacheDir();
  405. ResourceCache* cache = GetSubsystem<ResourceCache>();
  406. Vector<SharedPtr<PackageFile> > packages = cache->GetPackageFiles();
  407. for (unsigned i = 0; i < packages.Size(); ++i)
  408. {
  409. PackageFile* package = packages[i];
  410. if (!package->GetName().Find(packageCacheDir))
  411. cache->RemovePackageFile(package, true);
  412. }
  413. // Now check which packages we have in the resource cache or in the download cache, and which we need to download
  414. unsigned numPackages = msg.ReadVLE();
  415. packages = cache->GetPackageFiles(); // Refresh resource cache's package list after possible removals
  416. Vector<String> downloadedPackages;
  417. if (!packageCacheDir.Empty())
  418. GetSubsystem<FileSystem>()->ScanDir(downloadedPackages, packageCacheDir, "*.*", SCAN_FILES, false);
  419. for (unsigned i = 0; i < numPackages; ++i)
  420. {
  421. String name = msg.ReadString();
  422. unsigned fileSize = msg.ReadUInt();
  423. unsigned checksum = msg.ReadUInt();
  424. String checksumString = ToStringHex(checksum);
  425. bool found = false;
  426. // Check first the resource cache
  427. for (unsigned j = 0; j < packages.Size(); ++j)
  428. {
  429. PackageFile* package = packages[j];
  430. if (!GetFileNameAndExtension(package->GetName()).Compare(name, false) && package->GetTotalSize() == fileSize &&
  431. package->GetChecksum() == checksum)
  432. {
  433. found = true;
  434. break;
  435. }
  436. }
  437. // Then the download cache
  438. for (unsigned j = 0; j < downloadedPackages.Size(); ++j)
  439. {
  440. const String& fileName = downloadedPackages[j];
  441. if (!fileName.Find(checksumString) && !fileName.Substring(9).Compare(name, false))
  442. {
  443. // Name matches. Check filesize and actual checksum to be sure
  444. SharedPtr<PackageFile> newPackage(new PackageFile(context_, packageCacheDir + fileName));
  445. if (newPackage->GetTotalSize() == fileSize && newPackage->GetChecksum() == checksum)
  446. {
  447. // Add the package to the resource system now, as we will need it to load the scene
  448. cache->AddPackageFile(newPackage, true);
  449. found = true;
  450. break;
  451. }
  452. }
  453. }
  454. // Package not found, need to request a download
  455. if (!found)
  456. {
  457. if (!packageCacheDir.Empty())
  458. RequestPackage(name, fileSize, checksum);
  459. else
  460. {
  461. LOGERROR("Can not download required packages, as package cache directory is not set");
  462. OnSceneLoadFailed();
  463. return;
  464. }
  465. }
  466. }
  467. // If no downloads were queued, can load the scene directly
  468. if (downloads_.Empty())
  469. OnPackagesReady();
  470. }
  471. void Connection::ProcessSceneChecksumError(int msgID, MemoryBuffer& msg)
  472. {
  473. if (IsClient())
  474. {
  475. LOGWARNING("Received unexpected SceneChecksumError message from client " + ToString());
  476. return;
  477. }
  478. LOGERROR("Scene checksum error");
  479. OnSceneLoadFailed();
  480. }
  481. void Connection::ProcessSceneUpdate(int msgID, MemoryBuffer& msg)
  482. {
  483. /// \todo On mobile devices processing this message may potentially cause a crash if it attempts to load new GPU resources
  484. /// while the application is minimized
  485. if (IsClient())
  486. {
  487. LOGWARNING("Received unexpected SceneUpdate message from client " + ToString());
  488. return;
  489. }
  490. if (!scene_)
  491. return;
  492. switch (msgID)
  493. {
  494. case MSG_CREATENODE:
  495. {
  496. unsigned nodeID = msg.ReadNetID();
  497. // In case of the root node (scene), it should already exist. Do not create in that case
  498. Node* node = scene_->GetNode(nodeID);
  499. if (!node)
  500. {
  501. // Add initially to the root level. May be moved as we receive the parent attribute
  502. node = scene_->CreateChild(nodeID, REPLICATED);
  503. // Create smoothed transform component
  504. node->CreateComponent<SmoothedTransform>(LOCAL);
  505. }
  506. // Read initial attributes, then snap the motion smoothing immediately to the end
  507. node->ReadDeltaUpdate(msg);
  508. SmoothedTransform* transform = node->GetComponent<SmoothedTransform>();
  509. if (transform)
  510. transform->Update(1.0f, 0.0f);
  511. // Read initial user variables
  512. unsigned numVars = msg.ReadVLE();
  513. while (numVars)
  514. {
  515. StringHash key = msg.ReadStringHash();
  516. node->SetVar(key, msg.ReadVariant());
  517. --numVars;
  518. }
  519. // Read components
  520. unsigned numComponents = msg.ReadVLE();
  521. while (numComponents)
  522. {
  523. --numComponents;
  524. StringHash type = msg.ReadStringHash();
  525. unsigned componentID = msg.ReadNetID();
  526. // Check if the component by this ID and type already exists in this node
  527. Component* component = scene_->GetComponent(componentID);
  528. if (!component || component->GetType() != type || component->GetNode() != node)
  529. {
  530. if (component)
  531. component->Remove();
  532. component = node->CreateComponent(type, REPLICATED, componentID);
  533. }
  534. // If was unable to create the component, would desync the message and therefore have to abort
  535. if (!component)
  536. {
  537. LOGERROR("CreateNode message parsing aborted due to unknown component");
  538. return;
  539. }
  540. // Read initial attributes and apply
  541. component->ReadDeltaUpdate(msg);
  542. component->ApplyAttributes();
  543. }
  544. }
  545. break;
  546. case MSG_NODEDELTAUPDATE:
  547. {
  548. unsigned nodeID = msg.ReadNetID();
  549. Node* node = scene_->GetNode(nodeID);
  550. if (node)
  551. {
  552. node->ReadDeltaUpdate(msg);
  553. // ApplyAttributes() is deliberately skipped, as Node has no attributes that require late applying.
  554. // Furthermore it would propagate to components and child nodes, which is not desired in this case
  555. unsigned changedVars = msg.ReadVLE();
  556. while (changedVars)
  557. {
  558. StringHash key = msg.ReadStringHash();
  559. node->SetVar(key, msg.ReadVariant());
  560. --changedVars;
  561. }
  562. }
  563. else
  564. LOGWARNING("NodeDeltaUpdate message received for missing node " + String(nodeID));
  565. }
  566. break;
  567. case MSG_NODELATESTDATA:
  568. {
  569. unsigned nodeID = msg.ReadNetID();
  570. Node* node = scene_->GetNode(nodeID);
  571. if (node)
  572. {
  573. node->ReadLatestDataUpdate(msg);
  574. // ApplyAttributes() is deliberately skipped, as Node has no attributes that require late applying.
  575. // Furthermore it would propagate to components and child nodes, which is not desired in this case
  576. }
  577. else
  578. {
  579. // Latest data messages may be received out-of-order relative to node creation, so cache if necessary
  580. PODVector<unsigned char>& data = nodeLatestData_[nodeID];
  581. data.Resize(msg.GetSize());
  582. memcpy(&data[0], msg.GetData(), msg.GetSize());
  583. }
  584. }
  585. break;
  586. case MSG_REMOVENODE:
  587. {
  588. unsigned nodeID = msg.ReadNetID();
  589. Node* node = scene_->GetNode(nodeID);
  590. if (node)
  591. node->Remove();
  592. nodeLatestData_.Erase(nodeID);
  593. }
  594. break;
  595. case MSG_CREATECOMPONENT:
  596. {
  597. unsigned nodeID = msg.ReadNetID();
  598. Node* node = scene_->GetNode(nodeID);
  599. if (node)
  600. {
  601. StringHash type = msg.ReadStringHash();
  602. unsigned componentID = msg.ReadNetID();
  603. // Check if the component by this ID and type already exists in this node
  604. Component* component = scene_->GetComponent(componentID);
  605. if (!component || component->GetType() != type || component->GetNode() != node)
  606. {
  607. if (component)
  608. component->Remove();
  609. component = node->CreateComponent(type, REPLICATED, componentID);
  610. }
  611. // If was unable to create the component, would desync the message and therefore have to abort
  612. if (!component)
  613. {
  614. LOGERROR("CreateComponent message parsing aborted due to unknown component");
  615. return;
  616. }
  617. // Read initial attributes and apply
  618. component->ReadDeltaUpdate(msg);
  619. component->ApplyAttributes();
  620. }
  621. else
  622. LOGWARNING("CreateComponent message received for missing node " + String(nodeID));
  623. }
  624. break;
  625. case MSG_COMPONENTDELTAUPDATE:
  626. {
  627. unsigned componentID = msg.ReadNetID();
  628. Component* component = scene_->GetComponent(componentID);
  629. if (component)
  630. {
  631. component->ReadDeltaUpdate(msg);
  632. component->ApplyAttributes();
  633. }
  634. else
  635. LOGWARNING("ComponentDeltaUpdate message received for missing component " + String(componentID));
  636. }
  637. break;
  638. case MSG_COMPONENTLATESTDATA:
  639. {
  640. unsigned componentID = msg.ReadNetID();
  641. Component* component = scene_->GetComponent(componentID);
  642. if (component)
  643. {
  644. component->ReadLatestDataUpdate(msg);
  645. component->ApplyAttributes();
  646. }
  647. else
  648. {
  649. // Latest data messages may be received out-of-order relative to component creation, so cache if necessary
  650. PODVector<unsigned char>& data = componentLatestData_[componentID];
  651. data.Resize(msg.GetSize());
  652. memcpy(&data[0], msg.GetData(), msg.GetSize());
  653. }
  654. }
  655. break;
  656. case MSG_REMOVECOMPONENT:
  657. {
  658. unsigned componentID = msg.ReadNetID();
  659. Component* component = scene_->GetComponent(componentID);
  660. if (component)
  661. component->Remove();
  662. componentLatestData_.Erase(componentID);
  663. }
  664. break;
  665. }
  666. }
  667. void Connection::ProcessPackageDownload(int msgID, MemoryBuffer& msg)
  668. {
  669. switch (msgID)
  670. {
  671. case MSG_REQUESTPACKAGE:
  672. if (!IsClient())
  673. {
  674. LOGWARNING("Received unexpected RequestPackage message from server");
  675. return;
  676. }
  677. else
  678. {
  679. String name = msg.ReadString();
  680. if (!scene_)
  681. {
  682. LOGWARNING("Received a RequestPackage message without an assigned scene from client " + ToString());
  683. return;
  684. }
  685. // The package must be one of those required by the scene
  686. const Vector<SharedPtr<PackageFile> >& packages = scene_->GetRequiredPackageFiles();
  687. for (unsigned i = 0; i < packages.Size(); ++i)
  688. {
  689. PackageFile* package = packages[i];
  690. String packageFullName = package->GetName();
  691. if (!GetFileNameAndExtension(packageFullName).Compare(name, false))
  692. {
  693. StringHash nameHash(name);
  694. // Do not restart upload if already exists
  695. if (uploads_.Contains(nameHash))
  696. {
  697. LOGWARNING("Received a request for package " + name + " already in transfer");
  698. return;
  699. }
  700. // Try to open the file now
  701. SharedPtr<File> file(new File(context_, packageFullName));
  702. if (!file->IsOpen())
  703. {
  704. LOGERROR("Failed to transmit package file " + name);
  705. SendPackageError(name);
  706. return;
  707. }
  708. LOGINFO("Transmitting package file " + name + " to client " + ToString());
  709. uploads_[nameHash].file_ = file;
  710. uploads_[nameHash].fragment_ = 0;
  711. uploads_[nameHash].totalFragments_ = (file->GetSize() + PACKAGE_FRAGMENT_SIZE - 1) / PACKAGE_FRAGMENT_SIZE;
  712. return;
  713. }
  714. }
  715. LOGERROR("Client requested an unexpected package file " + name);
  716. // Send the name hash only to indicate a failed download
  717. SendPackageError(name);
  718. return;
  719. }
  720. break;
  721. case MSG_PACKAGEDATA:
  722. if (IsClient())
  723. {
  724. LOGWARNING("Received unexpected PackageData message from client");
  725. return;
  726. }
  727. else
  728. {
  729. StringHash nameHash = msg.ReadStringHash();
  730. HashMap<StringHash, PackageDownload>::Iterator i = downloads_.Find(nameHash);
  731. // In case of being unable to create the package file into the cache, we will still receive all data from the server.
  732. // Simply disregard it
  733. if (i == downloads_.End())
  734. return;
  735. PackageDownload& download = i->second_;
  736. // If no further data, this is an error reply
  737. if (msg.IsEof())
  738. {
  739. OnPackageDownloadFailed(download.name_);
  740. return;
  741. }
  742. // If file has not yet been opened, try to open now. Prepend the checksum to the filename to allow multiple versions
  743. if (!download.file_)
  744. {
  745. download.file_ = new File(context_, GetSubsystem<Network>()->GetPackageCacheDir() + ToStringHex(download.checksum_) + "_" + download.name_, FILE_WRITE);
  746. if (!download.file_->IsOpen())
  747. {
  748. OnPackageDownloadFailed(download.name_);
  749. return;
  750. }
  751. }
  752. // Write the fragment data to the proper index
  753. unsigned char buffer[PACKAGE_FRAGMENT_SIZE];
  754. unsigned index = msg.ReadUInt();
  755. unsigned fragmentSize = msg.GetSize() - msg.GetPosition();
  756. msg.Read(buffer, fragmentSize);
  757. download.file_->Seek(index * PACKAGE_FRAGMENT_SIZE);
  758. download.file_->Write(buffer, fragmentSize);
  759. download.receivedFragments_.Insert(index);
  760. // Check if all fragments received
  761. if (download.receivedFragments_.Size() == download.totalFragments_)
  762. {
  763. LOGINFO("Package " + download.name_ + " downloaded successfully");
  764. // Instantiate the package and add to the resource system, as we will need it to load the scene
  765. download.file_->Close();
  766. SharedPtr<PackageFile> newPackage(new PackageFile(context_, download.file_->GetName()));
  767. GetSubsystem<ResourceCache>()->AddPackageFile(newPackage, true);
  768. // Then start the next download if there are more
  769. downloads_.Erase(i);
  770. if (downloads_.Empty())
  771. OnPackagesReady();
  772. else
  773. {
  774. PackageDownload& nextDownload = downloads_.Begin()->second_;
  775. LOGINFO("Requesting package " + nextDownload.name_ + " from server");
  776. msg_.Clear();
  777. msg_.WriteString(nextDownload.name_);
  778. SendMessage(MSG_REQUESTPACKAGE, true, true, msg_);
  779. nextDownload.initiated_ = true;
  780. }
  781. }
  782. }
  783. break;
  784. }
  785. }
  786. void Connection::ProcessIdentity(int msgID, MemoryBuffer& msg)
  787. {
  788. if (!IsClient())
  789. {
  790. LOGWARNING("Received unexpected Identity message from server");
  791. return;
  792. }
  793. identity_ = msg.ReadVariantMap();
  794. using namespace ClientIdentity;
  795. VariantMap eventData = identity_;
  796. eventData[P_CONNECTION] = this;
  797. eventData[P_ALLOW] = true;
  798. SendEvent(E_CLIENTIDENTITY, eventData);
  799. // If connection was denied as a response to the identity event, disconnect now
  800. if (!eventData[P_ALLOW].GetBool())
  801. Disconnect();
  802. }
  803. void Connection::ProcessControls(int msgID, MemoryBuffer& msg)
  804. {
  805. if (!IsClient())
  806. {
  807. LOGWARNING("Received unexpected Controls message from server");
  808. return;
  809. }
  810. Controls newControls;
  811. newControls.buttons_ = msg.ReadUInt();
  812. newControls.yaw_ = msg.ReadFloat();
  813. newControls.pitch_ = msg.ReadFloat();
  814. newControls.extraData_ = msg.ReadVariantMap();
  815. SetControls(newControls);
  816. // Client may or may not send observer position & rotation for interest management
  817. if (!msg.IsEof())
  818. position_ = msg.ReadVector3();
  819. if (!msg.IsEof())
  820. rotation_ = msg.ReadPackedQuaternion();
  821. }
  822. void Connection::ProcessSceneLoaded(int msgID, MemoryBuffer& msg)
  823. {
  824. if (!IsClient())
  825. {
  826. LOGWARNING("Received unexpected SceneLoaded message from server");
  827. return;
  828. }
  829. if (!scene_)
  830. {
  831. LOGWARNING("Received a SceneLoaded message without an assigned scene from client " + ToString());
  832. return;
  833. }
  834. unsigned checksum = msg.ReadUInt();
  835. if (checksum != scene_->GetChecksum())
  836. {
  837. LOGINFO("Scene checksum error from client " + ToString());
  838. msg_.Clear();
  839. SendMessage(MSG_SCENECHECKSUMERROR, true, true, msg_);
  840. OnSceneLoadFailed();
  841. }
  842. else
  843. {
  844. sceneLoaded_ = true;
  845. using namespace ClientSceneLoaded;
  846. VariantMap& eventData = GetEventDataMap();
  847. eventData[P_CONNECTION] = this;
  848. SendEvent(E_CLIENTSCENELOADED, eventData);
  849. }
  850. }
  851. void Connection::ProcessRemoteEvent(int msgID, MemoryBuffer& msg)
  852. {
  853. using namespace RemoteEventData;
  854. if (msgID == MSG_REMOTEEVENT)
  855. {
  856. StringHash eventType = msg.ReadStringHash();
  857. if (!GetSubsystem<Network>()->CheckRemoteEvent(eventType))
  858. {
  859. LOGWARNING("Discarding not allowed remote event " + eventType.ToString());
  860. return;
  861. }
  862. VariantMap eventData = msg.ReadVariantMap();
  863. eventData[P_CONNECTION] = this;
  864. SendEvent(eventType, eventData);
  865. }
  866. else
  867. {
  868. if (!scene_)
  869. {
  870. LOGERROR("Can not receive remote node event without an assigned scene");
  871. return;
  872. }
  873. unsigned nodeID = msg.ReadNetID();
  874. StringHash eventType = msg.ReadStringHash();
  875. if (!GetSubsystem<Network>()->CheckRemoteEvent(eventType))
  876. {
  877. LOGWARNING("Discarding not allowed remote event " + eventType.ToString());
  878. return;
  879. }
  880. VariantMap eventData = msg.ReadVariantMap();
  881. Node* sender = scene_->GetNode(nodeID);
  882. if (!sender)
  883. {
  884. LOGWARNING("Missing sender for remote node event, discarding");
  885. return;
  886. }
  887. eventData[P_CONNECTION] = this;
  888. sender->SendEvent(eventType, eventData);
  889. }
  890. }
  891. kNet::MessageConnection* Connection::GetMessageConnection() const
  892. {
  893. return const_cast<kNet::MessageConnection*>(connection_.ptr());
  894. }
  895. Scene* Connection::GetScene() const
  896. {
  897. return scene_;
  898. }
  899. bool Connection::IsConnected() const
  900. {
  901. return connection_->GetConnectionState() == kNet::ConnectionOK;
  902. }
  903. String Connection::ToString() const
  904. {
  905. return GetAddress() + ":" + String(GetPort());
  906. }
  907. unsigned Connection::GetNumDownloads() const
  908. {
  909. return downloads_.Size();
  910. }
  911. const String& Connection::GetDownloadName() const
  912. {
  913. for (HashMap<StringHash, PackageDownload>::ConstIterator i = downloads_.Begin(); i != downloads_.End(); ++i)
  914. {
  915. if (i->second_.initiated_)
  916. return i->second_.name_;
  917. }
  918. return String::EMPTY;
  919. }
  920. float Connection::GetDownloadProgress() const
  921. {
  922. for (HashMap<StringHash, PackageDownload>::ConstIterator i = downloads_.Begin(); i != downloads_.End(); ++i)
  923. {
  924. if (i->second_.initiated_)
  925. return (float)i->second_.receivedFragments_.Size() / (float)i->second_.totalFragments_;
  926. }
  927. return 1.0f;
  928. }
  929. void Connection::HandleAsyncLoadFinished(StringHash eventType, VariantMap& eventData)
  930. {
  931. sceneLoaded_ = true;
  932. msg_.Clear();
  933. msg_.WriteUInt(scene_->GetChecksum());
  934. SendMessage(MSG_SCENELOADED, true, true, msg_);
  935. }
  936. void Connection::ProcessNode(unsigned nodeID)
  937. {
  938. // Check that we have not already processed this due to dependency recursion
  939. if (!nodesToProcess_.Erase(nodeID))
  940. return;
  941. // Find replication state for the node
  942. HashMap<unsigned, NodeReplicationState>::Iterator i = sceneState_.nodeStates_.Find(nodeID);
  943. if (i != sceneState_.nodeStates_.End())
  944. {
  945. // Replication state found: the node is either be existing or removed
  946. Node* node = i->second_.node_;
  947. if (!node)
  948. {
  949. msg_.Clear();
  950. msg_.WriteNetID(nodeID);
  951. // Note: we will send MSG_REMOVENODE redundantly for each node in the hierarchy, even if removing the root node
  952. // would be enough. However, this may be better due to the client not possibly having updated parenting
  953. // information at the time of receiving this message
  954. SendMessage(MSG_REMOVENODE, true, true, msg_);
  955. sceneState_.nodeStates_.Erase(nodeID);
  956. }
  957. else
  958. ProcessExistingNode(node, i->second_);
  959. }
  960. else
  961. {
  962. // Replication state not found: this is a new node
  963. Node* node = scene_->GetNode(nodeID);
  964. if (node)
  965. ProcessNewNode(node);
  966. else
  967. {
  968. // Did not find the new node (may have been created, then removed immediately): erase from dirty set.
  969. sceneState_.dirtyNodes_.Erase(nodeID);
  970. }
  971. }
  972. }
  973. void Connection::ProcessNewNode(Node* node)
  974. {
  975. // Process depended upon nodes first, if they are dirty
  976. const PODVector<Node*>& dependencyNodes = node->GetDependencyNodes();
  977. for (PODVector<Node*>::ConstIterator i = dependencyNodes.Begin(); i != dependencyNodes.End(); ++i)
  978. {
  979. unsigned nodeID = (*i)->GetID();
  980. if (sceneState_.dirtyNodes_.Contains(nodeID))
  981. ProcessNode(nodeID);
  982. }
  983. msg_.Clear();
  984. msg_.WriteNetID(node->GetID());
  985. NodeReplicationState& nodeState = sceneState_.nodeStates_[node->GetID()];
  986. nodeState.connection_ = this;
  987. nodeState.sceneState_ = &sceneState_;
  988. nodeState.node_ = node;
  989. node->AddReplicationState(&nodeState);
  990. // Write node's attributes
  991. node->WriteInitialDeltaUpdate(msg_);
  992. // Write node's user variables
  993. const VariantMap& vars = node->GetVars();
  994. msg_.WriteVLE(vars.Size());
  995. for (VariantMap::ConstIterator i = vars.Begin(); i != vars.End(); ++i)
  996. {
  997. msg_.WriteStringHash(i->first_);
  998. msg_.WriteVariant(i->second_);
  999. }
  1000. // Write node's components
  1001. msg_.WriteVLE(node->GetNumNetworkComponents());
  1002. const Vector<SharedPtr<Component> >& components = node->GetComponents();
  1003. for (unsigned i = 0; i < components.Size(); ++i)
  1004. {
  1005. Component* component = components[i];
  1006. // Check if component is not to be replicated
  1007. if (component->GetID() >= FIRST_LOCAL_ID)
  1008. continue;
  1009. ComponentReplicationState& componentState = nodeState.componentStates_[component->GetID()];
  1010. componentState.connection_ = this;
  1011. componentState.nodeState_ = &nodeState;
  1012. componentState.component_ = component;
  1013. component->AddReplicationState(&componentState);
  1014. msg_.WriteStringHash(component->GetType());
  1015. msg_.WriteNetID(component->GetID());
  1016. component->WriteInitialDeltaUpdate(msg_);
  1017. }
  1018. SendMessage(MSG_CREATENODE, true, true, msg_);
  1019. nodeState.markedDirty_ = false;
  1020. sceneState_.dirtyNodes_.Erase(node->GetID());
  1021. }
  1022. void Connection::ProcessExistingNode(Node* node, NodeReplicationState& nodeState)
  1023. {
  1024. // Process depended upon nodes first, if they are dirty
  1025. const PODVector<Node*>& dependencyNodes = node->GetDependencyNodes();
  1026. for (PODVector<Node*>::ConstIterator i = dependencyNodes.Begin(); i != dependencyNodes.End(); ++i)
  1027. {
  1028. unsigned nodeID = (*i)->GetID();
  1029. if (sceneState_.dirtyNodes_.Contains(nodeID))
  1030. ProcessNode(nodeID);
  1031. }
  1032. // Check from the interest management component, if exists, whether should update
  1033. /// \todo Searching for the component is a potential CPU hotspot. It should be cached
  1034. NetworkPriority* priority = node->GetComponent<NetworkPriority>();
  1035. if (priority && (!priority->GetAlwaysUpdateOwner() || node->GetOwner() != this))
  1036. {
  1037. float distance = (node->GetWorldPosition() - position_).Length();
  1038. if (!priority->CheckUpdate(distance, nodeState.priorityAcc_))
  1039. return;
  1040. }
  1041. // Check if attributes have changed
  1042. if (nodeState.dirtyAttributes_.Count())
  1043. {
  1044. const Vector<AttributeInfo>* attributes = node->GetNetworkAttributes();
  1045. unsigned numAttributes = attributes->Size();
  1046. bool hasLatestData = false;
  1047. for (unsigned i = 0; i < numAttributes; ++i)
  1048. {
  1049. if (nodeState.dirtyAttributes_.IsSet(i) && (attributes->At(i).mode_ & AM_LATESTDATA))
  1050. {
  1051. hasLatestData = true;
  1052. nodeState.dirtyAttributes_.Clear(i);
  1053. }
  1054. }
  1055. // Send latestdata message if necessary
  1056. if (hasLatestData)
  1057. {
  1058. msg_.Clear();
  1059. msg_.WriteNetID(node->GetID());
  1060. node->WriteLatestDataUpdate(msg_);
  1061. SendMessage(MSG_NODELATESTDATA, true, false, msg_, node->GetID());
  1062. }
  1063. // Send deltaupdate if remaining dirty bits, or vars have changed
  1064. if (nodeState.dirtyAttributes_.Count() || nodeState.dirtyVars_.Size())
  1065. {
  1066. msg_.Clear();
  1067. msg_.WriteNetID(node->GetID());
  1068. node->WriteDeltaUpdate(msg_, nodeState.dirtyAttributes_);
  1069. // Write changed variables
  1070. msg_.WriteVLE(nodeState.dirtyVars_.Size());
  1071. const VariantMap& vars = node->GetVars();
  1072. for (HashSet<StringHash>::ConstIterator i = nodeState.dirtyVars_.Begin(); i != nodeState.dirtyVars_.End(); ++i)
  1073. {
  1074. VariantMap::ConstIterator j = vars.Find(*i);
  1075. if (j != vars.End())
  1076. {
  1077. msg_.WriteStringHash(j->first_);
  1078. msg_.WriteVariant(j->second_);
  1079. }
  1080. else
  1081. {
  1082. // Variable has been marked dirty, but is removed (which is unsupported): send a dummy variable in place
  1083. LOGWARNING("Sending dummy user variable as original value was removed");
  1084. msg_.WriteStringHash(StringHash());
  1085. msg_.WriteVariant(Variant::EMPTY);
  1086. }
  1087. }
  1088. SendMessage(MSG_NODEDELTAUPDATE, true, true, msg_);
  1089. nodeState.dirtyAttributes_.ClearAll();
  1090. nodeState.dirtyVars_.Clear();
  1091. }
  1092. }
  1093. // Check for removed or changed components
  1094. for (HashMap<unsigned, ComponentReplicationState>::Iterator i = nodeState.componentStates_.Begin();
  1095. i != nodeState.componentStates_.End(); )
  1096. {
  1097. HashMap<unsigned, ComponentReplicationState>::Iterator current = i++;
  1098. ComponentReplicationState& componentState = current->second_;
  1099. Component* component = componentState.component_;
  1100. if (!component)
  1101. {
  1102. // Removed component
  1103. msg_.Clear();
  1104. msg_.WriteNetID(current->first_);
  1105. SendMessage(MSG_REMOVECOMPONENT, true, true, msg_);
  1106. nodeState.componentStates_.Erase(current);
  1107. }
  1108. else
  1109. {
  1110. // Existing component. Check if attributes have changed
  1111. if (componentState.dirtyAttributes_.Count())
  1112. {
  1113. const Vector<AttributeInfo>* attributes = component->GetNetworkAttributes();
  1114. unsigned numAttributes = attributes->Size();
  1115. bool hasLatestData = false;
  1116. for (unsigned i = 0; i < numAttributes; ++i)
  1117. {
  1118. if (componentState.dirtyAttributes_.IsSet(i) && (attributes->At(i).mode_ & AM_LATESTDATA))
  1119. {
  1120. hasLatestData = true;
  1121. componentState.dirtyAttributes_.Clear(i);
  1122. }
  1123. }
  1124. // Send latestdata message if necessary
  1125. if (hasLatestData)
  1126. {
  1127. msg_.Clear();
  1128. msg_.WriteNetID(component->GetID());
  1129. component->WriteLatestDataUpdate(msg_);
  1130. SendMessage(MSG_COMPONENTLATESTDATA, true, false, msg_, component->GetID());
  1131. }
  1132. // Send deltaupdate if remaining dirty bits
  1133. if (componentState.dirtyAttributes_.Count())
  1134. {
  1135. msg_.Clear();
  1136. msg_.WriteNetID(component->GetID());
  1137. component->WriteDeltaUpdate(msg_, componentState.dirtyAttributes_);
  1138. SendMessage(MSG_COMPONENTDELTAUPDATE, true, true, msg_);
  1139. componentState.dirtyAttributes_.ClearAll();
  1140. }
  1141. }
  1142. }
  1143. }
  1144. // Check for new components
  1145. if (nodeState.componentStates_.Size() != node->GetNumNetworkComponents())
  1146. {
  1147. const Vector<SharedPtr<Component> >& components = node->GetComponents();
  1148. for (unsigned i = 0; i < components.Size(); ++i)
  1149. {
  1150. Component* component = components[i];
  1151. // Check if component is not to be replicated
  1152. if (component->GetID() >= FIRST_LOCAL_ID)
  1153. continue;
  1154. HashMap<unsigned, ComponentReplicationState>::Iterator j = nodeState.componentStates_.Find(component->GetID());
  1155. if (j == nodeState.componentStates_.End())
  1156. {
  1157. // New component
  1158. ComponentReplicationState& componentState = nodeState.componentStates_[component->GetID()];
  1159. componentState.connection_ = this;
  1160. componentState.nodeState_ = &nodeState;
  1161. componentState.component_ = component;
  1162. component->AddReplicationState(&componentState);
  1163. msg_.Clear();
  1164. msg_.WriteNetID(node->GetID());
  1165. msg_.WriteStringHash(component->GetType());
  1166. msg_.WriteNetID(component->GetID());
  1167. component->WriteInitialDeltaUpdate(msg_);
  1168. SendMessage(MSG_CREATECOMPONENT, true, true, msg_);
  1169. }
  1170. }
  1171. }
  1172. nodeState.markedDirty_ = false;
  1173. sceneState_.dirtyNodes_.Erase(node->GetID());
  1174. }
  1175. void Connection::RequestPackage(const String& name, unsigned fileSize, unsigned checksum)
  1176. {
  1177. StringHash nameHash(name);
  1178. if (downloads_.Contains(nameHash))
  1179. return; // Download already exists
  1180. PackageDownload& download = downloads_[nameHash];
  1181. download.name_ = name;
  1182. download.totalFragments_ = (fileSize + PACKAGE_FRAGMENT_SIZE - 1) / PACKAGE_FRAGMENT_SIZE;
  1183. download.checksum_ = checksum;
  1184. // Start download now only if no existing downloads, else wait for the existing ones to finish
  1185. if (downloads_.Size() == 1)
  1186. {
  1187. LOGINFO("Requesting package " + name + " from server");
  1188. msg_.Clear();
  1189. msg_.WriteString(name);
  1190. SendMessage(MSG_REQUESTPACKAGE, true, true, msg_);
  1191. download.initiated_ = true;
  1192. }
  1193. }
  1194. void Connection::SendPackageError(const String& name)
  1195. {
  1196. msg_.Clear();
  1197. msg_.WriteStringHash(name);
  1198. SendMessage(MSG_PACKAGEDATA, true, false, msg_);
  1199. }
  1200. void Connection::OnSceneLoadFailed()
  1201. {
  1202. sceneLoaded_ = false;
  1203. using namespace NetworkSceneLoadFailed;
  1204. VariantMap& eventData = GetEventDataMap();
  1205. eventData[P_CONNECTION] = this;
  1206. SendEvent(E_NETWORKSCENELOADFAILED, eventData);
  1207. }
  1208. void Connection::OnPackageDownloadFailed(const String& name)
  1209. {
  1210. LOGERROR("Download of package " + name + " failed");
  1211. // As one package failed, we can not join the scene in any case. Clear the downloads
  1212. downloads_.Clear();
  1213. OnSceneLoadFailed();
  1214. }
  1215. void Connection::OnPackagesReady()
  1216. {
  1217. if (!scene_)
  1218. return;
  1219. // if sceneLoaded_ is true, then we are at sync state - skip loading, else we at start - loading scene from xml.
  1220. // FIXIT: may be use another flag - syncState_, and set it on start syncing and reset here.
  1221. if (sceneLoaded_)
  1222. return;
  1223. if (sceneFileName_.Empty())
  1224. {
  1225. // If the scene filename is empty, just clear the scene of all existing replicated content, and send the loaded reply
  1226. scene_->Clear(true, false);
  1227. sceneLoaded_ = true;
  1228. msg_.Clear();
  1229. msg_.WriteUInt(scene_->GetChecksum());
  1230. SendMessage(MSG_SCENELOADED, true, true, msg_);
  1231. }
  1232. else
  1233. {
  1234. // Otherwise start the async loading process
  1235. String extension = GetExtension(sceneFileName_);
  1236. SharedPtr<File> file = GetSubsystem<ResourceCache>()->GetFile(sceneFileName_);
  1237. bool success;
  1238. if (extension == ".xml")
  1239. success = scene_->LoadAsyncXML(file);
  1240. else
  1241. success = scene_->LoadAsync(file);
  1242. if (!success)
  1243. OnSceneLoadFailed();
  1244. }
  1245. }
  1246. void Connection::SyncPackages(bool allClients)
  1247. {
  1248. if (!scene_)
  1249. return;
  1250. if (IsClient())
  1251. {
  1252. // If we are server, then broadcast message to all clients
  1253. SendSyncPackagesInfo(true);
  1254. }
  1255. else
  1256. {
  1257. // If we are client. then send message to server to send required packages info
  1258. msg_.Clear();
  1259. msg_.WriteBool(allClients);
  1260. SendMessage(MSG_SYNCPACKAGES, true, false, msg_);
  1261. }
  1262. }
  1263. void Connection::SendSyncPackagesInfo(bool allClients)
  1264. {
  1265. const Vector<SharedPtr<PackageFile> >& packages = scene_->GetRequiredPackageFiles();
  1266. unsigned numPackages = packages.Size();
  1267. if (numPackages == 0)
  1268. return;
  1269. if (!CheckPackagesUpdate())
  1270. return;
  1271. cachedPackages_.Clear();
  1272. msg_.Clear();
  1273. msg_.WriteVLE(numPackages);
  1274. for (unsigned i = 0; i < numPackages; ++i)
  1275. {
  1276. PackageFile* package = packages[i];
  1277. String filename = GetFileNameAndExtension(package->GetName());
  1278. cachedPackages_.Push(filename);
  1279. msg_.WriteString(filename);
  1280. msg_.WriteUInt(package->GetTotalSize());
  1281. msg_.WriteUInt(package->GetChecksum());
  1282. }
  1283. if (allClients)
  1284. {
  1285. Network* network = GetSubsystem<Network>();
  1286. network->BroadcastMessage(MSG_SYNCPACKAGESINFO, true, true, msg_);
  1287. }
  1288. else
  1289. SendMessage(MSG_SYNCPACKAGESINFO, true, true, msg_);
  1290. }
  1291. void Connection::ProcessSyncPackages(int msgID, MemoryBuffer& msg)
  1292. {
  1293. if (!scene_)
  1294. return;
  1295. if (!IsClient())
  1296. {
  1297. LOGWARNING("Received unexpected Sync message from server");
  1298. return;
  1299. }
  1300. // Server receive MSG_SYNCPACKAGES message. Server send info about all required packages to client
  1301. SendSyncPackagesInfo(msg.ReadBool());
  1302. }
  1303. void Connection::ProcessSyncPackagesInfo(int msgID, MemoryBuffer& msg)
  1304. {
  1305. if (!scene_)
  1306. return;
  1307. if (IsClient())
  1308. {
  1309. LOGWARNING("Received unexpected Sync packages info message from server");
  1310. return;
  1311. }
  1312. // Now client check all available packages and retrieve missing packages
  1313. Network* network = GetSubsystem<Network>();
  1314. const String& packageCacheDir = network->GetPackageCacheDir();
  1315. if (packageCacheDir.Empty())
  1316. {
  1317. LOGERROR("Can not download required packages, as package cache directory is not set");
  1318. return;
  1319. }
  1320. ResourceCache* cache = GetSubsystem<ResourceCache>();
  1321. Vector<SharedPtr<PackageFile> > packages = cache->GetPackageFiles();
  1322. // Now check which packages we have in the resource cache or in the download cache, and which we need to download
  1323. unsigned numPackages = msg.ReadVLE();
  1324. Vector<String> downloadedPackages;
  1325. if (!packageCacheDir.Empty())
  1326. GetSubsystem<FileSystem>()->ScanDir(downloadedPackages, packageCacheDir, "*.*", SCAN_FILES, false);
  1327. for (unsigned i = 0; i < numPackages; ++i)
  1328. {
  1329. String name = msg.ReadString();
  1330. unsigned fileSize = msg.ReadUInt();
  1331. unsigned checksum = msg.ReadUInt();
  1332. String checksumString = ToStringHex(checksum);
  1333. bool found = false;
  1334. // Check first the resource cache
  1335. for (unsigned j = 0; j < packages.Size(); ++j)
  1336. {
  1337. PackageFile* package = packages[j];
  1338. if (!GetFileNameAndExtension(package->GetName()).Compare(name, false) && package->GetTotalSize() == fileSize &&
  1339. package->GetChecksum() == checksum)
  1340. {
  1341. found = true;
  1342. break;
  1343. }
  1344. }
  1345. // Then the download cache
  1346. for (unsigned j = 0; j < downloadedPackages.Size(); ++j)
  1347. {
  1348. const String& fileName = downloadedPackages[j];
  1349. if (!fileName.Find(checksumString) && !fileName.Substring(9).Compare(name, false))
  1350. {
  1351. // Name matches. Check filesize and actual checksum to be sure
  1352. SharedPtr<PackageFile> newPackage(new PackageFile(context_, packageCacheDir + fileName));
  1353. if (newPackage->GetTotalSize() == fileSize && newPackage->GetChecksum() == checksum)
  1354. {
  1355. // Add the package to the resource system now, as we will need it to load the scene
  1356. cache->AddPackageFile(newPackage, true);
  1357. found = true;
  1358. break;
  1359. }
  1360. }
  1361. }
  1362. // Package not found, need to request a download
  1363. if (!found)
  1364. RequestPackage(name, fileSize, checksum);
  1365. } // for
  1366. }
  1367. bool Connection::CheckPackagesUpdate()
  1368. {
  1369. if (cachedPackages_.Empty())
  1370. return true;
  1371. const Vector<SharedPtr<PackageFile> >& packages = scene_->GetRequiredPackageFiles();
  1372. unsigned numPackages = packages.Size();
  1373. for (unsigned i = 0; i < numPackages; ++i)
  1374. {
  1375. PackageFile* package = packages[i];
  1376. String filename = GetFileNameAndExtension(package->GetName());
  1377. if (!cachedPackages_.Contains(filename))
  1378. return true;
  1379. }
  1380. return false;
  1381. }
  1382. }