connectionManager.h 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. #ifndef CONNECTIONMANAGER_H
  9. #define CONNECTIONMANAGER_H
  10. #if !defined(Q_MOC_RUN)
  11. #include <AzCore/std/function/function_fwd.h> // <functional> complains about exception handling and is not okay to mix with azcore/etc stuff.
  12. #include <QReadWriteLock>
  13. #include <QMap>
  14. #include <QMultiMap>
  15. #include <QObject>
  16. #include <QString>
  17. #include <QHostAddress>
  18. #include <QStringListModel>
  19. #include "native/utilities/AssetUtilEBusHelper.h"
  20. #include <QAbstractItemModel>
  21. #endif
  22. class Connection;
  23. //! defines the callback type for registering handlers to handle messages coming from outside into Asset Processor.
  24. //! params are
  25. //! connectionId, who its coming from
  26. //! messageType, type of message, for use when you bind the same handler interface to many different message types
  27. //! int serial number of message, to check for duplicates and also if you want to respond, you must respond with the same number
  28. //! QByteArray payload, the payload of the message
  29. //! QString platform - the platform of the sender ("pc" for example)
  30. typedef AZStd::function<void(unsigned int, unsigned int, unsigned int, QByteArray, QString)> regFunc;
  31. typedef QMap<unsigned int, Connection*> ConnectionMap;
  32. typedef QMultiMap<unsigned int, regFunc> RouteMultiMap;
  33. class ConnectionManagerRequests
  34. : public AZ::EBusTraits
  35. {
  36. public:
  37. virtual void RegisterService(unsigned int messageType, regFunc func) = 0;
  38. };
  39. using ConnectionManagerRequestBus = AZ::EBus<ConnectionManagerRequests>;
  40. namespace AssetProcessor
  41. {
  42. class PlatformConfiguration;
  43. }
  44. /** This is a container class for connection
  45. */
  46. class ConnectionManager
  47. : public QAbstractItemModel,
  48. public ConnectionManagerRequestBus::Handler
  49. {
  50. Q_OBJECT
  51. public:
  52. enum Column
  53. {
  54. StatusColumn,
  55. IdColumn,
  56. IpColumn,
  57. PortColumn,
  58. PlatformColumn,
  59. AutoConnectColumn,
  60. Max
  61. };
  62. enum Roles
  63. {
  64. UserConnectionRole = Qt::UserRole + 1,
  65. };
  66. explicit ConnectionManager(QObject* parent = 0);
  67. virtual ~ConnectionManager();
  68. // Singleton pattern:
  69. static ConnectionManager* Get();
  70. Q_INVOKABLE int getCount() const;
  71. Q_INVOKABLE Connection* getConnection(unsigned int connectionId);
  72. Q_INVOKABLE ConnectionMap& getConnectionMap();
  73. Q_INVOKABLE unsigned int addConnection(qintptr socketDescriptor = -1);
  74. Q_INVOKABLE unsigned int addUserConnection();
  75. Q_INVOKABLE void removeConnection(unsigned int connectionId);
  76. unsigned int GetConnectionId(QString ipaddress, int port);
  77. void SaveConnections(QString settingPrefix = ""); // settingPrefix allowed for testing purposes.
  78. void LoadConnections(QString settingPrefix = ""); // settingPrefix allowed for testing purposes.
  79. void RegisterService(unsigned int type, regFunc func) override;
  80. //QAbstractItemListModel
  81. QVariant data(const QModelIndex& index, int role) const override;
  82. QVariant headerData(int section, Qt::Orientation orientation, int role) const override;
  83. QModelIndex index(int row, int column, const QModelIndex&) const override;
  84. QModelIndex parent(const QModelIndex&) const override;
  85. int columnCount(const QModelIndex& parent) const override;
  86. int rowCount(const QModelIndex& parent = QModelIndex()) const override;
  87. Qt::ItemFlags flags(const QModelIndex& index) const override;
  88. bool setData(const QModelIndex& index, const QVariant& value, int role) override;
  89. void removeConnection(const QModelIndex& index);
  90. Q_SIGNALS:
  91. void connectionAdded(unsigned int connectionId, Connection* connection);
  92. void beforeConnectionRemoved(unsigned int connectionId);
  93. void ConnectionDisconnected(unsigned int connectionId);
  94. void ConnectionRemoved(unsigned int connectionId);
  95. void ConnectionError(unsigned int connId, QString error);
  96. void ConnectionReady(unsigned int connectionId, QStringList platforms);
  97. void ReadyToQuit(QObject* source);
  98. void SyncAllowedListAndRejectedList(QStringList allowedList, QStringList rejectedList);
  99. // this is a response to the allowedlist request with that same token.
  100. void AddressIsInAllowedList(void* token, bool result);
  101. void FirstTimeAddedToRejctedList(QString ipAddress);
  102. public Q_SLOTS:
  103. void SendMessageToService(unsigned int connId, unsigned int type, unsigned int serial, QByteArray payload);
  104. void QuitRequested();
  105. void RemoveConnectionFromMap(unsigned int connectionId);
  106. void MakeSureConnectionMapEmpty();
  107. void NewConnection(qintptr socketDescriptor);
  108. void AllowedListingEnabled(bool enabled);
  109. void IsAddressInAllowedList(QHostAddress hostAddress, void* token);
  110. void AddAddressToAllowedList(QString address);
  111. void RemoveAddressFromAllowedList(QString address);
  112. void AddRejectedAddress(QString address, bool surpressWarning = false);
  113. void RemoveRejectedAddress(QString address);
  114. //metrics
  115. void AddBytesReceived(unsigned int connId, qint64 add, bool update);
  116. void AddBytesSent(unsigned int connId, qint64 add, bool update);
  117. void AddBytesRead(unsigned int connId, qint64 add, bool update);
  118. void AddBytesWritten(unsigned int connId, qint64 add, bool update);
  119. void AddOpenRequest(unsigned int connId, bool update);
  120. void AddCloseRequest(unsigned int connId, bool update);
  121. void AddOpened(unsigned int connId, bool update);
  122. void AddClosed(unsigned int connId, bool update);
  123. void AddReadRequest(unsigned int connId, bool update);
  124. void AddWriteRequest(unsigned int connId, bool update);
  125. void AddTellRequest(unsigned int connId, bool update);
  126. void AddSeekRequest(unsigned int connId, bool update);
  127. void AddIsReadOnlyRequest(unsigned int connId, bool update);
  128. void AddIsDirectoryRequest(unsigned int connId, bool update);
  129. void AddSizeRequest(unsigned int connId, bool update);
  130. void AddModificationTimeRequest(unsigned int connId, bool update);
  131. void AddExistsRequest(unsigned int connId, bool update);
  132. void AddFlushRequest(unsigned int connId, bool update);
  133. void AddCreatePathRequest(unsigned int connId, bool update);
  134. void AddDestroyPathRequest(unsigned int connId, bool update);
  135. void AddRemoveRequest(unsigned int connId, bool update);
  136. void AddCopyRequest(unsigned int connId, bool update);
  137. void AddRenameRequest(unsigned int connId, bool update);
  138. void AddFindFileNamesRequest(unsigned int connId, bool update);
  139. void UpdateBytesReceived(unsigned int connId);
  140. void UpdateBytesSent(unsigned int connId);
  141. void UpdateBytesRead(unsigned int connId);
  142. void UpdateBytesWritten(unsigned int connId);
  143. void UpdateOpenRequest(unsigned int connId);
  144. void UpdateCloseRequest(unsigned int connId);
  145. void UpdateOpened(unsigned int connId);
  146. void UpdateClosed(unsigned int connId);
  147. void UpdateReadRequest(unsigned int connId);
  148. void UpdateWriteRequest(unsigned int connId);
  149. void UpdateTellRequest(unsigned int connId);
  150. void UpdateSeekRequest(unsigned int connId);
  151. void UpdateIsReadOnlyRequest(unsigned int connId);
  152. void UpdateIsDirectoryRequest(unsigned int connId);
  153. void UpdateSizeRequest(unsigned int connId);
  154. void UpdateModificationTimeRequest(unsigned int connId);
  155. void UpdateExistsRequest(unsigned int connId);
  156. void UpdateFlushRequest(unsigned int connId);
  157. void UpdateCreatePathRequest(unsigned int connId);
  158. void UpdateDestroyPathRequest(unsigned int connId);
  159. void UpdateRemoveRequest(unsigned int connId);
  160. void UpdateCopyRequest(unsigned int connId);
  161. void UpdateRenameRequest(unsigned int connId);
  162. void UpdateFindFileNamesRequest(unsigned int connId);
  163. void UpdateConnectionMetrics();
  164. void OnStatusChanged(unsigned int connId);
  165. void UpdateAllowedListFromBootStrap();
  166. private:
  167. unsigned int internalAddConnection(bool isUserConnection, qintptr socketDescriptor = -1);
  168. bool IsResponse(unsigned int serial);
  169. void RouteIncomingMessage(unsigned int connId, unsigned int type, unsigned int serial, QByteArray payload);
  170. Connection* FindConnection(const QModelIndex& index) const;
  171. unsigned int m_nextConnectionId;
  172. ConnectionMap m_connectionMap;
  173. RouteMultiMap m_messageRoute;
  174. QHostAddress m_lastHostAddress = QHostAddress::Null;
  175. AZ::u64 m_lastConnectionTimeInUTCMilliSecs = 0;
  176. // keeps track of how many platforms are connected of a given type
  177. // the key is the name of the platform, and the value is the number of those kind of platforms.
  178. QHash<QString, int> m_platformsConnected;
  179. //allowed listing
  180. bool m_allowedListingEnabled = true;
  181. //these lists are just caches, only used for updating
  182. QStringList m_allowedListAddresses;
  183. QStringList m_rejectedAddresses;
  184. };
  185. #endif // CONNECTIONMANAGER_H