|
| 1 | +using System; |
| 2 | +using SuperSocket.Common; |
| 3 | +using SuperSocket.SocketBase.Protocol; |
| 4 | + |
| 5 | +namespace SuperSocket.SocketEngine.Protocol |
| 6 | +{ |
| 7 | + /// <summary> |
| 8 | + /// ReceiveFilter for the protocol that each request has bengin and end mark |
| 9 | + /// </summary> |
| 10 | + /// <typeparam name="TRequestInfo">The type of the request info.</typeparam> |
| 11 | + public abstract class BeginEndMarkReceiveFilter<TRequestInfo> : ReceiveFilterBase<TRequestInfo> |
| 12 | + where TRequestInfo : IRequestInfo |
| 13 | + { |
| 14 | + private readonly SearchMarkState<byte> m_BeginSearchState; |
| 15 | + private readonly SearchMarkState<byte> m_EndSearchState; |
| 16 | + |
| 17 | + private bool m_FoundBegin = false; |
| 18 | + |
| 19 | + /// <summary> |
| 20 | + /// Null request info |
| 21 | + /// </summary> |
| 22 | + protected TRequestInfo NullRequestInfo = default(TRequestInfo); |
| 23 | + |
| 24 | + /// <summary> |
| 25 | + /// Initializes a new instance of the <see cref="BeginEndMarkReceiveFilter<TRequestInfo>"/> class. |
| 26 | + /// </summary> |
| 27 | + /// <param name="beginMark">The begin mark.</param> |
| 28 | + /// <param name="endMark">The end mark.</param> |
| 29 | + protected BeginEndMarkReceiveFilter(byte[] beginMark, byte[] endMark) |
| 30 | + { |
| 31 | + m_BeginSearchState = new SearchMarkState<byte>(beginMark); |
| 32 | + m_EndSearchState = new SearchMarkState<byte>(endMark); |
| 33 | + } |
| 34 | + |
| 35 | + /// <summary> |
| 36 | + /// Filters the specified session. |
| 37 | + /// </summary> |
| 38 | + /// <param name="readBuffer">The read buffer.</param> |
| 39 | + /// <param name="offset">The offset.</param> |
| 40 | + /// <param name="length">The length.</param> |
| 41 | + /// <param name="toBeCopied">if set to <c>true</c> [to be copied].</param> |
| 42 | + /// <param name="rest">The rest.</param> |
| 43 | + /// <returns></returns> |
| 44 | + public override TRequestInfo Filter(byte[] readBuffer, int offset, int length, bool toBeCopied, out int rest) |
| 45 | + { |
| 46 | + rest = 0; |
| 47 | + |
| 48 | + int searchEndMarkOffset; |
| 49 | + int searchEndMarkLength; |
| 50 | + |
| 51 | + //prev macthed begin mark length |
| 52 | + int prevMatched = 0; |
| 53 | + int totalParsed = 0; |
| 54 | + |
| 55 | + if (!m_FoundBegin) |
| 56 | + { |
| 57 | + prevMatched = m_BeginSearchState.Matched; |
| 58 | + int pos = readBuffer.SearchMark(offset, length, m_BeginSearchState, out totalParsed); |
| 59 | + |
| 60 | + if (pos < 0) |
| 61 | + { |
| 62 | + //Don't cache invalid data |
| 63 | + if (prevMatched > 0 || (m_BeginSearchState.Matched > 0 && length != m_BeginSearchState.Matched)) |
| 64 | + { |
| 65 | + State = FilterState.Error; |
| 66 | + return NullRequestInfo; |
| 67 | + } |
| 68 | + |
| 69 | + return NullRequestInfo; |
| 70 | + } |
| 71 | + else //Found the matched begin mark |
| 72 | + { |
| 73 | + //But not at the beginning |
| 74 | + if(pos != offset) |
| 75 | + { |
| 76 | + State = FilterState.Error; |
| 77 | + return NullRequestInfo; |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + //Found start mark |
| 82 | + m_FoundBegin = true; |
| 83 | + |
| 84 | + searchEndMarkOffset = pos + m_BeginSearchState.Mark.Length - prevMatched; |
| 85 | + |
| 86 | + //This block only contain (part of)begin mark |
| 87 | + if (offset + length <= searchEndMarkOffset) |
| 88 | + { |
| 89 | + AddArraySegment(m_BeginSearchState.Mark, 0, m_BeginSearchState.Mark.Length, false); |
| 90 | + return NullRequestInfo; |
| 91 | + } |
| 92 | + |
| 93 | + searchEndMarkLength = offset + length - searchEndMarkOffset; |
| 94 | + } |
| 95 | + else//Already found begin mark |
| 96 | + { |
| 97 | + searchEndMarkOffset = offset; |
| 98 | + searchEndMarkLength = length; |
| 99 | + } |
| 100 | + |
| 101 | + while (true) |
| 102 | + { |
| 103 | + var prevEndMarkMatched = m_EndSearchState.Matched; |
| 104 | + var parsedLen = 0; |
| 105 | + var endPos = readBuffer.SearchMark(searchEndMarkOffset, searchEndMarkLength, m_EndSearchState, out parsedLen); |
| 106 | + |
| 107 | + //Haven't found end mark |
| 108 | + if (endPos < 0) |
| 109 | + { |
| 110 | + rest = 0; |
| 111 | + if(prevMatched > 0)//Also cache the prev matched begin mark |
| 112 | + AddArraySegment(m_BeginSearchState.Mark, 0, prevMatched, false); |
| 113 | + AddArraySegment(readBuffer, offset, length, toBeCopied); |
| 114 | + return NullRequestInfo; |
| 115 | + } |
| 116 | + |
| 117 | + totalParsed += parsedLen; |
| 118 | + rest = length - totalParsed; |
| 119 | + |
| 120 | + byte[] commandData = new byte[BufferSegments.Count + prevMatched + totalParsed]; |
| 121 | + |
| 122 | + if (BufferSegments.Count > 0) |
| 123 | + BufferSegments.CopyTo(commandData, 0, 0, BufferSegments.Count); |
| 124 | + |
| 125 | + if(prevMatched > 0) |
| 126 | + Array.Copy(m_BeginSearchState.Mark, 0, commandData, BufferSegments.Count, prevMatched); |
| 127 | + |
| 128 | + Array.Copy(readBuffer, offset, commandData, BufferSegments.Count + prevMatched, totalParsed); |
| 129 | + |
| 130 | + var requestInfo = ProcessMatchedRequest(commandData, 0, commandData.Length); |
| 131 | + |
| 132 | + if (!ReferenceEquals(requestInfo, NullRequestInfo)) |
| 133 | + { |
| 134 | + Reset(); |
| 135 | + return requestInfo; |
| 136 | + } |
| 137 | + |
| 138 | + if (rest > 0) |
| 139 | + { |
| 140 | + searchEndMarkOffset = endPos + m_EndSearchState.Mark.Length; |
| 141 | + searchEndMarkLength = rest; |
| 142 | + continue; |
| 143 | + } |
| 144 | + |
| 145 | + //Not match |
| 146 | + if(prevMatched > 0)//Also cache the prev matched begin mark |
| 147 | + AddArraySegment(m_BeginSearchState.Mark, 0, prevMatched, false); |
| 148 | + AddArraySegment(readBuffer, offset, length, toBeCopied); |
| 149 | + return NullRequestInfo; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + /// <summary> |
| 154 | + /// Processes the matched request. |
| 155 | + /// </summary> |
| 156 | + /// <param name="readBuffer">The read buffer.</param> |
| 157 | + /// <param name="offset">The offset.</param> |
| 158 | + /// <param name="length">The length.</param> |
| 159 | + /// <returns></returns> |
| 160 | + protected abstract TRequestInfo ProcessMatchedRequest(byte[] readBuffer, int offset, int length); |
| 161 | + |
| 162 | + /// <summary> |
| 163 | + /// Resets this instance. |
| 164 | + /// </summary> |
| 165 | + public override void Reset() |
| 166 | + { |
| 167 | + m_BeginSearchState.Matched = 0; |
| 168 | + m_EndSearchState.Matched = 0; |
| 169 | + m_FoundBegin = false; |
| 170 | + base.Reset(); |
| 171 | + } |
| 172 | + } |
| 173 | +} |
0 commit comments