connectionworker.h 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  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. #pragma once
  9. #if !defined(Q_MOC_RUN)
  10. #include <QTcpSocket>
  11. #include <QHostAddress>
  12. #include "native/connection/connectionMessages.h"
  13. #include <AzCore/Memory/Memory.h>
  14. #include <AzCore/Memory/SystemAllocator.h>
  15. #endif
  16. /** This Class is responsible for connecting to the client
  17. */
  18. #undef SendMessage
  19. namespace AssetProcessor
  20. {
  21. class ConnectionWorker
  22. : public QObject
  23. {
  24. Q_OBJECT
  25. public:
  26. explicit ConnectionWorker(qintptr socketDescriptor = -1, QObject* parent = 0);
  27. virtual ~ConnectionWorker();
  28. QTcpSocket& GetSocket();
  29. void Reset();
  30. bool Terminate();
  31. bool ReadMessage(QTcpSocket& socket, AssetProcessor::Message& message);
  32. bool ReadData(QTcpSocket& socket, char* buffer, qint64 size);
  33. bool WriteMessage(QTcpSocket& socket, const AssetProcessor::Message& message);
  34. bool WriteData(QTcpSocket& socket, const char* buffer, qint64 size);
  35. //! True if we initiated the connection, false if someone connected to us.
  36. bool InitiatedConnection() const;
  37. Q_SIGNALS:
  38. void ReceiveMessage(unsigned int type, unsigned int serial, QByteArray payload);
  39. void SocketIPAddress(QString ipAddress);
  40. void SocketPort(int port);
  41. void Identifier(QString identifier);
  42. void AssetPlatformsString(QString platform);
  43. void ConnectionDisconnected();
  44. void ConnectionEstablished(QString ipAddress, quint16 port);
  45. void ErrorMessage(QString msg);
  46. // the token identifies the unique connection instance, since multiple may have the same address
  47. void IsAddressInAllowedList(QHostAddress hostAddress, void* token);
  48. public Q_SLOTS:
  49. void ConnectSocket(qintptr socketDescriptor);
  50. void ConnectToEngine(QString ipAddress, quint16 port);
  51. void EngineSocketHasData();
  52. void EngineSocketStateChanged(QAbstractSocket::SocketState socketState);
  53. void SendMessage(unsigned int type, unsigned int serial, QByteArray payload);
  54. void DisconnectSockets();
  55. void RequestTerminate();
  56. bool NegotiateDirect(bool initiate);
  57. // the token will be the same token sent in the allowedlisting request.
  58. void AddressIsInAllowedList(void* token, bool result);
  59. private Q_SLOTS:
  60. void TerminateConnection();
  61. private:
  62. QTcpSocket m_engineSocket;
  63. volatile bool m_terminate;
  64. volatile bool m_alreadySentTermination = false;
  65. bool m_initiatedConnection = false;
  66. bool m_engineSocketIsConnected = false;
  67. int m_waitDelay = 10000; //increased to 10000 as 5000 milliseconds was enough in the unloaded general case but when the computer is loaded we need more time to negotiate a connection or we only get connection failures
  68. };
  69. } // namespace AssetProcessor