Added IDataReceiverSink and refactored to separate the EventSink from the GarbageData...
[lwes-dotnet/github-mirror.git] / Org.Lwes / Listener / MulticastEventListener.cs
blob8ad155e65bc93b0c83bc108f6aa2e0bd6522ae8d
1 //
2 // This file is part of the LWES .NET Binding (LWES.net)
3 //
4 // COPYRIGHT© 2009, Phillip Clark (phillip[at*flitbit[dot*org)
5 // original .NET implementation
6 //
7 // LWES.net is free software: you can redistribute it and/or modify
8 // it under the terms of the Lesser GNU General Public License as published by
9 // the Free Software Foundation, either version 3 of the License, or
10 // (at your option) any later version.
12 // LWES.net is distributed in the hope that it will be useful,
13 // but WITHOUT ANY WARRANTY; without even the implied warranty of
14 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 // Lesser GNU General Public License for more details.
17 // You should have received a copy of the Lesser GNU General Public License
18 // along with LWES.net. If not, see <http://www.gnu.org/licenses/>.
20 namespace Org.Lwes.Listener
22 using System;
23 using System.Net;
24 using System.Net.Sockets;
26 using Org.Lwes.DB;
27 using Org.Lwes.Properties;
29 /// <summary>
30 /// Implementation of the IEventListener for listening to multicast events.
31 /// </summary>
32 public sealed class MulticastEventListener : EventListenerBase
34 #region Methods
36 /// <summary>
37 /// Initializes the instance.
38 /// </summary>
39 /// <param name="db"></param>
40 /// <param name="multicastAddress"></param>
41 /// <param name="multicastPort"></param>
42 /// <param name="parallel"></param>
43 /// <param name="garbageHandling"></param>
44 public void InitializeAll(IEventTemplateDB db
45 , IPAddress multicastAddress
46 , int multicastPort
47 , bool parallel
48 , ListenerGarbageHandling garbageHandling)
50 TemplateDB = db;
51 Address = multicastAddress;
52 Port = multicastPort;
53 IsParallel = parallel;
54 Initialize();
57 /// <summary>
58 /// Initializes the listener on a multicast address.
59 /// </summary>
60 protected override void PerformInitialization()
62 base.FinishInitialize(new IPEndPoint(Address, Port),
63 (s, e) =>
65 s.SetSocketOption(SocketOptionLevel.Udp, SocketOptionName.NoDelay, 1);
66 s.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
67 if (e.AddressFamily == AddressFamily.InterNetworkV6)
69 s.Bind(new IPEndPoint(IPAddress.IPv6Any, e.Port));
70 s.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership,
71 new IPv6MulticastOption(e.Address));
73 else
75 s.Bind(new IPEndPoint(IPAddress.Any, e.Port));
76 s.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership,
77 new MulticastOption(e.Address));
79 });
82 #endregion Methods