コード例 #1
0
ファイル: DiscoveryResponse.cs プロジェクト: zyanfx/Zyan
        /// <summary>
        /// Checks if the response packet matches the specified request packet.
        /// </summary>
        /// <param name="dp">The request packet.</param>
        public override bool Matches(DiscoveryMetadata dp)
        {
            var request = dp as DiscoveryRequest;

            if (request == null)
            {
                return(false);
            }

            try
            {
                if (!Regex.IsMatch(HostUrl, request.NamePattern))
                {
                    return(false);
                }
            }
            catch (ArgumentException)
            {
                // invalid regular expression
                return(false);
            }

            if (Version == null && request.Version != null)
            {
                return(false);
            }

            return(request.Version == null || Version == request.Version);
        }
コード例 #2
0
ファイル: DiscoveryResponse.cs プロジェクト: yallie/zyan
        /// <summary>
        /// Checks if the response packet matches the specified request packet.
        /// </summary>
        /// <param name="dp">The request packet.</param>
        public override bool Matches(DiscoveryMetadata dp)
        {
            var request = dp as DiscoveryRequest;
            if (request == null)
            {
                return false;
            }

            try
            {
                if (!Regex.IsMatch(HostUrl, request.NamePattern))
                {
                    return false;
                }
            }
            catch (ArgumentException)
            {
                // invalid regular expression
                return false;
            }

            if (Version == null && request.Version != null)
            {
                return false;
            }

            return request.Version == null || Version == request.Version;
        }
コード例 #3
0
ファイル: DiscoveryServer.cs プロジェクト: yallie/zyan
        /// <summary>
        /// Initializes a new instance of the <see cref="DiscoveryServer"/> class.
        /// </summary>
        /// <param name="responseMetadata">The response metadata.</param>
        /// <param name="port">The port to bind to.</param>
        /// <param name="localAddress">The local address to bind to.</param>
        public DiscoveryServer(DiscoveryMetadata responseMetadata, int port = DefaultDiscoveryPort, IPAddress localAddress = null)
        {
            if (responseMetadata == null)
            {
                throw new ArgumentNullException("responseMetadata", "Discovery response metadata is missing.");
            }

            Port = port;
            LocalAddress = localAddress ?? IPAddress.Any;
            ResponseMetadata = responseMetadata;
            ResponseMetadataPacket = DiscoveryMetadataHelper.Encode(ResponseMetadata);
        }
コード例 #4
0
ファイル: DiscoveryClient.cs プロジェクト: yallie/zyan
        /// <summary>
        /// Initializes a new instance of the <see cref="DiscoveryClient"/> class.
        /// </summary>
        /// <param name="requestMetadata">The request metadata.</param>
        /// <param name="port">The port of the discovery server.</param>
        public DiscoveryClient(DiscoveryMetadata requestMetadata, int port = DiscoveryServer.DefaultDiscoveryPort)
        {
            if (requestMetadata == null)
            {
                throw new ArgumentNullException("requestMetadata", "Discovery request metadata is missing.");
            }

            Port = port;
            RequestMetadata = requestMetadata;
            RequestMetadataPacket = DiscoveryMetadataHelper.Encode(RequestMetadata);
            DestinationAddress = IPAddress.Broadcast;
            DestinationEndpoint = new IPEndPoint(DestinationAddress, Port);
            Results = new HashSet<DiscoveryMetadata>();
            RetryTimeout = TimeSpan.FromSeconds(1);
            RetryCount = 10;
        }
コード例 #5
0
        /// <summary>
        /// Encodes the specified discovery metadata packet into byte array.
        /// </summary>
        /// <param name="dp">The packet to encode.</param>
        public static byte[] Encode(DiscoveryMetadata dp)
        {
            // convert the packet into string
            var sb = new StringBuilder();

            sb.AppendFormat("{0}|", Escape(dp.Signature));
            foreach (var entry in dp.Properties)
            {
                sb.AppendFormat("{0}={1}|", Escape(entry.Key), Escape(entry.Value));
            }

            // calculate checksum and merge
            var content = Encoding.UTF8.GetBytes(sb.ToString());
            var crc     = new Crc32Calculator();

            crc.UpdateWithBlock(content, 0, content.Length);
            var checksum      = BitConverter.GetBytes(crc.Crc32);
            var contentLength = BitConverter.GetBytes(content.Length);

            // packet contents: length, checksum, content
            return(contentLength.Concat(checksum).Concat(content).ToArray());
        }
コード例 #6
0
ファイル: DiscoveryEventArgs.cs プロジェクト: yallie/zyan
 /// <summary>
 /// Initializes a new instance of the <see cref="DiscoveryEventArgs"/> class.
 /// </summary>
 /// <param name="metadata">Discovery metadata.</param>
 public DiscoveryEventArgs(DiscoveryMetadata metadata)
 {
     Metadata = metadata;
 }
コード例 #7
0
ファイル: DiscoveryMetadata.cs プロジェクト: yallie/zyan
 /// <summary>
 /// Checks if the packet matches the specified request.
 /// </summary>
 /// <param name="request">The request.</param>
 public virtual bool Matches(DiscoveryMetadata request)
 {
     return false;
 }
コード例 #8
0
ファイル: DiscoveryClient.cs プロジェクト: yallie/zyan
        private void HandleResponse(DiscoveryMetadata response)
        {
            lock (lockObject)
            {
                if (response == null || Results.Contains(response))
                {
                    return;
                }

                Results.Add(response);
            }

            OnDiscovered(new DiscoveryEventArgs(response));
        }
コード例 #9
0
 /// <summary>
 /// Checks if the packet matches the specified request.
 /// </summary>
 /// <param name="request">The request.</param>
 public virtual bool Matches(DiscoveryMetadata request)
 {
     return(false);
 }
コード例 #10
0
        /// <summary>
        /// Encodes the specified discovery metadata packet into byte array.
        /// </summary>
        /// <param name="dp">The packet to encode.</param>
        public static byte[] Encode(DiscoveryMetadata dp)
        {
            // convert the packet into string
            var sb = new StringBuilder();
            sb.AppendFormat("{0}|", Escape(dp.Signature));
            foreach (var entry in dp.Properties)
            {
                sb.AppendFormat("{0}={1}|", Escape(entry.Key), Escape(entry.Value));
            }

            // calculate checksum and merge
            var content = Encoding.UTF8.GetBytes(sb.ToString());
            var crc = new Crc32Calculator();
            crc.UpdateWithBlock(content, 0, content.Length);
            var checksum = BitConverter.GetBytes(crc.Crc32);
            var contentLength = BitConverter.GetBytes(content.Length);

            // packet contents: length, checksum, content
            return contentLength.Concat(checksum).Concat(content).ToArray();
        }