<PackageReference Include="SSH.NET" Version="2016.1.0-beta2" />

ServiceFactory

Basic factory for creating new services.
using Renci.SshNet.Common; using Renci.SshNet.Messages.Transport; using Renci.SshNet.Security; using Renci.SshNet.Sftp; using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Renci.SshNet { internal class ServiceFactory : IServiceFactory { public IClientAuthentication CreateClientAuthentication() { return new ClientAuthentication(); } public ISession CreateSession(ConnectionInfo connectionInfo) { return new Session(connectionInfo, this); } public ISftpSession CreateSftpSession(ISession session, int operationTimeout, Encoding encoding, ISftpResponseFactory sftpMessageFactory) { return new SftpSession(session, operationTimeout, encoding, sftpMessageFactory); } public PipeStream CreatePipeStream() { return new PipeStream(); } public IKeyExchange CreateKeyExchange(IDictionary<string, Type> clientAlgorithms, string[] serverAlgorithms) { if (clientAlgorithms == null) throw new ArgumentNullException("clientAlgorithms"); if (serverAlgorithms == null) throw new ArgumentNullException("serverAlgorithms"); Type type = (from c in clientAlgorithms from s in serverAlgorithms where s == c.Key select c.Value).FirstOrDefault(); if ((object)type == null) throw new SshConnectionException("Failed to negotiate key exchange algorithm.", DisconnectReason.KeyExchangeFailed); return type.CreateInstance<IKeyExchange>(); } public ISftpFileReader CreateSftpFileReader(string fileName, ISftpSession sftpSession, uint bufferSize) { SftpOpenAsyncResult asyncResult = sftpSession.BeginOpen(fileName, Flags.Read, null, null); SFtpStatAsyncResult asyncResult2 = sftpSession.BeginLStat(fileName, null, null); uint num = sftpSession.CalculateOptimalReadLength(bufferSize); long? fileSize; int maxPendingReads; try { SftpFileAttributes sftpFileAttributes = sftpSession.EndLStat(asyncResult2); fileSize = sftpFileAttributes.Size; maxPendingReads = Math.Min(10, (int)Math.Ceiling((double)sftpFileAttributes.Size / (double)num) + 1); } catch (SshException) { fileSize = null; maxPendingReads = 3; } byte[] handle = sftpSession.EndOpen(asyncResult); return sftpSession.CreateFileReader(handle, sftpSession, num, maxPendingReads, fileSize); } public ISftpResponseFactory CreateSftpResponseFactory() { return new SftpResponseFactory(); } } }