Exemplo n.º 1
0
        /// <summary>
        /// Returns a SftpHost
        /// </summary>
        /// <param name="properties"></param>
        /// <returns></returns>
        public static SftpHost GetHostByName(SftpTransmitProperties properties)//string hostName, bool trace, int connectionLimit)
        {
            foreach (SftpHost host in Hosts)
            {
                if (host.HostName == properties.SSHHost)
                {
                    if (host.ConnectionLimit != properties.ConnectionLimit)
                    {
                        host.ConnectionLimit = properties.ConnectionLimit;

                        Trace.WriteLineIf(properties.DebugTrace, "[SftpConnectionPool] Overriding connection pool settings");
                    }
                    return(host);
                }
            }

            SftpHost newHost = new SftpHost(properties.SSHHost, properties.ConnectionLimit, properties.DebugTrace);

            Hosts.Add(newHost);

            return(newHost);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Returns a new or  free connection from the pool
        /// </summary>
        /// <param name="properties"></param>
        /// <param name="shutdownRequested"></param>
        /// <returns></returns>
        public ISftp GetConnection(SftpTransmitProperties properties, bool shutdownRequested)//.SSHHoststring username, string password, string identityFile, int port, bool shutdownRequested, string passphrase)
        {
            while (!shutdownRequested)
            {
                if (ConnectionLimit == 0)
                {
                    TraceMessage(
                        "[SftpConnectionPool] GetConnectionFromPool creating a new connection (not from pool)");
                    //ISftp sftp = new Sftp(this.HostName, username, password, identityFile, port, passphrase, this._trace);
                    ISftp sftp;
                    if (string.IsNullOrEmpty(properties.ProxyHost))
                    {
                        sftp = new SharpSsh.Sftp(properties.SSHHost,
                                                 properties.SSHUser,
                                                 properties.SSHPasswordProperty,
                                                 properties.SSHIdentityFile,
                                                 properties.SSHIdentityThumbprint,
                                                 properties.SSHPort,
                                                 properties.SSHPassphrase,
                                                 properties.DebugTrace);
                    }
                    else
                    {
                        sftp = new SharpSsh.Sftp(properties.SSHHost,
                                                 properties.SSHUser,
                                                 properties.SSHPasswordProperty,
                                                 properties.SSHIdentityFile,
                                                 properties.SSHIdentityThumbprint,
                                                 properties.SSHPort,
                                                 properties.SSHPassphrase,
                                                 properties.DebugTrace,
                                                 properties.ProxyHost,
                                                 properties.ProxyPort,
                                                 properties.ProxyUserName,
                                                 properties.ProxyPassword);
                    }

                    return(sftp);
                }
                ISftp connection;
                if (Connections.TryPop(out connection))
                {
                    TraceMessage("[SftpConnectionPool] GetConnectionFromPool found a free connection in the pool");
                    return(connection);
                }
                if (_currentCount < ConnectionLimit)
                {
                    TraceMessage("[SftpConnectionPool] GetConnectionFromPool creating a new connection for pool");
                    //ISftp sftp = new SharpSsh.Sftp(this.HostName, username, password, identityFile, port, passphrase, this._trace);

                    ISftp sftp;
                    if (string.IsNullOrEmpty(properties.ProxyHost))
                    {
                        sftp = new SharpSsh.Sftp(properties.SSHHost,
                                                 properties.SSHUser,
                                                 properties.SSHPasswordProperty,
                                                 properties.SSHIdentityFile,
                                                 properties.SSHIdentityThumbprint,
                                                 properties.SSHPort,
                                                 properties.SSHPassphrase,
                                                 properties.DebugTrace);
                        //(this.HostName, username, password, identityFile, port, passphrase, this._trace);
                    }
                    else
                    {
                        sftp = new SharpSsh.Sftp(properties.SSHHost,
                                                 properties.SSHUser,
                                                 properties.SSHPasswordProperty,
                                                 properties.SSHIdentityFile,
                                                 properties.SSHIdentityThumbprint,
                                                 properties.SSHPort,
                                                 properties.SSHPassphrase,
                                                 properties.DebugTrace,
                                                 properties.ProxyHost,
                                                 properties.ProxyPort,
                                                 properties.ProxyUserName,
                                                 properties.ProxyPassword);
                    }

                    _currentCount++;
                    return(sftp);
                }
            }
            return(null);
        }