ConnectorBase
using Renci.SshNet.Abstractions;
using Renci.SshNet.Common;
using Renci.SshNet.Messages.Transport;
using System;
using System.Net;
using System.Net.Sockets;
using System.Runtime.CompilerServices;
using System.Threading;
using System.Threading.Tasks;
namespace Renci.SshNet.Connection
{
internal abstract class ConnectorBase : IConnector
{
internal ISocketFactory SocketFactory { get; set; }
protected ConnectorBase(ISocketFactory socketFactory)
{
if (socketFactory == null)
throw new ArgumentNullException("socketFactory");
SocketFactory = socketFactory;
}
public abstract Socket Connect(IConnectionInfo connectionInfo);
public abstract Task<Socket> ConnectAsync(IConnectionInfo connectionInfo, CancellationToken cancellationToken);
protected Socket SocketConnect(string host, int port, TimeSpan timeout)
{
IPEndPoint iPEndPoint = new IPEndPoint(DnsAbstraction.GetHostAddresses(host)[0], port);
Socket socket = SocketFactory.Create(iPEndPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp);
try {
SocketAbstraction.Connect(socket, iPEndPoint, timeout);
socket.SendBufferSize = 685360;
socket.ReceiveBufferSize = 685360;
return socket;
} catch (Exception) {
socket.Dispose();
throw;
}
}
[AsyncStateMachine(typeof(<SocketConnectAsync>d__8))]
protected Task<Socket> SocketConnectAsync(string host, int port, CancellationToken cancellationToken)
{
<SocketConnectAsync>d__8 stateMachine = default(<SocketConnectAsync>d__8);
stateMachine.<>t__builder = AsyncTaskMethodBuilder<Socket>.Create();
stateMachine.<>4__this = this;
stateMachine.host = host;
stateMachine.port = port;
stateMachine.cancellationToken = cancellationToken;
stateMachine.<>1__state = -1;
stateMachine.<>t__builder.Start(ref stateMachine);
return stateMachine.<>t__builder.Task;
}
protected static byte SocketReadByte(Socket socket)
{
byte[] array = new byte[1];
SocketRead(socket, array, 0, 1, Session.InfiniteTimeSpan);
return array[0];
}
protected static byte SocketReadByte(Socket socket, TimeSpan readTimeout)
{
byte[] array = new byte[1];
SocketRead(socket, array, 0, 1, readTimeout);
return array[0];
}
protected static int SocketRead(Socket socket, byte[] buffer, int offset, int length)
{
return SocketRead(socket, buffer, offset, length, Session.InfiniteTimeSpan);
}
protected static int SocketRead(Socket socket, byte[] buffer, int offset, int length, TimeSpan readTimeout)
{
int num = SocketAbstraction.Read(socket, buffer, offset, length, readTimeout);
if (num == 0)
throw new SshConnectionException("An established connection was aborted by the server.", DisconnectReason.ConnectionLost);
return num;
}
}
}