private bool CheckTradeTimeout(Trade trade) { // User has accepted the trade. Disregard time out. if (trade.OtherUserAccepted) return false; var now = DateTime.Now; DateTime actionTimeout = lastOtherActionTime.AddSeconds(MaxActionGapSec); int untilActionTimeout = (int)Math.Round((actionTimeout - now).TotalSeconds); DebugPrint(String.Format("{0} {1}", actionTimeout, untilActionTimeout)); DateTime tradeTimeout = tradeStartTime.AddSeconds(MaxTradeTimeSec); int untilTradeTimeout = (int)Math.Round((tradeTimeout - now).TotalSeconds); double secsSinceLastTimeoutMessage = (now - lastTimeoutMessage).TotalSeconds; if (untilActionTimeout <= 0 || untilTradeTimeout <= 0) { DebugPrint("timed out..."); if (OnTimeout != null) { trade.EnqueueAction(() => OnTimeout(this, null)); } trade.EnqueueAction(() => trade.CancelTrade()); return true; } else if (untilActionTimeout <= 20 && secsSinceLastTimeoutMessage >= 10) { trade.EnqueueAction(() => { trade.SendMessage("Are You AFK? The trade will be canceled in " + untilActionTimeout + " seconds if you don't do something."); lastTimeoutMessage = now; }); } return false; }
/// <summary> /// Starts the actual trade-polling thread. /// </summary> public void StartTradeThread(Trade trade) { // initialize data to use in thread tradeStartTime = DateTime.Now; lastOtherActionTime = DateTime.Now; lastTimeoutMessage = DateTime.Now.AddSeconds(-1000); var pollThread = new Thread(() => { IsTradeThreadRunning = true; DebugPrint("Trade thread starting."); // main thread loop for polling try { while (IsTradeThreadRunning) { bool action = trade.Poll(); if (action) { lastOtherActionTime = DateTime.Now; } if (trade.OtherUserCancelled || trade.HasTradeCompletedOk) { IsTradeThreadRunning = false; break; } Thread.Sleep(TradePollingInterval); } } catch (Exception ex) { // TODO: find a new way to do this w/o the trade events //if (OnError != null) // OnError("Error Polling Trade: " + e); // ok then we should stop polling... IsTradeThreadRunning = false; DebugPrint("[TRADEMANAGER] general error caught: " + ex); trade.EnqueueAction(() => trade.FireOnErrorEvent("Unknown error occurred: " + ex.ToString())); } finally { DebugPrint("Trade thread shutting down."); try { try //Yikes, that's a lot of nested 'try's. Is there some way to clean this up? { if (trade.HasTradeCompletedOk) { trade.EnqueueAction(() => trade.FireOnSuccessEvent()); } } finally { //Make sure OnClose is always fired after OnSuccess, even if OnSuccess throws an exception //(which it NEVER should, but...) trade.EnqueueAction(() => trade.FireOnCloseEvent()); } } catch (Exception ex) { trade.EnqueueAction(() => trade.FireOnErrorEvent("Unknown error occurred DURING CLEANUP(!?): " + ex.ToString())); } } }); pollThread.Start(); }
/// <summary> /// Starts the actual trade-polling thread. /// </summary> public void StartTradeThread(Trade trade) { // initialize data to use in thread tradeStartTime = DateTime.Now; lastOtherActionTime = DateTime.Now; lastTimeoutMessage = DateTime.Now.AddSeconds(-1000); var pollThread = new Thread(() => { IsTradeThreadRunning = true; DebugPrint("Trade thread starting."); // main thread loop for polling try { while (IsTradeThreadRunning) { bool action = trade.Poll(); if (action) lastOtherActionTime = DateTime.Now; if (trade.OtherUserCancelled || trade.HasTradeCompletedOk) { IsTradeThreadRunning = false; break; } Thread.Sleep(TradePollingInterval); } } catch (Exception ex) { // TODO: find a new way to do this w/o the trade events //if (OnError != null) // OnError("Error Polling Trade: " + e); // ok then we should stop polling... IsTradeThreadRunning = false; DebugPrint("[TRADEMANAGER] general error caught: " + ex); trade.EnqueueAction(() => trade.FireOnErrorEvent("Unknown error occurred: " + ex.ToString())); } finally { DebugPrint("Trade thread shutting down."); try { try //Yikes, that's a lot of nested 'try's. Is there some way to clean this up? { if (trade.HasTradeCompletedOk) trade.EnqueueAction(() => trade.FireOnSuccessEvent()); } finally { //Make sure OnClose is always fired after OnSuccess, even if OnSuccess throws an exception //(which it NEVER should, but...) trade.EnqueueAction(() => trade.FireOnCloseEvent()); } } catch (Exception ex) { trade.EnqueueAction(() => trade.FireOnErrorEvent("Unknown error occurred DURING CLEANUP(!?): " + ex.ToString())); } } }); pollThread.Start(); }