annotate external/oscpack/ip/UdpSocket.h @ 511:a39da72c7980

Example config for OSC
author tomwalters@google.com
date Fri, 22 Jun 2012 12:27:44 +0000
parents 0284d2152e17
children
rev   line source
tomwalters@509 1 /*
tomwalters@509 2 oscpack -- Open Sound Control packet manipulation library
tomwalters@509 3 http://www.audiomulch.com/~rossb/oscpack
tomwalters@509 4
tomwalters@509 5 Copyright (c) 2004-2005 Ross Bencina <rossb@audiomulch.com>
tomwalters@509 6
tomwalters@509 7 Permission is hereby granted, free of charge, to any person obtaining
tomwalters@509 8 a copy of this software and associated documentation files
tomwalters@509 9 (the "Software"), to deal in the Software without restriction,
tomwalters@509 10 including without limitation the rights to use, copy, modify, merge,
tomwalters@509 11 publish, distribute, sublicense, and/or sell copies of the Software,
tomwalters@509 12 and to permit persons to whom the Software is furnished to do so,
tomwalters@509 13 subject to the following conditions:
tomwalters@509 14
tomwalters@509 15 The above copyright notice and this permission notice shall be
tomwalters@509 16 included in all copies or substantial portions of the Software.
tomwalters@509 17
tomwalters@509 18 Any person wishing to distribute modifications to the Software is
tomwalters@509 19 requested to send the modifications to the original developer so that
tomwalters@509 20 they can be incorporated into the canonical version.
tomwalters@509 21
tomwalters@509 22 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
tomwalters@509 23 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
tomwalters@509 24 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
tomwalters@509 25 IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
tomwalters@509 26 ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
tomwalters@509 27 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
tomwalters@509 28 WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
tomwalters@509 29 */
tomwalters@509 30 #ifndef INCLUDED_UDPSOCKET_H
tomwalters@509 31 #define INCLUDED_UDPSOCKET_H
tomwalters@509 32
tomwalters@509 33 #ifndef INCLUDED_NETWORKINGUTILITIES_H
tomwalters@509 34 #include "NetworkingUtils.h"
tomwalters@509 35 #endif /* INCLUDED_NETWORKINGUTILITIES_H */
tomwalters@509 36
tomwalters@509 37 #ifndef INCLUDED_IPENDPOINTNAME_H
tomwalters@509 38 #include "IpEndpointName.h"
tomwalters@509 39 #endif /* INCLUDED_IPENDPOINTNAME_H */
tomwalters@509 40
tomwalters@509 41
tomwalters@509 42 class PacketListener;
tomwalters@509 43 class TimerListener;
tomwalters@509 44
tomwalters@509 45 class UdpSocket;
tomwalters@509 46
tomwalters@509 47 class SocketReceiveMultiplexer{
tomwalters@509 48 class Implementation;
tomwalters@509 49 Implementation *impl_;
tomwalters@509 50
tomwalters@509 51 friend class UdpSocket;
tomwalters@509 52
tomwalters@509 53 public:
tomwalters@509 54 SocketReceiveMultiplexer();
tomwalters@509 55 ~SocketReceiveMultiplexer();
tomwalters@509 56
tomwalters@509 57 // only call the attach/detach methods _before_ calling Run
tomwalters@509 58
tomwalters@509 59 // only one listener per socket, each socket at most once
tomwalters@509 60 void AttachSocketListener( UdpSocket *socket, PacketListener *listener );
tomwalters@509 61 void DetachSocketListener( UdpSocket *socket, PacketListener *listener );
tomwalters@509 62
tomwalters@509 63 void AttachPeriodicTimerListener( int periodMilliseconds, TimerListener *listener );
tomwalters@509 64 void AttachPeriodicTimerListener(
tomwalters@509 65 int initialDelayMilliseconds, int periodMilliseconds, TimerListener *listener );
tomwalters@509 66 void DetachPeriodicTimerListener( TimerListener *listener );
tomwalters@509 67
tomwalters@509 68 void Run(); // loop and block processing messages indefinitely
tomwalters@509 69 void RunUntilSigInt();
tomwalters@509 70 void Break(); // call this from a listener to exit once the listener returns
tomwalters@509 71 void AsynchronousBreak(); // call this from another thread or signal handler to exit the Run() state
tomwalters@509 72 };
tomwalters@509 73
tomwalters@509 74
tomwalters@509 75 class UdpSocket{
tomwalters@509 76 class Implementation;
tomwalters@509 77 Implementation *impl_;
tomwalters@509 78
tomwalters@509 79 friend class SocketReceiveMultiplexer::Implementation;
tomwalters@509 80
tomwalters@509 81 public:
tomwalters@509 82
tomwalters@509 83 // ctor throws std::runtime_error if there's a problem
tomwalters@509 84 // initializing the socket.
tomwalters@509 85 UdpSocket();
tomwalters@509 86 virtual ~UdpSocket();
tomwalters@509 87
tomwalters@509 88 // the socket is created in an unbound, unconnected state
tomwalters@509 89 // such a socket can only be used to send to an arbitrary
tomwalters@509 90 // address using SendTo(). To use Send() you need to first
tomwalters@509 91 // connect to a remote endpoint using Connect(). To use
tomwalters@509 92 // ReceiveFrom you need to first bind to a local endpoint
tomwalters@509 93 // using Bind().
tomwalters@509 94
tomwalters@509 95 // retrieve the local endpoint name when sending to 'to'
tomwalters@509 96 IpEndpointName LocalEndpointFor( const IpEndpointName& remoteEndpoint ) const;
tomwalters@509 97
tomwalters@509 98 // Connect to a remote endpoint which is used as the target
tomwalters@509 99 // for calls to Send()
tomwalters@509 100 void Connect( const IpEndpointName& remoteEndpoint );
tomwalters@509 101 void Send( const char *data, int size );
tomwalters@509 102 void SendTo( const IpEndpointName& remoteEndpoint, const char *data, int size );
tomwalters@509 103
tomwalters@509 104
tomwalters@509 105 // Bind a local endpoint to receive incoming data. Endpoint
tomwalters@509 106 // can be 'any' for the system to choose an endpoint
tomwalters@509 107 void Bind( const IpEndpointName& localEndpoint );
tomwalters@509 108 bool IsBound() const;
tomwalters@509 109
tomwalters@509 110 int ReceiveFrom( IpEndpointName& remoteEndpoint, char *data, int size );
tomwalters@509 111 };
tomwalters@509 112
tomwalters@509 113
tomwalters@509 114 // convenience classes for transmitting and receiving
tomwalters@509 115 // they just call Connect and/or Bind in the ctor.
tomwalters@509 116 // note that you can still use a receive socket
tomwalters@509 117 // for transmitting etc
tomwalters@509 118
tomwalters@509 119 class UdpTransmitSocket : public UdpSocket{
tomwalters@509 120 public:
tomwalters@509 121 UdpTransmitSocket( const IpEndpointName& remoteEndpoint )
tomwalters@509 122 { Connect( remoteEndpoint ); }
tomwalters@509 123 };
tomwalters@509 124
tomwalters@509 125
tomwalters@509 126 class UdpReceiveSocket : public UdpSocket{
tomwalters@509 127 public:
tomwalters@509 128 UdpReceiveSocket( const IpEndpointName& localEndpoint )
tomwalters@509 129 { Bind( localEndpoint ); }
tomwalters@509 130 };
tomwalters@509 131
tomwalters@509 132
tomwalters@509 133 // UdpListeningReceiveSocket provides a simple way to bind one listener
tomwalters@509 134 // to a single socket without having to manually set up a SocketReceiveMultiplexer
tomwalters@509 135
tomwalters@509 136 class UdpListeningReceiveSocket : public UdpSocket{
tomwalters@509 137 SocketReceiveMultiplexer mux_;
tomwalters@509 138 PacketListener *listener_;
tomwalters@509 139 public:
tomwalters@509 140 UdpListeningReceiveSocket( const IpEndpointName& localEndpoint, PacketListener *listener )
tomwalters@509 141 : listener_( listener )
tomwalters@509 142 {
tomwalters@509 143 Bind( localEndpoint );
tomwalters@509 144 mux_.AttachSocketListener( this, listener_ );
tomwalters@509 145 }
tomwalters@509 146
tomwalters@509 147 ~UdpListeningReceiveSocket()
tomwalters@509 148 { mux_.DetachSocketListener( this, listener_ ); }
tomwalters@509 149
tomwalters@509 150 // see SocketReceiveMultiplexer above for the behaviour of these methods...
tomwalters@509 151 void Run() { mux_.Run(); }
tomwalters@509 152 void RunUntilSigInt() { mux_.RunUntilSigInt(); }
tomwalters@509 153 void Break() { mux_.Break(); }
tomwalters@509 154 void AsynchronousBreak() { mux_.AsynchronousBreak(); }
tomwalters@509 155 };
tomwalters@509 156
tomwalters@509 157
tomwalters@509 158 #endif /* INCLUDED_UDPSOCKET_H */