diff --git a/source/NetCoreServer/UdpClient.cs b/source/NetCoreServer/UdpClient.cs index dcfb339..b9c515d 100644 --- a/source/NetCoreServer/UdpClient.cs +++ b/source/NetCoreServer/UdpClient.cs @@ -1,5 +1,6 @@ using System; using System.Net; +using System.Net.NetworkInformation; using System.Net.Sockets; using System.Text; @@ -330,35 +331,70 @@ public virtual void SetupMulticast(bool enable) } /// - /// Join multicast group with a given IP address (synchronous) + /// Join multicast group with a given IP address and network interface index (synchronous) /// /// IP address - public virtual void JoinMulticastGroup(IPAddress address) + /// Network interface index + public virtual void JoinMulticastGroup(IPAddress address, int interfaceIndex) { if (Endpoint.AddressFamily == AddressFamily.InterNetworkV6) - Socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new IPv6MulticastOption(address)); + Socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new IPv6MulticastOption(address, interfaceIndex)); else - Socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(address)); + Socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(address, interfaceIndex)); // Call the client joined multicast group notification OnJoinedMulticastGroup(address); } /// + /// Join multicast group with a given IP address and network interface index (synchronous) + /// + /// IP address + /// Network interface index + public virtual void JoinMulticastGroup(string address, int interfaceIndex) { JoinMulticastGroup(IPAddress.Parse(address), interfaceIndex); } + /// + /// Join multicast group with a given IP address (synchronous) + /// + /// IP address + public virtual void JoinMulticastGroup(IPAddress address) { JoinMulticastGroup(address, 0); } + /// /// Join multicast group with a given IP address (synchronous) /// /// IP address public virtual void JoinMulticastGroup(string address) { JoinMulticastGroup(IPAddress.Parse(address)); } + /// + /// Join multicast group with a given IP address and network interface (synchronous) + /// + /// IP address + /// Network interface + public virtual void JoinMulticastGroup(IPAddress address, NetworkInterface networkInterface) + { + if (Endpoint.AddressFamily == AddressFamily.InterNetworkV6) + Socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.AddMembership, new IPv6MulticastOption(address, networkInterface.GetIPProperties().GetIPv6Properties().Index)); + else + Socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(address, networkInterface.GetIPProperties().GetIPv4Properties().Index)); + + // Call the client joined multicast group notification + OnJoinedMulticastGroup(address); + } + /// + /// Join multicast group with a given IP address and network interface (synchronous) + /// + /// IP address + /// Network interface + public virtual void JoinMulticastGroup(string address, NetworkInterface networkInterface) { JoinMulticastGroup(IPAddress.Parse(address), networkInterface); } + /// /// Leave multicast group with a given IP address (synchronous) /// /// IP address - public virtual void LeaveMulticastGroup(IPAddress address) + /// Network interface index + public virtual void LeaveMulticastGroup(IPAddress address, int interfaceIndex) { if (Endpoint.AddressFamily == AddressFamily.InterNetworkV6) - Socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.DropMembership, new IPv6MulticastOption(address)); + Socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.DropMembership, new IPv6MulticastOption(address, interfaceIndex)); else - Socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, new MulticastOption(address)); + Socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, new MulticastOption(address, interfaceIndex)); // Call the client left multicast group notification OnLeftMulticastGroup(address); @@ -367,8 +403,41 @@ public virtual void LeaveMulticastGroup(IPAddress address) /// Leave multicast group with a given IP address (synchronous) /// /// IP address + /// Network interface index + public virtual void LeaveMulticastGroup(string address, int interfaceIndex) { LeaveMulticastGroup(IPAddress.Parse(address), interfaceIndex); } + /// + /// Leave multicast group with a given IP address (synchronous) + /// + /// IP address + public virtual void LeaveMulticastGroup(IPAddress address) { LeaveMulticastGroup(address, 0); } + /// + /// Leave multicast group with a given IP address (synchronous) + /// + /// IP address public virtual void LeaveMulticastGroup(string address) { LeaveMulticastGroup(IPAddress.Parse(address)); } + /// + /// Leave multicast group with a given IP address and network interface (synchronous) + /// + /// IP address + /// Network interface + public virtual void LeaveMulticastGroup(IPAddress address, NetworkInterface networkInterface) + { + if (Endpoint.AddressFamily == AddressFamily.InterNetworkV6) + Socket.SetSocketOption(SocketOptionLevel.IPv6, SocketOptionName.DropMembership, new IPv6MulticastOption(address, networkInterface.GetIPProperties().GetIPv6Properties().Index)); + else + Socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.DropMembership, new MulticastOption(address, networkInterface.GetIPProperties().GetIPv4Properties().Index)); + + // Call the client left multicast group notification + OnLeftMulticastGroup(address); + } + /// + /// Leave multicast group with a given IP address and network interface (synchronous) + /// + /// IP address + /// Network interface + public virtual void LeaveMulticastGroup(string address, NetworkInterface networkInterface) { LeaveMulticastGroup(IPAddress.Parse(address), networkInterface); } + #endregion #region Send/Receive data