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();
            }
    
        
    
        }
  • 相关阅读:
    LeetCode 1275. 找出井字棋的获胜者 Find Winner on a Tic Tac Toe Game
    LeetCode 307. 区域和检索
    LeetCode 1271 十六进制魔术数字 Hexspeak
    秋实大哥与花 线段树模板
    AcWing 835. Trie字符串统计
    Leetcode 216. 组合总和 III
    Mybatis 示例之 复杂(complex)属性(property)
    Mybatis 示例之 复杂(complex)属性(property)
    Mybatis 高级结果映射 ResultMap Association Collection
    Mybatis 高级结果映射 ResultMap Association Collection
  • 原文地址:https://www.cnblogs.com/solq/p/4311659.html
Copyright © 2011-2022 走看看