connection.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  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 CONNECTION_H
  9. #define CONNECTION_H
  10. #if !defined(Q_MOC_RUN)
  11. #include <QThread>
  12. #include <QElapsedTimer>
  13. #include "native/utilities/AssetUtilEBusHelper.h"
  14. #include <QHostAddress>
  15. #include <QTimer>
  16. #include <QString>
  17. #include <QPointer>
  18. #endif
  19. class QSettings;
  20. namespace AssetProcessor
  21. {
  22. class ConnectionWorker;
  23. class PlatformConfiguration;
  24. }
  25. #undef SendMessage
  26. /** This Class contains all the information related to a single connecton
  27. */
  28. class Connection
  29. : public QObject
  30. , public AssetProcessor::ConnectionBus::Handler
  31. {
  32. Q_OBJECT
  33. Q_PROPERTY(QString identifier READ Identifier WRITE SetIdentifier NOTIFY IdentifierChanged)
  34. Q_PROPERTY(QString ipAddress READ IpAddress WRITE SetIpAddress NOTIFY IpAddressChanged)
  35. Q_PROPERTY(int port READ Port WRITE SetPort NOTIFY PortChanged)
  36. Q_PROPERTY(ConnectionStatus status READ Status NOTIFY StatusChanged)
  37. Q_PROPERTY(QStringList assetPlatform READ AssetPlatforms WRITE SetAssetPlatforms NOTIFY AssetPlatformChanged)
  38. Q_PROPERTY(QString assetPlatformsString READ AssetPlatformsString WRITE SetAssetPlatformsString)
  39. Q_PROPERTY(bool autoConnect READ AutoConnect WRITE SetAutoConnect NOTIFY AutoConnectChanged)
  40. Q_PROPERTY(QString displayName READ DisplayName NOTIFY DisplayNameChanged)
  41. Q_PROPERTY(QString elapsed READ Elapsed NOTIFY ElapsedChanged)
  42. //metrics
  43. Q_PROPERTY(qint64 numOpenRequests MEMBER m_numOpenRequests NOTIFY NumOpenRequestsChanged)
  44. Q_PROPERTY(qint64 numCloseRequests MEMBER m_numCloseRequests NOTIFY NumCloseRequestsChanged)
  45. Q_PROPERTY(qint64 numOpened MEMBER m_numOpened NOTIFY NumOpenedChanged)
  46. Q_PROPERTY(qint64 numClosed MEMBER m_numClosed NOTIFY NumClosedChanged)
  47. Q_PROPERTY(qint64 numReadRequests MEMBER m_numReadRequests NOTIFY NumReadRequestsChanged)
  48. Q_PROPERTY(qint64 numWriteRequests MEMBER m_numWriteRequests NOTIFY NumWriteRequestsChanged)
  49. Q_PROPERTY(qint64 numSeekRequests MEMBER m_numSeekRequests NOTIFY NumSeekRequestsChanged)
  50. Q_PROPERTY(qint64 numTellRequests MEMBER m_numTellRequests NOTIFY NumTellRequestsChanged)
  51. Q_PROPERTY(qint64 numEofRequests MEMBER m_numEofRequests NOTIFY NumEofRequestsChanged)
  52. Q_PROPERTY(qint64 numIsReadOnlyRequests MEMBER m_numIsReadOnlyRequests NOTIFY NumIsReadOnlyRequestsChanged)
  53. Q_PROPERTY(qint64 numIsDirectoryRequests MEMBER m_numIsDirectoryRequests NOTIFY NumIsDirectoryRequestsChanged)
  54. Q_PROPERTY(qint64 numSizeRequests MEMBER m_numSizeRequests NOTIFY NumSizeRequestsChanged)
  55. Q_PROPERTY(qint64 numModificationTimeRequests MEMBER m_numModificationTimeRequests NOTIFY NumModificationTimeRequestsChanged)
  56. Q_PROPERTY(qint64 numExistsRequests MEMBER m_numExistsRequests NOTIFY NumExistsRequestsChanged)
  57. Q_PROPERTY(qint64 numFlushRequests MEMBER m_numFlushRequests NOTIFY NumFlushRequestsChanged)
  58. Q_PROPERTY(qint64 numCreatePathRequests MEMBER m_numCreatePathRequests NOTIFY NumCreatePathRequestsChanged)
  59. Q_PROPERTY(qint64 numDestroyPathRequests MEMBER m_numDestroyPathRequests NOTIFY NumDestroyPathRequestsChanged)
  60. Q_PROPERTY(qint64 numRemoveRequests MEMBER m_numRemoveRequests NOTIFY NumRemoveRequestsChanged)
  61. Q_PROPERTY(qint64 numCopyRequests MEMBER m_numCopyRequests NOTIFY NumCopyRequestsChanged)
  62. Q_PROPERTY(qint64 numRenameRequests MEMBER m_numRenameRequests NOTIFY NumRenameRequestsChanged)
  63. Q_PROPERTY(qint64 numFindFileNamesRequests MEMBER m_numFindFileNamesRequests NOTIFY NumFindFileNamesRequestsChanged)
  64. Q_PROPERTY(qint64 bytesRead MEMBER m_bytesRead NOTIFY BytesReadChanged)
  65. Q_PROPERTY(qint64 bytesWritten MEMBER m_bytesWritten NOTIFY BytesWrittenChanged)
  66. Q_PROPERTY(qint64 bytesSent MEMBER m_bytesSent NOTIFY BytesSentChanged)
  67. Q_PROPERTY(qint64 bytesReceived MEMBER m_bytesReceived NOTIFY BytesReceivedChanged)
  68. Q_PROPERTY(qint64 numOpenFiles MEMBER m_numOpenFiles NOTIFY NumOpenFilesChanged)
  69. public:
  70. explicit Connection(qintptr socketDescriptor = -1, QObject* parent = 0);
  71. explicit Connection(bool isUserCreatedConnection, qintptr socketDescriptor = -1, QObject* parent = 0);
  72. virtual ~Connection();
  73. enum ConnectionStatus
  74. {
  75. Disconnected, Connected, Connecting
  76. };
  77. Q_ENUMS(ConnectionStatus)
  78. void Activate(qintptr socketDescriptor);
  79. QString Identifier() const;
  80. QString IpAddress() const;
  81. int Port() const;
  82. ConnectionStatus Status() const;
  83. QStringList AssetPlatforms() const;
  84. QString AssetPlatformsString() const;
  85. void SaveConnection(QSettings& qSettings);
  86. void LoadConnection(QSettings& qSettings);
  87. bool AutoConnect() const;
  88. QString DisplayName() const;
  89. QString Elapsed() const;
  90. bool InitiatedConnection() const;
  91. bool UserCreatedConnection() const;
  92. void Disconnect();
  93. unsigned int ConnectionId() const;
  94. void SetConnectionId(unsigned int ConnectionId);
  95. void Terminate();
  96. void SendMessageToWorker(unsigned int type, unsigned int serial, QByteArray payload);
  97. void AddBytesReceived(qint64 add, bool update);
  98. void AddBytesSent(qint64 add, bool update);
  99. void AddBytesRead(qint64 add, bool update);
  100. void AddBytesWritten(qint64 add, bool update);
  101. void AddOpenRequest(bool update);
  102. void AddCloseRequest(bool update);
  103. void AddOpened(bool update);
  104. void AddClosed(bool update);
  105. void AddReadRequest(bool update);
  106. void AddWriteRequest(bool update);
  107. void AddTellRequest(bool update);
  108. void AddSeekRequest(bool update);
  109. void AddEofRequest(bool update);
  110. void AddIsReadOnlyRequest(bool update);
  111. void AddIsDirectoryRequest(bool update);
  112. void AddSizeRequest(bool update);
  113. void AddModificationTimeRequest(bool update);
  114. void AddExistsRequest(bool update);
  115. void AddFlushRequest(bool update);
  116. void AddCreatePathRequest(bool update);
  117. void AddDestroyPathRequest(bool update);
  118. void AddRemoveRequest(bool update);
  119. void AddCopyRequest(bool update);
  120. void AddRenameRequest(bool update);
  121. void AddFindFileNamesRequest(bool update);
  122. void UpdateBytesReceived();
  123. void UpdateBytesSent();
  124. void UpdateBytesRead();
  125. void UpdateBytesWritten();
  126. void UpdateOpenRequest();
  127. void UpdateCloseRequest();
  128. void UpdateOpened();
  129. void UpdateClosed();
  130. void UpdateReadRequest();
  131. void UpdateWriteRequest();
  132. void UpdateTellRequest();
  133. void UpdateSeekRequest();
  134. void UpdateEofRequest();
  135. void UpdateIsReadOnlyRequest();
  136. void UpdateIsDirectoryRequest();
  137. void UpdateSizeRequest();
  138. void UpdateModificationTimeRequest();
  139. void UpdateExistsRequest();
  140. void UpdateFlushRequest();
  141. void UpdateCreatePathRequest();
  142. void UpdateDestroyPathRequest();
  143. void UpdateRemoveRequest();
  144. void UpdateCopyRequest();
  145. void UpdateRenameRequest();
  146. void UpdateFindFileNamesRequest();
  147. void UpdateMetrics();
  148. // AssetProcessor::ConnectionBus interface
  149. size_t Send(unsigned int serial, const AzFramework::AssetSystem::BaseAssetProcessorMessage& message) override;
  150. size_t SendRaw(unsigned int type, unsigned int serial, const QByteArray& data) override;
  151. size_t SendPerPlatform(unsigned int serial, const AzFramework::AssetSystem::BaseAssetProcessorMessage& message, const QString& platform) override;
  152. size_t SendRawPerPlatform(unsigned int type, unsigned int serial, const QByteArray& data, const QString& platform) override;
  153. //! callback runs on the main thread, be sure to keep the work to an absolute minimum
  154. unsigned int SendRequest(const AzFramework::AssetSystem::BaseAssetProcessorMessage& message, const AssetProcessor::ConnectionBusTraits::ResponseCallback& callback) override;
  155. size_t SendResponse(unsigned int serial, const AzFramework::AssetSystem::BaseAssetProcessorMessage& message) override;
  156. void RemoveResponseHandler(unsigned int serial) override;
  157. void InvokeResponseHandler(AZ::u32 serial, AZ::u32 type, QByteArray data);
  158. Q_SIGNALS:
  159. void IdentifierChanged();
  160. void IpAddressChanged();
  161. void PortChanged();
  162. void StatusChanged(unsigned int connId);
  163. void AssetPlatformChanged();
  164. void AutoConnectChanged();
  165. void DisplayNameChanged();
  166. void ElapsedChanged();
  167. void NormalConnectionRequested(QString IpAddress, quint16 Port);
  168. void ConnectionReady(unsigned int connId, QStringList platforms);
  169. void connectionEnded();
  170. void TerminateConnection();
  171. void SendMessage(unsigned int type, unsigned int serial, QByteArray payload);
  172. void DeliverMessage(unsigned int connId, unsigned int type, unsigned int serial, QByteArray payload);
  173. void ConnectionDestroyed(unsigned int connId);
  174. void DisconnectConnection(unsigned int connId);
  175. void AddGameMessageToOutgoingQueue();
  176. void Error(unsigned int connId, QString errorString);
  177. // the token is just any identifier to identify a particular connection, potentially from the same host.
  178. // the response (AddressIsInAllowedList) will have the same token as was sent.
  179. void IsAddressInAllowedList(QHostAddress hostAddress, void* token);
  180. void AddressIsInAllowedList(void* token, bool result);
  181. //metrics
  182. void NumOpenRequestsChanged();
  183. void NumCloseRequestsChanged();
  184. void NumOpenedChanged();
  185. void NumClosedChanged();
  186. void NumReadRequestsChanged();
  187. void NumWriteRequestsChanged();
  188. void NumSeekRequestsChanged();
  189. void NumTellRequestsChanged();
  190. void NumEofRequestsChanged();
  191. void NumIsReadOnlyRequestsChanged();
  192. void NumIsDirectoryRequestsChanged();
  193. void NumSizeRequestsChanged();
  194. void NumModificationTimeRequestsChanged();
  195. void NumExistsRequestsChanged();
  196. void NumFlushRequestsChanged();
  197. void NumCreatePathRequestsChanged();
  198. void NumDestroyPathRequestsChanged();
  199. void NumRemoveRequestsChanged();
  200. void NumCopyRequestsChanged();
  201. void NumRenameRequestsChanged();
  202. void NumFindFileNamesRequestsChanged();
  203. void BytesReadChanged();
  204. void BytesWrittenChanged();
  205. void BytesSentChanged();
  206. void BytesReceivedChanged();
  207. void NumOpenFilesChanged();
  208. public Q_SLOTS:
  209. void SetIdentifier(QString Identifier);
  210. void SetIpAddress(QString IpAddress);
  211. void SetPort(int Port);
  212. void SetStatus(ConnectionStatus Status);
  213. void SetAssetPlatforms(QStringList assetPlatform);
  214. void SetAssetPlatformsString(QString assetPlatforms);
  215. void SetAutoConnect(bool AutoConnect);
  216. void OnConnectionDisconnect();
  217. void OnConnectionEstablished(QString ipAddress, quint16 port);
  218. void ReceiveMessage(unsigned int type, unsigned int serial, QByteArray payload);
  219. void ErrorMessage(QString negotiateFailure);
  220. void UpdateElapsed();
  221. void Connect();
  222. private:
  223. AZ::u32 GetNextSerial();
  224. unsigned int m_connectionId;
  225. QString m_identifier;
  226. QString m_ipAddress;
  227. quint16 m_port;
  228. ConnectionStatus m_status;
  229. QStringList m_assetPlatforms;
  230. bool m_autoConnect;
  231. QThread m_connectionWorkerThread;
  232. QPointer<AssetProcessor::ConnectionWorker> m_connectionWorker;
  233. bool m_runElapsed;
  234. QElapsedTimer m_elapsedTimer;
  235. qint64 m_elapsed;
  236. QString m_elapsedDisplay;
  237. bool m_queuedReconnect = false;
  238. bool m_userCreatedConnection = false;
  239. AZStd::mutex m_responseHandlerMutex;
  240. AZStd::unordered_map<AZ::u32, AssetProcessor::ConnectionBusTraits::ResponseCallback> m_responseHandlerMap;
  241. //metrics
  242. qint64 m_numOpenRequests;
  243. qint64 m_numCloseRequests;
  244. qint64 m_numOpened;
  245. qint64 m_numClosed;
  246. qint64 m_numReadRequests;
  247. qint64 m_numWriteRequests;
  248. qint64 m_numTellRequests;
  249. qint64 m_numSeekRequests;
  250. qint64 m_numEofRequests;
  251. qint64 m_numIsReadOnlyRequests;
  252. qint64 m_numIsDirectoryRequests;
  253. qint64 m_numSizeRequests;
  254. qint64 m_numModificationTimeRequests;
  255. qint64 m_numExistsRequests;
  256. qint64 m_numFlushRequests;
  257. qint64 m_numCreatePathRequests;
  258. qint64 m_numDestroyPathRequests;
  259. qint64 m_numRemoveRequests;
  260. qint64 m_numCopyRequests;
  261. qint64 m_numRenameRequests;
  262. qint64 m_numFindFileNamesRequests;
  263. qint64 m_bytesRead;
  264. qint64 m_bytesWritten;
  265. qint64 m_bytesSent;
  266. qint64 m_bytesReceived;
  267. qint64 m_numOpenFiles;
  268. Q_DISABLE_COPY(Connection)
  269. };
  270. #endif // CONNECTION_H