コード例 #1
0
        public void ReceiveErrorAnswer()
        {
            var service = Guid.NewGuid().ToString() + ".local";
            var done    = new ManualResetEvent(false);

            var mdns = new MulticastService();

            mdns.NetworkInterfaceDiscovered += (s, e) => mdns.SendQuery(service);
            mdns.QueryReceived += (s, e) =>
            {
                var msg = e.Message;
                if (msg.Questions.Any(q => q.Name == service))
                {
                    var res = msg.CreateResponse();
                    res.Status = MessageStatus.Refused;
                    res.Answers.Add(new ARecord
                    {
                        Name    = service,
                        Address = IPAddress.Parse("127.1.1.1")
                    });
                    mdns.SendAnswer(res);
                }
            };
            mdns.AnswerReceived += (s, e) =>
            {
                var msg = e.Message;
                if (msg.Answers.Any(a => a.Name == service))
                {
                    done.Set();
                }
            };
            try
            {
                mdns.Start();
                Assert.IsFalse(done.WaitOne(TimeSpan.FromSeconds(0.5)), "answer was not ignored");
            }
            finally
            {
                mdns.Stop();
            }
        }
コード例 #2
0
        public void Nics()
        {
            var done = new ManualResetEvent(false);
            var mdns = new MulticastService();
            IEnumerable <NetworkInterface> nics = null;

            mdns.NetworkInterfaceDiscovered += (s, e) =>
            {
                nics = e.NetworkInterfaces;
                done.Set();
            };
            mdns.Start();
            try
            {
                Assert.IsTrue(done.WaitOne(TimeSpan.FromSeconds(1)), "timeout");
                Assert.IsTrue(nics.Count() > 0);
            }
            finally
            {
                mdns.Stop();
            }
        }
コード例 #3
0
        public void Discover_ServiceInstance_WithAnswersContainingAdditionRecords()
        {
            var service = new ServiceProfile("y", "_sdtest-2._udp", 1024);
            var done    = new ManualResetEvent(false);
            var mdns    = new MulticastService();
            var sd      = new ServiceDiscovery(mdns)
            {
                AnswersContainsAdditionalRecords = true
            };
            Message discovered = null;

            mdns.NetworkInterfaceDiscovered += (s, e) =>
            {
                sd.QueryServiceInstances(service.ServiceName);
            };

            sd.ServiceInstanceDiscovered += (s, e) =>
            {
                if (e.ServiceInstanceName == service.FullyQualifiedName)
                {
                    Assert.IsNotNull(e.Message);
                    discovered = e.Message;
                    done.Set();
                }
            };
            try
            {
                sd.Advertise(service);
                mdns.Start();
                Assert.IsTrue(done.WaitOne(TimeSpan.FromSeconds(1)), "instance not found");
                Assert.AreEqual(0, discovered.AdditionalRecords.Count);
                Assert.IsTrue(discovered.Answers.Count > 1);
            }
            finally
            {
                sd.Dispose();
                mdns.Stop();
            }
        }
コード例 #4
0
        public void SendNonQuery()
        {
            var query = new Message
            {
                Opcode = MessageOperation.Status,
                QR     = false
            };
            var done = new ManualResetEvent(false);

            var mdns = new MulticastService();

            mdns.NetworkInterfaceDiscovered += (s, e) => mdns.SendQuery(query);
            mdns.QueryReceived += (s, e) => done.Set();
            try
            {
                mdns.Start();
                Assert.IsFalse(done.WaitOne(TimeSpan.FromSeconds(0.5)), "query was not ignored");
            }
            finally
            {
                mdns.Stop();
            }
        }
コード例 #5
0
        public void Discover_ServiceInstance_with_Subtype()
        {
            var service1 = new ServiceProfile("x", "_sdtest-2._udp", 1024);
            var service2 = new ServiceProfile("y", "_sdtest-2._udp", 1024);

            service2.Subtypes.Add("apiv2");
            var done = new ManualResetEvent(false);
            var mdns = new MulticastService();
            var sd   = new ServiceDiscovery(mdns);

            mdns.NetworkInterfaceDiscovered += (s, e) =>
            {
                sd.QueryServiceInstances("_sdtest-2._udp", "apiv2");
            };

            sd.ServiceInstanceDiscovered += (s, e) =>
            {
                if (e.ServiceInstanceName == service2.FullyQualifiedName)
                {
                    Assert.IsNotNull(e.Message);
                    done.Set();
                }
            };
            try
            {
                sd.Advertise(service1);
                sd.Advertise(service2);
                mdns.Start();
                Assert.IsTrue(done.WaitOne(TimeSpan.FromSeconds(1)), "instance not found");
            }
            finally
            {
                sd.Dispose();
                mdns.Stop();
            }
        }