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