Exemple #1
0
        /// <summary>
        /// 清除数据
        /// </summary>
        public void Clear()
        {
            m_LockFlushAndPending.Enter();
            {
                while (m_PendingBuffer.Count > 0)
                {
                    m_PendingBuffer.Dequeue().Release();
                }

                if (m_FlushBuffer.IsNull == false)
                {
                    m_FlushBuffer.Release();
                    m_FlushBuffer = SendBuffer.NullBuffer;
                }

                // 清空
                m_WaitSendSize = 0;
            }
            m_LockFlushAndPending.Exit();
        }
Exemple #2
0
        /// <summary>
        /// 发送缓存的数据
        /// </summary>
        /// <returns>是否成功处理缓存的发送</returns>
        internal bool Flush()
        {
            if (m_SendQueue.IsEmpty == true)
            {
                return(true);
            }

            if (this.Running == false || m_BlockSend == true)
            {
                return(false);
            }


            if (m_SendPackSize1Sec >= s_SendMaxSize1Sec)
            {
                DateTime nowDateTime = DateTime.Now;

                // 如果小于计算的时间,表示已经超过了数据流量,等待下一个时间
                if (nowDateTime < (m_SendTimeStart + s_TimeSpan1Sec))
                {
                    return(true);
                }
                else    // 如果已经超过计算的时间,开始重新计算时间与数据
                {
                    m_SendTimeStart    = nowDateTime;
                    m_SendPackSize1Sec = 0;
                }
            }

            SendBuffer sendBuffer = m_SendQueue.Dequeue();

            while (sendBuffer.IsNull == false)
            {
                MessageBlock messageBlock = m_Socket.GetNewSendMessageBlock();
                if (messageBlock == null)
                {
                    throw new Exception("NetState.Flush(...) - messageBlock == null error!");
                }

                Marshal.Copy(sendBuffer.Buffer, 0, messageBlock.WritePointer(), (int)sendBuffer.Length);

                messageBlock.WritePointer((int)sendBuffer.Length);

                m_Socket.SendTo(messageBlock);

                // 释放数据
                sendBuffer.Release();

                // 累计发送的数据大小
                m_SendPackSize1Sec += sendBuffer.Length;

                // 已经超过了数据流量,就开始重新计算
                if (m_SendPackSize1Sec >= s_SendMaxSize1Sec)
                {
                    m_SendTimeStart += s_TimeSpan1Sec; // 下一次发送数据的时间
                    break;
                }


                // 需添加检测是否还有数据 否则以后就发不出去数据了(该问题解决)
                sendBuffer = m_SendQueue.Dequeue();
            }

            return(true);
        }