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.

165 lines
6.5KB

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