private bool AcessFileSafe(file_desc file) { int result = 0; DialogResult dialogResult = DialogResult.None; if (InvokeRequired) { this.BeginInvoke(new Action <file_desc>((s) => { dialogResult = MessageBox.Show("Хотите принять файл " + s.file_name + ", размером " + s.file_size.ToString() + " байт ?", "Проверка", MessageBoxButtons.YesNo); }), file); } while (dialogResult == System.Windows.Forms.DialogResult.None) { } if (dialogResult == DialogResult.Yes) { return(true); } else if (dialogResult == DialogResult.No) { return(false); } return(false); }
// Приём файла private void recieveThread_DoWork(object sender, DoWorkEventArgs e) { BackgroundWorker worker = sender as BackgroundWorker; TcpListener Listener = new TcpListener(5678); string filename; Listener.Start(); Socket ReceiveSocket; int progress = 0; while (send_allow) { try { ReceiveSocket = Listener.AcceptSocket(); System.Windows.Forms.SaveFileDialog savedial = new SaveFileDialog(); //if (System.Windows.Forms.DialogResult.OK == savedial.ShowDialog()) // { filename = "C:/test.txt";//savedial.FileName; //} Byte[] Receive = new Byte[256]; //using (MemoryStream MessageR = new MemoryStream()) { //Количество считанных байт Int32 ReceivedBytes; Int32 Firest256Bytes = 0; String FilePath = ""; Int64 filesize = 0; Int64 all_size = -1; FileStream fl = null; String resFilePath = ""; Stopwatch stopWatch = new Stopwatch(); Stopwatch stopWatch2 = new Stopwatch(); string speed = "0"; int speed_last = 0; int count = 0; do {//Собственно читаем if (count == 0) { stopWatch.Restart(); } ReceivedBytes = ReceiveSocket.Receive(Receive, Receive.Length, 0); all_size += ReceivedBytes; //Разбираем первые 256 байт if (Firest256Bytes < 256) { Firest256Bytes += ReceivedBytes; Byte[] ToStr = Receive; all_size -= 256; //Накапливаем имя файла FilePath += Encoding.Default.GetString(ToStr); resFilePath = FilePath.Substring(0, FilePath.IndexOf('\0')); fl = new FileStream(resFilePath, FileMode.Create); } else if (Firest256Bytes < 512) { all_size -= 256; Firest256Bytes += ReceivedBytes; Byte[] ToStr = Receive; string str_size = ""; str_size += Encoding.Default.GetString(ToStr); String res_size = str_size.Substring(0, str_size.IndexOf('\0')); filesize = Convert.ToInt64(res_size); all_size = 0; file_desc f = new file_desc(resFilePath, filesize); byte[] mess = new byte[1]; if (!AcessFileSafe(f)) { toLogSafe(login + " | Приём файла " + resFilePath + " | Передача отклонена"); mess[0] = 0; ReceiveSocket.Send(mess); return; } else { mess[0] = 1; ReceiveSocket.Send(mess); stopWatch.Start(); stopWatch2.Start(); } } else { count++; if (count == 100) { TimeSpan ts = stopWatch.Elapsed; int progres = (int)(all_size / (filesize / 100)); int speed_cur = Convert.ToInt32((25600 * 1000 / 1024) / ts.TotalMilliseconds); TimeSpan ts2 = stopWatch2.Elapsed; if (ts2.Seconds > 1) { stopWatch2.Restart(); speed = Convert.ToString(speed_cur); speed_last = speed_cur; } recieveThread.ReportProgress(progres, speed); count = 0; } //и записываем в поток fl.Write(Receive, 0, ReceivedBytes); } //Читаем до тех пор, пока в очереди не останется данных } while (all_size != filesize && !sendThread.CancellationPending); if (recieveThread.CancellationPending) { ReceiveSocket.Close(); recieveThread.ReportProgress(0, "0"); toLogSafe(login + " | Приём файла " + resFilePath + " | Передача отменена"); } else { ReceiveSocket.Close(); toLogSafe(login + " | Приём файла " + resFilePath + " | завершен от " + another_login); } } } catch (System.Exception er) { toLogSafe("Ошибка : " + er.Message); } } }