public TransferTask( FtpSupportTcpClient cmd, Action <long> updateCall, // call when sent a new data, para stands for total transmitted size Action finishCall, // call when finish all the task Action <string> failCall, // call when the task fail to run, para stands for the message string localPath, string ftpPath, string file, TaskType type ) { this.cmd = cmd; this.updateCall = updateCall; this.finishCall = finishCall; this.failCall = failCall; Type = type; LocalPath = localPath; FtpPath = ftpPath; FileName = file; createTask(); }
// create a client for this Form private FtpSupportTcpClient createClient(string ip, ushort port) { if (!Regex.IsMatch(BoxIP.Text, IPv4Match) && !Regex.IsMatch(BoxIP.Text, IPv6Match)) { //IP address is not valid return(null); } /* evan_choo: this try-catch block is deleted and the exception is thrown to the method buttonConnection_click*/ //try //{ var r = new FtpSupportTcpClient(ip, port, s => { ListBoxLog.Items.Add(s); ListBoxLog.SelectedIndex = ListBoxLog.Items.Count - 1; }); // for port is "ushort", no need to check return(r); //} //catch (Exception e) //{ // //connnection error // throw e; //} }
private void ButtonConnect_Click(object sender, EventArgs e) { // core run paras Cursor cr = Cursor.Current; Cursor.Current = Cursors.WaitCursor; // disable all { inputBatch(false); actionBatch(false); } // core run if (ButtonConnect.Text == LanguageConstant.CONNECT_STRING) { BoxIP.Text = Regex.Replace(BoxIP.Text, @"\s", ""); if (BoxPort.Text.Equals("")) { BoxPort.Text = "21"; } /* evan_choo: the try-catch block is added here. if there is an exception, there is connection error*/ try { cmd = createClient(BoxIP.Text, Convert.ToUInt16(BoxPort.Text)); } catch (Exception) { MessageBox.Show(LanguageConstant.CONNECTION_ERROR); goto brk; } if (cmd == null) { MessageBox.Show(LanguageConstant.INVALID_IP_ERROR); goto brk; } ListBoxLog.Items.Clear(); // Login // User Name string rets = cmd.login(BoxUsername.Text, BoxPassword.Text).Substring(0, 3); if (!rets.Equals("230")) { MessageBox.Show(LanguageConstant.PASSWORD_ERROR); goto brk; } // just try to test all kinds of commands // while (true) // { // rets = Interaction.InputBox("命令"); // MessageBox.Show( // cmd.sendCommand(rets + CommandConstant.CRLF) // ); // } ButtonConnect.Text = LanguageConstant.DISCONNECT_STRING; functionEnable(); goto ret; // means to keep the original disconnection mode brk: inputBatch(true); } else if (ButtonConnect.Text == LanguageConstant.DISCONNECT_STRING) { if (!checkDisconnect()) { MessageBox.Show(LanguageConstant.CANNOT_CLOSE); goto brk; // keep connect } // rsy56640 // server might disconnect before client. if (task != null) { cmd.quitThis(); } cmd.closeStreams(); ButtonConnect.Text = LanguageConstant.CONNECT_STRING; cmd = null; setToReadyForLink(); goto ret; brk: actionBatch(true); if (task == null) { runningPartBatch(true); } else { runningPartBatch(false); } } else { throw new InvalidDataException("Not a valid string for this button"); } ret: Cursor.Current = cr; ButtonConnect.Enabled = true; }