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 前言 这里相信大部分玩家之前现在都应该有过使用定时器的时候或者需求,例如什么定时 ...
随机推荐
- [iOS]The app icon set named "AppIcon" did not have any applicable content.
Develop Tools: xCode 5.1 I write a demo for app settings feature. The tutorial url is here. When I a ...
- 常用js方法整理common.js
项目中常用js方法整理成了common.js var h = {}; h.get = function (url, data, ok, error) { $.ajax({ url: url, data ...
- Command模式
Command模式只是封装了一个没有任何变量的函数. interface Command{ void Excute();} 具有强烈的分解功能的味道.把函数层面的任务提升到了类的层面(一个类仅仅是为了 ...
- HTML5骨骼动画Demo | 使用min2d、createjs、pixi播放spine动画
Spine做骨骼动画是比较流行的,使用起来可能相对复杂,但功能毕竟强大,所以市场占有率较大. 在unity.cocos2d.starling中使用spine已经很成熟了,而HTML5这一块可能刚刚起步 ...
- Leetcode 234 Palindrome Linked List 链表
判断链表是否是回文. 我直接将链表的一半进行倒置,然后将两半的链表进行比较 /** * Definition for singly-linked list. * struct ListNode { * ...
- NEWS - InstallShield 2015 正式发布
如果您需要为Windows®应用程序创建安装,InstallShield®便是您的最佳解决方案.在为桌面.服务器.云.Web和虚拟环境构建可靠的Windows Installer (MSI)和Inst ...
- 滑动返回类库SwipeBackLayout的使用问题,解决返回黑屏,和看到桌面
SwipeBackLayout是一个很好的类库,它可以让Android实现类似iOS系统的右滑返回效果,但是很多用户在使用官方提供的Demo会发现,可能出现黑屏或者返回只是看到桌面背景而没有看到上一个 ...
- Undokumentierte @Formeln/LotusScript im Lotus Notes Client/Server
Erstellung von Replik-IDs mittels @Text-Funktion@Text( @Now; "*" )Source NoPersist option ...
- Scala深入浅出实战经典之 List的foldLeft、foldRight、sort操作代码实战
Scala 深入浅出实战经典(1-64讲)完整视频.PPT.代码下载:百度云盘:http://pan.baidu.com/s/1c0noOt6 腾讯微云:http://url.cn/TnGbdC 3 ...
- Codeforces Round #379 (Div. 2) C. Anton and Making Potions 枚举+二分
C. Anton and Making Potions 题目连接: http://codeforces.com/contest/734/problem/C Description Anton is p ...