comparison tango/tango/net/MulticastConduit.d @ 132:1700239cab2e trunk

[svn r136] MAJOR UNSTABLE UPDATE!!! Initial commit after moving to Tango instead of Phobos. Lots of bugfixes... This build is not suitable for most things.
author lindquist
date Fri, 11 Jan 2008 17:57:40 +0100
parents
children
comparison
equal deleted inserted replaced
131:5825d48b27d1 132:1700239cab2e
1 /*******************************************************************************
2
3 copyright: Copyright (c) 2004 Kris Bell. All rights reserved
4
5 license: BSD style: $(LICENSE)
6
7 version: Jun 2004 : Initial release
8 version: Dec 2006 : South pacific version
9
10 author: Kris
11
12 *******************************************************************************/
13
14 module tango.net.MulticastConduit;
15
16 public import tango.io.Conduit;
17
18 private import tango.net.DatagramConduit,
19 tango.net.InternetAddress;
20
21 /******************************************************************************
22
23 MulticastConduit sends and receives data on a multicast group, as
24 described by a class-D address. To send data, the recipient group
25 should be handed to the write() method. To receive, the socket is
26 bound to an available local adapter/port as a listener and must
27 join() the group before it becomes eligible for input from there.
28
29 While MulticastConduit is a flavour of datagram, it doesn't support
30 being connected to a specific endpoint.
31
32 Sending and receiving via a multicast group:
33 ---
34 auto group = new InternetAddress ("225.0.0.10", 8080);
35
36 // listen for datagrams on the group address (via port 8080)
37 auto multi = new MulticastConduit (group);
38
39 // join and broadcast to the group
40 multi.join.write ("hello", group);
41
42 // we are listening also ...
43 char[8] tmp;
44 auto bytes = multi.read (tmp);
45 ---
46
47 Note that this example is expecting to receive its own broadcast;
48 thus it may be necessary to enable loopback operation (see below)
49 for successful receipt of the broadcast.
50
51 Note that class D addresses range from 225.0.0.0 to 239.255.255.255
52
53 see: http://www.kohala.com/start/mcast.api.txt
54
55 *******************************************************************************/
56
57 class MulticastConduit : DatagramConduit
58 {
59 private IPv4Address group;
60
61 enum {Host=0, Subnet=1, Site=32, Region=64, Continent=128, Unrestricted=255}
62
63 /***********************************************************************
64
65 Create a writable multicast socket
66
67 ***********************************************************************/
68
69 this ()
70 {
71 super ();
72 }
73
74 /***********************************************************************
75
76 Create a read/write multicast socket
77
78 This flavour is necessary only for a multicast receiver
79 (e.g. use this ctor in conjunction with SocketListener).
80
81 You should specify both a group address and a port to
82 listen upon. The resultant socket will be bound to the
83 specified port (locally), and listening on the class-D
84 address. Expect this to fail without a network adapter
85 present, as bind() will not find anything to work with.
86
87 The reuse parameter dictates how to behave when the port
88 is already in use. Default behaviour is to throw an IO
89 exception, and the alternate is to force usage.
90
91 To become eligible for incoming group datagrams, you must
92 also invoke the join() method
93
94 ***********************************************************************/
95
96 this (InternetAddress group, bool reuse = false)
97 {
98 super ();
99
100 this.group = group;
101 socket.setAddressReuse(reuse).bind(new InternetAddress(group.port));
102 }
103
104 /***********************************************************************
105
106 Enable/disable the receipt of multicast packets sent
107 from the same adapter. The default state is OS specific
108
109 ***********************************************************************/
110
111 MulticastConduit loopback (bool yes = true)
112 {
113 uint[1] onoff = yes;
114 socket.setOption (SocketOptionLevel.IP, SocketOption.IP_MULTICAST_LOOP, onoff);
115 return this;
116 }
117
118 /***********************************************************************
119
120 Set the number of hops (time to live) of this socket.
121 Convenient values are
122 ---
123 Host: packets are restricted to the same host
124 Subnet: packets are restricted to the same subnet
125 Site: packets are restricted to the same site
126 Region: packets are restricted to the same region
127 Continent: packets are restricted to the same continent
128 Unrestricted: packets are unrestricted in scope
129 ---
130
131 ***********************************************************************/
132
133 MulticastConduit ttl (uint value=Subnet)
134 {
135 uint[1] options = value;
136 socket.setOption (SocketOptionLevel.IP, SocketOption.IP_MULTICAST_TTL, options);
137 return this;
138 }
139
140 /***********************************************************************
141
142 Add this socket to the listening group
143
144 ***********************************************************************/
145
146 MulticastConduit join ()
147 {
148 socket.joinGroup (group, true);
149 return this;
150 }
151
152 /***********************************************************************
153
154 Remove this socket from the listening group
155
156 ***********************************************************************/
157
158 MulticastConduit leave ()
159 {
160 socket.joinGroup (group, false);
161 return this;
162 }
163 }
164
165
166 /******************************************************************************
167
168 *******************************************************************************/
169
170 debug (Multicast)
171 {
172 void main()
173 {
174 auto group = new InternetAddress ("225.0.0.10", 8080);
175
176 // listen for datagrams on the group address
177 auto multi = new MulticastConduit (group);
178
179 // join and broadcast to the group
180 multi.join.write ("hello", group);
181
182 // we are listening also ...
183 char[8] tmp;
184 auto bytes = multi.read (tmp);
185 }
186 }