c\#socket 프레임 워 크 학습 SocketAsyncEventArgsPool 패키지

11987 단어 socket
    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();

        }



    



    }

좋은 웹페이지 즐겨찾기