The JUCE cross-platform C++ framework, with DISTRHO/KXStudio specific changes
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

164 lines
6.5KB

  1. /*
  2. ==============================================================================
  3. This file is part of the JUCE library.
  4. Copyright (c) 2020 - Raw Material Software Limited
  5. JUCE is an open source library subject to commercial or open-source
  6. licensing.
  7. By using JUCE, you agree to the terms of both the JUCE 6 End-User License
  8. Agreement and JUCE Privacy Policy (both effective as of the 16th June 2020).
  9. End User License Agreement: www.juce.com/juce-6-licence
  10. Privacy Policy: www.juce.com/juce-privacy-policy
  11. Or: You may also use this code under the terms of the GPL v3 (see
  12. www.gnu.org/licenses).
  13. JUCE IS PROVIDED "AS IS" WITHOUT ANY WARRANTY, AND ALL WARRANTIES, WHETHER
  14. EXPRESSED OR IMPLIED, INCLUDING MERCHANTABILITY AND FITNESS FOR PURPOSE, ARE
  15. DISCLAIMED.
  16. ==============================================================================
  17. */
  18. namespace juce
  19. {
  20. //==============================================================================
  21. /**
  22. An OSC message sender.
  23. An OSCSender object can connect to a network port. It then can send OSC
  24. messages and bundles to a specified host over an UDP socket.
  25. @tags{OSC}
  26. */
  27. class JUCE_API OSCSender
  28. {
  29. public:
  30. //==============================================================================
  31. /** Constructs a new OSCSender. */
  32. OSCSender();
  33. /** Destructor. */
  34. ~OSCSender();
  35. //==============================================================================
  36. /** Connects to a datagram socket and prepares the socket for sending OSC
  37. packets to the specified target.
  38. Note: The operating system will choose which specific network adapter(s)
  39. to bind your socket to, and which local port to use for the sender.
  40. @param targetHostName The remote host to which messages will be send.
  41. @param targetPortNumber The remote UDP port number on which the host will
  42. receive the messages.
  43. @returns true if the connection was successful; false otherwise.
  44. @see send, disconnect.
  45. */
  46. bool connect (const String& targetHostName, int targetPortNumber);
  47. /** Uses an existing datagram socket for sending OSC packets to the specified target.
  48. @param socket An existing datagram socket. Make sure this doesn't
  49. get deleted while this class is still using it!
  50. @param targetHostName The remote host to which messages will be send.
  51. @param targetPortNumber The remote UDP port number on which the host will
  52. receive the messages.
  53. @returns true if the connection was successful; false otherwise.
  54. @see connect, send, disconnect.
  55. */
  56. bool connectToSocket (DatagramSocket& socket, const String& targetHostName, int targetPortNumber);
  57. //==============================================================================
  58. /** Disconnects from the currently used UDP port.
  59. @returns true if the disconnection was successful; false otherwise.
  60. @see connect.
  61. */
  62. bool disconnect();
  63. //==============================================================================
  64. /** Sends an OSC message to the target.
  65. @param message The OSC message to send.
  66. @returns true if the operation was successful.
  67. */
  68. bool send (const OSCMessage& message);
  69. /** Send an OSC bundle to the target.
  70. @param bundle The OSC bundle to send.
  71. @returns true if the operation was successful.
  72. */
  73. bool send (const OSCBundle& bundle);
  74. /** Sends an OSC message to a specific IP address and port.
  75. This overrides the address and port that was originally set for this sender.
  76. @param targetIPAddress The IP address to send to
  77. @param targetPortNumber The target port number
  78. @param message The OSC message to send.
  79. @returns true if the operation was successful.
  80. */
  81. bool sendToIPAddress (const String& targetIPAddress, int targetPortNumber,
  82. const OSCMessage& message);
  83. /** Sends an OSC bundle to a specific IP address and port.
  84. This overrides the address and port that was originally set for this sender.
  85. @param targetIPAddress The IP address to send to
  86. @param targetPortNumber The target port number
  87. @param bundle The OSC bundle to send.
  88. @returns true if the operation was successful.
  89. */
  90. bool sendToIPAddress (const String& targetIPAddress, int targetPortNumber,
  91. const OSCBundle& bundle);
  92. /** Creates a new OSC message with the specified address pattern and list
  93. of arguments, and sends it to the target.
  94. @param address The OSC address pattern of the message
  95. (you can use a string literal here).
  96. @param args The list of arguments for the message.
  97. */
  98. template <typename... Args>
  99. bool send (const OSCAddressPattern& address, Args&&... args);
  100. /** Creates a new OSC message with the specified address pattern and list
  101. of arguments, and sends it to the target.
  102. @param targetIPAddress The IP address to send to
  103. @param targetPortNumber The target port number
  104. @param address The OSC address pattern of the message
  105. (you can use a string literal here).
  106. @param args The list of arguments for the message.
  107. */
  108. template <typename... Args>
  109. bool sendToIPAddress (const String& targetIPAddress, int targetPortNumber,
  110. const OSCAddressPattern& address, Args&&... args);
  111. private:
  112. //==============================================================================
  113. struct Pimpl;
  114. std::unique_ptr<Pimpl> pimpl;
  115. JUCE_DECLARE_NON_COPYABLE_WITH_LEAK_DETECTOR (OSCSender)
  116. };
  117. //==============================================================================
  118. template <typename... Args>
  119. bool OSCSender::send (const OSCAddressPattern& address, Args&&... args)
  120. {
  121. return send (OSCMessage (address, std::forward<Args> (args)...));
  122. }
  123. template <typename... Args>
  124. bool OSCSender::sendToIPAddress (const String& targetIPAddress, int targetPortNumber,
  125. const OSCAddressPattern& address, Args&&... args)
  126. {
  127. return sendToIPAddress (targetIPAddress, targetPortNumber, OSCMessage (address, std::forward<Args> (args)...));
  128. }
  129. } // namespace juce