/// <summary>Resumes a download from a previously stopped position.</summary> /// <param name="token">The aborted token.</param> public E<CommandError> Resume(FileTransferToken token) { lock (token) { if (token.Status != TransferStatus.Cancelled) return Util.CustomError("Only cancelled transfers can be resumed"); if (token.Direction == TransferDirection.Upload) { var result = parent.FileTransferInitUpload(token.ChannelId, token.Path, token.ChannelPassword, token.ClientTransferId, token.Size, false, true); if (!result.Ok) return result.Error; var request = result.Value; token.ServerTransferId = request.ServerFileTransferId; token.SeekPosition = request.SeekPosistion; token.Port = request.Port; token.TransferKey = request.FileTransferKey; } else // Download { var result = parent.FileTransferInitDownload(token.ChannelId, token.Path, token.ChannelPassword, token.ClientTransferId, token.LocalStream.Position); if (!result.Ok) return result.Error; var request = result.Value; token.ServerTransferId = request.ServerFileTransferId; token.SeekPosition = -1; token.Port = request.Port; token.TransferKey = request.FileTransferKey; } token.Status = TransferStatus.Waiting; } StartWorker(token); return E<CommandError>.OkR; }
public void Wait(FileTransferToken token) { while (token.Status == TransferStatus.Waiting || token.Status == TransferStatus.Trasfering) { Thread.Sleep(10); } }
/// <summary>Gets information about the current transfer status.</summary> /// <param name="token">The transfer to check.</param> /// <returns>Returns an information object or <code>null</code> when not available.</returns> public R<FileTransfer, CommandError> GetStats(FileTransferToken token) { lock (token) { if (token.Status != TransferStatus.Transfering) return Util.CustomError("No transfer found"); } var result = parent.FileTransferList(); if (result.Ok) return result.Value.Where(x => x.ServerFileTransferId == token.ServerTransferId).WrapSingle(); return R<FileTransfer, CommandError>.Err(result.Error); }
private void StartWorker(FileTransferToken token) { lock (transferQueue) { transferQueue.Enqueue(token); if (threadEnd || workerThread == null || !workerThread.IsAlive) { workerThread = new Thread(TransferLoop); workerThread.Start(); } } }
/// <summary>Initiate a file download from the server.</summary> /// <param name="stream">Data stream to write to.</param> /// <param name="channel">The channel id to download from.</param> /// <param name="path">The download path within the channel. Eg: "file.txt", "path/file.png"</param> /// <param name="channelPassword">The password for the channel.</param> /// <param name="closeStream">True will <see cref="IDisposable.Dispose"/> the stream after the download is finished.</param> /// <returns>A token to track the file transfer.</returns> public R<FileTransferToken, CommandError> DownloadFile(Stream stream, ChannelIdT channel, string path, string channelPassword = "", bool closeStream = true) { ushort cftid = GetFreeTransferId(); var request = parent.FileTransferInitDownload(channel, path, channelPassword, cftid, 0); if (!request.Ok) { if (closeStream) stream.Close(); return request.Error; } var token = new FileTransferToken(stream, request.Value, channel, path, channelPassword, 0) { CloseStreamWhenDone = closeStream }; StartWorker(token); return token; }
/// <summary>Initiate a file upload to the server.</summary> /// <param name="stream">Data stream to upload.</param> /// <param name="channel">The channel id to upload to.</param> /// <param name="path">The upload path within the channel. Eg: "file.txt", "path/file.png"</param> /// <param name="overwrite">True if the upload should overwrite the file if it exists. /// False will throw an exception if the file already exists.</param> /// <param name="channelPassword">The password for the channel.</param> /// <param name="closeStream">True will <see cref="IDisposable.Dispose"/> the stream after the upload is finished.</param> /// <param name="createMd5">Will generate a md5 sum of the uploaded file.</param> /// <returns>A token to track the file transfer.</returns> public R<FileTransferToken, CommandError> UploadFile(Stream stream, ChannelIdT channel, string path, bool overwrite = false, string channelPassword = "", bool closeStream = true, bool createMd5 = false) { ushort cftid = GetFreeTransferId(); var request = parent.FileTransferInitUpload(channel, path, channelPassword, cftid, stream.Length, overwrite, false); if (!request.Ok) { if (closeStream) stream.Close(); return request.Error; } var token = new FileTransferToken(stream, request.Value, channel, path, channelPassword, stream.Length, createMd5) { CloseStreamWhenDone = closeStream }; StartWorker(token); return token; }
public FileTransferToken UploadFile(Stream stream, ChannelIdT channel, string path, bool overwrite = false, string channelPassword = "") { ushort cftid = GetFreeTransferId(); var request = parent.FileTransferInitUpload(channel, path, channelPassword, cftid, stream.Length, overwrite, false); if (!string.IsNullOrEmpty(request.Message)) { throw new Ts3Exception(request.Message); } var token = new FileTransferToken(stream, request, channel, path, channelPassword, stream.Length); StartWorker(token); return(token); }
/// <summary>Stops an active transfer.</summary> /// <param name="token">The token to abort.</param> /// <param name="delete">True to delete the file. /// False to only temporarily stop the transfer (can be resumed again with <see cref="Resume"/>).</param> public void Abort(FileTransferToken token, bool delete = false) { lock (token) { if (token.Status != TransferStatus.Transfering && token.Status != TransferStatus.Waiting) return; parent.FileTransferStop(token.ServerTransferId, delete); token.Status = TransferStatus.Cancelled; if (delete && token.CloseStreamWhenDone) { token.LocalStream.Close(); } } }
public FileTransfer GetStats(FileTransferToken token) { lock (token) { if (token.Status != TransferStatus.Trasfering) { return(null); } } try { return(parent.FileTransferList().FirstOrDefault(x => x.ServerFileTransferId == token.ServerTransferId)); } // catch case when transfer is not found (probably already over or not yet started) catch (Ts3CommandException ts3ex) when(ts3ex.ErrorStatus.Id == Ts3ErrorCode.database_empty_result) { return(null); } }
private void StartWorker(FileTransferToken token) { lock (transferQueue) { transferQueue.Enqueue(token); if (threadEnd || workerThread is null || !workerThread.IsAlive) { threadEnd = false; workerThread = new Thread(() => { Util.SetLogId(parent.ConnectionData.LogId); TransferLoop(); }) { Name = $"FileTransfer[{parent.ConnectionData.LogId}]" }; workerThread.Start(); } } }
public FileTransferToken DownloadFile(Stream stream, ChannelIdT channel, string path, string channelPassword = "", bool closeStream = false) { ushort cftid = GetFreeTransferId(); var request = parent.FileTransferInitDownload(channel, path, channelPassword, cftid, 0); if (!string.IsNullOrEmpty(request.Message)) { throw new Ts3Exception(request.Message); } var token = new FileTransferToken(stream, request, channel, path, channelPassword, 0) { CloseStreamWhenDone = closeStream }; StartWorker(token); return(token); }
public void Resume(FileTransferToken token) { lock (token) { if (token.Status != TransferStatus.Cancelled) { throw new Ts3Exception("Only cancelled transfers can be resumed"); } if (token.Direction == TransferDirection.Upload) { var request = parent.FileTransferInitUpload(token.ChannelId, token.Path, token.ChannelPassword, token.ClientTransferId, token.Size, false, true); if (!string.IsNullOrEmpty(request.Message)) { throw new Ts3Exception(request.Message); } token.ServerTransferId = request.ServerFileTransferId; token.SeekPosition = request.SeekPosistion; token.Port = request.Port; token.TransferKey = request.FileTransferKey; } else // Download { var request = parent.FileTransferInitDownload(token.ChannelId, token.Path, token.ChannelPassword, token.ClientTransferId, token.LocalStream.Position); if (!string.IsNullOrEmpty(request.Message)) { throw new Ts3Exception(request.Message); } token.ServerTransferId = request.ServerFileTransferId; token.SeekPosition = -1; token.Port = request.Port; token.TransferKey = request.FileTransferKey; } token.Status = TransferStatus.Waiting; } StartWorker(token); }