You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
87 lines
2.7 KiB
C++
87 lines
2.7 KiB
C++
/*
|
|
* All or portions of this file Copyright (c) Amazon.com, Inc. or its affiliates or
|
|
* its licensors.
|
|
*
|
|
* For complete copyright and license terms please see the LICENSE at the root of this
|
|
* distribution (the "License"). All use of this software is governed by the License,
|
|
* or, if provided, by the license below or the license accompanying this file. Do not
|
|
* remove or modify any license notices. This file is distributed on an "AS IS" BASIS,
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
*
|
|
*/
|
|
|
|
#include <AtomToolsFramework/Communication/LocalSocket.h>
|
|
#include <AzCore/Debug/Trace.h>
|
|
#include <QByteArray>
|
|
#include <QString>
|
|
|
|
namespace AtomToolsFramework
|
|
{
|
|
LocalSocket::LocalSocket()
|
|
{
|
|
}
|
|
|
|
LocalSocket::~LocalSocket()
|
|
{
|
|
Disconnect();
|
|
}
|
|
|
|
bool LocalSocket::Connect(const QString& serverName)
|
|
{
|
|
Disconnect();
|
|
|
|
m_serverName = serverName;
|
|
|
|
AZ_TracePrintf("AtomToolsFramework::LocalSocket", "Connecting to: %s\n", m_serverName.toUtf8().constData());
|
|
m_socket.connectToServer(m_serverName);
|
|
|
|
if (IsConnected())
|
|
{
|
|
AZ_TracePrintf("AtomToolsFramework::LocalSocket", "Waiting for connection to: %s\n", m_serverName.toUtf8().constData());
|
|
if (m_socket.waitForConnected())
|
|
{
|
|
AZ_TracePrintf("AtomToolsFramework::LocalSocket", "Connected to: %s\n", m_serverName.toUtf8().constData());
|
|
return true;
|
|
}
|
|
}
|
|
|
|
AZ_TracePrintf("AtomToolsFramework::LocalSocket", "Connecting failed: %s\n", m_serverName.toUtf8().constData());
|
|
Disconnect();
|
|
return false;
|
|
}
|
|
|
|
void LocalSocket::Disconnect()
|
|
{
|
|
if (IsConnected())
|
|
{
|
|
AZ_TracePrintf("AtomToolsFramework::LocalSocket", "Disconnecting from: %s\n", m_serverName.toUtf8().constData());
|
|
m_socket.disconnectFromServer();
|
|
m_socket.waitForDisconnected();
|
|
|
|
AZ_TracePrintf("AtomToolsFramework::LocalSocket", "Closing socket\n");
|
|
m_socket.close();
|
|
}
|
|
}
|
|
|
|
bool LocalSocket::IsConnected() const
|
|
{
|
|
return m_socket.isOpen();
|
|
}
|
|
|
|
bool LocalSocket::Send(const QByteArray& buffer)
|
|
{
|
|
if (IsConnected())
|
|
{
|
|
AZ_TracePrintf("AtomToolsFramework::LocalSocket", "Sending data to: %s\n", m_serverName.toUtf8().constData());
|
|
m_socket.write(buffer);
|
|
|
|
AZ_TracePrintf("AtomToolsFramework::LocalSocket", "Waiting for write to: %s\n", m_serverName.toUtf8().constData());
|
|
m_socket.waitForBytesWritten();
|
|
return true;
|
|
}
|
|
return false;
|
|
}
|
|
} // namespace AtomToolsFramework
|
|
|
|
#include <AtomToolsFramework/Communication/moc_LocalSocket.cpp>
|