using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SuperSocket.Common;
namespace SuperSocket.SocketBase.Protocol
{
///
/// Receive filter base class
///
/// The type of the request info.
public abstract class ReceiveFilterBase : IReceiveFilter
where TRequestInfo : IRequestInfo
{
private ArraySegmentList m_BufferSegments;
///
/// Gets the buffer segments which can help you parse your request info conviniently.
///
protected ArraySegmentList BufferSegments
{
get { return m_BufferSegments; }
}
///
/// Initializes a new instance of the class.
///
protected ReceiveFilterBase()
{
m_BufferSegments = new ArraySegmentList();
}
///
/// Initializes a new instance of the class.
///
/// The previous Receive filter.
protected ReceiveFilterBase(ReceiveFilterBase previousRequestFilter)
{
Initialize(previousRequestFilter);
}
///
/// Initializes the specified previous Receive filter.
///
/// The previous Receive filter.
public void Initialize(ReceiveFilterBase previousRequestFilter)
{
m_BufferSegments = previousRequestFilter.BufferSegments;
}
#region IReceiveFilter Members
///
/// Filters received data of the specific session into request info.
///
/// The read buffer.
/// The offset of the current received data in this read buffer.
/// The length of the current received data.
/// if set to true [to be copied].
/// The rest, the length of the data which hasn't been parsed.
///
public abstract TRequestInfo Filter(byte[] readBuffer, int offset, int length, bool toBeCopied, out int rest);
///
/// Gets the size of the rest buffer.
///
///
/// The size of the rest buffer.
///
public int LeftBufferSize
{
get { return m_BufferSegments.Count; }
}
///
/// Gets or sets the next Receive filter.
///
///
/// The next Receive filter.
///
public IReceiveFilter NextReceiveFilter { get; protected set; }
#endregion
///
/// Adds the array segment.
///
/// The buffer.
/// The offset.
/// The length.
/// if set to true [to be copied].
protected void AddArraySegment(byte[] buffer, int offset, int length, bool toBeCopied)
{
m_BufferSegments.AddSegment(buffer, offset, length, toBeCopied);
}
///
/// Clears the buffer segments.
///
protected void ClearBufferSegments()
{
m_BufferSegments.ClearSegements();
}
///
/// Resets this instance to initial state.
///
public virtual void Reset()
{
if(m_BufferSegments != null && m_BufferSegments.Count > 0)
m_BufferSegments.ClearSegements();
}
///
/// Gets the filter state.
///
///
/// The state.
///
public FilterState State { get; protected set; }
}
}