Exemplo n.º 1
0
        /// <summary>
        /// Resolves the contact point according to its type. If it is not an IP address, then considers it a hostname and
        /// attempts to resolve it with DNS.
        /// </summary>
        private async Task <IEnumerable <IConnectionEndPoint> > ResolveContactPointAsync(string contactPointText)
        {
            if (_resolvedContactPoints.TryGetValue(contactPointText, out var ipEndPoints))
            {
                return(ipEndPoints);
            }

            if (IPAddress.TryParse(contactPointText, out var ipAddress))
            {
                var ipEndpoint                = new IPEndPoint(ipAddress, _protocolOptions.Port);
                var connectionEndPoint        = new ConnectionEndPoint(ipEndpoint, GetServerName);
                var listOfConnectionEndPoints = new List <IConnectionEndPoint> {
                    connectionEndPoint
                };
                _resolvedContactPoints.AddOrUpdate(
                    contactPointText,
                    key => listOfConnectionEndPoints,
                    (key, list) => listOfConnectionEndPoints);
                return(listOfConnectionEndPoints);
            }

            IPHostEntry hostEntry = null;

            try
            {
                hostEntry = await _dns.GetHostEntryAsync(contactPointText).ConfigureAwait(false);
            }
            catch (Exception)
            {
                Cluster.Logger.Warning($"Host '{contactPointText}' could not be resolved");
            }

            var connectionEndPoints = new List <IConnectionEndPoint>();

            if (hostEntry != null && hostEntry.AddressList.Length > 0)
            {
                connectionEndPoints.AddRange(
                    hostEntry.AddressList.Select(resolvedAddress =>
                                                 new ConnectionEndPoint(new IPEndPoint(resolvedAddress, _protocolOptions.Port), GetServerName)));
            }

            _resolvedContactPoints.AddOrUpdate(
                contactPointText,
                key => connectionEndPoints,
                (key, list) => connectionEndPoints);

            return(connectionEndPoints);
        }
Exemplo n.º 2
0
        /// <inheritdoc />
        public override Task <IEnumerable <IConnectionEndPoint> > GetOrResolveContactPointAsync(object contactPoint)
        {
            if (contactPoint is IPEndPoint endpoint)
            {
                var connectionEndPoint        = new ConnectionEndPoint(endpoint, GetServerName);
                var listOfConnectionEndPoints = new List <IConnectionEndPoint> {
                    connectionEndPoint
                };
                return(Task.FromResult(listOfConnectionEndPoints.AsEnumerable()));
            }

            if (!(contactPoint is string contactPointText))
            {
                throw new InvalidOperationException("Contact points should be either string or IPEndPoint instances");
            }

            return(ResolveContactPointAsync(contactPointText));
        }