/// <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; }
/// <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); }
public CmdR UploadAvatar(System.IO.Stream image) { var token = FileTransferManager.UploadFile(image, 0, "/avatar", overwrite: true, createMd5: true); if (!token.Ok) { return(token.Error); } token.Value.Wait(); if (token.Value.Status != TransferStatus.Done) { return(Util.CustomError("Avatar upload failed")); } var md5 = string.Concat(token.Value.Md5Sum.Select(x => x.ToString("x2"))); return(Send("clientupdate", new CommandParameter("client_flag_avatar", md5))); }
public LazyNotification?PushMessage(ReadOnlyMemory <byte> message) { var msgSpan = message.Span; string notifyname; int splitindex = msgSpan.IndexOf(AsciiSpace); if (splitindex < 0) { notifyname = msgSpan.TrimEnd(AsciiSpace).NewUtf8String(); } else { notifyname = msgSpan.Slice(0, splitindex).NewUtf8String(); } bool hasEqual; NotificationType ntfyType; if ((hasEqual = notifyname.IndexOf('=') >= 0) || (ntfyType = findTypeOfNotification(notifyname)) == NotificationType.Unknown) { if (!hasEqual) { Log.Debug("Maybe unknown notification: {0}", notifyname); } cmdLineBuffer = message; return(null); } var lineDataPart = splitindex < 0 ? ReadOnlySpan <byte> .Empty : msgSpan.Slice(splitindex); // if it's not an error it is a notification if (ntfyType != NotificationType.CommandError) { var notification = Deserializer.GenerateNotification(lineDataPart, ntfyType); if (!notification.Ok) { Log.Warn("Got unparsable message. ({0})", msgSpan.NewUtf8String()); return(null); } var lazyNotification = new LazyNotification(notification.Value, ntfyType); lock (waitBlockLock) { var dependantList = dependingBlocks[(int)ntfyType]; if (dependantList != null) { foreach (var item in dependantList) { item.SetNotification(lazyNotification); if (item.DependsOn != null) { foreach (var otherDepType in item.DependsOn) { if (otherDepType == ntfyType) { continue; } dependingBlocks[(int)otherDepType]?.Remove(item); } } } dependantList.Clear(); } } return(lazyNotification); } var result = Deserializer.GenerateSingleNotification(lineDataPart, NotificationType.CommandError); var errorStatus = result.Ok ? (CommandError)result.Value : Util.CustomError("Invalid Error code"); return(PushMessageInternal(errorStatus, ntfyType)); }
public LazyNotification?PushMessage(string message) { string notifyname; int splitindex = message.IndexOf(' '); if (splitindex < 0) { notifyname = message.TrimEnd(); } else { notifyname = message.Substring(0, splitindex); } bool hasEqual; NotificationType ntfyType; if ((hasEqual = notifyname.IndexOf('=') >= 0) || (ntfyType = MessageHelper.GetNotificationType(notifyname)) == NotificationType.Unknown) { if (!hasEqual) { Log.Debug("Maybe unknown notification: {0}", notifyname); } cmdLineBuffer = message; return(null); } var lineDataPart = splitindex < 0 ? "" : message.Substring(splitindex); // if it's not an error it is a notification if (ntfyType != NotificationType.CommandError) { var notification = Deserializer.GenerateNotification(lineDataPart, ntfyType); var lazyNotification = new LazyNotification(notification, ntfyType); lock (waitBlockLock) { var dependantList = dependingBlocks[(int)ntfyType]; if (dependantList != null) { foreach (var item in dependantList) { item.SetNotification(lazyNotification); if (item.DependsOn != null) { foreach (var otherDepType in item.DependsOn) { if (otherDepType == ntfyType) { continue; } dependingBlocks[(int)otherDepType]?.Remove(item); } } } dependantList.Clear(); } } return(lazyNotification); } var result = Deserializer.GenerateSingleNotification(lineDataPart, NotificationType.CommandError); var errorStatus = result.Ok ? (CommandError)result.Value : Util.CustomError("Invalid Error code"); return(PushMessageInternal(errorStatus, ntfyType)); }
public LazyNotification?PushMessage(string message) { string notifyname; int splitindex = message.IndexOf(' '); if (splitindex < 0) { notifyname = message.TrimEnd(); } else { notifyname = message.Substring(0, splitindex); } var ntfyType = MessageHelper.GetNotificationType(notifyname); if (ntfyType == NotificationType.Unknown) { cmdLineBuffer = message; return(null); } var lineDataPart = splitindex < 0 ? "" : message.Substring(splitindex); // if it's not an error it is a notification if (ntfyType != NotificationType.Error) { var notification = Deserializer.GenerateNotification(lineDataPart, ntfyType); var lazyNotification = new LazyNotification(notification, ntfyType); lock (waitBlockLock) { var dependantList = dependingBlocks[(int)ntfyType]; if (dependantList != null) { foreach (var item in dependantList) { item.SetNotification(lazyNotification); if (item.DependsOn != null) { foreach (var otherDepType in item.DependsOn) { if (otherDepType == ntfyType) { continue; } dependingBlocks[(int)otherDepType]?.Remove(item); } } } dependantList.Clear(); } } return(lazyNotification); } var result = Deserializer.GenerateSingleNotification(lineDataPart, NotificationType.Error); var errorStatus = result.Ok ? (CommandError)result.Value : Util.CustomError("Invalid Error code"); if (synchronQueue) { if (!requestQueue.IsEmpty && requestQueue.TryDequeue(out var waitBlock)) { waitBlock.SetAnswer(errorStatus, cmdLineBuffer); cmdLineBuffer = null; } else /* ??? */ } { }