Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add OSC Extra Options #1395

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions modules/juce_osc/juce_osc.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,35 @@
#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_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.
*/
#ifndef JUCE_IP_AND_PORT_DETECTION
#define JUCE_IP_AND_PORT_DETECTION 0
#endif

#include <juce_core/juce_core.h>
#include <juce_events/juce_events.h>

Expand Down
11 changes: 9 additions & 2 deletions modules/juce_osc/osc/juce_OSCAddress.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -283,10 +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 CharPointer_ASCII (Traits::getDisallowedChars()).indexOf (c, false) >= 0;
return false;
}
#else
static bool isDisallowedChar(juce_wchar c) noexcept
{
return CharPointer_ASCII(Traits::getDisallowedChars()).indexOf(c, false) >= 0;
}
#endif

static bool containsOnlyAllowedPrintableASCIIChars (const String& string) noexcept
{
Expand Down
20 changes: 20 additions & 0 deletions modules/juce_osc/osc/juce_OSCMessage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
15 changes: 15 additions & 0 deletions modules/juce_osc/osc/juce_OSCMessage.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -178,6 +188,11 @@ class JUCE_API OSCMessage
//==============================================================================
OSCAddressPattern addressPattern;
Array<OSCArgument> arguments;

#if JUCE_IP_AND_PORT_DETECTION
String senderIPAddress;
int senderPortNumber = 0;
#endif
};


Expand Down
45 changes: 41 additions & 4 deletions modules/juce_osc/osc/juce_OSCReceiver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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. */
Expand Down Expand Up @@ -273,6 +282,11 @@ namespace
private:
MemoryInputStream input;

#if JUCE_IP_AND_PORT_DETECTION
String senderIPAddress;
int senderPortNumber;
#endif

//==============================================================================
void readPaddingZeros (size_t bytesRead)
{
Expand Down Expand Up @@ -340,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;
Expand Down Expand Up @@ -427,10 +450,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);
Expand Down Expand Up @@ -477,10 +505,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
}
}

Expand Down