iPlug2 - C++ Audio Plug-in Framework
Loading...
Searching...
No Matches
IPlugOSC.h
1/*
2 ==============================================================================
3
4 This file is part of the iPlug 2 library. Copyright (C) the iPlug 2 developers.
5
6 See LICENSE.txt for more info.
7
8 ==============================================================================
9*/
10
11#pragma once
12
20#include <cstring>
21#include <ctime>
22#include <functional>
23#include <memory>
24
25#include "jnetlib/jnetlib.h"
26
27#include "IPlugPlatform.h"
28#include "IPlugLogger.h"
29#include "IPlugOSC_msg.h"
30#include "IPlugTimer.h"
31
32
33BEGIN_IPLUG_NAMESPACE
34
35#ifndef OSC_TIMER_RATE
36static constexpr int OSC_TIMER_RATE = 100;
37#endif
38
39using OSCLogFunc = std::function<void(WDL_String& log)>;
40
43{
44public:
50 OSCDevice(const char* dest, int maxpacket, int sendsleep, struct sockaddr_in* listen_addr);
51
52 virtual ~OSCDevice();
53
55 void RunInput();
56
58 void RunOutput();
59
61 void AddInstance(void (*callback)(void* d1, int dev_idx, int msglen, void* msg), void* d1, int dev_idx);
62
67 void OnMessage(char type, const unsigned char* msg, int len);
68
72 void SendOSC(const char* src, int len);
73
74private:
75 struct rec
76 {
77 void (*callback)(void* d1, int dev_idx, int msglen, void* msg);
78 void* data1;
79 int dev_idx;
80 };
81
82 WDL_TypedBuf<rec> mInstances;
83public:
84 double mLastOpenTime = 0;
85 bool mHasInput = false;
86 bool mHasOutput = false;
87
88 SOCKET mSendSocket;
89 int mMaxMacketSize, mSendSleep;
90 WDL_String mDestination;
91
92 struct sockaddr_in mSendAddress, mReceiveAddress;
93 WDL_Queue mSendQueue, mReceiveQueue;
94};
95
98{
99 struct incomingEvent
100 {
101 OSCDevice* dev_ptr;
102 int sz; // size of msg
103 unsigned char msg[3];
104 };
105
106public:
109 OSCInterface(OSCLogFunc logFunc = nullptr);
110
111 virtual ~OSCInterface();
112
113 OSCInterface(const OSCInterface&) = delete;
114 OSCInterface& operator=(const OSCInterface&) = delete;
115
120 OSCDevice* CreateReceiver(WDL_String& log, int port = 8000);
121
126 OSCDevice* CreateSender(WDL_String& log, const char* ip = "127.0.0.1", int port = 8000);
127
128public:
131 virtual void OnOSCMessage(OscMessageRead& msg) {};
132
135 void SetLogFunc(OSCLogFunc logFunc) { mLogFunc = logFunc; }
136
137private:
138 static void MessageCallback(void *d1, int dev_idx, int msglen, void *msg);
139
140 void OnTimer(Timer& timer);
141
142 // these are non-owned refs
143 WDL_PtrList<OSCDevice> mDevices;
144
145protected:
146 OSCLogFunc mLogFunc;
147 static std::unique_ptr<Timer> mTimer;
148 static int sInstances;
149 WDL_HeapBuf mIncomingEvents; // incomingEvent list, each is 8-byte aligned
150 WDL_Mutex mIncomingEvents_mutex;
151};
152
155{
156public:
161 OSCSender(const char* destIP = "127.0.0.1", int port = 8000, OSCLogFunc logFunc = nullptr);
162
166 void SetDestination(const char* ip, int port);
167
171private:
172 int mPort = 0;
173 WDL_String mDestIP;
174 OSCDevice* mDevice = nullptr;
175};
176
178{
179public:
183 OSCReceiver(int port = 8000, OSCLogFunc logFunc = nullptr);
184
187 void SetReceivePort(int port);
188
190 virtual void OnOSCMessage(OscMessageRead& msg) = 0;
191
192private:
193 OSCDevice* mDevice = nullptr;
194 int mPort = 0;
195 char mReadBuf[MAX_OSC_MSG_LEN] = {};
196};
197
198
199END_IPLUG_NAMESPACE
IPlug logging a.k.a tracing functionality.
Include to get consistently named preprocessor macros for different platforms and logging functionali...
This file includes classes for implementing timers - in order to get a regular callback on the main t...
void OnMessage(char type, const unsigned char *msg, int len)
Definition: IPlugOSC.cpp:178
void RunOutput()
Definition: IPlugOSC.cpp:99
void SendOSC(const char *src, int len)
Definition: IPlugOSC.cpp:186
void AddInstance(void(*callback)(void *d1, int dev_idx, int msglen, void *msg), void *d1, int dev_idx)
Definition: IPlugOSC.cpp:172
void RunInput()
Definition: IPlugOSC.cpp:78
void SetLogFunc(OSCLogFunc logFunc)
Set the Log Func object.
Definition: IPlugOSC.h:135
OSCDevice * CreateReceiver(WDL_String &log, int port=8000)
Create a Receiver object.
Definition: IPlugOSC.cpp:309
OSCDevice * CreateSender(WDL_String &log, const char *ip="127.0.0.1", int port=8000)
Create a Sender object.
Definition: IPlugOSC.cpp:367
virtual void OnOSCMessage(OscMessageRead &msg)
Definition: IPlugOSC.h:131
void SetReceivePort(int port)
Set the Receive Port object.
Definition: IPlugOSC.cpp:455
virtual void OnOSCMessage(OscMessageRead &msg)=0
void SetDestination(const char *ip, int port)
Set the Destination object.
Definition: IPlugOSC.cpp:422
void SendOSCMessage(OscMessageWrite &msg)
Definition: IPlugOSC.cpp:442
Base class for timer.
Definition: IPlugTimer.h:40