-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudp.cpp
210 lines (182 loc) · 8.26 KB
/
udp.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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
/*
This file is part of HTTP2OSC
Copyright (C) 2014-2015 — Buzzing Light
Development: Guillaume Jacquemin (http://www.buzzinglight.com)
This file was written by Guillaume Jacquemin.
HTTP2OSC is a free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "udp.h"
Udp::Udp(QObject *parent, quint16 port) :
QObject(parent) {
allowLog = true;
socket = new QUdpSocket(this);
setPort(port);
connect(socket, SIGNAL(readyRead()), SLOT(parseOSC()));
}
void Udp::setPort(quint16 port) {
if(socket->isOpen())
socket->close();
socket->bind(port, QUdpSocket::ShareAddress);
}
void Udp::parseOSC() {
//Parse all UDP datagram
while(socket->hasPendingDatagrams()) {
//Extract host, port & UDP datagram
QHostAddress receivedHost;
quint16 receivedPort;
parsingBufferISize = socket->readDatagram((char*)parsingBufferI, 4096, &receivedHost, &receivedPort);
quint16 indexBuffer = 0;
//Parse UDP content
while(indexBuffer < parsingBufferISize) {
parsingIndexAddressBuffer = 0;
parsingIndexArgumentsBuffer = 0;
//Looking for '/'
while((indexBuffer < parsingBufferISize) && (parsingBufferI[indexBuffer] != '/'))
indexBuffer++;
//Parse header
if((parsingBufferI[indexBuffer] == '/') && (parsingBufferISize%4 == 0)) {
//OSC Adress
while((indexBuffer < parsingBufferISize) && (parsingBufferI[indexBuffer] != 0))
parsingAddressBuffer[parsingIndexAddressBuffer++] = parsingBufferI[indexBuffer++];
parsingAddressBuffer[parsingIndexAddressBuffer] = 0;
//Looking for ','
while((indexBuffer < parsingBufferISize) && (parsingBufferI[indexBuffer++] != ',')) {}
//OSC arguments type
indexBuffer--;
while((indexBuffer < parsingBufferISize) && (parsingBufferI[++indexBuffer] != 0))
parsingArgumentsBuffer[parsingIndexArgumentsBuffer++] = parsingBufferI[indexBuffer];
parsingArgumentsBuffer[parsingIndexArgumentsBuffer] = 0;
indexBuffer++;
//Index modulo 4
while((indexBuffer < parsingBufferISize) && ((indexBuffer++)%4 != 0)) {}
indexBuffer--;
//Parse content
QString commandDestination = QString(parsingAddressBuffer);
QString command = commandDestination + " ";
QList<QVariant> commandArguments;
quint16 indexDataBuffer = 0;
while((indexBuffer < parsingBufferISize) && (indexDataBuffer < parsingIndexArgumentsBuffer)) {
//Integer argument
if(parsingArgumentsBuffer[indexDataBuffer] == 'i') {
union { int i; char ch[4]; } u;
u.ch[3] = parsingBufferI[indexBuffer + 0];
u.ch[2] = parsingBufferI[indexBuffer + 1];
u.ch[1] = parsingBufferI[indexBuffer + 2];
u.ch[0] = parsingBufferI[indexBuffer + 3];
indexBuffer += 4;
QString commandValue = QString::number(u.i);
command += commandValue + " ";
commandArguments << u.i;
}
//Float argument
else if(parsingArgumentsBuffer[indexDataBuffer] == 'f') {
union { float f; char ch[4]; } u;
u.ch[3] = parsingBufferI[indexBuffer + 0];
u.ch[2] = parsingBufferI[indexBuffer + 1];
u.ch[1] = parsingBufferI[indexBuffer + 2];
u.ch[0] = parsingBufferI[indexBuffer + 3];
indexBuffer += 4;
QString commandValue = QString::number(u.f);
command += commandValue + " ";
commandArguments << u.f;
}
//String argument
else if(parsingArgumentsBuffer[indexDataBuffer] == 's') {
QString commandValue = "";
while((indexBuffer < parsingBufferISize) && (parsingBufferI[indexBuffer]) != 0)
commandValue += parsingBufferI[indexBuffer++];
indexBuffer++;
while(indexBuffer % 4 != 0)
indexBuffer++;
command += commandValue + " ";
commandArguments << commandValue;
}
else
indexBuffer += 4;
indexDataBuffer++;
}
//Fire events (log, message and script mapping)
if(commandArguments.count() > 2) {
QString ip = commandArguments.at(0).toString();
commandArguments.removeFirst();
quint16 port = commandArguments.at(0).toInt();
commandArguments.removeFirst();
QString destination = commandArguments.at(0).toString();
commandArguments.removeFirst();
commandArguments.prepend(receivedPort);
commandArguments.prepend(receivedHost.toString());
emit(outgoingMessage(MainWindowInterface::main->http->send(ip, port, destination, commandArguments)));
emit(outgoingData(ip, port, destination, commandArguments));
}
}
}
}
}
QString Udp::send(const QString &ip, quint16 port, const QString &destination, const QList<QVariant> &valeurs) {
new UdpDatagram(this, ip, port, destination, valeurs, false);
QString retour = QString("%4: OSC sent on %3 (%1:%2): ").arg(ip).arg(port).arg(destination).arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz"));
if(valeurs.count()) {
foreach(const QVariant &valeur, valeurs)
retour += valeur.toString() + " ";
}
else
retour += "no args";
return retour;
}
void Udp::send(UdpDatagram *datagram, quint16) {
if(!datagram->host.isNull())
socket->writeDatagram(datagram->buffer, datagram->host, datagram->port);
}
UdpDatagram::UdpDatagram(Udp *_udp, const QString &_ip, quint16 _port, const QString &_destination, const QList<QVariant> &_valeurs, bool _secure)
: QObject(_udp) {
ip = _ip;
port = _port;
destination = _destination;
secure = _secure;
valeurs = _valeurs;
udp = _udp;
counter = 0;
host = QHostAddress(ip);
if(!host.isNull()) {
verboseMessage = "";
address += destination;
address += (char)0;
pad(address);
typetag += ',';
foreach(const QVariant &valeur, valeurs) {
if(valeur.type() == QVariant::String) {
verboseMessage = verboseMessage + " " + qPrintable(valeur.toString());
arguments += valeur.toString();
arguments += (char)0;
pad(arguments);
typetag += 's';
}
else {
verboseMessage = verboseMessage + " " + QByteArray::number(valeur.toDouble());
union { float f; char ch[4]; } u;
u.f = valeur.toDouble();
arguments += u.ch[3];
arguments += u.ch[2];
arguments += u.ch[1];
arguments += u.ch[0];
typetag += 'f';
}
}
buffer += address;
buffer += typetag;
buffer += (char)0;
pad(buffer);
buffer += arguments;
udp->send(this, counter);
}
delete this;
}