MessagePool.cs 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. using System;
  2. #if !NOT_UNITY
  3. using System.Collections.Generic;
  4. #endif
  5. namespace ET
  6. {
  7. // 客户端为了0GC需要消息池,服务端消息需要跨协程不需要消息池
  8. public class MessagePool
  9. {
  10. public static MessagePool Instance
  11. {
  12. get;
  13. } = new MessagePool();
  14. #if !NOT_UNITY
  15. private readonly Dictionary<Type, Queue<object>> dictionary = new Dictionary<Type, Queue<object>>();
  16. #endif
  17. public object Fetch(Type type)
  18. {
  19. //Queue<object> queue;
  20. //if (!this.dictionary.TryGetValue(type, out queue))
  21. //{
  22. // queue = new Queue<object>();
  23. // this.dictionary.Add(type, queue);
  24. //}
  25. //
  26. //object obj;
  27. //if (queue.Count > 0)
  28. //{
  29. // obj = queue.Dequeue();
  30. //}
  31. //else
  32. //{
  33. // obj = Activator.CreateInstance(type);
  34. //}
  35. //return obj;
  36. return Activator.CreateInstance(type);
  37. }
  38. public T Fetch<T>() where T : class
  39. {
  40. T t = (T) this.Fetch(typeof (T));
  41. return t;
  42. }
  43. public void Recycle(object obj)
  44. {
  45. /*
  46. #if !NOT_CLIENT
  47. Type type = obj.GetType();
  48. Queue<object> queue;
  49. if (!this.dictionary.TryGetValue(type, out queue))
  50. {
  51. queue = new Queue<object>();
  52. this.dictionary.Add(type, queue);
  53. }
  54. queue.Enqueue(obj);
  55. #endif
  56. */
  57. }
  58. }
  59. }