/// <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); }
/// <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; }
/// <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); }
/// <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; }
/// <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()); }
/// <summary> /// Initializes a new instance of the <see cref="DiscoveryEventArgs"/> class. /// </summary> /// <param name="metadata">Discovery metadata.</param> public DiscoveryEventArgs(DiscoveryMetadata metadata) { Metadata = metadata; }
/// <summary> /// Checks if the packet matches the specified request. /// </summary> /// <param name="request">The request.</param> public virtual bool Matches(DiscoveryMetadata request) { return false; }
private void HandleResponse(DiscoveryMetadata response) { lock (lockObject) { if (response == null || Results.Contains(response)) { return; } Results.Add(response); } OnDiscovered(new DiscoveryEventArgs(response)); }
/// <summary> /// Checks if the packet matches the specified request. /// </summary> /// <param name="request">The request.</param> public virtual bool Matches(DiscoveryMetadata request) { return(false); }
/// <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(); }