00001 /******************************************************************************* 00002 00003 @file MulticastSocket.d 00004 00005 Copyright (C) 2004 Kris Bell 00006 00007 This software is provided 'as-is', without any express or implied 00008 warranty. In no event will the authors be held liable for damages 00009 of any kind arising from the use of this software. 00010 00011 Permission is hereby granted to anyone to use this software for any 00012 purpose, including commercial applications, and to alter it and/or 00013 redistribute it freely, subject to the following restrictions: 00014 00015 1. The origin of this software must not be misrepresented; you must 00016 not claim that you wrote the original software. If you use this 00017 software in a product, an acknowledgment within documentation of 00018 said product would be appreciated but is not required. 00019 00020 2. Altered source versions must be plainly marked as such, and must 00021 not be misrepresented as being the original software. 00022 00023 3. This notice may not be removed or altered from any distribution 00024 of the source. 00025 00026 00027 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 00028 00029 00030 @version Initial version, June 2004 00031 @author Kris 00032 00033 00034 *******************************************************************************/ 00035 00036 module mango.io.MulticastSocket; 00037 00038 private import mango.io.Socket, 00039 mango.io.Buffer, 00040 mango.io.Exception, 00041 mango.io.DatagramSocket; 00042 00043 /****************************************************************************** 00044 00045 Wrapper around multicast style of sockets, to simplify the API a 00046 bit, and to tie them into the IBuffer construct. Note that when used 00047 with a SocketListener you must first join the MulticastSocket to a 00048 group. Do not bind() it yourself, since that is performed by the 00049 join() method. 00050 00051 *******************************************************************************/ 00052 00053 class MulticastSocket : DatagramSocket 00054 { 00055 private InternetAddress groupAddress; 00056 00057 /*********************************************************************** 00058 00059 Enable/disable the receipt of multicast packets send 00060 from the same socket 00061 00062 ***********************************************************************/ 00063 00064 void setLoopback (bool loopback) 00065 { 00066 uint[1] onoff = loopback; 00067 setOption (OptionLevel.IP, Option.IP_MULTICAST_LOOP, onoff); 00068 } 00069 00070 /*********************************************************************** 00071 00072 Join a multicast group. This is necessary only for a 00073 multicast reciever (listener). You may call this for 00074 a send-only socket without harm, but the operation is 00075 completely superfluous. 00076 00077 Note that the socket will be bound to the specified port, 00078 and be listening on the provided class D address. Expect 00079 join() to fail without a network adapter present, or the 00080 NIC is not plugged into a router or switch. 00081 00082 ***********************************************************************/ 00083 00084 void join (InternetAddress groupAddress) 00085 { 00086 this.groupAddress = groupAddress; 00087 00088 setAddressReuse (true); 00089 bind (new InternetAddress(groupAddress.port())); 00090 resumeGroup (); 00091 } 00092 00093 /*********************************************************************** 00094 00095 Remove this socket from the current group. Method join() 00096 is expected to have been invoked previously, otherwise 00097 this is a noop. 00098 00099 ***********************************************************************/ 00100 00101 void pauseGroup () 00102 { 00103 if (groupAddress) 00104 if (! setGroup (groupAddress, Option.IP_DROP_MEMBERSHIP)) 00105 exception ("Unable to leave multicast group."); 00106 } 00107 00108 /*********************************************************************** 00109 00110 Add this socket to the current group again. Method join() 00111 is expected to have been invoked previously, otherwise 00112 this is a noop. 00113 00114 ***********************************************************************/ 00115 00116 void resumeGroup () 00117 { 00118 if (groupAddress) 00119 if (! setGroup (groupAddress, Option.IP_ADD_MEMBERSHIP)) 00120 exception ("Unable to join multicast group."); 00121 } 00122 00123 /*********************************************************************** 00124 00125 Leave a multicast group. This should only be invoked on 00126 sockets that have already joined a multicast group, and 00127 before said socket joins another group. 00128 00129 There does not appear to be a means to unbind the socket 00130 from the port specified during the original join(); thus 00131 we have to create a new underlying socket before join() 00132 will bind() cleanly. 00133 00134 ***********************************************************************/ 00135 00136 void leave () 00137 { 00138 if (groupAddress) 00139 { 00140 pauseGroup (); 00141 groupAddress = null; 00142 00143 // drop the original socket handle 00144 closure(); 00145 00146 // create a new socket for binding during another join(), since 00147 // there doesn't appear to be a means of unbinding 00148 create (Socket.AddressFamily.INET, Socket.Type.DGRAM, Socket.Protocol.IP); 00149 } 00150 } 00151 }