SupperSocket深入浅出
这篇文章出要是SuperSocket底层如何接收数据
Process(ArraySegment<byte> segment) 获取加载数据(直到数据全部接收后返回)
namespace SuperSocket.ProtoBase
{
/// <summary>
/// The default pipeline processor
/// </summary>
/// <typeparam name="TPackageInfo">The type of the package info.</typeparam>
public class DefaultPipelineProcessor<TPackageInfo> : IPipelineProcessor
where TPackageInfo : IPackageInfo
{
private IReceiveFilter<TPackageInfo> m_ReceiveFilter; private IReceiveFilter<TPackageInfo> m_FirstReceiveFilter; private BufferList m_ReceiveCache; private int m_MaxPackageLength; /// <summary>
/// Initializes a new instance of the <see cref="DefaultPipelineProcessor{TPackageInfo}"/> class.
/// </summary>
/// <param name="receiveFilter">The initializing receive filter.</param>
/// <param name="maxPackageLength">The max package size.</param>
public DefaultPipelineProcessor(IReceiveFilter<TPackageInfo> receiveFilter, int maxPackageLength = )
{
m_FirstReceiveFilter = m_ReceiveFilter = receiveFilter;
m_ReceiveCache = new BufferList();
m_MaxPackageLength = maxPackageLength;
} private void PushResetData(ArraySegment<byte> raw, int rest)
{
var segment = new ArraySegment<byte>(raw.Array, raw.Offset + raw.Count - rest, rest);
m_ReceiveCache.Add(segment);
} private IList<IPackageInfo> GetNotNullOne(IList<IPackageInfo> left, IList<IPackageInfo> right)
{
if (left != null)
return left; return right;
} /// <summary>
/// Processes the input segment.
/// </summary>
/// <param name="segment">The input segment.</param>
/// <returns>
/// the processing result
/// </returns>
public virtual ProcessResult Process(ArraySegment<byte> segment)
{
var receiveCache = m_ReceiveCache; receiveCache.Add(segment); var rest = ; var currentReceiveFilter = m_ReceiveFilter; SingleItemList<IPackageInfo> singlePackage = null; List<IPackageInfo> packageList = null; while (true)
{
var lastItemLength = receiveCache.Last.Count;
var packageInfo = currentReceiveFilter.Filter(receiveCache, out rest); if (currentReceiveFilter.State == FilterState.Error)
{
return ProcessResult.Create(ProcessState.Error);
}
//最大缓存空间
if (m_MaxPackageLength > )
{
var length = receiveCache.Total; if (length > m_MaxPackageLength)
{
return ProcessResult.Create(ProcessState.Error, string.Format("Max package length: {0}, current processed length: {1}", m_MaxPackageLength, length));
}
} var nextReceiveFilter = currentReceiveFilter.NextReceiveFilter; // don't reset the filter if no request is resolved
if(packageInfo != null)
currentReceiveFilter.Reset(); if (nextReceiveFilter != null)
{
currentReceiveFilter = nextReceiveFilter;
m_ReceiveFilter = currentReceiveFilter;
} // continue receive
if (packageInfo == null)
{
if (rest > )
{
var last = receiveCache.Last; if(rest != lastItemLength)
{
PushResetData(segment, rest);
} continue;
} return ProcessResult.Create(ProcessState.Cached, GetNotNullOne(packageList, singlePackage));
} if (packageList != null)
{
packageList.Add(packageInfo);
}
else if (singlePackage == null)
singlePackage = new SingleItemList<IPackageInfo>(packageInfo);
else
{
if (packageList == null)
packageList = new List<IPackageInfo>(); packageList.Add(singlePackage[]);
packageList.Add(packageInfo);
singlePackage = null;
} if (packageInfo is IBufferedPackageInfo // is a buffered package
&& (packageInfo as IBufferedPackageInfo).Data is BufferList) // and it uses receive buffer directly
{
// so we need to create a new receive buffer container to use
m_ReceiveCache = receiveCache = new BufferList(); if (rest <= )
{
return ProcessResult.Create(ProcessState.Cached, GetNotNullOne(packageList, singlePackage));
}
}
else
{
m_ReceiveCache.Clear(); if (rest <= )
{
return ProcessResult.Create(ProcessState.Completed, GetNotNullOne(packageList, singlePackage));
}
} PushResetData(segment, rest);
}
} /// <summary>
/// cleanup the cached the buffer by resolving them into one package at the end of the piple line
/// </summary>
/// <returns>return the processing result</returns>
public void Reset()
{
m_ReceiveCache.Clear();
m_FirstReceiveFilter.Reset(); if (m_ReceiveFilter != m_FirstReceiveFilter)
m_ReceiveFilter = m_FirstReceiveFilter;
} /// <summary>
/// Gets the received cache.
/// </summary>
/// <value>
/// The cache.
/// </value>
public BufferList Cache
{
get { return m_ReceiveCache; }
}
}
}
SupperSocket深入浅出的更多相关文章
- SupperSocket深入浅出(二)
如果还没有看SuperStock深入浅出(一) ,请先看 这一章,主要说下命令是如果运行的.刚开始的时候会发现拷别人的代码命令是可以运行的,在修改的过程中突然发现命令无效了? 这里什么原因?,我先把代 ...
- SupperSocket深入浅出(一)
花了几天时间了解了SupperSocket工作原理,各各类之间的工作关系.SupperSocket大部资料网上都有,但写的都不适合初学者. 今天花点时间写下这几天的学习成果,一方面是为了将来更好的回顾 ...
- 【深入浅出jQuery】源码浅析--整体架构
最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...
- 【深入浅出jQuery】源码浅析2--奇技淫巧
最近一直在研读 jQuery 源码,初看源码一头雾水毫无头绪,真正静下心来细看写的真是精妙,让你感叹代码之美. 其结构明晰,高内聚.低耦合,兼具优秀的性能与便利的扩展性,在浏览器的兼容性(功能缺陷.渐 ...
- 深入浅出Struts2+Spring+Hibernate框架
一.深入浅出Struts2 什么是Struts2? struts2是一种基于MVC的轻量级的WEB应用框架.有了这个框架我们就可以在这个框架的基础上做起,这样就大大的提高了我们的开发效率和质量,为公司 ...
- DOM 事件深入浅出(二)
在DOM事件深入浅出(一)中,我主要给大家讲解了不同DOM级别下的事件处理程序,同时介绍了事件冒泡和捕获的触发原理和方法.本文将继续介绍DOM事件中的知识点,主要侧重于DOM事件中Event对象的属性 ...
- DOM 事件深入浅出(一)
在项目开发时,我们时常需要考虑用户在使用产品时产生的各种各样的交互事件,比如鼠标点击事件.敲击键盘事件等.这样的事件行为都是前端DOM事件的组成部分,不同的DOM事件会有不同的触发条件和触发效果.本文 ...
- 深入浅出node(2) 模块机制
这部分主要总结深入浅出Node.js的第二章 一)CommonJs 1.1CommonJs模块定义 二)Node的模块实现 2.1模块分类 2.2 路径分析和文件定位 2.2.1 路径分析 2.2.2 ...
- IOS 网络-深入浅出(一 )-> 三方SDWebImage
首要我们以最为常用的UIImageView为例介绍实现原理: 1)UIImageView+WebCache: setImageWithURL:placeholderImage:options: 先显 ...
随机推荐
- problem during schema drop, statement alter table ACT_RU_VARIABLE
将activiti的databaseSchemaUpdate设置为drop-create抛出DbSqlSession 错误 发布于2015-02-14 16:03:57 浏览7851次 Excepti ...
- programming-languages学习笔记--第4部分
programming-languages学习笔记–第4部分 */--> pre.src {background-color: #292b2e; color: #b2b2b2;} program ...
- 1、RabbitMQ入门
RabbitMQ 可以参考官网进行学习! 开发语言:Erlang – 面向并发的编程语言. AMQP:是消息队列的一个协议. mysql 是 java 写的吗?不是 那么 java 能不能访问?可以, ...
- ethereumjs-vm/examples/run-transactions-simple
https://github.com/ethereumjs/ethereumjs-vm/tree/master/examples/run-transactions-simple prerequisit ...
- .NET完全手动搭建三层B/S架构
简介:三层架构(3-tier application) 通常意义上的三层架构就是将整个业务应用划分为:表现层(WebUI).业务逻辑层(BusinessLogicLayer).数据访问层(DataAc ...
- PAT——1049. 数列的片段和
给定一个正数数列,我们可以从中截取任意的连续的几个数,称为片段.例如,给定数列{0.1, 0.2, 0.3, 0.4},我们有(0.1) (0.1, 0.2) (0.1, 0.2, 0.3) (0.1 ...
- 【luogu P2831 愤怒的小鸟】 题解
题目链接:https://www.luogu.org/problemnew/show/P2831 写点做题总结:dp,搜索,重在设计状态,状态设的好,转移起来也方便. 对于一条抛物线,三点确定.(0, ...
- JOB SERVER 负载均衡
JOB SERVER 负载均衡 一.体系结构 1.job server group job server group 是由一个或者多个job server 组成的,做为一个整体对外提供服务,在内部实现 ...
- Oracle11gR2(ASM,UDEV)的RAC搭建安装
基本信息: 1) 安装包: 操作系统:rhel-server-6.7-x86_64-dvd.iso rac安装包: Oracle11gR2:linux.x64_11gR2_database_1of2. ...
- 【最新最全】为 iOS 和 Android 的真机和模拟器编译 Luajit 库
编译 Luajit 库,的确是一个挑战.因为官网的教程,在当前版本的 Xcode 和 NDK 环境中,已经不适用了.以前只是编译了适用于真机的 Luajit 库.最近在尝试编译模拟器 Luajit 库 ...