1 #ifndef INCLUDE_AL_OSC_HPP
2 #define INCLUDE_AL_OSC_HPP
53 #include "al/system/al_Thread.hpp"
54 #include "al/system/al_Time.hpp"
66 explicit Blob(
const void *data_,
unsigned long size_)
67 : data(data_), size(size_) {}
132 template <
class A,
class B>
140 template <
class A,
class B,
class C>
144 (*this) << a << b << c;
149 template <
class A,
class B,
class C,
class D>
151 const C &c,
const D &d) {
153 (*this) << a << b << c << d;
158 template <
class A,
class B,
class C,
class D,
class E>
160 const C &c,
const D &d,
const E &e) {
162 (*this) << a << b << c << d << e;
167 template <
class A,
class B,
class C,
class D,
class E,
class F>
169 const C &c,
const D &d,
const E &e,
const F &f) {
171 (*this) << a << b << c << d << e << f;
176 template <
class A,
class B,
class C,
class D,
class E,
class F,
class G>
178 const C &c,
const D &d,
const E &e,
const F &f,
181 (*this) << a << b << c << d << e << f << g;
186 template <
class A,
class B,
class C,
class D,
class E,
class F,
class G,
189 const C &c,
const D &d,
const E &e,
const F &f,
const G &g,
192 (*this) << a << b << c << d << e << f << g << h;
211 std::vector<char> mData;
225 const char *senderAddr =
nullptr);
240 const std::string senderAddress()
const {
return std::string(mSenderAddr); }
243 const std::string &
typeTags()
const {
return mTypeTags; }
261 std::string mAddressPattern;
262 std::string mTypeTags;
264 char mSenderAddr[32];
298 std::unique_ptr<SocketSender> socketSender;
299 std::string mAddress =
"";
311 Send(uint16_t port,
const char *address =
"localhost", al_sec timeout = 0,
316 bool open(uint16_t port,
const char *address);
318 const std::string &address()
const {
return mAddress; }
319 uint16_t port()
const {
return mPort; }
328 size_t send(
const std::string &addr) {
334 template <
class A>
size_t send(
const std::string &addr,
const A &a) {
340 template <
class A,
class B>
341 size_t send(
const std::string &addr,
const A &a,
const B &b) {
347 template <
class A,
class B,
class C>
348 size_t send(
const std::string &addr,
const A &a,
const B &b,
const C &c) {
354 template <
class A,
class B,
class C,
class D>
355 size_t send(
const std::string &addr,
const A &a,
const B &b,
const C &c,
362 template <
class A,
class B,
class C,
class D,
class E>
363 size_t send(
const std::string &addr,
const A &a,
const B &b,
const C &c,
364 const D &d,
const E &e) {
370 template <
class A,
class B,
class C,
class D,
class E,
class F>
371 size_t send(
const std::string &addr,
const A &a,
const B &b,
const C &c,
372 const D &d,
const E &e,
const F &f) {
378 template <
class A,
class B,
class C,
class D,
class E,
class F,
class G>
379 size_t send(
const std::string &addr,
const A &a,
const B &b,
const C &c,
380 const D &d,
const E &e,
const F &f,
const G &g) {
385 template <
class A,
class B,
class C,
class D,
class E,
class F,
class G,
387 size_t send(
const std::string &addr,
const A &a,
const B &b,
const C &c,
388 const D &d,
const E &e,
const F &f,
const G &g,
const H &h) {
400 class SocketReceiver;
401 std::unique_ptr<SocketReceiver> socketReceiver;
411 Recv(uint16_t port,
const char *address =
"", al_sec timeout = 0);
415 bool open(uint16_t port,
const char *address =
"", al_sec timeout = 0);
417 bool isOpen() {
return mOpen; }
419 const std::string &address()
const {
return mAddress; }
420 uint16_t port()
const {
return mPort; }
426 const char *
data()
const {
return &mBuffer[0]; }
439 mHandlers.push_back(&v);
457 void parse(
const char *packet,
int size,
const char *senderAddr);
460 static bool portAvailable(uint16_t port,
const char *address =
"");
462 static std::vector<std::shared_ptr<Message>>
463 parse(
const char *packet,
int size,
TimeTag timeTag = 1,
464 const char *senderAddr =
nullptr);
467 std::vector<PacketHandler *> mHandlers;
468 std::vector<char> mBuffer;
471 std::string mAddress =
"";
virtual bool consumeMessage(Message &m, std::string rootOSCPath)=0
Returns true if message was consumed by this class.
const TimeTag & timeTag() const
Get time tag.
Message(const char *message, int size, const TimeTag &timeTag=1, const char *senderAddr=nullptr)
Message & operator>>(char &v)
Extract next stream element as char.
void print() const
Pretty-print message information.
Message & operator>>(int &v)
Extract next stream element as integer.
const std::string & typeTags() const
Get type tags.
Message & resetStream()
Reset stream for converting from raw message bytes to types.
Message & operator>>(const char *&v)
Extract next stream element as C-string.
Message & operator>>(Blob &v)
Extract next stream element as Blob.
const std::string & addressPattern() const
Get address pattern.
Message & operator>>(double &v)
Extract next stream element as double.
Message & operator>>(std::string &v)
Extract next stream element as string.
Message & operator>>(float &v)
Extract next stream element as float.
virtual void onMessage(Message &m)=0
Called for each message contained in packet.
bool isMessage() const
Whether packet is a message.
Packet & beginMessage(const std::string &addressPattern)
Start a new message.
Packet & operator<<(double v)
Add double to message.
Packet & endBundle()
End bundle.
Packet & operator<<(const std::string &v)
Add string to message.
Packet & addMessage(const std::string &addr)
Add zero argument message.
bool isBundle() const
Whether packet is a bundle.
void printRaw() const
Pretty-print raw packet bytes.
Packet & operator<<(const char *v)
Add C-string to message.
Packet & addMessage(const std::string &addr, const A &a, const B &b, const C &c, const D &d, const E &e, const F &f, const G &g)
Add seven argument message.
Packet & operator<<(const Blob &v)
Add Blob to message.
Packet & operator<<(unsigned v)
Add integer to message.
Packet & operator<<(int v)
Add integer to message.
Packet & addMessage(const std::string &addr, const A &a, const B &b, const C &c)
Add three argument message.
Packet & addMessage(const std::string &addr, const A &a, const B &b, const C &c, const D &d, const E &e)
Add five argument message.
Packet(const char *contents, size_t size)
Packet & beginBundle(TimeTag timeTag=1)
Begin a new bundle.
Packet & operator<<(char v)
Add char to message.
Packet & clear()
Clear current packet contents.
Packet & addMessage(const std::string &addr, const A &a, const B &b, const C &c, const D &d, const E &e, const F &f, const G &g, const H &h)
Add eight argument message.
Packet & addMessage(const std::string &addr, const A &a, const B &b)
Add two argument message.
Packet & addMessage(const std::string &addr, const A &a, const B &b, const C &c, const D &d, const E &e, const F &f)
Add six argument message.
size_t size() const
Get number of bytes of current packet data.
Packet & addMessage(const std::string &addr, const A &a, const B &b, const C &c, const D &d)
Add four argument message.
const char * data() const
Get raw packet data.
Packet & operator<<(float v)
Add float to message.
Packet & addMessage(const std::string &addr, const A &a)
Add one argument message.
Packet & endMessage()
End message.
Socket for receiving OSC packets.
bool start()
Begin a background thread to poll the socket.
Recv & appendHandler(PacketHandler &v)
Add a packet handler to list. All handlers get all messages.
void stop()
Stop the background polling.
void bufferSize(int n)
Set size of internal buffer.
bool background() const
Whether background polling is activated.
Recv(uint16_t port, const char *address="", al_sec timeout=0)
const char * data() const
Get current received packet data.
Recv & handler(PacketHandler &v)
Set packet handling routine.
size_t send(const std::string &addr, const A &a, const B &b, const C &c)
Send three argument message immediately.
size_t send(const std::string &addr, const A &a, const B &b, const C &c, const D &d, const E &e)
Send five argument message immediately.
size_t send(const std::string &addr, const A &a, const B &b)
Send two argument message immediately.
size_t send(const std::string &addr, const A &a, const B &b, const C &c, const D &d, const E &e, const F &f, const G &g, const H &h)
Send eight argument message immediately.
size_t send()
Send and clear current packet contents.
Send(uint16_t port, const char *address="localhost", al_sec timeout=0, int size=1024)
size_t send(const std::string &addr)
Send zero argument message immediately.
size_t send(const std::string &addr, const A &a, const B &b, const C &c, const D &d)
Send four argument message immediately.
size_t send(const std::string &addr, const A &a)
Send one argument message immediately.
size_t send(const Packet &p)
Send a packet.
size_t send(const std::string &addr, const A &a, const B &b, const C &c, const D &d, const E &e, const F &f)
Send six argument message immediately.
size_t send(const std::string &addr, const A &a, const B &b, const C &c, const D &d, const E &e, const F &f, const G &g)
Send seven argument message immediately.
unsigned long long TimeTag
Time tag.