public void Add(Guid linkedAccountID, bool needSaveMappedOrderId, bool isLocal, XmlNode tran, Hashtable orderIdToMappedOrderId) { lock (lockObj) { if (!this.isStarted) { throw new InvalidOperationException("Please call function Start first"); } MappedTran mappedTran = new MappedTran(this.lastSequence++, linkedAccountID, needSaveMappedOrderId, isLocal, tran, orderIdToMappedOrderId); if (bookTranThread == null) { this.StartBookTranThread(); } int retryCount = 0; SaveMappedTranResult result = this.Save(mappedTran); while (result == SaveMappedTranResult.FileAlreadyExist) { AppDebug.LogEvent("StateServer", string.Format("File alrady exist while save map order {0}", mappedTran.ToXmlString()), System.Diagnostics.EventLogEntryType.Warning); Thread.Sleep(10); this.mappedTrans.Clear(); this.Load(); mappedTran.Sequence = this.lastSequence++; result = this.Save(mappedTran); if (retryCount++ > 3) { AppDebug.LogEvent("StateServer", string.Format("Save map order in backup file failed: {0}", mappedTran.ToXmlString()), System.Diagnostics.EventLogEntryType.Warning); break; } } mappedTrans.AddLast(mappedTran); this.tranNeedToBookEvent.Set(); } }
private SaveMappedTranResult Save(MappedTran tran) { SaveMappedTranResult result = SaveMappedTranResult.OK; try { string fileName = Path.Combine(this.cachePath, string.Format("{0}.tan", tran.Sequence)); using (FileStream stream = new FileStream(fileName, FileMode.CreateNew, FileAccess.Write, FileShare.Read, 4096, FileOptions.WriteThrough)) { using (StreamWriter writer = new StreamWriter(stream)) { writer.Write(tran.ToXmlString()); } } } catch (Exception ex) { result = (ex is IOException && ex.ToString().Contains("already exists")) ? SaveMappedTranResult.FileAlreadyExist : SaveMappedTranResult.Error; AppDebug.LogEvent("StateServer", ex.ToString(), System.Diagnostics.EventLogEntryType.Error); } return(result); }