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. iOS开发-xCode代码格式化xAlign

    xCode默认是可以进行代码格式化的,能满足基础开发需求,如果想要个性一些代码对齐方式宏对齐,等号对齐,属性对齐,xAlign就提供了以上三种功能,参考文中效果~ 基础效果 等号对齐: 属性对齐: 宏 ...

  2. hdu 1385 Minimum Transport Cost (floyd算法)

    貌似···················· 这个算法深的东西还是很不熟悉!继续学习!!!! ++++++++++++++++++++++++++++ ======================== ...

  3. eclipse安装activiti工作流插件

    方式一:在有网络的情况下,安装流程设计器步骤如下: 1.点击eclipse上方工具栏的Help,选择Install New Software 2.弹出如下窗口,然后填写插件名称和安装地址 Name: ...

  4. redis 学习指南

    一.介绍 Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.一个高性能的key-value数据库.并提供多种语言的API.说到Key-Value数据库NoSQL数 ...

  5. old header

    海纳百川 山不拒土 No Backspace in Real Life. Love Life![Cloud][LBS][GIS][GPS][MAPS][C++][Java]

  6. 私有云android客户端2.1.2最新版本(ownCloud简体中文优化版)

    通过安装Ubuntu16.04+LAMP+ownCloud9.1+SSL建立私有云,下载ownCloud android客户端最新源码,针对国际语言简体中文化,修改部分代码,并进行补充.优化,编译生成 ...

  7. iOS开发- UICollectionView详解+实例

    本章通过先总体介绍UICollectionView及其常用方法,再结合一个实例,了解如何使用UICollectionView. UICollectionView 和 UICollectionViewC ...

  8. RabbitMQ的安装使用

    1.下载安装 Rabbit MQ 是建立在强大的Erlang OTP平台上,因此安装RabbitMQ之前要先安装Erlang. erlang:http://www.erlang.org/downloa ...

  9. 【LeetCode】258. Add Digits (2 solutions)

    Add Digits Given a non-negative integer num, repeatedly add all its digits until the result has only ...

  10. C# WinForm 技巧八:界面开发之“WeifenLuo.WinFormsUI.Docking+OutLookBar” 使用

    概述      最近几天一直在关注WinFrom方面的文章主要还是园子里伍华聪的博客,在看看我们自己写的项目差不忍赌啊,有想着提炼一下项目的公共部分,公共部分有分为 界面,类库两方面,今天主要是把界面 ...