c# socket 框架学习 SocketAsyncEventArgsPool 封装
public class SocketAsyncEventArgsPool{
//已使用记录
private List<Int32> usedRecord;
//未使用记录
private List<Int32> unUsedRecord;
//池子
private List<SocketAsyncEventArgsMetadata> pool;
//池子最大容量
private int capacity;
//是否动态扩展容量
// private bool dynamic = false;
/**池子初始化*/
private void init() {
this.pool = new List<SocketAsyncEventArgsMetadata>(this.capacity);
this.usedRecord = new List<Int32>(this.capacity);
this.unUsedRecord = new List<Int32>(this.capacity);
for (int i = ; i < this.capacity; i++) {
this.unUsedRecord.Add(i);
this.pool.Add(SocketAsyncEventArgsMetadata.valueOf(i));
}
}
///////////////////公开方法////////////////////////
/**获取可使用数量**/
public int GetUsedCount()
{
return this.capacity - this.usedRecord.Count;
}
/**获取可使用 SocketAsyncEventArgs */
public SocketAsyncEventArgsMetadata Pop()
{
int index = ;
lock(this){
if (GetUsedCount() <= )
{
extCapacity();
}
index = this.unUsedRecord[];
this.unUsedRecord.RemoveAt();
this.usedRecord.Add(index);
return this.pool[index];
}
}
/**回收 SocketAsyncEventArgs */
public void Push(SocketAsyncEventArgsMetadata args)
{
int index = ;
lock (this)
{
index = args.GetIndex();
this.unUsedRecord.Add(index);
this.usedRecord.Remove(index);
}
}
/** 扩展容量 */
private void extCapacity()
{
int minNewCapacity = ;
int newCapacity = Math.Min(this.capacity, minNewCapacity);
//每次以minNewCapacity倍数扩展
if (newCapacity > minNewCapacity)
{
newCapacity += minNewCapacity;
}
else {
//以自身双倍扩展空间
newCapacity = ;
while (newCapacity < minNewCapacity)
{
newCapacity <<= ;
}
}
for (int i = this.capacity; i < newCapacity; i++) {
this.unUsedRecord.Add(i);
this.pool.Add(SocketAsyncEventArgsMetadata.valueOf(i));
}
this.capacity = newCapacity;
}
//getter
public int GetCapacity() {
return this.capacity;
}
/**构建方法*/
public static SocketAsyncEventArgsPool valueOf(int maxCapacity)
{
SocketAsyncEventArgsPool result = new SocketAsyncEventArgsPool();
result.capacity = maxCapacity;
result.init();
return result;
}
}
public class SocketAsyncEventArgsMetadata : SocketAsyncEventArgs
{
/**记录索引**/
private int index;
private SocketAsyncEventArgs args; public static SocketAsyncEventArgsMetadata valueOf(int index) {
SocketAsyncEventArgsMetadata result = new SocketAsyncEventArgsMetadata();
result.index = index;
return result;
} internal int GetIndex()
{
return this.index;
}
}
测试类:
class TestPool
{
private int count = ;
public void test() {
SocketAsyncEventArgsPool pool = SocketAsyncEventArgsPool.valueOf(); for (int i = ; i < count; i++) {
Thread th = new Thread(pop);
th.Start(pool);
} } private void pop(object msg)
{
((SocketAsyncEventArgsPool)msg).Pop();
} }
c# socket 框架学习 SocketAsyncEventArgsPool 封装的更多相关文章
- 2013 最新的 play web framework 版本 1.2.3 框架学习文档整理
Play framework框架学习文档 Play framework框架学习文档 1 一.什么是Playframework 3 二.playframework框架的优点 4 三.Play Frame ...
- SSH 框架学习之初识Java中的Action、Dao、Service、Model-收藏
SSH 框架学习之初识Java中的Action.Dao.Service.Model-----------------------------学到就要查,自己动手动脑!!! 基础知识目前不够,有感性 ...
- 各种demo——CI框架学习
各种demo——CI框架学习 寒假学习一下CI框架,请各位多多指教! 一.CI的HelloWorld! 注意:CI禁止直接通过文件目录来访问控制器. ./application/controlle ...
- 够快网盘支持与iOS-ASIHTTPRequest框架学习
够快网盘支持与iOS-ASIHTTPRequest框架学习 前段时间在公司的产品中支持了够快网盘,用于云盘存储. 在这个过程中,学习到了很多新的知识,也遇到了很多问题,在此记录一下. 首先就够快的AP ...
- Struts2框架学习(三) 数据处理
Struts2框架学习(三) 数据处理 Struts2框架框架使用OGNL语言和值栈技术实现数据的流转处理. 值栈就相当于一个容器,用来存放数据,而OGNL是一种快速查询数据的语言. 值栈:Value ...
- Struts2框架学习(一)
Struts2框架学习(一) 1,Struts2框架介绍 Struts2框架是MVC流程框架,适合分层开发.框架应用实现不依赖于Servlet,使用大量的拦截器来处理用户请求,属于无侵入式的设计. 2 ...
- Spring框架学习1
AnonymouL 兴之所至,心之所安;尽其在我,顺其自然 新随笔 管理 Spring框架学习(一) 阅读目录 一. spring概述 核心容器: Spring 上下文: Spring AOP ...
- ABP 框架学习-01篇
从来没有自己写过太多的技术性文章,博客里面的文章都是拷贝别人的东西,做一个笔记功能给自己用的.最近觉得应该写点自己的学习博客 https://aspnetboilerplate.com/ ABP框架, ...
- 【框架学习与探究之定时器--Quartz.Net 】
声明 本文欢迎转载,原文地址:http://www.cnblogs.com/DjlNet/p/7572174.html 前言 这里相信大部分玩家之前现在都应该有过使用定时器的时候或者需求,例如什么定时 ...
随机推荐
- C# VS JAVA 差异 (未完待续)
1. 静态构造函数 C#中有静态构造函数, Java中没有静态构造函数.其实Java中有一个类似静态构造函数的东东,称作静态初始化,或者静态代码块,可以通过这样的代码实现相同的功能: 但是Java中静 ...
- SSL在https和MySQL中的原理思考
之前对HTTPS通信过程有过了解,HTTPS是应用HTTP协议使用SSL加密的版本,在TCP和HTTP之间增加SSL协议.通过握手阶段认证双方身份,协商对称秘钥对通信信息进行加密.此处只描述常用的服务 ...
- SQL Server 内存中OLTP内部机制概述(三)
----------------------------我是分割线------------------------------- 本文翻译自微软白皮书<SQL Server In-Memory ...
- What is /proc/slabinfo?
/proc/slabinfo gives information about memory usage on the slab level. Linux kernels uses slab pools ...
- 使用Enitity Framework实现增删改查服务中的一些通用思路
添加 → 方法参数中有一个有关添加视图模型类型的形参,比如vm→ 根据vm的某个属性,比如Name判断在上下文中是否存在,如果不存在就抛EntityNotFoundException异常→ 判断vm所 ...
- [C/C++] zltabout(带缩进的格式化输出)v1.0。能以相同的代码绑定到 C FILE 或 C++流
作者:zyl910 一.缘由 在写一些生成文本的程序时,经常需要使用带缩进的格式化输出的功能.以前为此写过不少类似的函数,可惜它们的可重用性很差. 这是因为——1) C语言的FILE*不支持重定向到自 ...
- [算法导论]迪克斯特拉算法 @ Python
class Graph: def __init__(self): self.V = [] self.w = {} class Vertex: def __init__(self, x): self.k ...
- (ETW) Event Trace for Windows 提高 (含pdf下载)
内容提纲 • 托管代码与非托管代码介绍 • 不安全代码介绍 • 用户模式与内核模式 • ETW执行流程分析 • 日志分析工具介绍:PerfView.exe ETW与非托管代码 • ETW依赖的So ...
- memcpy memmove区别和实现
memcpy与memmove的目的都是将N个字节的源内存地址的内容拷贝到目标内存地址中. 但当源内存和目标内存存在重叠时,memcpy会出现错误,而memmove能正确地实施拷贝,但这也增加了一点点开 ...
- C#怎样去掉对于用Splict分隔的数组中的空值?
string[] arrayUserId = userIds.Split(new char[] { ',' },StringSplitOptions.RemoveEmptyEntries); 可以去掉 ...