Advertisement
Guest User

Connection Method

a guest
Aug 20th, 2018
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.66 KB | None | 0 0
  1. public void Connect()
  2.         {
  3.             ConnectEnabled = true;
  4.             Debug.Console(1, this, "attempting connect");
  5.            
  6.             // Cancel reconnect if running.
  7.             if (ReconnectTimer != null)
  8.             {
  9.                 ReconnectTimer.Stop();
  10.                 ReconnectTimer = null;
  11.             }
  12.  
  13.             // Don't try to connect if already
  14.             if (IsConnected)
  15.                 return;
  16.            
  17.             // Don't go unless everything is here
  18.             if (string.IsNullOrEmpty(Hostname) || Port < 1 || Port > 65535
  19.                 || Username == null || Password == null)
  20.             {
  21.                 Debug.Console(1, this, "Connect failed.  Check hostname, port, username and password are set or not null");
  22.                 return;
  23.             }
  24.  
  25.             // This handles both password and keyboard-interactive (like on OS-X, 'nixes)
  26.             KeyboardInteractiveAuthenticationMethod kauth = new KeyboardInteractiveAuthenticationMethod(Username);
  27.             kauth.AuthenticationPrompt += new EventHandler<AuthenticationPromptEventArgs>(kauth_AuthenticationPrompt);
  28.             PasswordAuthenticationMethod pauth = new PasswordAuthenticationMethod(Username, Password);
  29.  
  30.             // Make a new client if we need it or things have changed
  31.             //if (Client == null || PropertiesHaveChanged())
  32.             //{
  33.                 if (Client != null)
  34.                 {
  35.                     Debug.Console(1, this, "Cleaning up disconnected client");
  36.                     Client.ErrorOccurred -= Client_ErrorOccurred;
  37.                     KillStream();
  38.                    
  39.                     //if (TheStream != null)
  40.                     //{
  41.                     //    TheStream.DataReceived -= Stream_DataReceived;
  42.                     //    TheStream.ErrorOccurred -= TheStream_ErrorOccurred;
  43.                     //}
  44.                     //TheStream = null;
  45.                 }
  46.  
  47.                 Debug.Console(1, this, "Creating new SshClient");
  48.                 ConnectionInfo connectionInfo = new ConnectionInfo(Hostname, Port, Username, pauth, kauth);
  49.                 Client = new SshClient(connectionInfo);
  50.                 Client.ErrorOccurred += Client_ErrorOccurred;
  51.             //}
  52.             //PreviousHostname = Hostname;
  53.             //PreviousPassword = Password;
  54.             //PreviousPort = Port;
  55.             //PreviousUsername = Username;
  56.  
  57.             //You can do it!
  58.             ClientStatus = SocketStatus.SOCKET_STATUS_WAITING;
  59.             try
  60.             {
  61.                 Client.Connect();
  62.  
  63.                 // Have to assume client is connected cause Client.IsConnected is busted in some cases
  64.                 // All other conditions *should* error out...
  65.                 //if (Client.IsConnected)
  66.                 //{
  67.                     //Client.KeepAliveInterval = TimeSpan.FromSeconds(2);
  68.                     //Client.SendKeepAlive();
  69.                     TheStream = Client.CreateShellStream("PDTShell", 100, 80, 100, 200, 65534);
  70.                     TheStream.DataReceived += Stream_DataReceived;
  71.                     //TheStream.ErrorOccurred += TheStream_ErrorOccurred;
  72.                     Debug.Console(1, this, "Connected");
  73.                     ClientStatus = SocketStatus.SOCKET_STATUS_CONNECTED;
  74.                     //PreviousHostname = Hostname;
  75.                     //PreviousPassword = Password;
  76.                     //PreviousPort = Port;
  77.                     //PreviousUsername = Username;
  78.                 //}
  79.                 return; // Success will not pass here
  80.             }
  81.             catch (SshConnectionException e)
  82.             {
  83.                 var ie = e.InnerException; // The details are inside!!
  84.                 if (ie is SocketException)
  85.                     Debug.Console(1, this, "'{0}' CONNECTION failure: Cannot reach host, ({1})", Key, ie.GetType());
  86.                 else if (ie is System.Net.Sockets.SocketException)
  87.                     Debug.Console(1, this, "'{0}' Connection failure: Cannot reach host '{1}' on port {2}, ({3})",
  88.                         Key, Hostname, Port, ie.GetType());
  89.                 else if (ie is SshAuthenticationException)
  90.                 {
  91.                     Debug.Console(1, this, "Authentication failure for username '{0}', ({1})",
  92.                         Username, ie.GetType());
  93.                 }
  94.                 else
  95.                     Debug.Console(1, this, "Error on connect:\r({0})", e);
  96.             }
  97.             catch (Exception e)
  98.             {
  99.                 Debug.Console(1, this, "Unhandled exception on connect:\r({0})", e);
  100.             }
  101.            
  102.             // Sucess will not make it this far
  103.             ClientStatus = SocketStatus.SOCKET_STATUS_CONNECT_FAILED;
  104.             HandleConnectionFailure();
  105.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement