-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathudp.cpp
226 lines (200 loc) · 8.86 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
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
/*
This file is part of RTBroker
Made by Buzzing Light 2014-2015 — Buzzing Light
Development: Guillaume Jacquemin (https://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(QWidget *_uiFeedback, QObject *parent) :
QObject(parent), SenderInterface(_uiFeedback) {
allowLog = true;
socket = new QUdpSocket(this);
connect(socket, SIGNAL(readyRead()), SLOT(parseOSC()));
}
void Udp::setPort(quint16 port) {
if(port != socket->localPort()) {
setUiFeedback(false);
if((socket->isValid()) && (socket->localPort())) {
qDebug("Fermeture de OSC sur %d (%d)", socket->localPort(), socket->isValid());
socket->close();
}
if(socket->bind(port)) {
setUiFeedback(true);
qDebug("Ouverture OSC sur %d (%d)", port, socket->isValid());
}
else
qDebug("Echec de l'ouverture OSC sur %d", port);
}
else
qDebug("Port OSC déjà ouvert sur %d (%d)", socket->localPort(), socket->isValid());
}
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*32, &receivedHost, &receivedPort);
qint64 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 + " ";
QVariantList message;
qint64 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 + " ";
message << 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 + " ";
message << 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 + " ";
message << commandValue;
}
else
indexBuffer += 4;
indexDataBuffer++;
}
//Dispatching
if((commandDestination == "/ping") && (message.count()))
send(QVariantList(), receivedHost.toString(), message.first().toInt(), "/pong");
else {
message.prepend(commandDestination);
MainWindowInterface::main->dispatch(message, QVariantList() << "/websockets");
}
}
}
}
}
QString Udp::send(const QVariantList &valeurs, const QString &ip, quint16 port, const QString &destination, void *) {
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 + "\n";
}
void Udp::send(UdpDatagram *datagram, quint16) {
if(!datagram->host.isNull()) {
qint64 sent = socket->writeDatagram(datagram->buffer, datagram->host, datagram->port);
if(sent < 0)
qDebug("%s : %d", qPrintable(socket->errorString()), datagram->buffer.size());
}
}
UdpDatagram::UdpDatagram(Udp *_udp, const QString &_ip, quint16 _port, const QString &_destination, const QVariantList &_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 if(valeur.type() == QVariant::Int) {
verboseMessage = verboseMessage + " " + QByteArray::number(valeur.toInt());
union { int i; char ch[4]; } u;
u.i = valeur.toInt();
arguments += u.ch[3];
arguments += u.ch[2];
arguments += u.ch[1];
arguments += u.ch[0];
typetag += 'i';
}
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;
}