-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathoscsender.cpp
68 lines (57 loc) · 2.63 KB
/
oscsender.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include "oscsender.h"
#include "contrib/oscpack/OscOutboundPacketStream.h"
#include "contrib/oscpack/OscTypes.h"
#include <iostream>
// FIXME: we should also allow addresses such as "localhost", or "example.com"
OscSender::OscSender(const QString& hostAddress, quint16 port, QObject *parent) :
QObject(parent),
m_udpSocket(new QUdpSocket(this)),
m_hostAddress(hostAddress),
m_port(port)
{
m_udpSocket->connectToHost(QHostAddress(m_hostAddress) , m_port);
}
void OscSender::send(const QString& oscAddress, const QVariantList& arguments) {
QByteArray datagram;
this->variantListToByteArray(datagram, oscAddress, arguments);
qint64 written = m_udpSocket->write(datagram);
qDebug() << "C++OscSender Send" << datagram.size() << "bytes:" << datagram.toHex() <<
"to " << m_hostAddress << "on port" << m_port;
if (written == -1) {
qCritical() << "Failed to send OSC. (write bytes to the send socket)";
}
m_udpSocket->flush();
m_udpSocket->waitForBytesWritten();
}
void OscSender::variantListToByteArray(QByteArray& outputResult, const QString& oscAddress, const QVariantList& arguments) {
char buffer[1024];
osc::OutboundPacketStream packet(buffer, 1024);
// FIXME: Sending datagrams larger than 512 bytes is in general disadvised, as even if they are sent successfully,
// they are likely to be fragmented by the IP layer before arriving at their final destination.
// packet << osc::BeginBundleImmediate << osc::BeginMessage(oscAddress.toStdString().c_str());
packet << osc::BeginMessage(oscAddress.toStdString().c_str());
for (int i = 0; i < arguments.count(); ++ i) {
QVariant argument = arguments[i];
QVariant::Type type = argument.type();
if (type == QVariant::Type::Int) {
packet << argument.toInt();
} else if (type == QMetaType::Float) {
packet << argument.toFloat();
} else if (type == QMetaType::Double) {
packet << argument.toDouble();
} else if (type == QMetaType::QString) {
packet << argument.toString().toStdString().c_str();
//} else if (type == QMetaType::QByteArray) {
// osc::Blob b(argument.toByteArray().constData());
// packet << b;
} else if (type == QMetaType::Bool) {
packet << argument.toBool();
} else {
qDebug() << "Unhandled OSC argument type " << argument.typeName();
}
// TODO: implement other OSC types
}
// packet << osc::EndMessage << osc::EndBundle;
packet << osc::EndMessage;
outputResult.append(packet.Data(), packet.Size());
}