Skip to content

Commit

Permalink
Minor Fixes
Browse files Browse the repository at this point in the history
Fixed an issue with progress percentage never reaching 100% if there are an odd number of tasks.
Fixed an issue with the updater.exe getting stuck in a loop and never quitting when it relaunches the application (not sure exactly why, but the named pipe isn't needed for relaunch anyway).
  • Loading branch information
TrickUK committed Jul 13, 2012
1 parent bf09759 commit bd5769f
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 66 deletions.
7 changes: 4 additions & 3 deletions src/NAppUpdate.Framework/UpdateManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,9 @@ private void TaskProgressCallback(UpdateProgressInfo currentStatus, IUpdateTask
currentStatus.TaskDescription = task.Description;
currentStatus.TaskId = UpdatesToApply.IndexOf(task) + 1;

var taskPerc = 100 / UpdatesToApply.Count;
currentStatus.Percentage = (currentStatus.Percentage * taskPerc / 100) + (currentStatus.TaskId - 1) * taskPerc;
//This was an assumed int, which meant we never reached 100% with an odd number of tasks
float taskPerc = 100F / UpdatesToApply.Count;
currentStatus.Percentage = (int)Math.Round((currentStatus.Percentage * taskPerc / 100) + (currentStatus.TaskId - 1) * taskPerc);

ReportProgress(currentStatus);
}
Expand Down Expand Up @@ -463,7 +464,7 @@ public void ApplyUpdates(bool relaunchApplication, bool updaterDoLogging, bool u
bool createdNew;
using (new Mutex(true, Config.UpdateProcessName + "Mutex", out createdNew))
{
if (NauIpc.LaunchProcessAndSendDto(dto, info, Config.UpdateProcessName) == null)
if (NauIpc.LaunchProcessAndSendDto(dto, info, Config.UpdateProcessName, true) == null)
throw new UpdateProcessFailedException("Could not launch cold update process");

Environment.Exit(0);
Expand Down
141 changes: 82 additions & 59 deletions src/NAppUpdate.Framework/Utils/FileDownloader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,93 +5,116 @@

namespace NAppUpdate.Framework.Utils
{
public sealed class FileDownloader
{
private readonly Uri _uri;
private readonly int _bufferSize = 1024;
public IWebProxy Proxy { get; set; }

public FileDownloader()
{
Proxy = null;
}


public FileDownloader(string url)
{
_uri = new Uri(url);
}

public FileDownloader(Uri uri)
{
_uri = uri;
}

public byte[] Download()
{
using (var client = new WebClient())
return client.DownloadData(_uri);
}
public sealed class FileDownloader
{
private readonly Uri _uri;
private readonly int _bufferSize = 1024;
public IWebProxy Proxy { get; set; }

public FileDownloader()
{
Proxy = null;
}


public FileDownloader(string url)
{
_uri = new Uri(url);
}

public FileDownloader(Uri uri)
{
_uri = uri;
}

public byte[] Download()
{
using (var client = new WebClient())
return client.DownloadData(_uri);
}

public bool DownloadToFile(string tempLocation)
{
return DownloadToFile(tempLocation, null);
}

public bool DownloadToFile(string tempLocation, Action<UpdateProgressInfo> onProgress)
{
var request = WebRequest.Create(_uri);
{
var request = WebRequest.Create(_uri);
request.Proxy = Proxy;

using (var response = request.GetResponse())
using (var tempFile = File.Create(tempLocation))
{
using (var responseStream = response.GetResponseStream())
{
if (responseStream ==null)
if (responseStream == null)
return false;

var downloadSize = response.ContentLength;
var totalBytes = 0;
long downloadSize = response.ContentLength;
long totalBytes = 0;
var buffer = new byte[_bufferSize];
int reportInterval = 1;
DateTime stamp = DateTime.Now.Subtract(new TimeSpan(0, 0, reportInterval));
int bytesRead;
do
{
bytesRead = responseStream.Read(buffer, 0, buffer.Length);
totalBytes += bytesRead;
tempFile.Write(buffer, 0, bytesRead);

if (onProgress != null) onProgress(new DownloadProgressInfo {
DownloadedInBytes = totalBytes,
FileSizeInBytes = downloadSize,
Percentage = (int)(totalBytes * 100 / downloadSize),
Message = string.Format("Downloading... ({0} / {1} completed)", totalBytes, downloadSize), // TODO: KB / MB Formatting
StillWorking = totalBytes == downloadSize,
});
if (onProgress != null && DateTime.Now.Subtract(stamp).TotalSeconds >= reportInterval)
{
ReportProgress(onProgress, totalBytes, downloadSize);
stamp = DateTime.Now;
}

} while (bytesRead > 0 && !UpdateManager.Instance.ShouldStop);

ReportProgress(onProgress, totalBytes, downloadSize);
return totalBytes == downloadSize;
}
}
}

/*
public void DownloadAsync(Action<byte[]> finishedCallback)
{
DownloadAsync(finishedCallback, null);
}
public void DownloadAsync(Action<byte[]> finishedCallback, Action<long, long> progressChangedCallback)
{
using (var client = new WebClient())
{
if (progressChangedCallback != null)
client.DownloadProgressChanged += (sender, args) => progressChangedCallback(args.BytesReceived, args.TotalBytesToReceive);
client.DownloadDataCompleted += (sender, args) => finishedCallback(args.Result);
client.DownloadDataAsync(_uri);
}
}*/
}
}

private void ReportProgress(Action<UpdateProgressInfo> onProgress, long totalBytes, long downloadSize)
{
if (onProgress != null) onProgress(new DownloadProgressInfo
{
DownloadedInBytes = totalBytes,
FileSizeInBytes = downloadSize,
Percentage = (int)((totalBytes / downloadSize) * 100),
Message = string.Format("Downloading... ({0} / {1} completed)", ToFileSizeString(totalBytes), ToFileSizeString(downloadSize)),
StillWorking = totalBytes == downloadSize,
});
}

private string ToFileSizeString(long size)
{
if (size < 1000) return String.Format("{0} bytes", size);
if (size < 1000000) return String.Format("{0:F1} KB", (size / 1000));
if (size < 1000000000) return String.Format("{0:F1} MB", (size / 1000000));
if (size < 1000000000000) return String.Format("{0:F1} GB", (size / 1000000000));
if (size < 1000000000000000) return String.Format("{0:F1} TB", (size / 1000000000000));
return size.ToString();
}

/*
public void DownloadAsync(Action<byte[]> finishedCallback)
{
DownloadAsync(finishedCallback, null);
}
public void DownloadAsync(Action<byte[]> finishedCallback, Action<long, long> progressChangedCallback)
{
using (var client = new WebClient())
{
if (progressChangedCallback != null)
client.DownloadProgressChanged += (sender, args) => progressChangedCallback(args.BytesReceived, args.TotalBytesToReceive);
client.DownloadDataCompleted += (sender, args) => finishedCallback(args.Result);
client.DownloadDataAsync(_uri);
}
}*/
}
}
3 changes: 2 additions & 1 deletion src/NAppUpdate.Framework/Utils/NauIpc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ internal static string GetPipeName(string syncProcessName)

internal static uint BUFFER_SIZE = 4096;

public static Process LaunchProcessAndSendDto(NauDto dto, ProcessStartInfo processStartInfo, string syncProcessName)
public static Process LaunchProcessAndSendDto(NauDto dto, ProcessStartInfo processStartInfo, string syncProcessName, bool sendDto)
{
Process p;
using (var clientPipeHandle = CreateNamedPipe(
Expand All @@ -96,6 +96,7 @@ public static Process LaunchProcessAndSendDto(NauDto dto, ProcessStartInfo proce
return null;
}

if (sendDto)
while (true)
{
var success = 0;
Expand Down
2 changes: 1 addition & 1 deletion src/NAppUpdate.Tests/Core/UpdateStarterTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void UpdaterDeploymentAndIPCWorks()
Arguments = string.Format(@"""{0}"" -showConsole", dto.Configs.UpdateProcessName),
};

var p = NauIpc.LaunchProcessAndSendDto(dto, info, dto.Configs.UpdateProcessName);
var p = NauIpc.LaunchProcessAndSendDto(dto, info, dto.Configs.UpdateProcessName, false);
Assert.IsNotNull(p);
p.WaitForExit();

Expand Down
4 changes: 2 additions & 2 deletions src/NAppUpdate.Updater/AppStart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -159,8 +159,8 @@ private static void Main()
WorkingDirectory = appDir,
FileName = appPath,
};

var p = NauIpc.LaunchProcessAndSendDto(dto, info, syncProcessName);
var p = NauIpc.LaunchProcessAndSendDto(dto, info, syncProcessName, false);
if (p == null)
throw new UpdateProcessFailedException("Unable to relaunch application");
}
Expand Down

0 comments on commit bd5769f

Please sign in to comment.