Esempio n. 1
0
        public static bool ReConnect(int timeout_ms = NO_TIMEOUT)
        {
            if (ServerList.selected_serverinfo_textblock == null)
            {
                return(false);
            }

            string ip = ServerList.selected_serverinfo_textblock.serverinfo.ip;
            //string id = ServerList.selected_serverinfo_textblock.serverinfo.id;
            //string password = ServerList.selected_serverinfo_textblock.serverinfo.password;
            int port = ServerList.selected_serverinfo_textblock.serverinfo.port;

            try
            {
                //if(!CheckConnection(sftp, ip, port, id) || !CheckConnection(ssh, ip, port, id))
                if (!CheckConnection(sftp, ip, port) || !CheckConnection(ssh, ip, port))
                {
                    TextRange txt = new TextRange(Status.current.richTextBox_status.Document.ContentStart, Status.current.richTextBox_status.Document.ContentEnd);
                    txt.Text = "";

                    Window_LogIn wl = new Window_LogIn();

                    Point pt = ServerList.selected_serverinfo_textblock.PointToScreen(new Point(0, 0));
                    wl.Left = pt.X;
                    wl.Top  = pt.Y;

                    if (wl.ShowDialog() != true)
                    {
                        return(false);
                    }

                    string id       = wl.Id;
                    string password = wl.Password;
                    //string id = "cofile";
                    //string password = "******";
                    ServerList.selected_serverinfo_textblock.serverinfo.id = id;

                    sftp = new SftpClient(ip, port, id, password);
                    if (timeout_ms != NO_TIMEOUT)
                    {
                        sftp.ConnectionInfo.Timeout = new TimeSpan(0, 0, 0, 0, timeout_ms);
                    }
                    sftp.Connect();
                    ssh = new SshClient(ip, port, id, password);
                    if (timeout_ms != NO_TIMEOUT)
                    {
                        ssh.ConnectionInfo.Timeout = new TimeSpan(0, 0, 0, 0, timeout_ms);
                    }
                    ssh.Connect();

                    if (ssh.IsConnected)
                    {
                        shell_stream        = ssh.CreateShellStream("customCommand", 80, 24, 800, 600, 4096);
                        shell_stream_reader = new StreamReader(shell_stream);
                        shell_stream_writer = new StreamWriter(shell_stream);
                    }

                    if (shell_stream_read_timer == null)
                    {
                        shell_stream_read_timer          = new DispatcherTimer();
                        shell_stream_read_timer.Interval = new TimeSpan(0, 0, 0, 0, 100);
                        shell_stream_read_timer.Tick    += Shell_stream_read_timer_Tick;
                    }
                    //shell_stream_read_timer.Stop();
                    //shell_stream_read_timer.Start();
                    Log.PrintConsole(ip + " / " + port + " / " + id, "ReConnect" /*, test4.m_wnd.richTextBox_status*/);

                    readDummyMessageBlocking(null);
                    return(true);
                }
            }
            catch (Exception e)
            {
                Log.PrintError(e.Message, "ReConnect", Status.current.richTextBox_status);
                return(false);
            }
            return(true);
        }
Esempio n. 2
0
        private bool Connect(string ip, int port, int timeout_ms = NO_TIMEOUT)
        {
            bool retval = true;

            Window_LogIn wl = new Window_LogIn();

            if (WindowMain.current != null)
            {
                Point pt = WindowMain.current.PointToScreen(new Point((WindowMain.current.Width - wl.Width) / 2, (WindowMain.current.Height - wl.Height) / 2));
                wl.Left = pt.X;
                wl.Top  = pt.Y;
            }
            if (wl.ShowDialog() != true)
            {
                return(false);
            }

            parent.Id          = wl.Id;
            timeout_connect_ms = timeout_ms;
            string password = wl.Password;

            System.Threading.Thread th_popup = new System.Threading.Thread(delegate(object obj)
            {
                try
                {
                    Windows.Window_Waiting ww = new Window_Waiting("연결 중입니다..");
                    double[] arr = obj as double[];
                    if (arr != null && arr.Length >= 2)
                    {
                        ww.Left = arr[0] - ww.Width / 2;
                        ww.Top  = arr[1] - ww.Height / 2;
                    }
                    ww.Topmost = true;
                    ww.Show();
                    System.Windows.Threading.Dispatcher.Run();
                }
                catch (Exception ex)
                {
                    Log.PrintError(ex.Message, "Classes.SSHManager.Connect.th_popup");
                }
            }
                                                                           );
            th_popup.SetApartmentState(System.Threading.ApartmentState.STA);
            th_popup.IsBackground = true;
            Point _pt = WindowMain.current.PointToScreen(new Point(0, 0));

            th_popup.Start(new double[] { _pt.X + WindowMain.current.ActualWidth / 2, _pt.Y + WindowMain.current.ActualHeight / 2 });

            System.Threading.AutoResetEvent        resetEvent_connect = new System.Threading.AutoResetEvent(false);
            System.ComponentModel.BackgroundWorker bw_connect         = new System.ComponentModel.BackgroundWorker();
            bw_connect.DoWork += delegate(object sender, System.ComponentModel.DoWorkEventArgs e)
            {
                try
                {
                    ssh = new SshClient(ip, port, parent.Id, password);
                    if (timeout_connect_ms != NO_TIMEOUT)
                    {
                        ssh.ConnectionInfo.Timeout = new TimeSpan(0, 0, 0, 0, timeout_connect_ms);
                    }
                    ssh.Connect();
                    sftp = new SftpClient(ip, port, parent.Id, password);
                    if (timeout_connect_ms != NO_TIMEOUT)
                    {
                        sftp.ConnectionInfo.Timeout = new TimeSpan(0, 0, 0, 0, timeout_connect_ms);
                    }
                    sftp.Connect();

                    if (ssh.IsConnected)
                    {
                        shellStream       = ssh.CreateShellStream("customCommand", 80, 24, 800, 600, 4096);
                        shellStreamReader = new StreamReader(shellStream);
                        shellStreamWriter = new StreamWriter(shellStream);
                    }

                    Log.PrintLog("ip = " + ip + ", port = " + port, "Classes.SSHManager.Connect.bw_connect");
                    Log.PrintConsole("id = " + parent.Id, "Classes.SSHManager.Connect.bw_connect");
                }
                catch (Exception ex)
                {
                    Log.PrintError(ex.Message, "Classes.SSHManager.Connect.bw_connect");
                    //Log.ErrorIntoUI(ex.Message, "Connect", Status.current.richTextBox_status);
                    e.Result = ex.Message;
                }
                resetEvent_connect.Set();
                return;
            };
            bw_connect.RunWorkerCompleted += delegate(object sender, System.ComponentModel.RunWorkerCompletedEventArgs e)
            {
                if (e.Result as string != null)
                {
                    Log.ErrorIntoUI(e.Result as string, "Connect", Status.current.richTextBox_status);
                    WindowMain.current.ShowMessageDialog("Connect", "서버와 연결에 실패하였습니다.\n" + e.Result as string, MahApps.Metro.Controls.Dialogs.MessageDialogStyle.Affirmative);
                }
            };
            bw_connect.RunWorkerAsync("MyName");
            resetEvent_connect.WaitOne();

            try
            {
                th_popup.Abort();
            }
            catch (Exception e)
            {
                Log.PrintError("th_popup.Abort() : " + e.Message, "Classes.SSHManager.Connect");
            }


            //Log.PrintConsole("Connected", "Classes.SSHManager.ReConnect");

            //LinuxDirectoryViewer w = new LinuxDirectoryViewer(_PullListInDirectory("/home/cofile"));
            //w.Show();

            return(retval);
        }