| 1 | // Copyright (C) 2021 The Qt Company Ltd. |
| 2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only WITH Qt-GPL-exception-1.0 |
| 3 | |
| 4 | #include "resourceclient.h" |
| 5 | |
| 6 | // Messages: |
| 7 | // Uniforms [] |
| 8 | // Filenames (vert, frag) |
| 9 | |
| 10 | QT_BEGIN_NAMESPACE |
| 11 | |
| 12 | ResourceClient::ResourceClient(const QString &serverName) |
| 13 | : m_serverName(serverName) |
| 14 | { |
| 15 | |
| 16 | } |
| 17 | |
| 18 | void ResourceClient::init() |
| 19 | { |
| 20 | const int timeout = 10000; |
| 21 | QObject::connect(sender: &m_socket, signal: &QLocalSocket::readyRead, context: this, slot: [this]() { |
| 22 | const auto message = Message::getMessage(device&: m_socket); |
| 23 | if (message->type() != Message::Type::Invalid) |
| 24 | Q_EMIT messageReceived(message); |
| 25 | }); |
| 26 | QObject::connect(sender: &m_socket, signal: &QLocalSocket::errorOccurred, context: this, slot: [this]() { |
| 27 | qDebug(msg: "client: Error occurred when connecting to: \'%s\'\n - %s" , qPrintable(m_serverName), qPrintable(m_socket.errorString())); |
| 28 | }); |
| 29 | |
| 30 | m_socket.connectToServer(name: m_serverName); // ReadWrite |
| 31 | if (m_socket.waitForConnected(msecs: timeout)) { |
| 32 | qDebug(msg: "client: Connected to: %s" , qPrintable(m_serverName)); |
| 33 | Q_EMIT connected(); |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | void ResourceClient::sendMessage(const Message::MessagePtr &message) |
| 38 | { |
| 39 | if (message != nullptr) |
| 40 | Message::postMessage(device&: m_socket, message: *message); |
| 41 | } |
| 42 | |
| 43 | QT_END_NAMESPACE |
| 44 | |