• 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();
            }
    
        
    
        }
  • 相关阅读:
    Two Sum
    Binary Tree Preorder Traversal *
    Rotate Array
    Repeated DNA Sequences
    Symmetric Tree
    Path Sum
    Python初学——多线程Threading
    Python初学——窗口视窗Tkinter
    pyinstaller打包多个py文件和去除cmd黑框
    python获取当前路径
  • 原文地址:https://www.cnblogs.com/solq/p/4311659.html
Copyright © 2020-2023  润新知