public bool LogTimeout( [EventLogMethodParameter(Title = "Session Id", OrdinalInStruct = 0, TypeInStruct = "ushort")] uint sessionId, [EventLogMethodParameter(Title = "Session State", OrdinalInStruct = 1, TypeInStruct = "byte")] TcpStateEnum sessionState, [EventLogMethodParameter(Title = "Timeout Type", OrdinalInStruct = 2, TypeInStruct = "byte")] TcpSession.TcpTimeoutType timeoutType) { if ((debugOptions & LogTimeouts /* To Debugger */) != 0) { Core.Log("TCP: Ses{0,3} ({1}) Timeout of type '{2}' occurred", sessionId, TcpState.TcpStateNames[(uint)sessionState], TcpSession.TcpTimeoutTypeNames[(uint)timeoutType]); } if ((ControlFlags & LogTimeouts) != 0) { TimeoutEntry timeoutEntry = new TimeoutEntry(sessionId, sessionState, timeoutType); unsafe { return(LogEntry(ControlFlags, timeoutTypeHandle, (byte *)&timeoutEntry, sizeof(TimeoutEntry)) != 0); } } return(false); }
public long CreateTimeout(float time, Action <long, object> callback, object param = null) { var entry = new TimeoutEntry(time, callback, param); m_TimeoutEntryDict[entry.id] = entry; m_WaitCheck = true; return(entry.id); }
/// <summary> /// Attempts to get a value from cache. /// If it is found in cache, return true, and the value. /// If it is not found in cache, return false, and use the valueFactory callback to generate and return the value. /// </summary> /// <param name="key">The key.</param> /// <param name="value">The value.</param> /// <param name="valueFactory">A callback that can create the value.</param> /// <returns> /// True if the value came from cache, otherwise false. /// </returns> /// <exception cref="System.ArgumentNullException">valueFactory</exception> public bool TryGetOrAdd(TKey key, out TValue value, Func <TKey, TValue> valueFactory) { if (valueFactory == null) { throw new ArgumentNullException(nameof(valueFactory)); } Func <TKey, TimeoutEntry> callback = key1 => { TValue newValue = valueFactory(key1); var newEntry = TimeoutEntry.Create(newValue); return(newEntry); }; TimeoutEntry entry = null; bool isValid = false; bool fromCache = false; while (!isValid) { fromCache = _cache.TryGetOrAdd(key, out entry, callback); ///// // Check time stamp ///// if (fromCache) { isValid = IsValid(entry); if (!isValid) { Remove(key); } } else { isValid = true; } } value = entry.Value; return(fromCache); }
private void CheckTimeout() { CancelInvoke("SetTimeout"); m_WaitCheck = false; m_TimeoutEntryHashs.Clear(); var now = DateTime.Now; float minTime = 0f; TimeoutEntry minEntry = null; foreach (var entry in m_TimeoutEntryDict.Values) { float deltaTime = (float)(now - entry.dateTime).TotalMilliseconds / 1000f; if (deltaTime >= entry.timeout) { m_TimeoutEntryHashs.Add(entry); } else { deltaTime = entry.timeout - deltaTime; if (minEntry == null || deltaTime < minTime) { minTime = deltaTime; minEntry = entry; } } } foreach (var entry in m_TimeoutEntryHashs) { m_TimeoutEntryDict.Remove(entry.id); entry.OnTimeout(); } if (minEntry != null) { Invoke("SetTimeout", minTime); } }
/// <summary> /// Is the entry still valid. /// </summary> /// <param name="entry"></param> /// <returns></returns> private bool IsValid(TimeoutEntry entry) { return(entry.CreationTime + _expirationInterval > DateTime.UtcNow.Ticks); }
/// <summary> /// Adds or updates the specified key/value pair. /// </summary> /// <param name="key">The key.</param> /// <param name="value">The value.</param> /// <returns> /// True if the specified key/value pair was added; /// False if the specified key/value pair was updated. /// </returns> public bool Add(TKey key, TValue value) { return(_cache.Add(key, TimeoutEntry.Create(value))); }