Exemplo n.º 1
0
        // 获得发送缓冲区
        public SendDataBuffer MallocSendBuffer()
        {
            SendDataBuffer sendDataBuffer = null;

            lock (sendBufferGuard)
            {
                if (sendDataBufList.Count() > 0)
                {
                    sendDataBuffer = sendDataBufList[0];
                    sendDataBufList.RemoveAt(0);
                }
            }

            if (sendDataBuffer == null)
            {
                sendDataBuffer = new SendDataBuffer();
                if (sendDataBuffer == null)
                {
                    return(null);
                }

                // 初始化 Buffer
                if (!sendDataBuffer.Init(sendBufferSize, sendBufferExtend))
                {
                    sendDataBuffer = null;
                    return(null);
                }
            }

            return(sendDataBuffer);
        }
Exemplo n.º 2
0
        // 压入发送数据
        public bool PushSendData(UInt16 msgCommand, byte[] dataBuffer, Int32 bufferOffset, UInt16 bufferLength)
        {
            SendDataBuffer sendBuffer = null;

            lock (sendBufferListLock)
            {
                if (sendBufferList.Count() > 0)
                {
                    sendBuffer = sendBufferList[sendBufferList.Count() - 1];
                    if (sendBuffer != null && sendBuffer.GetFreeSpace() < (bufferLength + NetGlobalData.GetInstance().GetMsgHeadSize()))
                    {
                        sendBuffer = null;
                    }
                }

                if (sendBuffer == null)
                {
                    sendBuffer = DataBufferPool.GetInstance().MallocSendBuffer();
                    if (sendBuffer == null)
                    {
                        return(false);
                    }

                    // 添加新的 Buffer
                    sendBufferList.Add(sendBuffer);
                }

                return(sendBuffer.PushSendData(msgCommand, dataBuffer, bufferOffset, bufferLength));
            }
        }
Exemplo n.º 3
0
        // 判断所有缓冲区是否都为空
        public bool IsEmpty()
        {
            lock (sendBufferListLock)
            {
                if (sendBufferList.Count() <= 0)
                {
                    Trace.Assert(false, "sendBufferList is empty");
                    return(true);
                }

                if (sendBufferList.Count() > 1)
                {
                    return(false);
                }

                SendDataBuffer sendBuffer = sendBufferList[0];
                if (sendBuffer == null)
                {
                    Trace.Assert(false, "sendBufferList element is null");
                    return(true);
                }

                return(sendBuffer.IsEmpty());
            }
        }
Exemplo n.º 4
0
        // 发送消息成功(对写入线程和发送线程分离)
        public bool SendComplete(Int32 sendLength)
        {
            lock (sendBufferListLock)
            {
                if (!isAsyncSending || sendBufferList.Count() <= 0)
                {
                    Trace.Assert(isAsyncSending, "isAsyncSending is false");
                    Trace.Assert(sendBufferList.Count() > 0, "sendBufferList is empty");
                    return(false);
                }

                SendDataBuffer sendBuffer = sendBufferList[0];
                if (sendBuffer == null)
                {
                    Trace.Assert(false, "sendBufferList element is null");
                    return(false);
                }

                // 释放多余的缓冲区
                bool retValue = sendBuffer.SendComplete(sendLength);
                if (sendBufferList.Count() > 1 && sendBuffer.IsEmpty())
                {
                    DataBufferPool.GetInstance().FreeSendBuffer(sendBuffer);
                    sendBufferList.RemoveAt(0);
                }

                // 重置发送标记
                isAsyncSending = false;

                return(retValue);
            }
        }
Exemplo n.º 5
0
        // 初始化对象
        public bool Init()
        {
            sendBufferListLock = new object();
            if (sendBufferListLock == null)
            {
                return(false);
            }

            sendBufferList = new List <SendDataBuffer>();
            if (sendBufferList == null)
            {
                sendBufferListLock = null;
                return(false);
            }

            SendDataBuffer sendBuffer = DataBufferPool.GetInstance().MallocSendBuffer();

            if (sendBuffer == null)
            {
                sendBufferList     = null;
                sendBufferListLock = null;
                return(false);
            }

            sendBufferList.Add(sendBuffer);

            return(true);
        }
Exemplo n.º 6
0
        // 获得发送缓冲数据
        public void GetSendBuffer(ref byte[] sendDataBuffer, ref Int32 bufferOffset, ref Int32 dataLength)
        {
            lock (sendBufferListLock)
            {
                if (sendBufferList.Count() <= 0 || isAsyncSending)
                {
                    sendDataBuffer = null;
                    bufferOffset   = 0;
                    dataLength     = 0;

                    return;
                }

                SendDataBuffer sendBuffer = sendBufferList[0];
                if (sendBuffer == null)
                {
                    Trace.Assert(false, "sendBuffer is null");
                    sendDataBuffer = null;
                    bufferOffset   = 0;
                    dataLength     = 0;
                    sendBufferList.RemoveAt(0);

                    return;
                }

                sendBuffer.GetSendBuffer(ref sendDataBuffer, ref bufferOffset, ref dataLength);
                if (sendDataBuffer != null && bufferOffset >= 0 && dataLength > 0)
                {
                    isAsyncSending = true;
                }
            }
        }
Exemplo n.º 7
0
        // 释放发送缓冲区
        public void FreeSendBuffer(SendDataBuffer sendDataBuf)
        {
            if (sendDataBuf == null)
            {
                Trace.Assert(false, "sendDataBuf is null");
                return;
            }

            lock (sendBufferGuard)
            {
                if (sendDataBufList.Count() >= reservedSize)
                {
                    sendDataBuf.Release();
                    sendDataBuf = null;
                }
                else
                {
                    sendDataBuf.Reset();
                    sendDataBufList.Add(sendDataBuf);
                }
            }
        }