ReceiveFilterBase.cs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using SuperSocket.Common;
  6. namespace SuperSocket.SocketBase.Protocol
  7. {
  8. /// <summary>
  9. /// Receive filter base class
  10. /// </summary>
  11. /// <typeparam name="TRequestInfo">The type of the request info.</typeparam>
  12. public abstract class ReceiveFilterBase<TRequestInfo> : IReceiveFilter<TRequestInfo>
  13. where TRequestInfo : IRequestInfo
  14. {
  15. private ArraySegmentList m_BufferSegments;
  16. /// <summary>
  17. /// Gets the buffer segments which can help you parse your request info conviniently.
  18. /// </summary>
  19. protected ArraySegmentList BufferSegments
  20. {
  21. get { return m_BufferSegments; }
  22. }
  23. /// <summary>
  24. /// Initializes a new instance of the <see cref="ReceiveFilterBase&lt;TRequestInfo&gt;"/> class.
  25. /// </summary>
  26. protected ReceiveFilterBase()
  27. {
  28. m_BufferSegments = new ArraySegmentList();
  29. }
  30. /// <summary>
  31. /// Initializes a new instance of the <see cref="ReceiveFilterBase&lt;TRequestInfo&gt;"/> class.
  32. /// </summary>
  33. /// <param name="previousRequestFilter">The previous Receive filter.</param>
  34. protected ReceiveFilterBase(ReceiveFilterBase<TRequestInfo> previousRequestFilter)
  35. {
  36. Initialize(previousRequestFilter);
  37. }
  38. /// <summary>
  39. /// Initializes the specified previous Receive filter.
  40. /// </summary>
  41. /// <param name="previousRequestFilter">The previous Receive filter.</param>
  42. public void Initialize(ReceiveFilterBase<TRequestInfo> previousRequestFilter)
  43. {
  44. m_BufferSegments = previousRequestFilter.BufferSegments;
  45. }
  46. #region IReceiveFilter<TRequestInfo> Members
  47. /// <summary>
  48. /// Filters received data of the specific session into request info.
  49. /// </summary>
  50. /// <param name="readBuffer">The read buffer.</param>
  51. /// <param name="offset">The offset of the current received data in this read buffer.</param>
  52. /// <param name="length">The length of the current received data.</param>
  53. /// <param name="toBeCopied">if set to <c>true</c> [to be copied].</param>
  54. /// <param name="rest">The rest, the length of the data which hasn't been parsed.</param>
  55. /// <returns></returns>
  56. public abstract TRequestInfo Filter(byte[] readBuffer, int offset, int length, bool toBeCopied, out int rest);
  57. /// <summary>
  58. /// Gets the size of the rest buffer.
  59. /// </summary>
  60. /// <value>
  61. /// The size of the rest buffer.
  62. /// </value>
  63. public int LeftBufferSize
  64. {
  65. get { return m_BufferSegments.Count; }
  66. }
  67. /// <summary>
  68. /// Gets or sets the next Receive filter.
  69. /// </summary>
  70. /// <value>
  71. /// The next Receive filter.
  72. /// </value>
  73. public IReceiveFilter<TRequestInfo> NextReceiveFilter { get; protected set; }
  74. #endregion
  75. /// <summary>
  76. /// Adds the array segment.
  77. /// </summary>
  78. /// <param name="buffer">The buffer.</param>
  79. /// <param name="offset">The offset.</param>
  80. /// <param name="length">The length.</param>
  81. /// <param name="toBeCopied">if set to <c>true</c> [to be copied].</param>
  82. protected void AddArraySegment(byte[] buffer, int offset, int length, bool toBeCopied)
  83. {
  84. m_BufferSegments.AddSegment(buffer, offset, length, toBeCopied);
  85. }
  86. /// <summary>
  87. /// Clears the buffer segments.
  88. /// </summary>
  89. protected void ClearBufferSegments()
  90. {
  91. m_BufferSegments.ClearSegements();
  92. }
  93. /// <summary>
  94. /// Resets this instance to initial state.
  95. /// </summary>
  96. public virtual void Reset()
  97. {
  98. if(m_BufferSegments != null && m_BufferSegments.Count > 0)
  99. m_BufferSegments.ClearSegements();
  100. }
  101. /// <summary>
  102. /// Gets the filter state.
  103. /// </summary>
  104. /// <value>
  105. /// The state.
  106. /// </value>
  107. public FilterState State { get; protected set; }
  108. }
  109. }