private Task downloadFile(string sourceIP, string sourcePath, string destinationPath) { return(Task.Factory.StartNew(() => { bool isDone = false; FileDownloadManagement download = new FileDownloadManagement(); download.SourceIp = sourceIP; TransferDetails info = new TransferDetails(sourcePath, destinationPath); download.fileDetails.Add(info); download.AllFileDownloadingCompletedCallback = new FileDownloadManagement.AllFileDownloadingCompleted((FileDownloadManagement sender, RServer server) => { isDone = true; }); download.StartDownloading(); while (!isDone) { Thread.Sleep(10); } })); }
private void newConnectionEstablishedCallback(NetworkServer serverObj, CommunicationNetworkStream __stream) { RServer connection = serverObj.serverPort; double _readOut = 0; int noOfFilesReceived = 0; //lock(_readOut){ } //SET UP TIMER Stopwatch totalTimer = new Stopwatch(); //TOTAL TIME totalTimer.Start(); //START TIMER Stopwatch fileTimer = new Stopwatch(); //FILE TIMER fileTimer.Start(); //START TIMER //HANDLE THE DATA RECEIVED RESPONSES connection.setOnDataReceivedCallback( new RServer.DataReceived((RServer sender, CommunicationNetworkStream stream, string data) => { switch (CommandRecognization.Recognize(data)) { case ExchangeType.FILE_TRANSFER: double calling = 1; fileTimer.Restart(); //START TIMER TransferDetails CURRENT_FILE = fileDetails[noOfFilesReceived]; //CURRENT FILE FileReceiver fR = new FileReceiver(stream).Initialize(); //TRANSFER OBJ //PROGRESS CALLBACK fR.FileReceivingProgressCallback = new FileReceiver.FileReceivingProgress( (string FileName, double progressRatio) => { try { CURRENT_FILE.progressRatio = progressRatio; ActualSizeTransfered = _readOut + CURRENT_FILE.FileLength * progressRatio; totalTimeElapsed = totalTimer.Elapsed; //UPDATE PUBLIC try { if ((calling % skeepCalls) == 0) { DownloadingProgressCallback(this, CURRENT_FILE, progressRatio, fileTimer); //PASS THE CALLBACK } } catch (Exception) { } calling++; } catch (Exception) { } }); //COMPLETION CALLBACK fR.FileReceivingCompletedCallback = new FileReceiver.FileReceivingCompleted( (string FileName) => { //DOWNLOAD COMPLETED try { _readOut += CURRENT_FILE.FileLength; ActualSizeTransfered = _readOut; noOfFilesReceived++; fileTimer.Stop(); //STOP TIMER totalTimeElapsed = totalTimer.Elapsed; //UPDATE PUBLIC try { DownloadingProgressCallback(this, CURRENT_FILE, 1.0d, fileTimer); //FORCED CALLBACK } catch (Exception) { } try { DownloadingCompletedCallback(this, CURRENT_FILE, fileTimer); //PASS THE CALLBACK } catch (Exception) { } } catch (Exception) { } }); try { //NEW RECEIVING IS STARTED FileDownloadingStartedCallback(this, CURRENT_FILE); //RECEIVING STARTED CALLBACK } catch (Exception) { } fR.StartReceiving(true); //START RECEIVING if (fileDetails.Count <= noOfFilesReceived) { //ALL DOWNLOADS COMPLETED totalTimer.Stop(); //STOP TIMER totalTimeElapsed = totalTimer.Elapsed; //UPDATE PUBLIC try { AllFileDownloadingCompletedCallback(this, sender); //ALL FILES RECEIVED } catch (Exception) { } NetworkServer.closeConnection(connection); //CLOSE CONNECTION } break; default: break; } })); //MADE ALL REQUESTS foreach (TransferDetails info in fileDetails) { new FileReceiverRequest(info.SourcePath, info.DestinationPath, connection.Stream, MyGlobal.BUFFER_SIZE).Request(); } }
private void downloadFiles(List <r.FileInformationResponse.FileInfo> files, string destinationPath) { FileDownloadManagement download = new FileDownloadManagement(); //IP download.SourceIp = server.serverPort.ServerIP; //PROGRESS FORM TransferProgressBoxForm pBox = new TransferProgressBoxForm(); //FILES foreach (r.FileInformationResponse.FileInfo sourceFile in files) { TransferDetails details = new TransferDetails(); //SOURCE PATH details.SourcePath = sourceFile.FullName; //DESTINATION PATH if (destinationPath.EndsWith("\\")) { details.DestinationPath = destinationPath + sourceFile.Name; } else { details.DestinationPath = $"{destinationPath}\\{sourceFile.Name}"; } //LENGTH details.FileLength = sourceFile.Length; download.TotalSizeForTransfer += details.FileLength; //UI ATTACHMENT //FIRST TAG ListViewItem liv = new ListViewItem($"{server.nickName} - [{download.SourceIp}]"); liv.SubItems.Add("Waiting..."); liv.SubItems.Add("00:00:00"); liv.SubItems.Add(details.SourcePath); liv.SubItems.Add(details.DestinationPath); transferContentView.Items.Add(liv); details.Tag.Add(liv); //TAG //SECOND TAG ListViewItem l = new ListViewItem(details.DestinationPath); pBox.addListViewItems(l); details.Tag.Add(l); //TAG //ADD TO MASTER download.fileDetails.Add(details); } transferContentView.AutoResizeColumns(ColumnHeaderAutoResizeStyle.ColumnContent); //STARTED CALLBACK download.FileDownloadingStartedCallback = new FileDownloadManagement.FileDownloadingStarted( (FileDownloadManagement sender, TransferDetails objectDetails) => { Invoke(new UpdateData(() => { try { pBox.singleItemStarting((ListViewItem)objectDetails.Tag[1]); } catch (Exception) { } })); }); //PROGRESS CALLBACK download.DownloadingProgressCallback = new FileDownloadManagement.FileDownloadingProgress( (FileDownloadManagement sender, TransferDetails objectDetails, double progressRatio, Stopwatch Timer) => { Invoke(new UpdateData(() => { ListViewItem liv = (ListViewItem)objectDetails.Tag[0]; liv.SubItems[1].Text = $"{(progressRatio * 100).ToString("00.00")}%"; liv.SubItems[2].Text = $"{Timer.Elapsed.ToString("hh\\:mm\\:ss")}"; try { pBox.progressNotification((int)(sender.ActualSizeTransfered / sender.TotalSizeForTransfer * 100)); } catch (Exception) { } })); }); //COMPLETION CALLBACK download.DownloadingCompletedCallback = new FileDownloadManagement.FileDownloadingCompleted( (FileDownloadManagement sender, TransferDetails objectDetails, Stopwatch Timer) => { ListViewItem liv = (ListViewItem)objectDetails.Tag[0]; Invoke(new UpdateData(() => { liv.ForeColor = Color.Green; liv.SubItems[2].Text = $"{Timer.Elapsed.ToString("hh\\:mm\\:ss")}"; try { pBox.singleItemCompletion((ListViewItem)objectDetails.Tag[1]); } catch (Exception) { } })); }); //ALL TRANSFER COMPLETED CALLBACK download.AllFileDownloadingCompletedCallback = new FileDownloadManagement.AllFileDownloadingCompleted( (FileDownloadManagement sender, RServer server) => { Invoke(new UpdateData(() => { try { pBox.Close(); } catch (Exception) { } try { pBox.Dispose(); } catch (Exception) { } })); } ); //ERROR CALLBACK download.ConnectionEstablishErrorCallback = new FileDownloadManagement.ConnectionEstablishError( (FileDownloadManagement sender, RServer server, string message) => { foreach (TransferDetails item in sender.fileDetails) { ListViewItem liv = (ListViewItem)item.Tag[0]; Invoke(new UpdateData(() => { liv.SubItems[1].Text = message; liv.ForeColor = Color.Red; })); } } ); download.skeepCallback(100); //REDUCE CALLBACKS //SHOW FORM pBox.TotalNumberOfItems = files.Count; pBox.senderIP = server.serverPort.ServerIP; pBox.receiverIP = download.DestinationIp; pBox.progressNotification(0); pBox.Show(); //START DOWNLOADING download.StartDownloading(); }