annotate trunk/external/oscpack/ip/UdpSocket.h @ 706:f8e90b5d85fd tip

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