public static int CompareDescending(DataContainers.FileTransferContainer a, DataContainers.FileTransferContainer b) { if (a == null) { if (b == null) { return(0); } else { return(-1); } } else { if (b == null) { return(1); } if (a.sizeInBytes == b.sizeInBytes) { return(0); } else if (a.sizeInBytes > b.sizeInBytes) { return(1); } else { return(-1); } } }
public static int CompareAscending(DataContainers.FileTransferContainer a, DataContainers.FileTransferContainer b) { if (a == null) { if (b == null) { return(0); } else { return(-1); } } else { if (b == null) { return(1); } int _resultOfComparison = string.CompareOrdinal(a.fileName, b.fileName); if (_resultOfComparison < 0) { return(-1); } else if (_resultOfComparison > 0) { return(1); } else { return(0); } } }
private void OpenFileClicked(object sender, RoutedEventArgs e) { DataContainers.FileTransferContainer _fileTransferContainerObject = (DataContainers.FileTransferContainer)transfersListView.SelectedItem; if (_fileTransferContainerObject.status != FileTransferStatus.Finished || _fileTransferContainerObject.transferType != FileTransferType.Download) { return; } if (!System.IO.File.Exists(_fileTransferContainerObject.filePath)) { MessageBox.Show("File has been deleted or moved"); return; } if (Network.FileTransfer.IsFileExtensionDangerous(System.IO.Path.GetExtension(_fileTransferContainerObject.fileName))) { if (MessageBox.Show("This File Maybe Dangerous, Proceed to open file ?", "Open Possible Malicious File", MessageBoxButton.OKCancel) == MessageBoxResult.Cancel) { return; } } System.Diagnostics.Process.Start(_fileTransferContainerObject.filePath); }
public static int CompareAscending(DataContainers.FileTransferContainer a, DataContainers.FileTransferContainer b) { if (a == null) { if (b == null) { return(0); } else { return(-1); } } else { if (b == null) { return(1); } else if (a.status == b.status) { if (a.status == FileTransferStatus.Paused) { if (a.pausedBy == b.pausedBy) { return(0); } else if (a.pausedBy > b.pausedBy) { return(1); } else { return(-1); } } else if (a.transferType == b.transferType) { return(0); } else if (a.transferType > b.transferType) { return(1); } else { return(-1); } } else if (a.status < b.status) { return(1); } else { return(-1); } } }
public static int CompareAscending(DataContainers.FileTransferContainer a, DataContainers.FileTransferContainer b) { int result = CompareDescending(a, b); if (result == 1) { return(-1); } else if (result == -1) { return(1); } else { return(0); } }
public FileTransfer(DataContainers.FileTransferContainer fileTransferContainer, string filePath = null, int intialPort = 0) { resetEvent = new AutoResetEvent(false); acknowledgedTransfers = 0; syncObject = new object(); controlSocket = null; dataSocket = null; if (fileTransferContainer.transferType == FileTransferType.Upload) { port1 = intialPort; dataSocket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp); dataSocket.Bind(new System.Net.IPEndPoint(System.Net.IPAddress.Any, port1)); dataSocket.Listen(1); dataSocket.BeginAccept(new AsyncCallback(EndAcceptDataConnection), filePath); } this.fileTransferContainer = fileTransferContainer; fileTransferContainer.FileTransferClassInstance = this; }
public static int CompareAscending(DataContainers.FileTransferContainer a, DataContainers.FileTransferContainer b) { if (a == null) { if (b == null) { return(0); } else { return(-1); } } else { if (b == null) { return(1); } return(string.CompareOrdinal(a.ID, b.ID)); } }
private void DeleteFileClicked(object sender, RoutedEventArgs e) { DataContainers.FileTransferContainer _fileTransferContainerObject = (DataContainers.FileTransferContainer)transfersListView.SelectedItem; if (_fileTransferContainerObject.status != FileTransferStatus.Finished || _fileTransferContainerObject.transferType != FileTransferType.Download) { return; } if (!System.IO.File.Exists(_fileTransferContainerObject.filePath)) { MessageBox.Show("File has been already deleted or moved"); return; } try { System.IO.File.Delete(_fileTransferContainerObject.filePath); } catch { } lock (Network.FileTransfer.RunningTransfers) { Network.FileTransfer.RunningTransfers.Remove(_fileTransferContainerObject); } }
private void StopItemClicked(object sender, RoutedEventArgs e) { DataContainers.FileTransferContainer _fileTransferContainerObject = (DataContainers.FileTransferContainer)transfersListView.SelectedItem; lock (Network.FileTransfer.RunningTransfers) { if (_fileTransferContainerObject.status != FileTransferStatus.Finished) { _fileTransferContainerObject.status = FileTransferStatus.Cancelled; } } System.Threading.ThreadPool.QueueUserWorkItem(state => { lock (_fileTransferContainerObject.FileTransferClassInstance.controlSocket) { try { Network.NetworkCommunicationManagers.SendByteArrayOverSocket(_fileTransferContainerObject.FileTransferClassInstance.controlSocket, new byte[] { (byte)FileTransferCommands.EndTransfer }); } catch (System.ObjectDisposedException) {} catch (System.Net.Sockets.SocketException) {} } System.Threading.Thread.Sleep(500); Network.NetworkCommunicationManagers.Disconnect(_fileTransferContainerObject.FileTransferClassInstance.controlSocket); Network.NetworkCommunicationManagers.Disconnect(_fileTransferContainerObject.FileTransferClassInstance.dataSocket); }); }
private void PauseItemClicked(object sender, RoutedEventArgs e) { if (((System.Windows.Controls.MenuItem)sender).Parent is System.Windows.Controls.Menu) { lock (Network.FileTransfer.RunningTransfers) { foreach (DataContainers.FileTransferContainer _fileTransferContainersObject in Network.FileTransfer.RunningTransfers) { if (_fileTransferContainersObject.status == FileTransferStatus.Cancelled || _fileTransferContainersObject.status == FileTransferStatus.Error || _fileTransferContainersObject.status == FileTransferStatus.Finished) { continue; } if (_fileTransferContainersObject.transferType == FileTransferType.Upload) { _fileTransferContainersObject.status = FileTransferStatus.Paused; } if (_fileTransferContainersObject.pausedBy == PausedBy.None) { _fileTransferContainersObject.pausedBy = PausedBy.User; } else if (_fileTransferContainersObject.pausedBy == PausedBy.OtherPeer) { _fileTransferContainersObject.pausedBy = PausedBy.Both; } System.Threading.ThreadPool.QueueUserWorkItem(state => { lock (_fileTransferContainersObject.FileTransferClassInstance.controlSocket) { try { Network.NetworkCommunicationManagers.SendByteArrayOverSocket(_fileTransferContainersObject.FileTransferClassInstance.controlSocket, new byte[] { (byte)FileTransferCommands.Pause }); } catch (System.ObjectDisposedException) { Network.NetworkCommunicationManagers.Disconnect(_fileTransferContainersObject.FileTransferClassInstance.controlSocket); Network.NetworkCommunicationManagers.Disconnect(_fileTransferContainersObject.FileTransferClassInstance.dataSocket); } catch (System.Net.Sockets.SocketException) { Network.NetworkCommunicationManagers.Disconnect(_fileTransferContainersObject.FileTransferClassInstance.controlSocket); Network.NetworkCommunicationManagers.Disconnect(_fileTransferContainersObject.FileTransferClassInstance.dataSocket); } } }); } } } else { DataContainers.FileTransferContainer _fileTransferContainerObject = (DataContainers.FileTransferContainer)transfersListView.SelectedItem; lock (Network.FileTransfer.RunningTransfers) { if (_fileTransferContainerObject.transferType == FileTransferType.Upload) { _fileTransferContainerObject.status = FileTransferStatus.Paused; } if (_fileTransferContainerObject.pausedBy == PausedBy.None) { _fileTransferContainerObject.pausedBy = PausedBy.User; } else if (_fileTransferContainerObject.pausedBy == PausedBy.OtherPeer) { _fileTransferContainerObject.pausedBy = PausedBy.Both; } } System.Threading.ThreadPool.QueueUserWorkItem(state => { lock (_fileTransferContainerObject.FileTransferClassInstance.controlSocket) { try { Network.NetworkCommunicationManagers.SendByteArrayOverSocket(_fileTransferContainerObject.FileTransferClassInstance.controlSocket, new byte[] { (byte)FileTransferCommands.Pause }); } catch (System.ObjectDisposedException) { Network.NetworkCommunicationManagers.Disconnect(_fileTransferContainerObject.FileTransferClassInstance.controlSocket); Network.NetworkCommunicationManagers.Disconnect(_fileTransferContainerObject.FileTransferClassInstance.dataSocket); } catch (System.Net.Sockets.SocketException) { Network.NetworkCommunicationManagers.Disconnect(_fileTransferContainerObject.FileTransferClassInstance.controlSocket); Network.NetworkCommunicationManagers.Disconnect(_fileTransferContainerObject.FileTransferClassInstance.dataSocket); } } }); } }