-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
连天
committed
May 18, 2014
1 parent
0ee2b95
commit 8ce57c9
Showing
218 changed files
with
14,905 additions
and
454 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
176 changes: 176 additions & 0 deletions
176
Bounty Compilation/Plugins/Trinity/Cache/ItemStashSellAppender.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,176 @@ | ||
using System; | ||
using System.Collections.Concurrent; | ||
using System.IO; | ||
using System.Text; | ||
using System.Threading; | ||
using Trinity.Technicals; | ||
|
||
namespace Trinity.Cache | ||
{ | ||
public class ItemStashSellAppender : IDisposable | ||
{ | ||
bool _headerChecked; | ||
public ItemStashSellAppender() | ||
{ | ||
_logItemQueue = new ConcurrentQueue<string>(); | ||
|
||
_itemLogPath = Path.Combine(FileManager.TrinityLogsPath, "StashSellSalvage.csv"); | ||
|
||
CheckHeader(); | ||
|
||
_queueThread = new Thread(QueueWorker) | ||
{ | ||
Name = "StashSellSalvageWorker", | ||
IsBackground = true, | ||
Priority = ThreadPriority.Lowest | ||
}; | ||
_queueThread.Start(); | ||
|
||
} | ||
|
||
private void CheckHeader() | ||
{ | ||
if (_headerChecked) | ||
return; | ||
|
||
bool writeHeader = !File.Exists(_itemLogPath); | ||
|
||
if (writeHeader) | ||
{ | ||
_logItemQueue.Enqueue("ActorSNO,Name,InternalName,DBBaseType,DBItemType,TBaseType,TItemType,Quality,Level,Action,Stats\n"); | ||
} | ||
_headerChecked = true; | ||
} | ||
public void Dispose() | ||
{ | ||
try | ||
{ | ||
if (_queueThread != null) | ||
_queueThread.Abort(); | ||
} | ||
catch { } | ||
_queueThread = null; | ||
} | ||
|
||
private readonly Mutex _mutex = new Mutex(false, "ItemStashedMutex"); | ||
|
||
private static ItemStashSellAppender _instance; | ||
public static ItemStashSellAppender Instance { get { return _instance ?? (_instance = new ItemStashSellAppender()); } } | ||
|
||
private StreamWriter _logWriter; | ||
private FileStream _fileStream; | ||
|
||
private readonly ConcurrentQueue<string> _logItemQueue; | ||
|
||
private Thread _queueThread; | ||
private readonly string _itemLogPath; | ||
|
||
internal void AppendItem(CachedACDItem item, string action) | ||
{ | ||
StringBuilder sb = new StringBuilder(); | ||
|
||
sb.Append(FormatCSVField(item.AcdItem.ActorSNO)); | ||
sb.Append(FormatCSVField(item.RealName)); | ||
sb.Append(FormatCSVField(item.InternalName)); | ||
sb.Append(FormatCSVField(item.DBBaseType.ToString())); | ||
sb.Append(FormatCSVField(item.DBItemType.ToString())); | ||
sb.Append(FormatCSVField(item.TrinityItemBaseType.ToString())); | ||
sb.Append(FormatCSVField(item.TrinityItemType.ToString())); | ||
sb.Append(FormatCSVField(item.Quality.ToString())); | ||
sb.Append(FormatCSVField(item.Level)); | ||
sb.Append(FormatCSVField(action)); | ||
var stats = item.AcdItem.Stats.ToString(); | ||
stats = stats.Replace("\r\n\t", " ").Replace(" - ", ": "); | ||
sb.Append(FormatCSVField(stats)); | ||
sb.Append("\n"); | ||
|
||
_logItemQueue.Enqueue(sb.ToString()); | ||
|
||
} | ||
|
||
private void QueueWorker() | ||
{ | ||
const int bufferSize = 65536; | ||
const int maxAttempts = 50; | ||
|
||
while (true) | ||
{ | ||
try | ||
{ | ||
CheckHeader(); | ||
|
||
if (_fileStream == null) | ||
_fileStream = File.Open(_itemLogPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.ReadWrite); | ||
|
||
if (_logWriter == null) | ||
_logWriter = new StreamWriter(_fileStream, Encoding.UTF8, bufferSize); | ||
|
||
string queueItem; | ||
while (_logItemQueue.TryDequeue(out queueItem)) | ||
{ | ||
bool success = false; | ||
int attempts = 0; | ||
while (!string.IsNullOrWhiteSpace(queueItem) && !success && attempts <= maxAttempts) | ||
{ | ||
try | ||
{ | ||
_mutex.WaitOne(); | ||
|
||
attempts++; | ||
_logWriter.Write(queueItem); | ||
_logWriter.Flush(); | ||
_fileStream.Flush(); | ||
success = true; | ||
queueItem = ""; | ||
|
||
} | ||
catch (Exception ex) | ||
{ | ||
Logger.LogError("Error in StashSellSalvage QueueWorker: " + ex.Message); | ||
} | ||
finally | ||
{ | ||
_mutex.ReleaseMutex(); | ||
} | ||
} | ||
} | ||
} | ||
catch (ThreadAbortException) | ||
{ | ||
// ssh... | ||
} | ||
catch (Exception ex) | ||
{ | ||
Logger.LogError("Error in StashSellSalvage QueueWorker: " + ex.Message); | ||
} | ||
|
||
Thread.Sleep(10); | ||
} | ||
} | ||
|
||
private static string FormatCSVField(DateTime time) | ||
{ | ||
return String.Format("\"{0:yyyy-MM-ddTHH:mm:ss.ffff}\",", time.ToLocalTime()); | ||
} | ||
|
||
private static string FormatCSVField(string text) | ||
{ | ||
return String.Format("\"{0}\",", text); | ||
} | ||
|
||
private static string FormatCSVField(int number) | ||
{ | ||
return String.Format("\"{0}\",", number); | ||
} | ||
|
||
private static string FormatCSVField(double number) | ||
{ | ||
return String.Format("\"{0:0}\",", number); | ||
} | ||
|
||
private static string FormatCSVField(bool value) | ||
{ | ||
return String.Format("\"{0}\",", value); | ||
} | ||
} | ||
} |
Oops, something went wrong.