diff --git a/modules/juce_osc/osc/juce_OSCSender.cpp b/modules/juce_osc/osc/juce_OSCSender.cpp index 91cbb0176f..9ccbdca095 100644 --- a/modules/juce_osc/osc/juce_OSCSender.cpp +++ b/modules/juce_osc/osc/juce_OSCSender.cpp @@ -35,6 +35,8 @@ namespace */ struct OSCOutputStream { + OSCOutputStream() noexcept {} + /** Returns a pointer to the data that has been written to the stream. */ const void* getData() const noexcept { return output.getData(); } @@ -189,6 +191,8 @@ namespace private: MemoryOutputStream output; + + JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OSCOutputStream) }; } // namespace @@ -224,32 +228,34 @@ struct OSCSender::Pimpl } //========================================================================== - bool send (const OSCMessage& message) + bool send (const OSCMessage& message, const String& hostName, int portNumber) { OSCOutputStream outStream; outStream.writeMessage (message); - return sendOutputStream (outStream); + return sendOutputStream (outStream, hostName, portNumber); } - bool send (const OSCBundle& bundle) + bool send (const OSCBundle& bundle, const String& hostName, int portNumber) { OSCOutputStream outStream; outStream.writeBundle (bundle); - return sendOutputStream (outStream); + return sendOutputStream (outStream, hostName, portNumber); } + bool send (const OSCMessage& message) { return send (message, targetHostName, targetPortNumber); } + bool send (const OSCBundle& bundle) { return send (bundle, targetHostName, targetPortNumber); } + private: //========================================================================== - bool sendOutputStream (OSCOutputStream& outStream) + bool sendOutputStream (OSCOutputStream& outStream, const String& hostName, int portNumber) { if (socket != nullptr) { - int bytesWritten = socket->write (targetHostName, - targetPortNumber, - outStream.getData(), - (int) outStream.getDataSize()); + const int streamSize = (int) outStream.getDataSize(); - return bytesWritten == (int) outStream.getDataSize(); + const int bytesWritten = socket->write (hostName, portNumber, + outStream.getData(), streamSize); + return bytesWritten == streamSize; } // if you hit this, you tried to send some OSC data without being @@ -294,6 +300,8 @@ bool OSCSender::disconnect() bool OSCSender::send (const OSCMessage& message) { return pimpl->send (message); } bool OSCSender::send (const OSCBundle& bundle) { return pimpl->send (bundle); } +bool OSCSender::sendToIPAddress (const String& host, int port, const OSCMessage& message) { return pimpl->send (message, host, port); } +bool OSCSender::sendToIPAddress (const String& host, int port, const OSCBundle& bundle) { return pimpl->send (bundle, host, port); } //============================================================================== //============================================================================== diff --git a/modules/juce_osc/osc/juce_OSCSender.h b/modules/juce_osc/osc/juce_OSCSender.h index cba371a181..d8b57e9c7b 100644 --- a/modules/juce_osc/osc/juce_OSCSender.h +++ b/modules/juce_osc/osc/juce_OSCSender.h @@ -70,14 +70,37 @@ public: //========================================================================== /** Sends an OSC message to the target. - @param message The OSC message to send. - @returns true if the operation was successful. */ bool send (const OSCMessage& message); - //========================================================================== + /** Send an OSC bundle to the target. + @param bundle The OSC bundle to send. + @returns true if the operation was successful. + */ + bool send (const OSCBundle& bundle); + + /** Sends an OSC message to a specific IP address and port. + This overrides the address and port that was originally set for this sender. + @param targetIPAddress The IP address to send to + @param targetPortNumber The target port number + @param message The OSC message to send. + @returns true if the operation was successful. + */ + bool sendToIPAddress (const String& targetIPAddress, int targetPortNumber, + const OSCMessage& message); + + /** Sends an OSC bundle to a specific IP address and port. + This overrides the address and port that was originally set for this sender. + @param targetIPAddress The IP address to send to + @param targetPortNumber The target port number + @param message The OSC message to send. + @returns true if the operation was successful. + */ + bool sendToIPAddress (const String& targetIPAddress, int targetPortNumber, + const OSCBundle& bundle); + #if JUCE_COMPILER_SUPPORTS_VARIADIC_TEMPLATES && JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS /** Creates a new OSC message with the specified address pattern and list of arguments, and sends it to the target. @@ -88,16 +111,20 @@ public: */ template bool send (const OSCAddressPattern& address, Args&&... args); - #endif - - //========================================================================== - /** Send an OSC bundle to the target. - @param bundle The OSC bundle to send. + /** Creates a new OSC message with the specified address pattern and list + of arguments, and sends it to the target. - @returns true if the operation was successful. + @param targetIPAddress The IP address to send to + @param targetPortNumber The target port number + @param address The OSC address pattern of the message + (you can use a string literal here). + @param args The list of arguments for the message. */ - bool send (const OSCBundle& bundle); + template + bool sendToIPAddress (const String& targetIPAddress, int targetPortNumber, + const OSCAddressPattern& address, Args&&... args); + #endif private: //========================================================================== @@ -117,6 +144,13 @@ private: { return send (OSCMessage (address, std::forward (args)...)); } + + template + bool OSCSender::sendToIPAddress (const String& targetIPAddress, int targetPortNumber, + const OSCAddressPattern& address, Args&&... args) + { + return sendToIPAddress (targetIPAddress, targetPortNumber, OSCMessage (address, std::forward (args)...)); + } #endif // JUCE_COMPILER_SUPPORTS_VARIADIC_TEMPLATES && JUCE_COMPILER_SUPPORTS_MOVE_SEMANTICS #endif // JUCE_OSCSENDER_H_INCLUDED