Skip to content

Instantly share code, notes, and snippets.

@photex
Created April 4, 2014 16:30
Show Gist options
  • Save photex/9978262 to your computer and use it in GitHub Desktop.
Save photex/9978262 to your computer and use it in GitHub Desktop.
simple osc udp socket in qt5
#include <qtoscsocket.h>
#include <QtCore/qbytearray.h>
#include <QtNetwork/qhostaddress.h>
QOSCSocket::QOSCSocket(QObject *parent) : QUdpSocket(parent) {
connect(this, &QUdpSocket::readyRead,
[=] () {
while(hasPendingDatagrams()) {
QByteArray datagram;
QHostAddress sender;
quint16 sender_port;
datagram.resize(pendingDatagramSize());
readDatagram(datagram.data(), datagram.size(), &sender, &sender_port);
_packet_reader.init(datagram.data(), datagram.size());
while(_packet_reader.isOk()) {
oscpkt::Message *msg;
msg = _packet_reader.popMessage();
if (msg != nullptr) {
emit messageReady(msg);
} else {
break;
}
}
}
});
}
void QOSCSocket::sendMessageTo(oscpkt::Message *message, QHostAddress address, quint16 port) {
_packet_writer.init().addMessage(*message);
writeDatagram(_packet_writer.packetData(), _packet_writer.packetSize(), address, port);
}
#ifndef _QTOSCSOCKET_H_
#define _QTOSCSOCKET_H_
#include <QtNetwork/qudpsocket.h>
#include <oscpkt/oscpkt.hh>
class QOSCSocket : public QUdpSocket {
Q_OBJECT
public:
QOSCSocket(QObject *parent = nullptr);
void sendMessageTo(oscpkt::Message *message, QHostAddress address, quint16 port);
signals:
void messageReady(oscpkt::Message *message);
private:
void init();
oscpkt::PacketReader _packet_reader;
oscpkt::PacketWriter _packet_writer;
};
#endif /* _QTOSCSOCKET_H_ */
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment