UdpSocket.h 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. /*
  2. oscpack -- Open Sound Control packet manipulation library
  3. http://www.audiomulch.com/~rossb/oscpack
  4. Copyright (c) 2004-2005 Ross Bencina <[email protected]>
  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. Any person wishing to distribute modifications to the Software is
  15. requested to send the modifications to the original developer so that
  16. they can be incorporated into the canonical version.
  17. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  18. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  19. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  20. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  21. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  22. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  23. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  24. */
  25. #ifndef INCLUDED_UDPSOCKET_H
  26. #define INCLUDED_UDPSOCKET_H
  27. #ifndef INCLUDED_NETWORKINGUTILITIES_H
  28. #include "NetworkingUtils.h"
  29. #endif /* INCLUDED_NETWORKINGUTILITIES_H */
  30. #ifndef INCLUDED_IPENDPOINTNAME_H
  31. #include "IpEndpointName.h"
  32. #endif /* INCLUDED_IPENDPOINTNAME_H */
  33. class PacketListener;
  34. class TimerListener;
  35. class UdpSocket;
  36. class SocketReceiveMultiplexer{
  37. class Implementation;
  38. Implementation *impl_;
  39. friend class UdpSocket;
  40. public:
  41. SocketReceiveMultiplexer();
  42. ~SocketReceiveMultiplexer();
  43. // only call the attach/detach methods _before_ calling Run
  44. // only one listener per socket, each socket at most once
  45. void AttachSocketListener( UdpSocket *socket, PacketListener *listener );
  46. void DetachSocketListener( UdpSocket *socket, PacketListener *listener );
  47. void AttachPeriodicTimerListener( int periodMilliseconds, TimerListener *listener );
  48. void AttachPeriodicTimerListener(
  49. int initialDelayMilliseconds, int periodMilliseconds, TimerListener *listener );
  50. void DetachPeriodicTimerListener( TimerListener *listener );
  51. void Run(); // loop and block processing messages indefinitely
  52. void RunUntilSigInt();
  53. void Break(); // call this from a listener to exit once the listener returns
  54. void AsynchronousBreak(); // call this from another thread or signal handler to exit the Run() state
  55. };
  56. class UdpSocket{
  57. class Implementation;
  58. Implementation *impl_;
  59. friend class SocketReceiveMultiplexer::Implementation;
  60. public:
  61. // ctor throws std::runtime_error if there's a problem
  62. // initializing the socket.
  63. UdpSocket();
  64. virtual ~UdpSocket();
  65. // the socket is created in an unbound, unconnected state
  66. // such a socket can only be used to send to an arbitrary
  67. // address using SendTo(). To use Send() you need to first
  68. // connect to a remote endpoint using Connect(). To use
  69. // ReceiveFrom you need to first bind to a local endpoint
  70. // using Bind().
  71. // retrieve the local endpoint name when sending to 'to'
  72. IpEndpointName LocalEndpointFor( const IpEndpointName& remoteEndpoint ) const;
  73. // Connect to a remote endpoint which is used as the target
  74. // for calls to Send()
  75. void Connect( const IpEndpointName& remoteEndpoint );
  76. void Send( const char *data, int size );
  77. void SendTo( const IpEndpointName& remoteEndpoint, const char *data, int size );
  78. // Bind a local endpoint to receive incoming data. Endpoint
  79. // can be 'any' for the system to choose an endpoint
  80. void Bind( const IpEndpointName& localEndpoint );
  81. bool IsBound() const;
  82. int ReceiveFrom( IpEndpointName& remoteEndpoint, char *data, int size );
  83. };
  84. // convenience classes for transmitting and receiving
  85. // they just call Connect and/or Bind in the ctor.
  86. // note that you can still use a receive socket
  87. // for transmitting etc
  88. class UdpTransmitSocket : public UdpSocket{
  89. public:
  90. UdpTransmitSocket( const IpEndpointName& remoteEndpoint )
  91. { Connect( remoteEndpoint ); }
  92. };
  93. class UdpReceiveSocket : public UdpSocket{
  94. public:
  95. UdpReceiveSocket( const IpEndpointName& localEndpoint )
  96. { Bind( localEndpoint ); }
  97. };
  98. // UdpListeningReceiveSocket provides a simple way to bind one listener
  99. // to a single socket without having to manually set up a SocketReceiveMultiplexer
  100. class UdpListeningReceiveSocket : public UdpSocket{
  101. SocketReceiveMultiplexer mux_;
  102. PacketListener *listener_;
  103. public:
  104. UdpListeningReceiveSocket( const IpEndpointName& localEndpoint, PacketListener *listener )
  105. : listener_( listener )
  106. {
  107. Bind( localEndpoint );
  108. mux_.AttachSocketListener( this, listener_ );
  109. }
  110. ~UdpListeningReceiveSocket()
  111. { mux_.DetachSocketListener( this, listener_ ); }
  112. // see SocketReceiveMultiplexer above for the behaviour of these methods...
  113. void Run() { mux_.Run(); }
  114. void RunUntilSigInt() { mux_.RunUntilSigInt(); }
  115. void Break() { mux_.Break(); }
  116. void AsynchronousBreak() { mux_.AsynchronousBreak(); }
  117. };
  118. #endif /* INCLUDED_UDPSOCKET_H */