zoukankan      html  css  js  c++  java
  • 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 = 0; 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 = 0;
                lock(this){
                    if (GetUsedCount() <= 0)
                    {
                        extCapacity();
                    }
                    index = this.unUsedRecord[0];
                    this.unUsedRecord.RemoveAt(0);
                    this.usedRecord.Add(index);
                    return this.pool[index];
                }
            }
            /**回收 SocketAsyncEventArgs */
            public void Push(SocketAsyncEventArgsMetadata args)
            {
                int index = 0;
                lock (this)
                {
                    index = args.GetIndex();
                    this.unUsedRecord.Add(index);
                    this.usedRecord.Remove(index);                
                }
            }
    
            /** 扩展容量   */
            private void extCapacity()
            {
                int minNewCapacity = 200;
                int newCapacity = Math.Min(this.capacity, minNewCapacity);
    
                //每次以minNewCapacity倍数扩展
                if (newCapacity > minNewCapacity)
                {
                    newCapacity += minNewCapacity;
                }
                else {
                    //以自身双倍扩展空间
                    newCapacity = 64;
                    while (newCapacity < minNewCapacity)
                    {
                        newCapacity <<= 1;
                    }
                }
    
    
                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 = 200;
            public void test() {
                SocketAsyncEventArgsPool pool = SocketAsyncEventArgsPool.valueOf(4);
    
                for (int i = 0; i < count; i++) {
                    Thread th = new Thread(pop);
                    th.Start(pool);
                }
               
            
            }
    
            private void pop(object msg)
            {
                ((SocketAsyncEventArgsPool)msg).Pop();
            }
    
        
    
        }
  • 相关阅读:
    大爽Python入门教程 3-6 答案
    大爽Python入门教程 2-5 *拓展实践,对比与思考
    大爽Python入门教程 3-1 布尔值: True, False
    大爽Python入门教程 3-2 条件判断: if...elif..else
    企业微信获取code
    python inspect模块
    数据仓库之数据质量建设(深度好文)
    seleniumwire
    Jacoco增量代码覆盖率
    git对已经提交过的文件添加到.gitignore
  • 原文地址:https://www.cnblogs.com/solq/p/4311659.html
Copyright © 2011-2022 走看看