1 | // Copyright (C) 2021 The Qt Company Ltd. |
---|---|
2 | // SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0-only |
3 | |
4 | #include "message.h" |
5 | #include <QtNetwork/qlocalsocket.h> |
6 | |
7 | QT_BEGIN_NAMESPACE |
8 | |
9 | |
10 | Message::Type Message::type() const { return Message::Type::Invalid; } |
11 | |
12 | UniformsMessage::~UniformsMessage() |
13 | { |
14 | |
15 | } |
16 | |
17 | bool UniformsMessage::read(QDataStream &stream) |
18 | { |
19 | stream >> uniforms; |
20 | return (stream.status() == QDataStream::Ok); |
21 | } |
22 | |
23 | bool UniformsMessage::write(QDataStream &stream) const |
24 | { |
25 | if (uniforms.size() > 0) |
26 | stream << id.toByteArray() << uniforms; |
27 | |
28 | return (stream.status() == QDataStream::Ok); |
29 | } |
30 | |
31 | FilenamesMessage::~FilenamesMessage() |
32 | { |
33 | |
34 | } |
35 | |
36 | bool FilenamesMessage::read(QDataStream &stream) |
37 | { |
38 | stream >> vertFilename >> fragFilename; |
39 | return (stream.status() == QDataStream::Ok); |
40 | } |
41 | |
42 | bool FilenamesMessage::write(QDataStream &stream) const |
43 | { |
44 | stream << vertFilename << fragFilename; |
45 | return (stream.status() == QDataStream::Ok); |
46 | } |
47 | |
48 | Message::~Message() |
49 | { |
50 | |
51 | } |
52 | |
53 | Message::MessagePtr Message::getMessage(QIODevice &device) |
54 | { |
55 | if (device.isOpen()) { |
56 | if (device.isReadable()) { |
57 | QByteArray id; |
58 | QDataStream stream(&device); |
59 | stream >> id; |
60 | if (UniformsMessage::id == id) { |
61 | UniformsMessage uniforms; |
62 | if (uniforms.read(stream)) |
63 | return std::make_shared<UniformsMessage>(args&: uniforms); |
64 | } else if (id == FilenamesMessage::id) { |
65 | FilenamesMessage filenames; |
66 | if (filenames.read(stream)) |
67 | return std::make_shared<FilenamesMessage>(args&: filenames); |
68 | } else { |
69 | qDebug(msg: "Unknown message!"); |
70 | } |
71 | } else { |
72 | qDebug(msg: "Device not writeable!"); |
73 | } |
74 | } else { |
75 | qDebug(msg: "Device not open!"); |
76 | } |
77 | |
78 | return Message::MessagePtr(); |
79 | } |
80 | |
81 | bool Message::postMessage(QIODevice &device, const Message &message) |
82 | { |
83 | bool ok = false; |
84 | if (device.isOpen()) { |
85 | if (device.isWritable()) { |
86 | QDataStream stream(&device); |
87 | stream << message; |
88 | ok = (stream.status() == QDataStream::Ok); |
89 | } else { |
90 | qDebug(msg: "Device not writeable!"); |
91 | } |
92 | } else { |
93 | qDebug(msg: "Device not open!"); |
94 | } |
95 | |
96 | return ok; |
97 | } |
98 | |
99 | QT_END_NAMESPACE |
100 |