/// <summary>
/// 资源下载类,使用webclient, by williams
/// </summary>
internal class DownloadHelper:IDisposable
{
#region 私有成员
IsolatedStorageUtil isu;
private WebClient client;
IGameCmd downloadEvent;
private SynchronizationContext ui;
private bool isThreadSingle=true;
System.Threading.Thread t;
private ManualResetEvent downloadDone = new ManualResetEvent(false);
private String[] filePaths;
private bool isDownLoadIng=false;
private DownLoadTypeEnum type = DownLoadTypeEnum.Map;
#endregion
internal DownloadHelper(IGameCmd up)
{
isu = new IsolatedStorageUtil();
client = new WebClient();
client.AllowReadStreamBuffering = true;
ui = System.Threading.SynchronizationContext.Current;
downloadEvent = up;
}
/// <summary>
/// 批量异步下载多个文件,效果需要验证
/// </summary>
/// <param name="filepaths"></param>
internal void DownloadMultiFile(String[] filepaths,DownLoadTypeEnum type)
{
filePaths = filepaths;
t = new Thread(DownloadFilesByWhile);
t.IsBackground = true;
t.Start();
isThreadSingle = false;
this.type = type;
}
private void DownloadFilesByWhile()
{
foreach (string filePath in filePaths)
{
if (isu.FileExist(filePath))
{
continue;
}
if (!client.IsBusy)
{
downloadDone.Reset();
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(new Uri(filePath, UriKind.Relative), filePath);
downloadDone.WaitOne();
}
}
byte type =Convert.ToByte(GameCmdEnums.GetFileOk);
byte flag = Convert.ToByte(this.type);
byte[] content = System.Text.UTF8Encoding.UTF8.GetBytes("文件全部获取完毕");
ui.Post(downloadEvent.DownloadFilesComplete, new Message { Class=type, Flag=flag, Content=content, Size=content.Length });
}
/// <summary>
/// 下载指定文件
/// </summary>
/// <param name="filePath"></param>
/// <returns></returns>
internal bool DownloadFile(string filePath)
{
if (!string.IsNullOrEmpty(filePath))
{
if (!client.IsBusy)
{
client.DownloadProgressChanged += new DownloadProgressChangedEventHandler(client_DownloadProgressChanged);
client.OpenReadCompleted += new OpenReadCompletedEventHandler(client_OpenReadCompleted);
client.OpenReadAsync(new Uri(filePath, UriKind.Relative), filePath);
return true;
}
else
{
return false;
}
}
else
{
return false;
}
}
/// <summary>
/// 下载进度发生变化时
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void client_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
string mess = "下载:" + e.UserState.ToString() + "中... " + e.ProgressPercentage.ToString() + "%";
if (e.ProgressPercentage == 100)
{
mess = "下载:" + e.UserState.ToString() + "完成";
}
ui.Post(downloadEvent.DownloadProgressChange, mess);
}
/// <summary>
/// 文件下载完成后,储存进独立存储区域
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
void client_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e)
{
Stream fs = e.Result as Stream;
if (fs != null)
{
long filezise = fs.Length;
ui.Post(downloadEvent.DownloadComplete, e);
if (filezise < isu.availableFreeSpace)//独立存储区域的初始大小是1M
{
SaveInIsolatedSpace(fs, e.UserState.ToString());
}
else
{
ui.Post(this.downloadEvent.DownloadError, "存储空间不够");
}
}
else
{
ui.Post(this.downloadEvent.DownloadError, "文件读取失败");
}
if (!isThreadSingle)
{
downloadDone.Set();
}
}
/// <summary>
/// 把文件存入独立储存区里
/// </summary>
/// <param name="fs"></param>
/// <param name="filename"></param>
void SaveInIsolatedSpace(Stream fs, string filename)
{
try
{
if (!isu.FileExist(filename))
{
Stream newfs = isu.CreateFile(filename);//事先把地图文件名存储在 e 里。
newfs.Close();
}
isu.WriteToFile(filename, fs);
}
catch(IsolatedStorageException ex)
{
ui.Post(downloadEvent.DownloadError,"文件保存失败");
}
catch
{
ui.Post(downloadEvent.DownloadError, "文件保存失败");
}
}
#region IDisposable 成员
public void Dispose()
{
if (t != null)
{
t.Abort();
}
}
#endregion