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 前言 这里相信大部分玩家之前现在都应该有过使用定时器的时候或者需求,例如什么定时 ...
随机推荐
- Java-基础练习3
1.编写一个Java程序,计算半径为3.0的圆周长和面积并输出结果.把圆周率π定义为常量,半径定义为变量,然后进行计算并输出结果. package com.java; public class zm ...
- xml Schema import
first_.xsd <?xml version="1.0" encoding="UTF-8"?> <xs:schema xmlns:xs=& ...
- Nagios学习笔记四:基于NRPE监控远程Linux主机
1.NRPE简介 Nagios监控远程主机的方法有多种,其方式包括SNMP.NRPE.SSH和NCSA等.这里介绍其通过NRPE监控远程Linux主机的方式. NRPE(Nagios Remote P ...
- H5常用代码:适配方案1
在工作中接到H5项目,第一件想到的事就应该是屏幕适配问题,解决了屏幕适配,接下来的事才能真正开始.从此篇博客开始会连续记录下我经常用到的一些适配方案. 对于传统的PC项目,直接在移动端打开也都是会以视 ...
- atitit.web的动态include 跟变量传递 java .net php
atitit.web的动态include 跟变量传递 java .net php 1. 动态include <jsp:include 1 2. 使用QueryString 1 3. 使用Ses ...
- Rails下cloud datastore的使用
Rails下cloud datastore的使用 背景 部门有一个项目要用Ruby做 WebAPI,DB使用关系型数据库Cloud Sql和非关系型数据库Cloud Datastore . 还不了 ...
- 匿名管道读取CMD回显信息
之前用了很坑爹的做法去读取了cmd命令的回显信息,现在发现了用匿名管道的实现方法,由于楼主没有学过Windows核心编程,找了一个代码来凑数 存下来以后研究 #include <windows. ...
- 如何在安装程序中判断操作系统是否是64位 inno
[Setup]; 开启64位模式ArchitecturesInstallIn64BitMode=x64 [Run] ;根据是否是64位进行不同的操作Filename: "..."; ...
- vba中MsgBox的参数及用法
1.作用 在消息框中显示信息,并等待用户单击按钮,可返回单击的按钮值(比如“确定”或者“取消”).通常用作显示变量值的一种方式.2.语法 MsgBox(Prompt[,Buttons ...
- [轉]redis;mongodb;memcache三者的性能比較
先说我自己用的情况: 最先用的memcache ,用于键值对关系的服务器端缓存,用于存储一些常用的不是很大,但需要快速反应的数据 然后,在另一个地方,要用到redis,然后就去研究了下redis. 一 ...