博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SuperSocket内置的命令行协议的解析
阅读量:5835 次
发布时间:2019-06-18

本文共 8049 字,大约阅读时间需要 26 分钟。

SuperSocket\SocketBase\Protocol\TerminatorReceiveFilter.cs

///         /// 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. ///
return the parsed TRequestInfo
public override TRequestInfo Filter(byte[] readBuffer, int offset, int length, bool toBeCopied, out int rest) { rest = 0; int prevMatched = m_SearchState.Matched; int result = readBuffer.SearchMark(offset, length, m_SearchState); if (result < 0) { if (m_OffsetDelta != m_ParsedLengthInBuffer) { Buffer.BlockCopy(readBuffer, offset - m_ParsedLengthInBuffer, readBuffer, offset - m_OffsetDelta, m_ParsedLengthInBuffer + length); m_ParsedLengthInBuffer += length; m_OffsetDelta = m_ParsedLengthInBuffer; } else { m_ParsedLengthInBuffer += length; if (m_ParsedLengthInBuffer >= m_Session.Config.ReceiveBufferSize) { this.AddArraySegment(readBuffer, offset + length - m_ParsedLengthInBuffer, m_ParsedLengthInBuffer, toBeCopied); m_ParsedLengthInBuffer = 0; m_OffsetDelta = 0; return NullRequestInfo; } m_OffsetDelta += length; } return NullRequestInfo; } var findLen = result - offset; var currentMatched = m_SearchState.Mark.Length - prevMatched; //The prev matched part is not belong to the current matched terminator mark if (prevMatched > 0 && findLen != 0) { //rest prevMatched to 0 prevMatched = 0; currentMatched = m_SearchState.Mark.Length; } rest = length - findLen - currentMatched; TRequestInfo requestInfo; if (findLen > 0) { if(this.BufferSegments != null && this.BufferSegments.Count > 0) { this.AddArraySegment(readBuffer, offset - m_ParsedLengthInBuffer, findLen + m_ParsedLengthInBuffer, toBeCopied); requestInfo = ProcessMatchedRequest(BufferSegments, 0, BufferSegments.Count); } else { requestInfo = ProcessMatchedRequest(readBuffer, offset - m_ParsedLengthInBuffer, findLen + m_ParsedLengthInBuffer); } } else if (prevMatched > 0) { if (m_ParsedLengthInBuffer > 0) { if (m_ParsedLengthInBuffer < prevMatched) { BufferSegments.TrimEnd(prevMatched - m_ParsedLengthInBuffer); requestInfo = ProcessMatchedRequest(BufferSegments, 0, BufferSegments.Count); } else { if (this.BufferSegments != null && this.BufferSegments.Count > 0) { this.AddArraySegment(readBuffer, offset - m_ParsedLengthInBuffer, m_ParsedLengthInBuffer - prevMatched, toBeCopied); requestInfo = ProcessMatchedRequest(BufferSegments, 0, BufferSegments.Count); } else { requestInfo = ProcessMatchedRequest(readBuffer, offset - m_ParsedLengthInBuffer, m_ParsedLengthInBuffer - prevMatched); } } } else { BufferSegments.TrimEnd(prevMatched); requestInfo = ProcessMatchedRequest(BufferSegments, 0, BufferSegments.Count); } } else { if (this.BufferSegments != null && this.BufferSegments.Count > 0) { if (m_ParsedLengthInBuffer > 0) { this.BufferSegments.AddSegment(readBuffer, offset, m_ParsedLengthInBuffer); } requestInfo = ProcessMatchedRequest(BufferSegments, 0, BufferSegments.Count); } else {
//在这里解析的时候,返回类型 requestInfo = ProcessMatchedRequest(readBuffer, offset - m_ParsedLengthInBuffer, m_ParsedLengthInBuffer); } } InternalReset(); if(rest == 0) { m_OffsetDelta = 0; } else { m_OffsetDelta += (length - rest); } return requestInfo; }

 

protected override StringRequestInfo ProcessMatchedRequest(byte[] data, int offset, int length)        {            if(length == 0)                return m_RequestParser.ParseRequestInfo(string.Empty);            return m_RequestParser.ParseRequestInfo(m_Encoding.GetString(data, offset, length));        }

 

 

SuperSocket\SocketBase\Protocol\TerminatorReceiveFilter.cs

TerminatorReceiveFilter.cs文件包含2个类

public abstract class TerminatorReceiveFilter
: ReceiveFilterBase
, IOffsetAdapter, IReceiveFilterInitializer where TRequestInfo : IRequestInfo

 

public class TerminatorReceiveFilter : TerminatorReceiveFilter

 

SuperSocket\SocketBase\Protocol\StringRequestInfo.cs

public class StringRequestInfo : RequestInfo

 

 

AppSession在初始化的时候,指定了ReceiveFilter

SuperSocket\SocketBase\AppSession.cs

///         /// Initializes the specified app session by AppServer and SocketSession.        ///         /// The app server.        /// The socket session.        public virtual void Initialize(IAppServer
appServer, ISocketSession socketSession) { var castedAppServer = (AppServerBase
)appServer; AppServer = castedAppServer; Charset = castedAppServer.TextEncoding; SocketSession = socketSession; SessionID = socketSession.SessionID; m_Connected = true; m_ReceiveFilter = castedAppServer.ReceiveFilterFactory.CreateFilter(appServer, this, socketSession.RemoteEndPoint); var filterInitializer = m_ReceiveFilter as IReceiveFilterInitializer; if (filterInitializer != null) filterInitializer.Initialize(castedAppServer, this); socketSession.Initialize(this); OnInit(); }

 

F:\CodeForGitBlit\SuperSocket\SocketBase\AppServer.cs

internal override IReceiveFilterFactory
CreateDefaultReceiveFilterFactory() { return new CommandLineReceiveFilterFactory(TextEncoding); }

F:\CodeForGitBlit\SuperSocket\SocketBase\Protocol\CommandLineReceiveFilterFactory.cs

public class CommandLineReceiveFilterFactory : TerminatorReceiveFilterFactory

 

 

 return m_RequestParser.ParseRequestInfo(m_Encoding.GetString(data, offset, length));

F:\CodeForGitBlit\SuperSocket\SocketBase\Protocol\BasicRequestInfoParser.cs

public class BasicRequestInfoParser : IRequestInfoParser

 

public StringRequestInfo ParseRequestInfo(string source)        {            int pos = source.IndexOf(m_Spliter);            string name = string.Empty;            string param = string.Empty;            if (pos > 0)            {                name = source.Substring(0, pos);                param = source.Substring(pos + 1);            }            else            {                name = source;            }            return new StringRequestInfo(name, param,                param.Split(m_ParameterSpliters, StringSplitOptions.RemoveEmptyEntries));        }

 

转载地址:http://gxucx.baihongyu.com/

你可能感兴趣的文章
mysql遇见error,1049
查看>>
uva 10034(最小生成树)
查看>>
NYOJ311 完全背包
查看>>
shp格式数据发布服务:postGIS + postgresql + geoserver
查看>>
101. Symmetric Tree - Easy
查看>>
codevs——2822 爱在心中
查看>>
标准C++中的string类的用法总结
查看>>
生成随机图片验证码
查看>>
Python基础班---第一部分(基础)---Python基础知识---认识Python
查看>>
利用JCom调用MS Office或者Acrobat API转换文档为PDF(转)
查看>>
JAVA MAC 配置
查看>>
c#代码技巧
查看>>
LeetCode 453 Minimum Moves to Equal Array Elements
查看>>
1134 最长上升子序列 (序列型 DP)
查看>>
js冒泡排序
查看>>
ssh命令使用
查看>>
第一次作业 4班卢炳武
查看>>
const int * 与 int *const
查看>>
抽象类的调用
查看>>
libjpeg.a exists or that its path is correct
查看>>