Beispiel #1
0
        // 初始化
        public bool Init(Int32 initCounts, Int32 bufferSize)
        {
            msgBufferSize    = bufferSize;
            initBufferCounts = initCounts;

            msgBufferGuard = new object();
            if (msgBufferGuard == null)
            {
                return(false);
            }

            msgBufferList = new List <MsgBuffer>();
            if (msgBufferList == null)
            {
                msgBufferGuard = null;
                return(false);
            }

            // 预生成 MsgBuffer
            MsgBuffer msgBuffer = null;

            for (Int32 i = 0; i < initBufferCounts; ++i)
            {
                msgBuffer = MakeMsgBuffer();
                if (msgBuffer == null)
                {
                    return(false);
                }

                msgBufferList.Add(msgBuffer);
            }

            return(true);
        }
Beispiel #2
0
        // 私有函数
        #region
        private MsgBuffer MakeMsgBuffer()
        {
            MsgBuffer msgBuffer = new MsgBuffer();

            if (msgBuffer == null)
            {
                return(null);
            }

            if (!msgBuffer.Init(msgBufferSize))
            {
                msgBuffer = null;
                return(null);
            }

            return(msgBuffer);
        }
Beispiel #3
0
        // 生成 MsgBuffer
        public MsgBuffer MallocMsgBuffer()
        {
            MsgBuffer msgBuffer = null;

            lock (msgBufferGuard)
            {
                if (msgBufferList.Count() > 0)
                {
                    msgBuffer = msgBufferList[0];
                    msgBufferList.RemoveAt(0);
                }

                if (msgBuffer == null)
                {
                    msgBuffer = MakeMsgBuffer();
                }

                return(msgBuffer);
            }
        }
Beispiel #4
0
        // 释放 MsgBuffer
        public void FreeMsgBuffer(MsgBuffer msgBuffer)
        {
            if (msgBuffer == null)
            {
                Trace.Assert(false, "msgBuffer is null");
                return;
            }

            lock (msgBufferGuard)
            {
                if (msgBufferList.Count() >= initBufferCounts)
                {
                    msgBuffer.Release();
                    msgBuffer = null;
                }
                else
                {
                    msgBuffer.Reset();
                    msgBufferList.Add(msgBuffer);
                }
            }
        }