From d2cf768b0a49261a36f0b319c390533fa1dce1cc Mon Sep 17 00:00:00 2001 From: Ben Kuper Date: Sat, 22 Jun 2024 20:09:54 +0200 Subject: [PATCH 1/4] Add IP and Port detection option in OSCReceiver --- modules/juce_osc/juce_osc.h | 6 ++++ modules/juce_osc/osc/juce_OSCMessage.cpp | 20 +++++++++++++ modules/juce_osc/osc/juce_OSCMessage.h | 15 ++++++++++ modules/juce_osc/osc/juce_OSCReceiver.cpp | 34 +++++++++++++++++++++-- 4 files changed, 72 insertions(+), 3 deletions(-) diff --git a/modules/juce_osc/juce_osc.h b/modules/juce_osc/juce_osc.h index 6195b449b231..d171c103557c 100644 --- a/modules/juce_osc/juce_osc.h +++ b/modules/juce_osc/juce_osc.h @@ -60,6 +60,12 @@ #pragma once #define JUCE_OSC_H_INCLUDED +/** Config: JUCE_IP_AND_PORT_DETECTION + If enabled, this will add remoteIP and remotePort variables to osc packets, corresponding to the sender's ip and port when receiving messages. +*/ +#ifndef JUCE_IP_AND_PORT_DETECTION +#define JUCE_IP_AND_PORT_DETECTION 0 +#endif #include #include diff --git a/modules/juce_osc/osc/juce_OSCMessage.cpp b/modules/juce_osc/osc/juce_OSCMessage.cpp index 47f0021cabc4..4a6da3d4d31d 100644 --- a/modules/juce_osc/osc/juce_OSCMessage.cpp +++ b/modules/juce_osc/osc/juce_OSCMessage.cpp @@ -50,6 +50,26 @@ OSCAddressPattern OSCMessage::getAddressPattern() const noexcept return addressPattern; } +#if JUCE_IP_AND_PORT_DETECTION +String OSCMessage::getSenderIPAddress() const noexcept +{ + return senderIPAddress; +} + +void OSCMessage::setSenderIPAddress(const String& ip) noexcept +{ + senderIPAddress = ip; +} + +int OSCMessage::getSenderPortNumber() const noexcept +{ + return senderPortNumber; +} +void OSCMessage::setSenderPortNumber(int port) noexcept +{ + senderPortNumber = port; +} +#endif //============================================================================== int OSCMessage::size() const noexcept { diff --git a/modules/juce_osc/osc/juce_OSCMessage.h b/modules/juce_osc/osc/juce_OSCMessage.h index f49e931c61a4..b07eac5c5a10 100644 --- a/modules/juce_osc/osc/juce_OSCMessage.h +++ b/modules/juce_osc/osc/juce_OSCMessage.h @@ -91,6 +91,16 @@ class JUCE_API OSCMessage /** Returns the address pattern of the OSCMessage. */ OSCAddressPattern getAddressPattern() const noexcept; +#if JUCE_IP_AND_PORT_DETECTION + /** Returns the sender's IP Address. */ + String getSenderIPAddress() const noexcept; + void setSenderIPAddress(const String& ip) noexcept; + + /** Returns the sender's port number. */ + int getSenderPortNumber() const noexcept; + void setSenderPortNumber(int port) noexcept; +#endif + /** Returns the number of OSCArgument objects that belong to this OSCMessage. */ int size() const noexcept; @@ -178,6 +188,11 @@ class JUCE_API OSCMessage //============================================================================== OSCAddressPattern addressPattern; Array arguments; + +#if JUCE_IP_AND_PORT_DETECTION + String senderIPAddress; + int senderPortNumber = 0; +#endif }; diff --git a/modules/juce_osc/osc/juce_OSCReceiver.cpp b/modules/juce_osc/osc/juce_OSCReceiver.cpp index 545a289299cc..f0510ba5d3c4 100644 --- a/modules/juce_osc/osc/juce_OSCReceiver.cpp +++ b/modules/juce_osc/osc/juce_OSCReceiver.cpp @@ -56,9 +56,18 @@ namespace @param sourceData the block of data to use as the stream's source @param sourceDataSize the number of bytes in the source data block */ + +#if JUCE_IP_AND_PORT_DETECTION + OSCInputStream (const void* sourceData, size_t sourceDataSize, const String& senderIPAddress, const int& senderPortNumber) : + input(sourceData, sourceDataSize, false), + senderIPAddress(senderIPAddress), + senderPortNumber(senderPortNumber) + {} +#else OSCInputStream (const void* sourceData, size_t sourceDataSize) : input (sourceData, sourceDataSize, false) {} +#endif //============================================================================== /** Returns a pointer to the source data block from which this stream is reading. */ @@ -273,6 +282,11 @@ namespace private: MemoryInputStream input; +#if JUCE_IP_AND_PORT_DETECTION + String senderIPAddress; + int senderPortNumber; +#endif + //============================================================================== void readPaddingZeros (size_t bytesRead) { @@ -427,10 +441,15 @@ struct OSCReceiver::Pimpl : private Thread, }; //============================================================================== - void handleBuffer (const char* data, size_t dataSize) +#if JUCE_IP_AND_PORT_DETECTION + void handleBuffer(const char* data, size_t dataSize, const String& senderIPAddress, const int& senderPortNumber) { - OSCInputStream inStream (data, dataSize); - + OSCInputStream inStream(data, dataSize, senderIPAddress, senderPortNumber); +#else + void handleBuffer(const char* data, size_t dataSize) + { + OSCInputStream inStream(data, dataSize); +#endif try { auto content = inStream.readElementWithKnownSize (dataSize); @@ -477,10 +496,19 @@ struct OSCReceiver::Pimpl : private Thread, if (ready == 0) continue; + +#if JUCE_IP_AND_PORT_DETECTION + String senderIPAddress = ""; + int senderPortNumber = 0; + auto bytesRead = (size_t) socket->read (oscBuffer.getData(), bufferSize, false, senderIPAddress, senderPortNumber); + if (bytesRead >= 4) + handleBuffer(oscBuffer.getData(), bytesRead, senderIPAddress, senderPortNumber); +#else auto bytesRead = (size_t) socket->read (oscBuffer.getData(), bufferSize, false); if (bytesRead >= 4) handleBuffer (oscBuffer.getData(), bytesRead); +#endif } } From 669ac2cc8f0496c6f31eea741d09b7b7d66044f8 Mon Sep 17 00:00:00 2001 From: Ben Kuper Date: Sat, 22 Jun 2024 20:11:46 +0200 Subject: [PATCH 2/4] added allow special chars in address --- modules/juce_osc/juce_osc.h | 8 ++++++++ modules/juce_osc/osc/juce_OSCAddress.cpp | 6 +++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/modules/juce_osc/juce_osc.h b/modules/juce_osc/juce_osc.h index d171c103557c..19eeb7665cbc 100644 --- a/modules/juce_osc/juce_osc.h +++ b/modules/juce_osc/juce_osc.h @@ -60,6 +60,14 @@ #pragma once #define JUCE_OSC_H_INCLUDED + +/** Config: JUCE_ALLOW_SPECIAL_CHARS_IN_ADDRESS + Enables the use of characters in adress that are not allowed by the OSC specifications (like spaces), but that are used + by some applications anyway (e.g. /my spaced/address) +*/ +#ifndef JUCE_ALLOW_SPECIAL_CHARS_IN_ADDRESS +#define JUCE_ALLOW_SPECIAL_CHARS_IN_ADDRESS 0 +#endif /** Config: JUCE_IP_AND_PORT_DETECTION If enabled, this will add remoteIP and remotePort variables to osc packets, corresponding to the sender's ip and port when receiving messages. */ diff --git a/modules/juce_osc/osc/juce_OSCAddress.cpp b/modules/juce_osc/osc/juce_OSCAddress.cpp index c13872cf7ece..99e1358de6d5 100644 --- a/modules/juce_osc/osc/juce_OSCAddress.cpp +++ b/modules/juce_osc/osc/juce_OSCAddress.cpp @@ -285,7 +285,11 @@ namespace static bool isDisallowedChar (juce_wchar c) noexcept { - return CharPointer_ASCII (Traits::getDisallowedChars()).indexOf (c, false) >= 0; +#if JUCE_ALLOW_SPECIAL_CHARS_IN_ADDRESS + return false; +#else + return CharPointer_ASCII( Traits::getDisallowedChars()).indexOf (c, false) >= 0; +#endif } static bool containsOnlyAllowedPrintableASCIIChars (const String& string) noexcept From 4f98d0ec2f6ad092d3d5a3a3036971f7308a48a9 Mon Sep 17 00:00:00 2001 From: Ben Kuper Date: Sat, 22 Jun 2024 20:12:10 +0200 Subject: [PATCH 3/4] added broadcast and binding options --- modules/juce_osc/juce_osc.h | 15 +++++++++++++++ modules/juce_osc/osc/juce_OSCReceiver.cpp | 11 ++++++++++- 2 files changed, 25 insertions(+), 1 deletion(-) diff --git a/modules/juce_osc/juce_osc.h b/modules/juce_osc/juce_osc.h index 19eeb7665cbc..28e5326b0969 100644 --- a/modules/juce_osc/juce_osc.h +++ b/modules/juce_osc/juce_osc.h @@ -68,6 +68,21 @@ #ifndef JUCE_ALLOW_SPECIAL_CHARS_IN_ADDRESS #define JUCE_ALLOW_SPECIAL_CHARS_IN_ADDRESS 0 #endif + +/** Config: JUCE_ENABLE_BROADCAST_BY_DEFAULT + Automatically enables broadcast on bound port in OSCReceiver +*/ +#ifndef JUCE_ENABLE_BROADCAST_BY_DEFAULT +#define JUCE_ENABLE_BROADCAST_BY_DEFAULT 0 +#endif + +/** Config: JUCE_EXCLUSIVE_BINDING_BY_DEFAULT + If enabled, this will make the binding of this port exclusive, so no other process can bind it. +*/ +#ifndef JUCE_EXCLUSIVE_BINDING_BY_DEFAULT +#define JUCE_EXCLUSIVE_BINDING_BY_DEFAULT 0 +#endif + /** Config: JUCE_IP_AND_PORT_DETECTION If enabled, this will add remoteIP and remotePort variables to osc packets, corresponding to the sender's ip and port when receiving messages. */ diff --git a/modules/juce_osc/osc/juce_OSCReceiver.cpp b/modules/juce_osc/osc/juce_OSCReceiver.cpp index f0510ba5d3c4..d6959dd2170b 100644 --- a/modules/juce_osc/osc/juce_OSCReceiver.cpp +++ b/modules/juce_osc/osc/juce_OSCReceiver.cpp @@ -354,7 +354,16 @@ struct OSCReceiver::Pimpl : private Thread, if (! disconnect()) return false; - socket.setOwned (new DatagramSocket (false)); +#if JUCE_ENABLE_BROADCAST_BY_DEFAULT + DatagramSocket* datagram = new DatagramSocket(true); +#else + DatagramSocket* datagram = new DatagramSocket(false); +#endif + socket.setOwned(datagram); + +#if JUCE_EXCLUSIVE_BINDING_BY_DEFAULT + socket->setEnablePortReuse(false); +#endif if (! socket->bindToPort (portNumber)) return false; From fe16f518bdf4cb58b29bdf3dcf16142d4b05e213 Mon Sep 17 00:00:00 2001 From: Ben Kuper Date: Sun, 23 Jun 2024 10:27:55 +0200 Subject: [PATCH 4/4] restructure to avoid warning when using allow special chars option --- modules/juce_osc/osc/juce_OSCAddress.cpp | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/modules/juce_osc/osc/juce_OSCAddress.cpp b/modules/juce_osc/osc/juce_OSCAddress.cpp index 99e1358de6d5..8b6540ff89bc 100644 --- a/modules/juce_osc/osc/juce_OSCAddress.cpp +++ b/modules/juce_osc/osc/juce_OSCAddress.cpp @@ -283,14 +283,17 @@ namespace return c >= ' ' && c <= '~'; } - static bool isDisallowedChar (juce_wchar c) noexcept - { #if JUCE_ALLOW_SPECIAL_CHARS_IN_ADDRESS + static bool isDisallowedChar (juce_wchar) noexcept + { return false; + } #else - return CharPointer_ASCII( Traits::getDisallowedChars()).indexOf (c, false) >= 0; -#endif + static bool isDisallowedChar(juce_wchar c) noexcept + { + return CharPointer_ASCII(Traits::getDisallowedChars()).indexOf(c, false) >= 0; } +#endif static bool containsOnlyAllowedPrintableASCIIChars (const String& string) noexcept {