コード例 #1
0
        public async Task Start()
        {
            using (var udpClient = new UdpClient(AddressFamily.InterNetworkV6))
            {
                udpClient.Client.SetSocketOption(SocketOptionLevel.Socket,
                                                 SocketOptionName.ReuseAddress, true);
                udpClient.Client.SetSocketOption(SocketOptionLevel.Socket,
                                                 SocketOptionName.ExclusiveAddressUse, false);
                udpClient.Client.Bind(new IPEndPoint(IPAddress.IPv6Any, _localPort));
                foreach (var nicIndex in CollectNetworkInterfaceIndexes())
                {
                    try
                    {
                        udpClient.JoinMulticastGroup(nicIndex, _mcastGroup);
                    }
                    catch
                    { }
                }
                while (true)
                {
                    UdpReceiveResult result = await udpClient.ReceiveAsync().ConfigureAwait(false);

                    if (_qualifier.All(q => q.IsSourceQualified(result.RemoteEndPoint)))
                    {
                        MulticastMsgEventArgs e = new MulticastMsgEventArgs(result.RemoteEndPoint, result.Buffer);
                        OnMessageReceived(e);
                    }
                }
            }
        }
コード例 #2
0
 protected void OnMessageReceived(MulticastMsgEventArgs e)
 {
     Volatile.Read(ref MessageReceived)?.Invoke(this, e);
 }