c# socket 架構學習 SocketAsyncEventArgsPool 封裝

來源:互聯網
上載者:User

標籤:

    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();        }        }

 

c# socket 架構學習 SocketAsyncEventArgsPool 封裝

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.