LocalServer.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 <AtomToolsFramework/Communication/LocalServer.h>
  9. #include <AzCore/Debug/Trace.h>
  10. #include <QString>
  11. namespace AtomToolsFramework
  12. {
  13. LocalServer::LocalServer()
  14. {
  15. AZ_TracePrintf("AtomToolsFramework::LocalServer", "Creating local server\n");
  16. m_server.setSocketOptions(QLocalServer::WorldAccessOption);
  17. QObject::connect(&m_server, &QLocalServer::newConnection, this, [this]() { AddConnection(m_server.nextPendingConnection()); });
  18. }
  19. LocalServer::~LocalServer()
  20. {
  21. Disconnect();
  22. }
  23. bool LocalServer::Connect(const QString& serverName)
  24. {
  25. Disconnect();
  26. m_serverName = serverName;
  27. AZ_TracePrintf("AtomToolsFramework::LocalServer", "Starting: %s\n", m_serverName.toUtf8().constData());
  28. if (m_server.listen(m_serverName))
  29. {
  30. AZ_TracePrintf("AtomToolsFramework::LocalServer", "Started: %s\n", m_serverName.toUtf8().constData());
  31. return true;
  32. }
  33. if (m_server.serverError() == QAbstractSocket::AddressInUseError)
  34. {
  35. AZ_TracePrintf("AtomToolsFramework::LocalServer", "Restarting: %s\n", m_serverName.toUtf8().constData());
  36. Disconnect();
  37. AZ_TracePrintf("AtomToolsFramework::LocalServer", "Starting: %s\n", m_serverName.toUtf8().constData());
  38. if (m_server.listen(m_serverName))
  39. {
  40. return true;
  41. }
  42. }
  43. AZ_TracePrintf("AtomToolsFramework::LocalServer", "Starting failed: %s\n", m_serverName.toUtf8().constData());
  44. Disconnect();
  45. return false;
  46. }
  47. void LocalServer::Disconnect()
  48. {
  49. AZ_TracePrintf("AtomToolsFramework::LocalServer", "Disconnecting: %s\n", m_serverName.toUtf8().constData());
  50. QLocalServer::removeServer(m_serverName);
  51. }
  52. bool LocalServer::IsConnected() const
  53. {
  54. return m_server.isListening();
  55. }
  56. void LocalServer::SetReadHandler(LocalServer::ReadHandler handler)
  57. {
  58. m_readHandler = handler;
  59. }
  60. void LocalServer::AddConnection(QLocalSocket* connection)
  61. {
  62. AZ_TracePrintf("AtomToolsFramework::LocalServer", "Connection added: %s\n", m_serverName.toUtf8().constData());
  63. QObject::connect(connection, &QLocalSocket::readyRead, this, [this, connection]() { ReadFromConnection(connection); });
  64. QObject::connect(connection, &QLocalSocket::disconnected, this, [this, connection]() { DeleteConnection(connection); });
  65. }
  66. void LocalServer::ReadFromConnection(QLocalSocket* connection)
  67. {
  68. if (connection)
  69. {
  70. AZ_TracePrintf("AtomToolsFramework::LocalServer", "Data received: %s\n", m_serverName.toUtf8().constData());
  71. QByteArray buffer = connection->readAll();
  72. if (m_readHandler)
  73. {
  74. m_readHandler(buffer);
  75. }
  76. }
  77. }
  78. void LocalServer::DeleteConnection(QLocalSocket* connection)
  79. {
  80. if (connection)
  81. {
  82. AZ_TracePrintf("AtomToolsFramework::LocalServer", "Deleting connection: %s\n", m_serverName.toUtf8().constData());
  83. connection->deleteLater();
  84. }
  85. }
  86. } // namespace AtomToolsFramework
  87. #include <AtomToolsFramework/Communication/moc_LocalServer.cpp>