Cleaned up the CDNCache threading even more.

--HG--
extra : convert_revision : svn%3A946a0da7-ebce-4904-9acb-2f1e67aed693%40143
This commit is contained in:
Ryan Stecker
2011-02-07 03:02:10 +00:00
parent 533ceb4f27
commit 557b32e677
4 changed files with 29 additions and 22 deletions

View File

@@ -3,6 +3,7 @@ using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Threading;
namespace Vapor
{
@@ -24,6 +25,8 @@ namespace Vapor
{
Steam3.Update();
Application.DoEvents();
Thread.Sleep( 1 ); // sue me, AzuiSleet.
}
Steam3.Shutdown();

View File

@@ -26,22 +26,21 @@ namespace Vapor
static Semaphore dlPool;
static object mapLock = new object();
static Dictionary<SteamID, byte[]> avatarMap;
static CDNCache()
{
dlPool = new Semaphore( 0, 3 );
dlPool.Release( 3 );
dlPool = new Semaphore( 0, 6 );
dlPool.Release( 6 );
avatarMap = new Dictionary<SteamID, byte[]>();
}
struct AvatarData
{
public SteamID friend;
public byte[] hash;
public string hashstr;
public string localFile;
public Action<AvatarDownloadDetails> callback;
}
@@ -55,19 +54,7 @@ namespace Vapor
public static void DownloadAvatar( SteamID steamId, byte[] avatarHash, Action<AvatarDownloadDetails> callBack )
{
ThreadPool.QueueUserWorkItem( DoDownload, new AvatarData() { friend = steamId, hash = avatarHash, callback = callBack } );
}
static void DoDownload( object state )
{
AvatarData data = ( AvatarData )state;
SteamID steamId = data.friend;
Action<AvatarDownloadDetails> callBack = data.callback;
byte[] avatarHash = data.hash;
lock ( mapLock )
avatarMap[ steamId ] = avatarHash;
avatarMap[ steamId ] = avatarHash;
string hashStr = BitConverter.ToString( avatarHash ).Replace( "-", "" ).ToLower();
string hashPrefix = hashStr.Substring( 0, 2 );
@@ -80,8 +67,9 @@ namespace Vapor
{
Directory.CreateDirectory( localPath ); // try making the cache directory
}
catch
catch ( Exception ex )
{
DebugLog.WriteLine( "CDNCache", "Unableto create cache directory.\n{0}", ex.ToString() );
callBack( new AvatarDownloadDetails() { Success = false, } );
return;
}
@@ -94,9 +82,23 @@ namespace Vapor
return;
}
ThreadPool.QueueUserWorkItem( DoDownload, new AvatarData() { callback = callBack, hashstr = hashStr, localFile = localFile } );
}
static void DoDownload( object state )
{
AvatarData data = ( AvatarData )state;
string hashStr = data.hashstr;
string hashPrefix = hashStr.Substring( 0, 2 );
string localPath = Path.Combine( Application.StartupPath, "cache" );
var callBack = data.callback;
dlPool.WaitOne();
string downloadUri = string.Format( AvatarRoot + AvatarSmall, hashPrefix, hashStr );
string localFile = Path.Combine( localPath, hashStr + ".jpg" );
using ( WebClient client = new WebClient() )
{
@@ -106,13 +108,15 @@ namespace Vapor
}
catch
{
dlPool.Release();
callBack( new AvatarDownloadDetails() { Success = false } );
return;
}
finally
{
dlPool.Release();
}
}
dlPool.Release();
callBack( new AvatarDownloadDetails() { Success = true, Filename = localFile } );
}
}

View File

@@ -51,7 +51,7 @@
<Compile Include="UI\ChatDialog.Designer.cs">
<DependentUpon>ChatDialog.cs</DependentUpon>
</Compile>
<Compile Include="ChatManager.cs" />
<Compile Include="Steam\ChatManager.cs" />
<Compile Include="UI\ChatTextBox.cs">
<SubType>Component</SubType>
</Compile>