tomwalters@570: /* tomwalters@570: oscpack -- Open Sound Control packet manipulation library tomwalters@570: http://www.audiomulch.com/~rossb/oscpack tomwalters@570: tomwalters@570: Copyright (c) 2004-2005 Ross Bencina tomwalters@570: tomwalters@570: Permission is hereby granted, free of charge, to any person obtaining tomwalters@570: a copy of this software and associated documentation files tomwalters@570: (the "Software"), to deal in the Software without restriction, tomwalters@570: including without limitation the rights to use, copy, modify, merge, tomwalters@570: publish, distribute, sublicense, and/or sell copies of the Software, tomwalters@570: and to permit persons to whom the Software is furnished to do so, tomwalters@570: subject to the following conditions: tomwalters@570: tomwalters@570: The above copyright notice and this permission notice shall be tomwalters@570: included in all copies or substantial portions of the Software. tomwalters@570: tomwalters@570: Any person wishing to distribute modifications to the Software is tomwalters@570: requested to send the modifications to the original developer so that tomwalters@570: they can be incorporated into the canonical version. tomwalters@570: tomwalters@570: THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, tomwalters@570: EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF tomwalters@570: MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. tomwalters@570: IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR tomwalters@570: ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF tomwalters@570: CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION tomwalters@570: WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. tomwalters@570: */ tomwalters@570: #ifndef INCLUDED_UDPSOCKET_H tomwalters@570: #define INCLUDED_UDPSOCKET_H tomwalters@570: tomwalters@570: #ifndef INCLUDED_NETWORKINGUTILITIES_H tomwalters@570: #include "NetworkingUtils.h" tomwalters@570: #endif /* INCLUDED_NETWORKINGUTILITIES_H */ tomwalters@570: tomwalters@570: #ifndef INCLUDED_IPENDPOINTNAME_H tomwalters@570: #include "IpEndpointName.h" tomwalters@570: #endif /* INCLUDED_IPENDPOINTNAME_H */ tomwalters@570: tomwalters@570: tomwalters@570: class PacketListener; tomwalters@570: class TimerListener; tomwalters@570: tomwalters@570: class UdpSocket; tomwalters@570: tomwalters@570: class SocketReceiveMultiplexer{ tomwalters@570: class Implementation; tomwalters@570: Implementation *impl_; tomwalters@570: tomwalters@570: friend class UdpSocket; tomwalters@570: tomwalters@570: public: tomwalters@570: SocketReceiveMultiplexer(); tomwalters@570: ~SocketReceiveMultiplexer(); tomwalters@570: tomwalters@570: // only call the attach/detach methods _before_ calling Run tomwalters@570: tomwalters@570: // only one listener per socket, each socket at most once tomwalters@570: void AttachSocketListener( UdpSocket *socket, PacketListener *listener ); tomwalters@570: void DetachSocketListener( UdpSocket *socket, PacketListener *listener ); tomwalters@570: tomwalters@570: void AttachPeriodicTimerListener( int periodMilliseconds, TimerListener *listener ); tomwalters@570: void AttachPeriodicTimerListener( tomwalters@570: int initialDelayMilliseconds, int periodMilliseconds, TimerListener *listener ); tomwalters@570: void DetachPeriodicTimerListener( TimerListener *listener ); tomwalters@570: tomwalters@570: void Run(); // loop and block processing messages indefinitely tomwalters@570: void RunUntilSigInt(); tomwalters@570: void Break(); // call this from a listener to exit once the listener returns tomwalters@570: void AsynchronousBreak(); // call this from another thread or signal handler to exit the Run() state tomwalters@570: }; tomwalters@570: tomwalters@570: tomwalters@570: class UdpSocket{ tomwalters@570: class Implementation; tomwalters@570: Implementation *impl_; tomwalters@570: tomwalters@570: friend class SocketReceiveMultiplexer::Implementation; tomwalters@570: tomwalters@570: public: tomwalters@570: tomwalters@570: // ctor throws std::runtime_error if there's a problem tomwalters@570: // initializing the socket. tomwalters@570: UdpSocket(); tomwalters@570: virtual ~UdpSocket(); tomwalters@570: tomwalters@570: // the socket is created in an unbound, unconnected state tomwalters@570: // such a socket can only be used to send to an arbitrary tomwalters@570: // address using SendTo(). To use Send() you need to first tomwalters@570: // connect to a remote endpoint using Connect(). To use tomwalters@570: // ReceiveFrom you need to first bind to a local endpoint tomwalters@570: // using Bind(). tomwalters@570: tomwalters@570: // retrieve the local endpoint name when sending to 'to' tomwalters@570: IpEndpointName LocalEndpointFor( const IpEndpointName& remoteEndpoint ) const; tomwalters@570: tomwalters@570: // Connect to a remote endpoint which is used as the target tomwalters@570: // for calls to Send() tomwalters@570: void Connect( const IpEndpointName& remoteEndpoint ); tomwalters@570: void Send( const char *data, int size ); tomwalters@570: void SendTo( const IpEndpointName& remoteEndpoint, const char *data, int size ); tomwalters@570: tomwalters@570: tomwalters@570: // Bind a local endpoint to receive incoming data. Endpoint tomwalters@570: // can be 'any' for the system to choose an endpoint tomwalters@570: void Bind( const IpEndpointName& localEndpoint ); tomwalters@570: bool IsBound() const; tomwalters@570: tomwalters@570: int ReceiveFrom( IpEndpointName& remoteEndpoint, char *data, int size ); tomwalters@570: }; tomwalters@570: tomwalters@570: tomwalters@570: // convenience classes for transmitting and receiving tomwalters@570: // they just call Connect and/or Bind in the ctor. tomwalters@570: // note that you can still use a receive socket tomwalters@570: // for transmitting etc tomwalters@570: tomwalters@570: class UdpTransmitSocket : public UdpSocket{ tomwalters@570: public: tomwalters@570: UdpTransmitSocket( const IpEndpointName& remoteEndpoint ) tomwalters@570: { Connect( remoteEndpoint ); } tomwalters@570: }; tomwalters@570: tomwalters@570: tomwalters@570: class UdpReceiveSocket : public UdpSocket{ tomwalters@570: public: tomwalters@570: UdpReceiveSocket( const IpEndpointName& localEndpoint ) tomwalters@570: { Bind( localEndpoint ); } tomwalters@570: }; tomwalters@570: tomwalters@570: tomwalters@570: // UdpListeningReceiveSocket provides a simple way to bind one listener tomwalters@570: // to a single socket without having to manually set up a SocketReceiveMultiplexer tomwalters@570: tomwalters@570: class UdpListeningReceiveSocket : public UdpSocket{ tomwalters@570: SocketReceiveMultiplexer mux_; tomwalters@570: PacketListener *listener_; tomwalters@570: public: tomwalters@570: UdpListeningReceiveSocket( const IpEndpointName& localEndpoint, PacketListener *listener ) tomwalters@570: : listener_( listener ) tomwalters@570: { tomwalters@570: Bind( localEndpoint ); tomwalters@570: mux_.AttachSocketListener( this, listener_ ); tomwalters@570: } tomwalters@570: tomwalters@570: ~UdpListeningReceiveSocket() tomwalters@570: { mux_.DetachSocketListener( this, listener_ ); } tomwalters@570: tomwalters@570: // see SocketReceiveMultiplexer above for the behaviour of these methods... tomwalters@570: void Run() { mux_.Run(); } tomwalters@570: void RunUntilSigInt() { mux_.RunUntilSigInt(); } tomwalters@570: void Break() { mux_.Break(); } tomwalters@570: void AsynchronousBreak() { mux_.AsynchronousBreak(); } tomwalters@570: }; tomwalters@570: tomwalters@570: tomwalters@570: #endif /* INCLUDED_UDPSOCKET_H */