using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections.Concurrent; namespace HPSocketCS { public class Extra { ConcurrentDictionary dict = new ConcurrentDictionary(); /// /// 获取附加数据 /// /// /// public T Get(IntPtr key) { T value; if (dict.TryGetValue(key, out value)) { return value; } return default(T); } /// /// 设置附加数据 /// /// /// /// /// public bool Set(IntPtr key, T newValue) { try { dict.AddOrUpdate(key, newValue, (tKey, existingVal) => { return newValue; }); return true; } catch (OverflowException) { // 字典数目超过int.max return false; } catch (ArgumentNullException) { // 参数为空 return false; } catch (Exception) { return false; } } /// /// 删除附加数据 /// /// /// public bool Remove(IntPtr key) { T value; return dict.TryRemove(key, out value); } } public class ConnectionExtra { ConcurrentDictionary dict = new ConcurrentDictionary(); /// /// 获取附加数据 /// /// /// public object GetExtra(IntPtr key) { object value; if (dict.TryGetValue(key, out value)) { return value; } return null; } /// /// 获取附加数据 /// /// /// public T GetExtra(IntPtr key) { object value; if (dict.TryGetValue(key, out value)) { return (T)value; } return default(T); } /// /// 设置附加数据 /// /// /// /// /// public bool SetExtra(IntPtr key, object newValue) { try { dict.AddOrUpdate(key, newValue, (tKey, existingVal) => { return newValue; }); return true; } catch (OverflowException) { // 字典数目超过int.max return false; } catch (ArgumentNullException) { // 参数为空 return false; } catch (Exception) { return false; } } /// /// 删除附加数据 /// /// /// public bool RemoveExtra(IntPtr key) { object value; return dict.TryRemove(key, out value); } } }