1 | // Copyright (C) 2021 The Qt Company Ltd. |
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include "resourceserver.h" |
5 | |
6 | #include <QtCore/qcommandlineoption.h> |
7 | #include <QtCore/qcommandlineparser.h> |
8 | |
9 | #include <QtNetwork/qlocalsocket.h> |
10 | |
11 | QT_BEGIN_NAMESPACE |
12 | |
13 | // BEGIN - Test Resource IPC |
14 | //const auto serverName = QLatin1String("testServer"); |
15 | //ResourceServer resServer(serverName); |
16 | //QObject::connect(&resServer, &ResourceServer::messageReceived, [](const Message::MessagePtr &msg) { |
17 | // if (msg) |
18 | // qDebug() << "Received message: " << int(msg->type()); |
19 | // else |
20 | // qDebug() << "Received null message!"; |
21 | |
22 | // if (msg && msg->type() == Message::Type::Uniforms) { |
23 | // const auto &um = static_cast<const UniformsMessage &>(*msg); |
24 | // qDebug() << "UniformMessage: " << um.uniforms; |
25 | // } |
26 | //}); |
27 | //resServer.init(); |
28 | |
29 | //ResourceClient resClient(serverName); |
30 | //resClient.init(); |
31 | |
32 | //resClient.sendMessage(std::make_shared<UniformsMessage>(UniformsMessage::UniformList{UniformsMessage::Uniform{"float", "foo"}})); |
33 | // End - Test Resoruce IPC |
34 | |
35 | ResourceServer::ResourceServer(const QString &serverName) |
36 | : m_serverName(serverName) |
37 | { |
38 | |
39 | } |
40 | |
41 | bool ResourceServer::init() |
42 | { |
43 | if (m_server.isListening()) |
44 | return false; |
45 | |
46 | QObject::connect(sender: &m_server, signal: &QLocalServer::newConnection, slot: [this]() { |
47 | qDebug() << "srv: Incoming connection!" ; |
48 | if (m_connection != nullptr && m_connection->isOpen()) { |
49 | qDebug(msg: "Client already connected! Connection refused!" ); |
50 | m_server.close(); |
51 | } else { |
52 | if (m_connection) |
53 | m_connection->close(); |
54 | m_connection = m_server.nextPendingConnection(); |
55 | QObject::connect(sender: m_connection, signal: &QLocalSocket::readyRead, slot: [this]() { |
56 | const auto message = Message::getMessage(device&: *m_connection); |
57 | if (message && message->type() != Message::Type::Invalid) |
58 | Q_EMIT messageReceived(message); |
59 | }); |
60 | QObject::connect(sender: m_connection, signal: &QLocalSocket::errorOccurred, slot: [this]() { |
61 | qDebug(msg: "srv: Error occurred\n - %s" , qPrintable(m_connection->errorString())); |
62 | m_server.close(); |
63 | m_server.listen(name: m_serverName); |
64 | }); |
65 | QObject::connect(sender: m_connection, signal: &QLocalSocket::connected, slot: []() { |
66 | qDebug(msg: "srv: Connection established!" ); |
67 | }); |
68 | } |
69 | }); |
70 | |
71 | |
72 | int attempts = 3; |
73 | for (;attempts && !m_server.isListening(); --attempts) { |
74 | if (!m_server.listen(name: m_serverName)) |
75 | QLocalServer::removeServer(name: m_serverName); |
76 | } |
77 | |
78 | if (m_server.isListening()) |
79 | qDebug(msg: "srv: Listening for incoming connection on \'%s\'" , qPrintable(m_serverName)); |
80 | |
81 | return true; |
82 | } |
83 | |
84 | void ResourceServer::sendMessage(const Message::MessagePtr &message) |
85 | { |
86 | if (message) { |
87 | if (m_connection) |
88 | Message::postMessage(device&: *m_connection, message: *message); |
89 | else |
90 | qDebug(msg: "srv: No connection!" ); |
91 | } else { |
92 | qDebug(msg: "srv: Inavlid message!" ); |
93 | } |
94 | } |
95 | |
96 | QT_END_NAMESPACE |
97 | |