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 封装的更多相关文章

  1. 2013 最新的 play web framework 版本 1.2.3 框架学习文档整理

    Play framework框架学习文档 Play framework框架学习文档 1 一.什么是Playframework 3 二.playframework框架的优点 4 三.Play Frame ...

  2. SSH 框架学习之初识Java中的Action、Dao、Service、Model-收藏

    SSH 框架学习之初识Java中的Action.Dao.Service.Model-----------------------------学到就要查,自己动手动脑!!!   基础知识目前不够,有感性 ...

  3. 各种demo——CI框架学习

    各种demo——CI框架学习   寒假学习一下CI框架,请各位多多指教! 一.CI的HelloWorld! 注意:CI禁止直接通过文件目录来访问控制器. ./application/controlle ...

  4. 够快网盘支持与iOS-ASIHTTPRequest框架学习

    够快网盘支持与iOS-ASIHTTPRequest框架学习 前段时间在公司的产品中支持了够快网盘,用于云盘存储. 在这个过程中,学习到了很多新的知识,也遇到了很多问题,在此记录一下. 首先就够快的AP ...

  5. Struts2框架学习(三) 数据处理

    Struts2框架学习(三) 数据处理 Struts2框架框架使用OGNL语言和值栈技术实现数据的流转处理. 值栈就相当于一个容器,用来存放数据,而OGNL是一种快速查询数据的语言. 值栈:Value ...

  6. Struts2框架学习(一)

    Struts2框架学习(一) 1,Struts2框架介绍 Struts2框架是MVC流程框架,适合分层开发.框架应用实现不依赖于Servlet,使用大量的拦截器来处理用户请求,属于无侵入式的设计. 2 ...

  7. Spring框架学习1

    AnonymouL 兴之所至,心之所安;尽其在我,顺其自然 新随笔 管理   Spring框架学习(一)   阅读目录 一. spring概述 核心容器: Spring 上下文: Spring AOP ...

  8. ABP 框架学习-01篇

    从来没有自己写过太多的技术性文章,博客里面的文章都是拷贝别人的东西,做一个笔记功能给自己用的.最近觉得应该写点自己的学习博客 https://aspnetboilerplate.com/ ABP框架, ...

  9. 【框架学习与探究之定时器--Quartz.Net 】

    声明 本文欢迎转载,原文地址:http://www.cnblogs.com/DjlNet/p/7572174.html 前言 这里相信大部分玩家之前现在都应该有过使用定时器的时候或者需求,例如什么定时 ...

随机推荐

  1. Backbone入门——开发第一个Backbone页面

    1. 功能描述在新建的html页面中,通过导入的backbone文件搭建一个简单的mvc结构.当用户进入该页时,id号为“divTip”的<div>元素中将显示“hello,backbon ...

  2. PHP读取大文件的几种方法介绍

    读取大文件一直是一个头痛的问题,我们像使用php开发读取小文件可以直接使用各种函数实现,但一到大文章就会发现常用的方法是无法正常使用或时间太长太卡了,下面我们就一起来看看关于php读取大文件问题解决办 ...

  3. swoole

    http://www.swoole.com/wiki/index/prid-1-p-project/road_map.html

  4. AT&T Assembly for Linux and Mac (sys_exit)

    Exit() in C : (sys_exit.c) int main(void) { ; } Exit() in AT&T for Linux: (sys_exit.s) .section ...

  5. paip.提升用户体验--radio图片选择器 easyui 实现..

    #paip.提升用户体验--radio图片选择器 easyui 实现.. =================================== ##原因... ------------------- ...

  6. paip.http 404错误 的解决

    paip.http 404错误 的解决 错误原因 1.查看web服务器log...看是否错误... 2.使用了spring 或者struts 等等mvc框架,但是设置错误.. 3.web服务器使用了配 ...

  7. MVC 添加 httpHandlers 支持 .aspx 页面访问

    <?xml version="1.0"?> <!-- For more information on how to configure your ASP.NET ...

  8. Javascript正则表达式的初步学习

    <html> <head> <meta charset="utf-8"> <title>正则表达式的学习</title> ...

  9. 阿里云产品介绍(二):云服务器ECS的孪生兄弟们

    上一篇介绍的云服务器ECS,是阿里云最基础的产品,也是每一个云计算厂商最基础的产品,俗称爆款.除了标准的云服务器,阿里云也不停的在推出面向特殊业务场地的云服务器,可以说是ECS的孪生兄弟们. 这一篇就 ...

  10. Asp.Net正在中止线程引发的问题

    背景: Asp.Net做的一个同步程序,同步的方法是通过JQuery的Ajax调用,同步过程大概要执行20多分钟,程序部署到服务器后执行一段时间后就弹出执行失败的对话框,日志记录的错误信息是“正在中止 ...