ConnectorBase
using Renci.SshNet.Abstractions;
using Renci.SshNet.Common;
using Renci.SshNet.Messages.Transport;
using System;
using System.Net;
using System.Net.Sockets;
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);
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 = 137072;
socket.ReceiveBufferSize = 137072;
return socket;
} catch (Exception) {
socket.Dispose();
throw;
}
}
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;
}
}
}