public void Cancel() { if (m_swDownloadStartTime != null) { m_swDownloadStartTime.Stop(); } if (m_writeAction != null) { m_CancelToken.Cancel(); m_writeAction.Complete(); try { m_writeAction.Completion.Wait(); } catch (Exception e) { Log.w(TAG, "Cancel excpetion for file " + FileName + " - " + e.Message); } m_writeAction = null; m_CancelToken.Dispose(); } if (m_fileStream != null) { m_fileStream.Close(); try { File.Delete(TempFilePath); Log.d(TAG, "Cancelled receiving file " + FileName); } catch (Exception e) { Log.w(TAG, "Error while deleting file " + TempFilePath + " - " + e.Message); } } }
public async Task UploadFile(BbRemoteServer server) { m_CancelToken = new CancellationTokenSource(); Status = EStatus.TRANSFERRING; m_swUploadStartTime = new LowResStopWatch(true); Log.i(TAG, "Sending " + FileName + " (" + FileSize.ToXByteSize() + ")", true); try { using (m_stream = File.OpenRead(FullPath)) { long lPosition = 0; while (lPosition < FileSize && server.Attached) { m_CancelToken.Token.ThrowIfCancellationRequested(); BbTcpPacket_SendFileData packet = new BbTcpPacket_SendFileData(server.UsedProtocolVersion, FileID, (ulong)lPosition); int nToRead = (int)Math.Min(packet.MaxDataSize, FileSize - lPosition); int nRead = await packet.WriteFileData(m_stream, nToRead, m_CancelToken.Token); if (nRead != nToRead) { throw new IOException("Unexpected read error (read less than expected)"); } lPosition += nRead; await server.SendLowPriorityDataPacketAsync(packet, m_CancelToken.Token); } } } finally { m_CancelToken.Dispose(); m_CancelToken = null; } Status = EStatus.FINISHED; m_swUploadStartTime.Stop(); Log.i(TAG, "Finished sending file " + FileName, true); }