IMsg.cs 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /****************************************************************************
  2. * Copyright (c) 2018 ~ 2021.1 liangxie
  3. *
  4. * http://qframework.io
  5. * https://github.com/liangxiegame/QFramework
  6. *
  7. * Permission is hereby granted, free of charge, to any person obtaining a copy
  8. * of this software and associated documentation files (the "Software"), to deal
  9. * in the Software without restriction, including without limitation the rights
  10. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11. * copies of the Software, and to permit persons to whom the Software is
  12. * furnished to do so, subject to the following conditions:
  13. *
  14. * The above copyright notice and this permission notice shall be included in
  15. * all copies or substantial portions of the Software.
  16. *
  17. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  23. * THE SOFTWARE.
  24. ****************************************************************************/
  25. using System;
  26. namespace QFramework
  27. {
  28. public interface IMsg
  29. {
  30. /// <summary>
  31. /// EventID
  32. /// </summary>
  33. int EventID { get; set; }
  34. /// <summary>
  35. /// Processed or not
  36. /// </summary>
  37. bool Processed { get; set; }
  38. /// <summary>
  39. /// reusable or not
  40. /// </summary>
  41. bool ReuseAble { get; set; }
  42. int ManagerID { get; }
  43. void Recycle2Cache();
  44. }
  45. /// <summary>
  46. /// msgbody
  47. /// </summary>
  48. public class QMsg : IMsg, IPoolable, IPoolType
  49. {
  50. /// <summary>
  51. /// EventID
  52. /// TODO: raname 2 Id
  53. /// </summary>
  54. public virtual int EventID { get; set; }
  55. /// <summary>
  56. /// Processed or not
  57. /// </summary>
  58. public bool Processed { get; set; }
  59. /// <summary>
  60. /// reusable or not
  61. /// </summary>
  62. public bool ReuseAble { get; set; }
  63. public int ManagerID
  64. {
  65. get { return EventID / QMsgSpan.Count * QMsgSpan.Count; }
  66. }
  67. public QMsg()
  68. {
  69. }
  70. #region Object Pool
  71. public static QMsg Allocate<T>(T eventId) where T : IConvertible
  72. {
  73. QMsg msg = SafeObjectPool<QMsg>.Instance.Allocate();
  74. msg.EventID = eventId.ToInt32(null);
  75. msg.ReuseAble = true;
  76. return msg;
  77. }
  78. public virtual void Recycle2Cache()
  79. {
  80. SafeObjectPool<QMsg>.Instance.Recycle(this);
  81. }
  82. void IPoolable.OnRecycled()
  83. {
  84. Processed = false;
  85. }
  86. bool IPoolable.IsRecycled { get; set; }
  87. #endregion
  88. #region deprecated since v0.0.5
  89. //[Obsolete("deprecated,use allocate instead")]
  90. public QMsg(int eventID)
  91. {
  92. EventID = eventID;
  93. }
  94. #endregion
  95. }
  96. }