Esempio n. 1
0
        public IClient Create(ClientSettings settings)
        {
            if (settings == null)
            {
                throw new ArgumentNullException(nameof(settings));
            }

            // special consideration for obsolete ClientType values that may appear in hfmx configuration files
            if (!_clientFactories.TryGetValue(settings.ClientType, out var factory))
            {
                return(null);
            }

            if (!ClientSettings.ValidateName(settings.Name))
            {
                throw new ArgumentException($"Client name {settings.Name} is not valid.", nameof(settings));
            }
            if (String.IsNullOrWhiteSpace(settings.Server))
            {
                throw new ArgumentException("Client server (host) name is empty.", nameof(settings));
            }
            if (!TcpPort.Validate(settings.Port))
            {
                throw new ArgumentException($"Client server (host) port {settings.Port} is not valid.", nameof(settings));
            }

            IClient client = factory.Create();

            if (client != null)
            {
                client.Settings = settings;
            }
            return(client);
        }
Esempio n. 2
0
        /// <summary>
        /// Connects asynchronously to a remote TCP host using the specified host and port number.
        /// </summary>
        /// <param name="host">The name of the remote host.  The host can be an IP address or DNS name.</param>
        /// <param name="port">The port number of the remote host.</param>
        /// <param name="timeout">The time (in milliseconds) to wait while trying to establish a connection before terminating the attempt and generating an error.</param>
        /// <exception cref="ObjectDisposedException" />
        /// <exception cref="InvalidOperationException">The connection is already connected.</exception>
        /// <exception cref="ArgumentNullException">The <paramref name="host" /> parameter is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">The <paramref name="port" /> parameter is not between zero and <see cref="Int16.MaxValue"/>.</exception>
        /// <exception cref="TimeoutException">A connection was not made before the timeout duration.</exception>
        public override async Task ConnectAsync(string host, int port, int timeout)
        {
            if (_disposed)
            {
                throw new ObjectDisposedException(GetType().FullName);
            }
            if (Connected)
            {
                throw new InvalidOperationException("The connection is already connected.");
            }
            if (host is null)
            {
                throw new ArgumentNullException(nameof(host));
            }
            if (!TcpPort.Validate(port))
            {
                throw new ArgumentOutOfRangeException(nameof(port));
            }

            var connectTask = TcpClient.ConnectAsync(host, port);

            if (connectTask != await Task.WhenAny(connectTask, Task.Delay(timeout)).ConfigureAwait(false))
            {
                Close();
                throw new TimeoutException("Connection attempt has timed out.");
            }

            ThrowIfConnectTaskIsFaulted(connectTask);
        }
Esempio n. 3
0
        private bool ValidatePort()
        {
            switch (WebDeploymentType)
            {
            case WebDeploymentType.Ftp:
                return(TcpPort.Validate(Port));

            default:
                return(true);
            }
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="FahClientConnection"/> class.
        /// </summary>
        /// <param name="host">The name of the remote host.  The host can be an IP address or DNS name.</param>
        /// <param name="port">The port number of the remote host.</param>
        /// <param name="tcpConnectionFactory">The factory that will be used to create connections for TCP network services.</param>
        /// <exception cref="ArgumentNullException">The <paramref name="host" /> parameter is null.</exception>
        /// <exception cref="ArgumentOutOfRangeException">The <paramref name="port" /> parameter is not between zero and <see cref="Int16.MaxValue"/>.</exception>
        public FahClientConnection(string host, int port, TcpConnectionFactory tcpConnectionFactory)
        {
            if (host is null)
            {
                throw new ArgumentNullException(nameof(host));
            }
            if (!TcpPort.Validate(port))
            {
                throw new ArgumentOutOfRangeException(nameof(port));
            }

            Host = host;
            Port = port;
            _tcpConnectionFactory = tcpConnectionFactory;
        }
        public string this[string columnName]
        {
            get
            {
                switch (columnName)
                {
                case nameof(Name):
                    return(ClientSettings.ValidateName(Name) ? null : NameError);

                case nameof(Server):
                    return(HostName.Validate(Server) ? null : ServerError);

                case nameof(Port):
                    return(TcpPort.Validate(Port) ? null : PortError);

                default:
                    return(null);
                }
            }
        }
Esempio n. 6
0
 public string ToServerPortString()
 {
     return(TcpPort.Validate(Port)
         ? String.Format(CultureInfo.InvariantCulture, "{0}:{1}", Server, Port)
         : Server);
 }