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.

177 lines
5.9KB

  1. /*
  2. oscpack -- Open Sound Control (OSC) packet manipulation library
  3. http://www.rossbencina.com/code/oscpack
  4. Copyright (c) 2004-2013 Ross Bencina <rossb@audiomulch.com>
  5. Permission is hereby granted, free of charge, to any person obtaining
  6. a copy of this software and associated documentation files
  7. (the "Software"), to deal in the Software without restriction,
  8. including without limitation the rights to use, copy, modify, merge,
  9. publish, distribute, sublicense, and/or sell copies of the Software,
  10. and to permit persons to whom the Software is furnished to do so,
  11. subject to the following conditions:
  12. The above copyright notice and this permission notice shall be
  13. included in all copies or substantial portions of the Software.
  14. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  17. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  18. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  19. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  20. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  21. */
  22. /*
  23. The text above constitutes the entire oscpack license; however,
  24. the oscpack developer(s) also make the following non-binding requests:
  25. Any person wishing to distribute modifications to the Software is
  26. requested to send the modifications to the original developer so that
  27. they can be incorporated into the canonical version. It is also
  28. requested that these non-binding requests be included whenever the
  29. above license is reproduced.
  30. */
  31. #ifndef INCLUDED_OSCPACK_UDPSOCKET_H
  32. #define INCLUDED_OSCPACK_UDPSOCKET_H
  33. #include <cstring> // size_t
  34. #include "NetworkingUtils.h"
  35. #include "IpEndpointName.h"
  36. class PacketListener;
  37. class TimerListener;
  38. class UdpSocket;
  39. class SocketReceiveMultiplexer{
  40. class Implementation;
  41. Implementation *impl_;
  42. friend class UdpSocket;
  43. public:
  44. SocketReceiveMultiplexer();
  45. ~SocketReceiveMultiplexer();
  46. // only call the attach/detach methods _before_ calling Run
  47. // only one listener per socket, each socket at most once
  48. void AttachSocketListener( UdpSocket *socket, PacketListener *listener );
  49. void DetachSocketListener( UdpSocket *socket, PacketListener *listener );
  50. void AttachPeriodicTimerListener( int periodMilliseconds, TimerListener *listener );
  51. void AttachPeriodicTimerListener(
  52. int initialDelayMilliseconds, int periodMilliseconds, TimerListener *listener );
  53. void DetachPeriodicTimerListener( TimerListener *listener );
  54. void Run(); // loop and block processing messages indefinitely
  55. void RunUntilSigInt();
  56. void Break(); // call this from a listener to exit once the listener returns
  57. void AsynchronousBreak(); // call this from another thread or signal handler to exit the Run() state
  58. };
  59. class UdpSocket{
  60. class Implementation;
  61. Implementation *impl_;
  62. friend class SocketReceiveMultiplexer::Implementation;
  63. public:
  64. // Ctor throws std::runtime_error if there's a problem
  65. // initializing the socket.
  66. UdpSocket();
  67. virtual ~UdpSocket();
  68. // Enable broadcast addresses (e.g. x.x.x.255)
  69. // Sets SO_BROADCAST socket option.
  70. void SetEnableBroadcast( bool enableBroadcast );
  71. // Enable multiple listeners for a single port on same
  72. // network interface*
  73. // Sets SO_REUSEADDR (also SO_REUSEPORT on OS X).
  74. // [*] The exact behavior of SO_REUSEADDR and
  75. // SO_REUSEPORT is undefined for some common cases
  76. // and may have drastically different behavior on different
  77. // operating systems.
  78. void SetAllowReuse( bool allowReuse );
  79. // The socket is created in an unbound, unconnected state
  80. // such a socket can only be used to send to an arbitrary
  81. // address using SendTo(). To use Send() you need to first
  82. // connect to a remote endpoint using Connect(). To use
  83. // ReceiveFrom you need to first bind to a local endpoint
  84. // using Bind().
  85. // Retrieve the local endpoint name when sending to 'to'
  86. IpEndpointName LocalEndpointFor( const IpEndpointName& remoteEndpoint ) const;
  87. // Connect to a remote endpoint which is used as the target
  88. // for calls to Send()
  89. void Connect( const IpEndpointName& remoteEndpoint );
  90. void Send( const char *data, std::size_t size );
  91. void SendTo( const IpEndpointName& remoteEndpoint, const char *data, std::size_t size );
  92. // Bind a local endpoint to receive incoming data. Endpoint
  93. // can be 'any' for the system to choose an endpoint
  94. void Bind( const IpEndpointName& localEndpoint );
  95. bool IsBound() const;
  96. std::size_t ReceiveFrom( IpEndpointName& remoteEndpoint, char *data, std::size_t size );
  97. };
  98. // convenience classes for transmitting and receiving
  99. // they just call Connect and/or Bind in the ctor.
  100. // note that you can still use a receive socket
  101. // for transmitting etc
  102. class UdpTransmitSocket : public UdpSocket{
  103. public:
  104. UdpTransmitSocket( const IpEndpointName& remoteEndpoint )
  105. { Connect( remoteEndpoint ); }
  106. };
  107. class UdpReceiveSocket : public UdpSocket{
  108. public:
  109. UdpReceiveSocket( const IpEndpointName& localEndpoint )
  110. { Bind( localEndpoint ); }
  111. };
  112. // UdpListeningReceiveSocket provides a simple way to bind one listener
  113. // to a single socket without having to manually set up a SocketReceiveMultiplexer
  114. class UdpListeningReceiveSocket : public UdpSocket{
  115. SocketReceiveMultiplexer mux_;
  116. PacketListener *listener_;
  117. public:
  118. UdpListeningReceiveSocket( const IpEndpointName& localEndpoint, PacketListener *listener )
  119. : listener_( listener )
  120. {
  121. Bind( localEndpoint );
  122. mux_.AttachSocketListener( this, listener_ );
  123. }
  124. ~UdpListeningReceiveSocket()
  125. { mux_.DetachSocketListener( this, listener_ ); }
  126. // see SocketReceiveMultiplexer above for the behaviour of these methods...
  127. void Run() { mux_.Run(); }
  128. void RunUntilSigInt() { mux_.RunUntilSigInt(); }
  129. void Break() { mux_.Break(); }
  130. void AsynchronousBreak() { mux_.AsynchronousBreak(); }
  131. };
  132. #endif /* INCLUDED_OSCPACK_UDPSOCKET_H */