History

Kinda working OSC packet sending

Ada committed 10 months ago

Commit 81c80e71 parent 2faebb94 files · +46 −20Browse files

Showing the changes to interface/src/scripting/OSCScriptingInterface.cpp (1 of 4 files). Show all

Unified · Split

interface/src/scripting/OSCScriptingInterface.cpp+37−16
@@ -17,7 +17,7 @@ · 16 unmodified lines
1717 #include "OSCScriptingInterface.h"
1818
1919
20−Q_LOGGING_CATEGORY(osc, "overte.osc")
20+Q_LOGGING_CATEGORY(osc, "osc")
2121
2222 enum OSCTag: char {
2323 Int = 'i',
@@ -26,7 +26,7 @@ enum OSCTag: char { · 2 unmodified lines
2626 Blob = 'b',
2727 };
2828
29−static const QRegularExpression invalidCharacters = QRegularExpression("([ #*,/?\\[\\]{}])");
29+static const QRegularExpression invalidCharacters = QRegularExpression("([ #*,?\\[\\]{}])");
3030 static const QMap<QChar, QVariant::Type> typeNameMap = {
3131 { OSCTag::Int, QVariant::Int },
3232 { OSCTag::Float, QVariant::Double },
@@ -35,8 +35,8 @@ static const QMap<QChar, QVariant::Type> typeNameMap = { · 2 unmodified lines
3535 };
3636
3737
38−OSCScriptingInterface::OSCScriptingInterface(ScriptManager* parent) :
39− _scriptManager(parent),
38+OSCScriptingInterface::OSCScriptingInterface(QObject* parent) :
39+ QObject(parent),
4040 _receivePort("osc/receivePort", 9000),
4141 _receiveHost("osc/receiveHost", "127.0.0.1"),
4242 _sendPort("osc/sendPort", 9001),
@@ -63,7 +63,14 @@ void OSCScriptingInterface::receivePacket() { · 20 unmodified lines
6363 void OSCScriptingInterface::sendMessage(const QString& address, const QString &argumentTypes, const QVariantList& arguments) {
6464 Q_ASSERT(_socket);
6565
66− auto scriptEngine = _scriptManager->engine();
66+ // FIXME: how do you get the script engine???
67+ struct HackRemoveThis {
68+ void raiseException(QString msg) {
69+ qCCritical(osc) << msg;
70+ }
71+ };
72+
73+ auto scriptEngine = std::make_unique<HackRemoveThis>(); //_scriptManager->engine();
6774
6875 if (address.length() < 2 || address[0] != '/') {
6976 scriptEngine->raiseException("address must be at least two characters and start with '/'");
@@ -76,14 +83,19 @@ void OSCScriptingInterface::sendMessage(const QString& address, const QString &a · 6 unmodified lines
7683 }
7784
7885 if (argumentTypes.length() != arguments.length()) {
79− scriptEngine->raiseException(QString("argumentTypes.length (%1) does not match arguments.length (%2)").arg(argumentTypes.length()).arg(argumentTypes.length()));
86+ scriptEngine->raiseException(QString("argumentTypes.length (%1) does not match arguments.length (%2)").arg(argumentTypes.length()).arg(arguments.length()));
8087 return;
8188 }
8289
90+ auto pad4 = [](QByteArray& array) {
91+ array.append((4 - array.length() % 4) % 4, 0);
92+ };
93+
8394 QByteArray bytes = address.toUtf8();
8495
8596 // pad to 4-byte boundary with at least one zero for the address string
86− bytes.append((bytes.length() + 1) % 4, 0);
97+ bytes.append('\x00');
98+ pad4(bytes);
8799
88100 // comma at the start of the argument list, even if empty
89101 bytes.append(',');
@@ -99,19 +111,23 @@ void OSCScriptingInterface::sendMessage(const QString& address, const QString &a · 9 unmodified lines
99111 }
100112
101113 auto expectedType = typeNameMap[argumentTypes[i]];
102− if (expectedType != arg.type()) {
114+ if (
115+ expectedType != arg.type() &&
116+ // JS doesn't have ints, so allow doubles to be interpreted as ints
117+ arg.type() != QVariant::Double
118+ ) {
103119 scriptEngine->raiseException(QString("Specified argument type '%1' does not match value of type '%2'").arg(QVariant::typeToName(expectedType)).arg(arg.typeName()));
104120 return;
105121 }
106122
107− switch (arg.type()) {
123+ switch (expectedType) {
108124 case QVariant::Int: {
109125 bytes.append(OSCTag::Int);
110126
111127 auto* ptr = bodyBytes.data() + bodyBytes.length();
112128 bodyBytes.append(4, 0);
113129
114− qToBigEndian(static_cast<qint32>(arg.toInt()), ptr);
130+ qToBigEndian(static_cast<qint32>(arg.toDouble()), ptr);
115131 } break;
116132
117133 case QVariant::Double: {
@@ -121,7 +137,7 @@ void OSCScriptingInterface::sendMessage(const QString& address, const QString &a · 3 unmodified lines
121137 bodyBytes.append(4, 0);
122138
123139 // FIXME: use std::bit_cast instead of this hack once c++20 is in
124− auto tmp = arg.toFloat();
140+ auto tmp = static_cast<float>(arg.toDouble());
125141 qToBigEndian(*reinterpret_cast<quint32*>(&tmp), ptr);
126142 } break;
127143
@@ -136,8 +152,10 @@ void OSCScriptingInterface::sendMessage(const QString& address, const QString &a · 8 unmodified lines
136152 }
137153
138154 bodyBytes.append(stringArg.toUtf8());
155+
139156 // pad to 4-byte boundary with at least one zero for the string
140− bodyBytes.append((bodyBytes.length() + 1) % 4, 0);
157+ bodyBytes.append('\x00');
158+ pad4(bodyBytes);
141159 } break;
142160
143161 case QVariant::ByteArray: {
@@ -153,8 +171,8 @@ void OSCScriptingInterface::sendMessage(const QString& address, const QString &a · 9 unmodified lines
153171 // write body
154172 bodyBytes.append(bytesArg);
155173
156− // pad to 4-byte boundary with at least one zero for the string
157− bodyBytes.append((bodyBytes.length() + 1) % 4, 0);
174+ // pad to 4-byte boundary
175+ pad4(bodyBytes);
158176 } break;
159177
160178 // unreachable
@@ -164,11 +182,14 @@ void OSCScriptingInterface::sendMessage(const QString& address, const QString &a · 3 unmodified lines
164182 }
165183 }
166184
167− // pad to 4-byte boundary with at least one zero for the argument list string
168− bytes.append((bytes.length() + 1) % 4, 0);
185+ // pad argument type string to 4-byte boundary with at least one zero
186+ bytes.append('\x00');
187+ pad4(bytes);
169188
170189 // pack the header + body data together for the datagram
171190 bytes.append(bodyBytes);
172191
173192 _socket->writeDatagram(bytes, QHostAddress(_sendHost.get()), _sendPort.get());
193+
194+ qCDebug(osc) << QHostAddress(_sendHost.get()) << _sendPort.get() << ":" << bytes.toHex(' ');
174195 }