connectionManager.cpp 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  1. /*
  2. * Copyright (c) Contributors to the Open 3D Engine Project.
  3. * For complete copyright and license terms please see the LICENSE at the root of this distribution.
  4. *
  5. * SPDX-License-Identifier: Apache-2.0 OR MIT
  6. *
  7. */
  8. #include "connectionManager.h"
  9. #include "connection.h"
  10. #include <QSettings>
  11. #include <QHostInfo>
  12. #include <QNetworkInterface>
  13. #include "native/utilities/assetUtils.h"
  14. namespace
  15. {
  16. // singleton Pattern
  17. ConnectionManager* s_singleton = nullptr;
  18. QString TranslateStatus(int status)
  19. {
  20. switch (status)
  21. {
  22. case 0:
  23. return QObject::tr("Disconnected");
  24. case 1:
  25. return QObject::tr("Connected");
  26. case 2:
  27. return QObject::tr("Connecting");
  28. default:
  29. return "";
  30. }
  31. }
  32. }
  33. ConnectionManager::ConnectionManager(QObject* parent)
  34. : QAbstractItemModel(parent)
  35. , m_nextConnectionId(1)
  36. {
  37. Q_ASSERT(!s_singleton);
  38. s_singleton = this;
  39. qRegisterMetaType<qintptr>("qintptr");
  40. qRegisterMetaType<quint16>("quint16");
  41. qRegisterMetaType<QHostAddress>("QHostAddress");
  42. ConnectionManagerRequestBus::Handler::BusConnect();
  43. QTimer::singleShot(0, this, SLOT(UpdateAllowedListFromBootStrap()));
  44. }
  45. void ConnectionManager::UpdateAllowedListFromBootStrap()
  46. {
  47. m_allowedListAddresses.clear();
  48. QString allowedlist = AssetUtilities::ReadAllowedlistFromSettingsRegistry();
  49. AZStd::vector<AZStd::string> allowedlistaddressList;
  50. AzFramework::StringFunc::Tokenize(allowedlist.toUtf8().constData(), allowedlistaddressList, ", \t\n\r");
  51. for (const AZStd::string& allowedlistaddress : allowedlistaddressList)
  52. {
  53. m_allowedListAddresses << allowedlistaddress.c_str();
  54. }
  55. Q_EMIT SyncAllowedListAndRejectedList(m_allowedListAddresses, m_rejectedAddresses);
  56. }
  57. ConnectionManager::~ConnectionManager()
  58. {
  59. ConnectionManagerRequestBus::Handler::BusDisconnect();
  60. s_singleton = nullptr;
  61. }
  62. ConnectionManager* ConnectionManager::Get()
  63. {
  64. return s_singleton;
  65. }
  66. int ConnectionManager::getCount() const
  67. {
  68. return m_connectionMap.size();
  69. }
  70. Connection* ConnectionManager::getConnection(unsigned int connectionId)
  71. {
  72. auto iter = m_connectionMap.find(connectionId);
  73. if (iter != m_connectionMap.end())
  74. {
  75. return iter.value();
  76. }
  77. return nullptr;
  78. }
  79. ConnectionMap& ConnectionManager::getConnectionMap()
  80. {
  81. return m_connectionMap;
  82. }
  83. unsigned int ConnectionManager::addConnection(qintptr socketDescriptor)
  84. {
  85. const bool isUserConnection = false;
  86. return internalAddConnection(isUserConnection, socketDescriptor);
  87. }
  88. unsigned int ConnectionManager::internalAddConnection(bool isUserConnection, qintptr socketDescriptor)
  89. {
  90. auto connectionId = m_nextConnectionId++;
  91. // If the connectionId grows, we are appending, otherwise we're inserting at the front
  92. if (connectionId < m_nextConnectionId)
  93. {
  94. beginInsertRows(QModelIndex(), m_connectionMap.count(), m_connectionMap.count());
  95. }
  96. else
  97. {
  98. beginInsertRows(QModelIndex(), 0, 0);
  99. }
  100. Connection* connection = new Connection(isUserConnection, socketDescriptor, this);
  101. connect(connection, &Connection::IsAddressInAllowedList, this, &ConnectionManager::IsAddressInAllowedList);
  102. connect(this, &ConnectionManager::AddressIsInAllowedList, connection, &Connection::AddressIsInAllowedList);
  103. connection->SetConnectionId(connectionId);
  104. connect(connection, &Connection::StatusChanged, this, &ConnectionManager::OnStatusChanged);
  105. connect(connection, &Connection::DeliverMessage, this, &ConnectionManager::RouteIncomingMessage);
  106. connect(connection, SIGNAL(DisconnectConnection(unsigned int)), this, SIGNAL(ConnectionDisconnected(unsigned int)));
  107. connect(connection, SIGNAL(ConnectionDestroyed(unsigned int)), this, SLOT(RemoveConnectionFromMap(unsigned int)));
  108. connect(connection, SIGNAL(Error(unsigned int, QString)), this, SIGNAL(ConnectionError(unsigned int, QString)));
  109. connect(connection, &Connection::ConnectionReady, this, &ConnectionManager::ConnectionReady);
  110. m_connectionMap.insert(connectionId, connection);
  111. Q_EMIT connectionAdded(connectionId, connection);
  112. endInsertRows();
  113. connection->Activate(socketDescriptor);
  114. return connectionId;
  115. }
  116. unsigned int ConnectionManager::addUserConnection()
  117. {
  118. const bool isUserConnection = true;
  119. unsigned int newConnectionId = internalAddConnection(isUserConnection);
  120. SaveConnections();
  121. return newConnectionId;
  122. }
  123. void ConnectionManager::OnStatusChanged(unsigned int connId)
  124. {
  125. QList<unsigned int> keys = m_connectionMap.keys();
  126. int row = keys.indexOf(connId);
  127. QModelIndex theIndex = index(row, 0, QModelIndex());
  128. QModelIndex theIndexLastColumn = index(row, Column::Max, QModelIndex());
  129. Q_EMIT dataChanged(theIndex, theIndexLastColumn);
  130. // Here, we only want to emit the bus event when the very last of a particular platform leaves, or the first one joins, so we keep a count:
  131. auto foundElement = m_connectionMap.find(connId);
  132. if (foundElement == m_connectionMap.end())
  133. {
  134. return;
  135. }
  136. Connection* connection = foundElement.value();
  137. QStringList assetPlatforms = connection->AssetPlatforms();
  138. if (connection->Status() == Connection::Connected)
  139. {
  140. int priorCount = 0;
  141. for (const auto& thisPlatform : assetPlatforms)
  142. {
  143. auto existingEntry = m_platformsConnected.find(thisPlatform);
  144. if (existingEntry == m_platformsConnected.end())
  145. {
  146. m_platformsConnected.insert(thisPlatform, 1);
  147. }
  148. else
  149. {
  150. priorCount = existingEntry.value();
  151. m_platformsConnected[thisPlatform] = priorCount + 1;
  152. }
  153. if (priorCount == 0)
  154. {
  155. AssetProcessorPlatformBus::Broadcast(
  156. &AssetProcessorPlatformBus::Events::AssetProcessorPlatformConnected, thisPlatform.toUtf8().data());
  157. }
  158. }
  159. }
  160. else
  161. {
  162. for (const auto& thisPlatform : assetPlatforms)
  163. {
  164. // connection dropped!
  165. int priorCount = m_platformsConnected[thisPlatform];
  166. m_platformsConnected[thisPlatform] = priorCount - 1;
  167. if (priorCount == 1)
  168. {
  169. AssetProcessorPlatformBus::Broadcast(
  170. &AssetProcessorPlatformBus::Events::AssetProcessorPlatformDisconnected, thisPlatform.toUtf8().data());
  171. }
  172. }
  173. }
  174. }
  175. QModelIndex ConnectionManager::parent(const QModelIndex& /*index*/) const
  176. {
  177. return QModelIndex();
  178. }
  179. QModelIndex ConnectionManager::index(int row, int column, const QModelIndex& parent) const
  180. {
  181. if (row >= rowCount(parent) || column >= columnCount(parent))
  182. {
  183. return QModelIndex();
  184. }
  185. return createIndex(row, column);
  186. }
  187. int ConnectionManager::columnCount(const QModelIndex& parent) const
  188. {
  189. return parent.isValid() ? 0 : Column::Max;
  190. }
  191. int ConnectionManager::rowCount(const QModelIndex& parent) const
  192. {
  193. if (parent.isValid())
  194. {
  195. return 0;
  196. }
  197. return m_connectionMap.count();
  198. }
  199. Connection* ConnectionManager::FindConnection(const QModelIndex& index) const
  200. {
  201. if (!index.isValid())
  202. {
  203. return nullptr;
  204. }
  205. if (index.row() >= m_connectionMap.count())
  206. {
  207. return nullptr;
  208. }
  209. QList<unsigned int> keys = m_connectionMap.keys();
  210. int key = keys[index.row()];
  211. auto connectionIter = m_connectionMap.find(key);
  212. if (connectionIter == m_connectionMap.end())
  213. {
  214. return nullptr;
  215. }
  216. return connectionIter.value();
  217. }
  218. QVariant ConnectionManager::data(const QModelIndex& index, int role) const
  219. {
  220. Connection* connection = FindConnection(index);
  221. if (!connection)
  222. {
  223. return QVariant();
  224. }
  225. bool isUserConnection = connection->UserCreatedConnection();
  226. switch (role)
  227. {
  228. case UserConnectionRole:
  229. return isUserConnection;
  230. case Qt::ToolTipRole:
  231. switch (index.column())
  232. {
  233. case IdColumn:
  234. if (!isUserConnection)
  235. {
  236. return QObject::tr("This connection was triggered automatically by another process connecting to the Asset Processor and can not be edited");
  237. }
  238. break;
  239. }
  240. break;
  241. case Qt::CheckStateRole:
  242. if (index.column() == AutoConnectColumn && isUserConnection)
  243. {
  244. return connection->AutoConnect() ? Qt::Checked : Qt::Unchecked;
  245. }
  246. break;
  247. case Qt::EditRole:
  248. case Qt::DisplayRole:
  249. switch (index.column())
  250. {
  251. case StatusColumn:
  252. return TranslateStatus(connection->Status());
  253. case IdColumn:
  254. return connection->Identifier();
  255. case IpColumn:
  256. return connection->IpAddress();
  257. case PortColumn:
  258. return connection->Port();
  259. case PlatformColumn:
  260. return connection->AssetPlatforms().join(',');
  261. case AutoConnectColumn:
  262. if (!isUserConnection)
  263. {
  264. return tr("Auto");
  265. }
  266. return QVariant();
  267. }
  268. break;
  269. }
  270. return QVariant();
  271. }
  272. Qt::ItemFlags ConnectionManager::flags(const QModelIndex& index) const
  273. {
  274. Connection* connection = FindConnection(index);
  275. if (!connection)
  276. {
  277. return QAbstractItemModel::flags(index);
  278. }
  279. bool isUserConnection = connection->UserCreatedConnection();
  280. if ((index.column() == IdColumn || index.column() == IpColumn || index.column() == PortColumn) && isUserConnection)
  281. {
  282. return Qt::ItemFlags(Qt::ItemIsSelectable) | Qt::ItemIsEnabled;
  283. }
  284. if (index.column() == AutoConnectColumn)
  285. {
  286. Qt::ItemFlags autoConnectFlags = Qt::ItemFlags(Qt::ItemIsSelectable) | Qt::ItemIsEnabled;
  287. if (isUserConnection)
  288. {
  289. autoConnectFlags |= (Qt::ItemIsUserCheckable);
  290. }
  291. return autoConnectFlags;
  292. }
  293. return Qt::ItemIsSelectable | Qt::ItemIsEnabled;
  294. }
  295. QVariant ConnectionManager::headerData(int section, Qt::Orientation orientation, int role) const
  296. {
  297. if (orientation == Qt::Horizontal && role == Qt::DisplayRole)
  298. {
  299. switch (section)
  300. {
  301. case StatusColumn:
  302. return tr("Status");
  303. case IdColumn:
  304. return tr("ID");
  305. case IpColumn:
  306. return tr("IP");
  307. case PortColumn:
  308. return tr("Port");
  309. case PlatformColumn:
  310. return tr("Platform");
  311. case AutoConnectColumn:
  312. return tr("Enabled");
  313. default:
  314. break;
  315. }
  316. }
  317. return QAbstractItemModel::headerData(section, orientation, role);
  318. }
  319. bool ConnectionManager::setData(const QModelIndex& index, const QVariant& value, int /*role*/)
  320. {
  321. if (!index.isValid())
  322. {
  323. return false;
  324. }
  325. QList<unsigned int> keys = m_connectionMap.keys();
  326. int key = keys[index.row()];
  327. Connection* connection = m_connectionMap[key];
  328. switch (index.column())
  329. {
  330. case PortColumn:
  331. connection->SetPort(value.toInt());
  332. break;
  333. case IpColumn:
  334. connection->SetIpAddress(value.toString());
  335. break;
  336. case IdColumn:
  337. connection->SetIdentifier(value.toString());
  338. break;
  339. case AutoConnectColumn:
  340. connection->SetAutoConnect(value.toBool());
  341. break;
  342. default:
  343. break;
  344. }
  345. Q_EMIT dataChanged(index, index);
  346. if (connection->UserCreatedConnection())
  347. {
  348. SaveConnections();
  349. }
  350. return true;
  351. }
  352. unsigned int ConnectionManager::GetConnectionId(QString ipaddress, int port)
  353. {
  354. for (auto iter = m_connectionMap.begin(); iter != m_connectionMap.end(); iter++)
  355. {
  356. if ((iter.value()->IpAddress().compare(ipaddress, Qt::CaseInsensitive) == 0) && iter.value()->Port() == port)
  357. {
  358. return iter.value()->ConnectionId();
  359. }
  360. }
  361. return 0;
  362. }
  363. void ConnectionManager::removeConnection(const QModelIndex& index)
  364. {
  365. if (!index.isValid())
  366. {
  367. return;
  368. }
  369. if (index.row() >= m_connectionMap.count())
  370. {
  371. return;
  372. }
  373. QList<unsigned int> keys = m_connectionMap.keys();
  374. int key = keys[index.row()];
  375. removeConnection(key);
  376. // Normally, removing a connection will cause RemoveConnectionFromMap to be called
  377. // later, when the connection is fully removed. However, SaveConnections stores all
  378. // user created connections, so this connection needs to be removed early.
  379. // RemoveConnectionFromMap doesn't call SaveConnections because asset builders connect
  380. // and disconnection shouldn't cause the settings to get saved constantly.
  381. // This does mean RemoveConnectionFromMap is called twice, but that's OK because it won't find
  382. // the key and it will safely handle that situation.
  383. RemoveConnectionFromMap(key);
  384. SaveConnections();
  385. }
  386. void ConnectionManager::SaveConnections(QString settingPrefix)
  387. {
  388. QSettings settings;
  389. settings.beginWriteArray(QStringLiteral("%1Connections").arg(settingPrefix));
  390. int idx = 0;
  391. for (auto iter = m_connectionMap.begin(); iter != m_connectionMap.end(); iter++)
  392. {
  393. if (iter.value()->UserCreatedConnection())
  394. {
  395. settings.setArrayIndex(idx);
  396. iter.value()->SaveConnection(settings);
  397. idx++;
  398. }
  399. }
  400. settings.endArray();
  401. }
  402. void ConnectionManager::LoadConnections(QString settingPrefix)
  403. {
  404. QSettings settings;
  405. int numElement = settings.beginReadArray(QStringLiteral("%1Connections").arg(settingPrefix));
  406. for (int idx = 0; idx < numElement; ++idx)
  407. {
  408. settings.setArrayIndex(idx);
  409. getConnection(addConnection())->LoadConnection(settings);
  410. }
  411. settings.endArray();
  412. }
  413. void ConnectionManager::RegisterService(unsigned int type, regFunc func)
  414. {
  415. m_messageRoute.insert(type, func);
  416. }
  417. void ConnectionManager::NewConnection(qintptr socketDescriptor)
  418. {
  419. addConnection(socketDescriptor);
  420. }
  421. void ConnectionManager::AllowedListingEnabled(bool enabled)
  422. {
  423. m_allowedListingEnabled = enabled;
  424. }
  425. void ConnectionManager::AddAddressToAllowedList(QString address)
  426. {
  427. UpdateAllowedListFromBootStrap();
  428. while (m_allowedListAddresses.removeOne(address)) {}
  429. m_allowedListAddresses << address;
  430. AssetUtilities::WriteAllowedlistToSettingsRegistry(m_allowedListAddresses);
  431. Q_EMIT SyncAllowedListAndRejectedList(m_allowedListAddresses, m_rejectedAddresses);
  432. }
  433. void ConnectionManager::RemoveAddressFromAllowedList(QString address)
  434. {
  435. UpdateAllowedListFromBootStrap();
  436. while (m_allowedListAddresses.removeOne(address)) {}
  437. AssetUtilities::WriteAllowedlistToSettingsRegistry(m_allowedListAddresses);
  438. Q_EMIT SyncAllowedListAndRejectedList(m_allowedListAddresses, m_rejectedAddresses);
  439. }
  440. void ConnectionManager::AddRejectedAddress(QString address, bool surpressWarning)
  441. {
  442. UpdateAllowedListFromBootStrap();
  443. bool alreadyRejected = false;
  444. while (m_rejectedAddresses.removeOne(address)) { alreadyRejected = true; }
  445. m_rejectedAddresses << address;
  446. if (!surpressWarning && !alreadyRejected)
  447. {
  448. Q_EMIT FirstTimeAddedToRejctedList(address);
  449. }
  450. Q_EMIT SyncAllowedListAndRejectedList(m_allowedListAddresses, m_rejectedAddresses);
  451. }
  452. void ConnectionManager::RemoveRejectedAddress(QString address)
  453. {
  454. UpdateAllowedListFromBootStrap();
  455. while (m_rejectedAddresses.removeOne(address)) {}
  456. Q_EMIT SyncAllowedListAndRejectedList(m_allowedListAddresses, m_rejectedAddresses);
  457. }
  458. void ConnectionManager::IsAddressInAllowedList(QHostAddress incominghostaddr, void* token)
  459. {
  460. if (!m_allowedListingEnabled)
  461. {
  462. Q_EMIT AddressIsInAllowedList(token, true);
  463. return;
  464. }
  465. QString incomingIpAddress;
  466. if (incominghostaddr != QHostAddress::Null)
  467. {
  468. // any ipv4 address will be like ::ffff:A.B.C.D, we have to retrieve A.B.C.D for comparision with the allowedlisted addresses.
  469. // for example Qt will tell us the ipv6 (::ffff:127.0.0.1) for 127.0.0.1,
  470. // but the allowedlist below will report ipv4 (127.0.0.1) and ipv6 (::1), and they both won't match to ipv6 (::ffff:127.0.0.1).
  471. bool wasConverted = false;
  472. quint32 incomingIPv4 = incominghostaddr.toIPv4Address(&wasConverted);
  473. if (wasConverted)
  474. {
  475. incomingIpAddress = QHostAddress(incomingIPv4).toString();
  476. }
  477. else
  478. {
  479. incomingIpAddress = incominghostaddr.toString();
  480. }
  481. }
  482. QHostInfo incomingInfo = QHostInfo::fromName(incomingIpAddress);
  483. QString allowedlist = AssetUtilities::ReadAllowedlistFromSettingsRegistry();
  484. AZStd::vector<AZStd::string> allowedlistaddressList;
  485. AzFramework::StringFunc::Tokenize(allowedlist.toUtf8().constData(), allowedlistaddressList, ", \t\n\r");
  486. // allow localhost, loopback regardless, there's no good reason to accidentally lock yourself out your own computer.
  487. for (const auto& address : QNetworkInterface::allAddresses()) // allAdresses returns the ip address of all local interfaces.
  488. {
  489. allowedlistaddressList.push_back(address.toString().toUtf8().constData());
  490. }
  491. // does the incoming connection match any entries?
  492. for (const AZStd::string& allowedListaddress : allowedlistaddressList)
  493. {
  494. //address range matching
  495. size_t maskLocation = allowedListaddress.find('/');
  496. if (maskLocation != AZStd::string::npos)
  497. {
  498. //x.x.x.x/0 is all addresses
  499. int mask = atoi(allowedListaddress.substr(maskLocation + 1).c_str());
  500. if (mask == 0)
  501. {
  502. Q_EMIT AddressIsInAllowedList(token, true);
  503. return;
  504. }
  505. else
  506. {
  507. //If we successfully convert to an ipv4 address then the incominghostaddr MAY have been in an ipv6 representation of
  508. //an ipv4 address. If this is the case the protocol of the incominghostaddr will be ipv6, this will cause the isInSubnet call
  509. //to fail even it correctly matches because of mismatching protocols. To get around this create a new host address from the incomingIpAddress
  510. //so that if it was an ipv6 representation of ipv4, then creating it directly from the ipv4 will allow the protocol check to pass
  511. //and wont fail the subnet check just because the ipv4 was represented in ipv6 format. This is due to an early out check QT does for protocol and in my opinion,
  512. //QT should first do a check and see if we are comparing convertible protocols before just early outing.
  513. //If it wasn't convertible to ipv4 then we know it is ipv6 in which case the protocols will match which make this unnecessary, but still correct.
  514. QHostAddress ha(incomingIpAddress);
  515. if (ha.isInSubnet(QHostAddress::parseSubnet(allowedListaddress.c_str())))
  516. {
  517. Q_EMIT AddressIsInAllowedList(token, true);
  518. return;
  519. }
  520. }
  521. }
  522. else//direct address matching
  523. {
  524. QHostInfo allowedInfo;
  525. QHostAddress allowedHostAddress(allowedListaddress.c_str());
  526. if ((allowedHostAddress.isNull()))
  527. {
  528. allowedInfo = QHostInfo::fromName(QString::fromUtf8(allowedListaddress.c_str()));
  529. }
  530. else
  531. {
  532. QList<QHostAddress> addresses;
  533. addresses << allowedHostAddress;
  534. allowedInfo.setAddresses(addresses);
  535. }
  536. for (const auto& allowedAddress : allowedInfo.addresses())
  537. {
  538. for (const auto& address : incomingInfo.addresses())
  539. {
  540. if (address == allowedAddress)
  541. {
  542. Q_EMIT AddressIsInAllowedList(token, true);
  543. return;
  544. }
  545. }
  546. }
  547. }
  548. }
  549. AddRejectedAddress(incomingIpAddress);
  550. Q_EMIT AddressIsInAllowedList(token, false);
  551. }
  552. void ConnectionManager::AddBytesReceived(unsigned int connId, qint64 add, bool update)
  553. {
  554. auto iter = m_connectionMap.find(connId);
  555. if (iter != m_connectionMap.end())
  556. {
  557. iter.value()->AddBytesReceived(add, update);
  558. }
  559. }
  560. void ConnectionManager::AddBytesSent(unsigned int connId, qint64 add, bool update)
  561. {
  562. auto iter = m_connectionMap.find(connId);
  563. if (iter != m_connectionMap.end())
  564. {
  565. iter.value()->AddBytesSent(add, update);
  566. }
  567. }
  568. void ConnectionManager::AddBytesRead(unsigned int connId, qint64 add, bool update)
  569. {
  570. auto iter = m_connectionMap.find(connId);
  571. if (iter != m_connectionMap.end())
  572. {
  573. iter.value()->AddBytesRead(add, update);
  574. }
  575. }
  576. void ConnectionManager::AddBytesWritten(unsigned int connId, qint64 add, bool update)
  577. {
  578. auto iter = m_connectionMap.find(connId);
  579. if (iter != m_connectionMap.end())
  580. {
  581. iter.value()->AddBytesWritten(add, update);
  582. }
  583. }
  584. void ConnectionManager::AddOpenRequest(unsigned int connId, bool update)
  585. {
  586. auto iter = m_connectionMap.find(connId);
  587. if (iter != m_connectionMap.end())
  588. {
  589. iter.value()->AddOpenRequest(update);
  590. }
  591. }
  592. void ConnectionManager::AddCloseRequest(unsigned int connId, bool update)
  593. {
  594. auto iter = m_connectionMap.find(connId);
  595. if (iter != m_connectionMap.end())
  596. {
  597. iter.value()->AddCloseRequest(update);
  598. }
  599. }
  600. void ConnectionManager::AddOpened(unsigned int connId, bool update)
  601. {
  602. auto iter = m_connectionMap.find(connId);
  603. if (iter != m_connectionMap.end())
  604. {
  605. iter.value()->AddOpened(update);
  606. }
  607. }
  608. void ConnectionManager::AddClosed(unsigned int connId, bool update)
  609. {
  610. auto iter = m_connectionMap.find(connId);
  611. if (iter != m_connectionMap.end())
  612. {
  613. iter.value()->AddClosed(update);
  614. }
  615. }
  616. void ConnectionManager::AddReadRequest(unsigned int connId, bool update)
  617. {
  618. auto iter = m_connectionMap.find(connId);
  619. if (iter != m_connectionMap.end())
  620. {
  621. iter.value()->AddReadRequest(update);
  622. }
  623. }
  624. void ConnectionManager::AddWriteRequest(unsigned int connId, bool update)
  625. {
  626. auto iter = m_connectionMap.find(connId);
  627. if (iter != m_connectionMap.end())
  628. {
  629. iter.value()->AddWriteRequest(update);
  630. }
  631. }
  632. void ConnectionManager::AddTellRequest(unsigned int connId, bool update)
  633. {
  634. auto iter = m_connectionMap.find(connId);
  635. if (iter != m_connectionMap.end())
  636. {
  637. iter.value()->AddTellRequest(update);
  638. }
  639. }
  640. void ConnectionManager::AddSeekRequest(unsigned int connId, bool update)
  641. {
  642. auto iter = m_connectionMap.find(connId);
  643. if (iter != m_connectionMap.end())
  644. {
  645. iter.value()->AddSeekRequest(update);
  646. }
  647. }
  648. void ConnectionManager::AddIsReadOnlyRequest(unsigned int connId, bool update)
  649. {
  650. auto iter = m_connectionMap.find(connId);
  651. if (iter != m_connectionMap.end())
  652. {
  653. iter.value()->AddIsReadOnlyRequest(update);
  654. }
  655. }
  656. void ConnectionManager::AddIsDirectoryRequest(unsigned int connId, bool update)
  657. {
  658. auto iter = m_connectionMap.find(connId);
  659. if (iter != m_connectionMap.end())
  660. {
  661. iter.value()->AddIsDirectoryRequest(update);
  662. }
  663. }
  664. void ConnectionManager::AddSizeRequest(unsigned int connId, bool update)
  665. {
  666. auto iter = m_connectionMap.find(connId);
  667. if (iter != m_connectionMap.end())
  668. {
  669. iter.value()->AddSizeRequest(update);
  670. }
  671. }
  672. void ConnectionManager::AddModificationTimeRequest(unsigned int connId, bool update)
  673. {
  674. auto iter = m_connectionMap.find(connId);
  675. if (iter != m_connectionMap.end())
  676. {
  677. iter.value()->AddModificationTimeRequest(update);
  678. }
  679. }
  680. void ConnectionManager::AddExistsRequest(unsigned int connId, bool update)
  681. {
  682. auto iter = m_connectionMap.find(connId);
  683. if (iter != m_connectionMap.end())
  684. {
  685. iter.value()->AddExistsRequest(update);
  686. }
  687. }
  688. void ConnectionManager::AddFlushRequest(unsigned int connId, bool update)
  689. {
  690. auto iter = m_connectionMap.find(connId);
  691. if (iter != m_connectionMap.end())
  692. {
  693. iter.value()->AddFlushRequest(update);
  694. }
  695. }
  696. void ConnectionManager::AddCreatePathRequest(unsigned int connId, bool update)
  697. {
  698. auto iter = m_connectionMap.find(connId);
  699. if (iter != m_connectionMap.end())
  700. {
  701. iter.value()->AddCreatePathRequest(update);
  702. }
  703. }
  704. void ConnectionManager::AddDestroyPathRequest(unsigned int connId, bool update)
  705. {
  706. auto iter = m_connectionMap.find(connId);
  707. if (iter != m_connectionMap.end())
  708. {
  709. iter.value()->AddDestroyPathRequest(update);
  710. }
  711. }
  712. void ConnectionManager::AddRemoveRequest(unsigned int connId, bool update)
  713. {
  714. auto iter = m_connectionMap.find(connId);
  715. if (iter != m_connectionMap.end())
  716. {
  717. iter.value()->AddRemoveRequest(update);
  718. }
  719. }
  720. void ConnectionManager::AddCopyRequest(unsigned int connId, bool update)
  721. {
  722. auto iter = m_connectionMap.find(connId);
  723. if (iter != m_connectionMap.end())
  724. {
  725. iter.value()->AddCopyRequest(update);
  726. }
  727. }
  728. void ConnectionManager::AddRenameRequest(unsigned int connId, bool update)
  729. {
  730. auto iter = m_connectionMap.find(connId);
  731. if (iter != m_connectionMap.end())
  732. {
  733. iter.value()->AddRenameRequest(update);
  734. }
  735. }
  736. void ConnectionManager::AddFindFileNamesRequest(unsigned int connId, bool update)
  737. {
  738. auto iter = m_connectionMap.find(connId);
  739. if (iter != m_connectionMap.end())
  740. {
  741. iter.value()->AddFindFileNamesRequest(update);
  742. }
  743. }
  744. void ConnectionManager::UpdateBytesReceived(unsigned int connId)
  745. {
  746. auto iter = m_connectionMap.find(connId);
  747. if (iter != m_connectionMap.end())
  748. {
  749. iter.value()->UpdateBytesReceived();
  750. }
  751. }
  752. void ConnectionManager::UpdateBytesSent(unsigned int connId)
  753. {
  754. auto iter = m_connectionMap.find(connId);
  755. if (iter != m_connectionMap.end())
  756. {
  757. iter.value()->UpdateBytesSent();
  758. }
  759. }
  760. void ConnectionManager::UpdateBytesRead(unsigned int connId)
  761. {
  762. auto iter = m_connectionMap.find(connId);
  763. if (iter != m_connectionMap.end())
  764. {
  765. iter.value()->UpdateBytesRead();
  766. }
  767. }
  768. void ConnectionManager::UpdateBytesWritten(unsigned int connId)
  769. {
  770. auto iter = m_connectionMap.find(connId);
  771. if (iter != m_connectionMap.end())
  772. {
  773. iter.value()->UpdateBytesWritten();
  774. }
  775. }
  776. void ConnectionManager::UpdateOpenRequest(unsigned int connId)
  777. {
  778. auto iter = m_connectionMap.find(connId);
  779. if (iter != m_connectionMap.end())
  780. {
  781. iter.value()->UpdateOpenRequest();
  782. }
  783. }
  784. void ConnectionManager::UpdateCloseRequest(unsigned int connId)
  785. {
  786. auto iter = m_connectionMap.find(connId);
  787. if (iter != m_connectionMap.end())
  788. {
  789. iter.value()->UpdateCloseRequest();
  790. }
  791. }
  792. void ConnectionManager::UpdateOpened(unsigned int connId)
  793. {
  794. auto iter = m_connectionMap.find(connId);
  795. if (iter != m_connectionMap.end())
  796. {
  797. iter.value()->UpdateOpened();
  798. }
  799. }
  800. void ConnectionManager::UpdateClosed(unsigned int connId)
  801. {
  802. auto iter = m_connectionMap.find(connId);
  803. if (iter != m_connectionMap.end())
  804. {
  805. iter.value()->UpdateClosed();
  806. }
  807. }
  808. void ConnectionManager::UpdateReadRequest(unsigned int connId)
  809. {
  810. auto iter = m_connectionMap.find(connId);
  811. if (iter != m_connectionMap.end())
  812. {
  813. iter.value()->UpdateReadRequest();
  814. }
  815. }
  816. void ConnectionManager::UpdateWriteRequest(unsigned int connId)
  817. {
  818. auto iter = m_connectionMap.find(connId);
  819. if (iter != m_connectionMap.end())
  820. {
  821. iter.value()->UpdateWriteRequest();
  822. }
  823. }
  824. void ConnectionManager::UpdateTellRequest(unsigned int connId)
  825. {
  826. auto iter = m_connectionMap.find(connId);
  827. if (iter != m_connectionMap.end())
  828. {
  829. iter.value()->UpdateTellRequest();
  830. }
  831. }
  832. void ConnectionManager::UpdateSeekRequest(unsigned int connId)
  833. {
  834. auto iter = m_connectionMap.find(connId);
  835. if (iter != m_connectionMap.end())
  836. {
  837. iter.value()->UpdateSeekRequest();
  838. }
  839. }
  840. void ConnectionManager::UpdateIsReadOnlyRequest(unsigned int connId)
  841. {
  842. auto iter = m_connectionMap.find(connId);
  843. if (iter != m_connectionMap.end())
  844. {
  845. iter.value()->UpdateIsReadOnlyRequest();
  846. }
  847. }
  848. void ConnectionManager::UpdateIsDirectoryRequest(unsigned int connId)
  849. {
  850. auto iter = m_connectionMap.find(connId);
  851. if (iter != m_connectionMap.end())
  852. {
  853. iter.value()->UpdateIsDirectoryRequest();
  854. }
  855. }
  856. void ConnectionManager::UpdateSizeRequest(unsigned int connId)
  857. {
  858. auto iter = m_connectionMap.find(connId);
  859. if (iter != m_connectionMap.end())
  860. {
  861. iter.value()->UpdateSizeRequest();
  862. }
  863. }
  864. void ConnectionManager::UpdateModificationTimeRequest(unsigned int connId)
  865. {
  866. auto iter = m_connectionMap.find(connId);
  867. if (iter != m_connectionMap.end())
  868. {
  869. iter.value()->UpdateModificationTimeRequest();
  870. }
  871. }
  872. void ConnectionManager::UpdateExistsRequest(unsigned int connId)
  873. {
  874. auto iter = m_connectionMap.find(connId);
  875. if (iter != m_connectionMap.end())
  876. {
  877. iter.value()->UpdateExistsRequest();
  878. }
  879. }
  880. void ConnectionManager::UpdateFlushRequest(unsigned int connId)
  881. {
  882. auto iter = m_connectionMap.find(connId);
  883. if (iter != m_connectionMap.end())
  884. {
  885. iter.value()->UpdateFlushRequest();
  886. }
  887. }
  888. void ConnectionManager::UpdateCreatePathRequest(unsigned int connId)
  889. {
  890. auto iter = m_connectionMap.find(connId);
  891. if (iter != m_connectionMap.end())
  892. {
  893. iter.value()->UpdateCreatePathRequest();
  894. }
  895. }
  896. void ConnectionManager::UpdateDestroyPathRequest(unsigned int connId)
  897. {
  898. auto iter = m_connectionMap.find(connId);
  899. if (iter != m_connectionMap.end())
  900. {
  901. iter.value()->UpdateDestroyPathRequest();
  902. }
  903. }
  904. void ConnectionManager::UpdateRemoveRequest(unsigned int connId)
  905. {
  906. auto iter = m_connectionMap.find(connId);
  907. if (iter != m_connectionMap.end())
  908. {
  909. iter.value()->UpdateRemoveRequest();
  910. }
  911. }
  912. void ConnectionManager::UpdateCopyRequest(unsigned int connId)
  913. {
  914. auto iter = m_connectionMap.find(connId);
  915. if (iter != m_connectionMap.end())
  916. {
  917. iter.value()->UpdateCopyRequest();
  918. }
  919. }
  920. void ConnectionManager::UpdateRenameRequest(unsigned int connId)
  921. {
  922. auto iter = m_connectionMap.find(connId);
  923. if (iter != m_connectionMap.end())
  924. {
  925. iter.value()->UpdateRenameRequest();
  926. }
  927. }
  928. void ConnectionManager::UpdateFindFileNamesRequest(unsigned int connId)
  929. {
  930. auto iter = m_connectionMap.find(connId);
  931. if (iter != m_connectionMap.end())
  932. {
  933. iter.value()->UpdateFindFileNamesRequest();
  934. }
  935. }
  936. void ConnectionManager::UpdateConnectionMetrics()
  937. {
  938. for (auto iter = m_connectionMap.begin(); iter != m_connectionMap.end(); iter++)
  939. {
  940. iter.value()->UpdateMetrics();
  941. }
  942. }
  943. bool ConnectionManager::IsResponse(unsigned int serial)
  944. {
  945. return serial & AzFramework::AssetSystem::RESPONSE_SERIAL_FLAG;
  946. }
  947. void ConnectionManager::RouteIncomingMessage(unsigned int connId, unsigned int type, unsigned int serial, QByteArray payload)
  948. {
  949. if (IsResponse(serial))
  950. {
  951. serial &= ~AzFramework::AssetSystem::RESPONSE_SERIAL_FLAG; // Clear the bit
  952. getConnection(connId)->InvokeResponseHandler(serial, type, payload);
  953. }
  954. else
  955. {
  956. SendMessageToService(connId, type, serial, payload);
  957. }
  958. }
  959. void ConnectionManager::SendMessageToService(unsigned int connId, unsigned int type, unsigned int serial, QByteArray payload)
  960. {
  961. QString platform = getConnection(connId)->AssetPlatforms().join(',');
  962. auto iter = m_messageRoute.find(type);
  963. while (iter != m_messageRoute.end() && iter.key() == type)
  964. {
  965. iter.value()(connId, type, serial, payload, platform);
  966. iter++;
  967. }
  968. }
  969. //Entry point to removing a connection
  970. //Callable from the GUI or the app about to close
  971. void ConnectionManager::removeConnection(unsigned int connectionId)
  972. {
  973. Connection* conn = getConnection(connectionId);
  974. if (conn)
  975. {
  976. Q_EMIT beforeConnectionRemoved(connectionId);
  977. conn->SetAutoConnect(false);
  978. conn->Terminate();
  979. }
  980. }
  981. void ConnectionManager::RemoveConnectionFromMap(unsigned int connectionId)
  982. {
  983. auto iter = m_connectionMap.find(connectionId);
  984. if (iter != m_connectionMap.end())
  985. {
  986. QList<unsigned int> keys = m_connectionMap.keys();
  987. int row = keys.indexOf(connectionId);
  988. beginRemoveRows(QModelIndex(), row, row);
  989. m_connectionMap.erase(iter);
  990. Q_EMIT ConnectionRemoved(connectionId);
  991. endRemoveRows();
  992. }
  993. }
  994. void ConnectionManager::QuitRequested()
  995. {
  996. QList<Connection*> temporaryList;
  997. for (auto iter = m_connectionMap.begin(); iter != m_connectionMap.end(); iter++)
  998. {
  999. temporaryList.append(iter.value());
  1000. }
  1001. for (auto iter = temporaryList.begin(); iter != temporaryList.end(); iter++)
  1002. {
  1003. (*iter)->Terminate();
  1004. }
  1005. QTimer::singleShot(0, this, SLOT(MakeSureConnectionMapEmpty()));
  1006. }
  1007. void ConnectionManager::MakeSureConnectionMapEmpty()
  1008. {
  1009. if (!m_connectionMap.isEmpty())
  1010. {
  1011. // keep trying to shut connections down, in case some is in an interesting state, where one was being negotiated while we died.
  1012. // note that the end of QuitRequested will ultimately result in this being tried again next time.
  1013. QuitRequested();
  1014. }
  1015. else
  1016. {
  1017. Q_EMIT ReadyToQuit(this);
  1018. }
  1019. }