示例#1
0
        public void EncodeDecodeDiscoveryResponse()
        {
            var response = new DiscoveryResponse("tcpex://some:123/SomeService", "1.0");
            var encoded  = DiscoveryMetadataHelper.Encode(response);
            var decoded  = DiscoveryMetadataHelper.Decode(encoded);

            Assert.IsNotNull(decoded);
            Assert.AreEqual(response, decoded);
        }
示例#2
0
        public void EncodeDecodeDiscoveryRequest()
        {
            var request = new DiscoveryRequest("SomeService");
            var encoded = DiscoveryMetadataHelper.Encode(request);
            var decoded = DiscoveryMetadataHelper.Decode(encoded);

            Assert.IsNotNull(decoded);
            Assert.AreEqual(request, decoded);
        }
示例#3
0
        public void DecodeSampleDiscoveryPacket()
        {
            var dp = new SamplePacket();

            dp.Version    = "EDC35F23-60AD-4730-A9AC-23D57B666DC1";
            dp.ServiceUri = @"tcp://msdn.microsoft.com/en-us/library/tst0kwb1(v=vs.110).aspx|Something=Else|#5235@#$%\a\p\|@@";
            dp.Properties["ExtraProperty"] = @"#%@$%45654!@#$\4214\|Shame=Value";
            DiscoveryMetadataHelper.RegisterDiscoveryMetadataFactory(dp.Signature, () => new SamplePacket());

            var packet  = DiscoveryMetadataHelper.Encode(dp);
            var decoded = DiscoveryMetadataHelper.Decode(packet);

            Assert.IsNotNull(decoded);
            Assert.AreEqual(decoded.GetType(), typeof(SamplePacket));
            Assert.AreEqual(dp, decoded);
        }
示例#4
0
        private void ReceiveCallback(IAsyncResult asyncResult)
        {
            var response = default(DiscoveryMetadata);

            SafePerform(() =>
            {
                var udpClient        = (UdpClient)asyncResult.AsyncState;
                var remoteEndpoint   = default(IPEndPoint);
                var responseMetadata = udpClient.EndReceive(asyncResult, ref remoteEndpoint);
                if (!Stopped)
                {
                    response = DiscoveryMetadataHelper.Decode(responseMetadata);
                }
            });

            if (response != null)
            {
                HandleResponse(response);
            }
        }
示例#5
0
        private void HandleRequest(UdpClient udpClient, IPEndPoint clientEndpoint, byte[] requestMetadata)
        {
            // check if request packet is damaged
            var request = DiscoveryMetadataHelper.Decode(requestMetadata);

            if (request == null)
            {
                return;
            }

            // make sure discovery metadata matches the request
            if (!ResponseMetadata.Matches(request) || Stopped)
            {
                return;
            }

            // send a response
            SafePerform(() =>
            {
                udpClient.BeginSend(ResponseMetadataPacket, ResponseMetadataPacket.Length, clientEndpoint, SendCallback, udpClient);
            });
        }