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