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.

157 lines
6.3KB

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