Ejemplo n.º 1
0
        /// <summary>
        /// 获取当前的数据
        /// </summary>
        /// <returns></returns>
        public SendBuffer Dequeue()
        {
            SendBuffer sendGram = SendBuffer.NullBuffer;

            SpinLockEx.ReliableEnter(ref m_LockFlushAndPending);
            try
            {
                if (m_PendingBuffer.Count > 0)
                {
                    sendGram = m_PendingBuffer.Dequeue();   // 再给出数据
                }
                else if (m_FlushBuffer.IsNull == false)
                {
                    sendGram      = m_FlushBuffer; // 再给出数据
                    m_FlushBuffer = SendBuffer.NullBuffer;
                }

                // 移去已发送的数据的大小
                m_WaitSendSize -= sendGram.Length;
            }
            catch (Exception e) { Logs.FatalError("SendQueue->Dequeue: Occurred error when dequeuing the data {0} ", e.Message); }
            finally
            {
                m_LockFlushAndPending.Exit();
            }

            return(sendGram);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// 清除数据
        /// </summary>
        public void Clear()
        {
            SpinLockEx.ReliableEnter(ref m_LockFlushAndPending);
            try
            {
                while (m_PendingBuffer.Count > 0)
                {
                    m_PendingBuffer.Dequeue().Release();
                }

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

                // 清空
                m_WaitSendSize = 0;
            }
            finally
            {
                m_LockFlushAndPending.Exit();
            }
        }
Ejemplo n.º 3
0
        // 可能是windows操作系统的最大可发送的字节数
        //private const int PENDING_MAX_BUFFER = 96 * 1024;
        #endregion
        /// <summary>
        /// 如果数据满了,且缓冲区内的数据是空的则返回需要发送的数据
        /// 调用Enqueue(...)后调用Dequeue(...),不能直接调用Dequeue(...)
        /// </summary>
        /// <param name="byteBuffer"></param>
        /// <param name="iOffset"></param>
        /// <param name="iLength"></param>
        /// <returns></returns>
        public void Enqueue(byte[] byteBuffer, long iOffset, long iLength)
        {
            if (byteBuffer == null)
            {
                throw new ArgumentNullException("byteBuffer", "SendQueue.Enqueue(...) - byteBuffer == null error!");
            }

            if (iOffset < 0 || iOffset >= byteBuffer.Length)
            {
                throw new Exception("SendQueue.Enqueue(...) - iOffset < 0 || iOffset >= byteBuffer.Length error!");
            }

            if (iLength < 0 || iLength > byteBuffer.Length) // 如果iLength == 0就返回空,如果iLength == 0就跳过
            {
                throw new Exception("SendQueue.Enqueue(...) - iLength < 0 || iLength > byteBuffer.Length error!");
            }

            if ((byteBuffer.Length - iOffset) < iLength)
            {
                throw new Exception("SendQueue.Enqueue(...) - ( byteBuffer.Length - iOffset ) < iLength error!");
            }

            SpinLockEx.ReliableEnter(ref m_LockFlushAndPending);
            try
            {
                do
                {
                    if (m_FlushBuffer.IsNull == true)
                    {
                        // nothing yet buffered
                        m_FlushBuffer = SendBuffer.Instance();

                        if (m_FlushBuffer.IsNull == true)
                        {
                            throw new Exception("SendQueue.Enqueue(...) - m_FlushBuffer.IsNull == true error!");
                        }
                    }

                    // 当前已经写入的字节
                    long iBytesWritten = m_FlushBuffer.Write(byteBuffer, iOffset, iLength);

                    iOffset += iBytesWritten;
                    iLength -= iBytesWritten;

                    // 写入需要发送的数据的大小
                    m_WaitSendSize += iBytesWritten;

                    // 如果数据没有满,且数据写入完毕则退出,返回空,不添加到集合内
                    if (m_FlushBuffer.IsFull == true)
                    {
                        // 如果满了添加到集合内的尾处
                        m_PendingBuffer.Enqueue(m_FlushBuffer);

                        m_FlushBuffer = SendBuffer.NullBuffer;  // 置空再次请求缓存
                    }
                } while (iLength > 0);
            }
            catch (Exception e)
            {
                Logs.Error("SendQueue->Enqueue: Occurred error when enqueuing the data {0} ", e);
            }
            finally
            {
                m_LockFlushAndPending.Exit();
            }
        }
Ejemplo n.º 4
0
        /// <summary>
        /// 发送缓存的数据
        /// </summary>
        /// <returns>是否成功处理缓存的发送</returns>
        internal bool Flush()
        {
            if (m_SendQueue.IsEmpty == true)
            {
                return(true);
            }

            if (Running == false)
            {
                return(false);
            }

            // 等待发送的数据累计过多,断开
            if (m_SendQueue.WaitSendSize >= SendCachedMaxSize)
            {
                // 需要注释
                Logs.Warn("NetState[{0}] Flush(...) - WaitSendSize[{1}] >= {2} warning (发送缓存的数据包过大)!", ToString(), m_SendQueue.WaitSendSize, SendCachedMaxSize);

                Dispose();

                return(false);
            }

            if (m_SendPackSize1Sec >= SendMaxSize1Sec)
            {
                DateTime nowDateTime = OneServer.NowTime;

                // 如果小于计算的时间,表示已经超过了数据流量,等待下一个时间
                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 ArgumentNullException("messageBlock", "NetState.Flush(...) - messageBlock == null error!");
                }

                if (sendBuffer.Length > messageBlock.Size)
                {
                    throw new ArgumentException("NetState.Flush(...) - sendBuffer.Length > messageBlock.Length error!", "sendBuffer");
                }

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

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

                // XG
                ++SanGuo.SanGuoMonitor.tcpMonitor.front.c13.runCount;
                SanGuo.SanGuoMonitor.tcpMonitor.front.c14.runCount += sendBuffer.Length;

                m_Socket.SendTo(messageBlock);

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

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

                // 已经超过了数据流量,就等待下一秒开始重新计算
                if (m_SendPackSize1Sec >= SendMaxSize1Sec)
                {
                    break;
                }

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

            return(true);
        }
Ejemplo n.º 5
0
        /// <summary>
        /// 如果数据满了,且缓冲区内的数据是空的则返回需要发送的数据
        /// 调用Enqueue(...)后调用Dequeue(...),不能直接调用Dequeue(...)
        /// </summary>
        /// <param name="byteBuffer"></param>
        /// <param name="iOffset"></param>
        /// <param name="iLength"></param>
        /// <returns></returns>
        public void Enqueue(byte[] byteBuffer, long iOffset, long iLength)
        {
            if (byteBuffer == null)
                throw new ArgumentNullException("byteBuffer", "SendQueue.Enqueue(...) - byteBuffer == null error!");

            if (iOffset < 0 || iOffset >= byteBuffer.Length)
                throw new Exception("SendQueue.Enqueue(...) - iOffset < 0 || iOffset >= byteBuffer.Length error!");

            if (iLength < 0 || iLength > byteBuffer.Length) // 如果iLength == 0就返回空,如果iLength == 0就跳过
                throw new Exception("SendQueue.Enqueue(...) - iLength < 0 || iLength > byteBuffer.Length error!");

            if ((byteBuffer.Length - iOffset) < iLength)
                throw new Exception("SendQueue.Enqueue(...) - ( byteBuffer.Length - iOffset ) < iLength error!");

            SpinLockEx.ReliableEnter(ref m_LockFlushAndPending);
            try
            {
                do
                {
                    if (m_FlushBuffer.IsNull == true)
                    {
                        // nothing yet buffered
                        m_FlushBuffer = SendBuffer.Instance();

                        if (m_FlushBuffer.IsNull == true)
                            throw new Exception("SendQueue.Enqueue(...) - m_FlushBuffer.IsNull == true error!");
                    }

                    // 当前已经写入的字节
                    long iBytesWritten = m_FlushBuffer.Write(byteBuffer, iOffset, iLength);

                    iOffset += iBytesWritten;
                    iLength -= iBytesWritten;

                    // 写入需要发送的数据的大小
                    m_WaitSendSize += iBytesWritten;

                    // 如果数据没有满,且数据写入完毕则退出,返回空,不添加到集合内
                    if (m_FlushBuffer.IsFull == true)
                    {
                        // 如果满了添加到集合内的尾处
                        m_PendingBuffer.Enqueue(m_FlushBuffer);

                        m_FlushBuffer = SendBuffer.NullBuffer; // 置空再次请求缓存
                    }
                } while (iLength > 0);
            }
            catch (Exception e)
            {
                Logs.Error("SendQueue->Enqueue: Occurred error when enqueuing the data {0} ", e);
            }
            finally
            {
                m_LockFlushAndPending.Exit();
            }
        }
Ejemplo n.º 6
0
        /// <summary>
        /// 获取当前的数据
        /// </summary>
        /// <returns></returns>
        public SendBuffer Dequeue()
        {
            SendBuffer sendGram = SendBuffer.NullBuffer;

            SpinLockEx.ReliableEnter(ref m_LockFlushAndPending);
            try
            {
                if (m_PendingBuffer.Count > 0)
                {
                    sendGram = m_PendingBuffer.Dequeue();   // 再给出数据
                }
                else if (m_FlushBuffer.IsNull == false)
                {
                    sendGram = m_FlushBuffer; // 再给出数据
                    m_FlushBuffer = SendBuffer.NullBuffer;
                }

                // 移去已发送的数据的大小
                m_WaitSendSize -= sendGram.Length;
            }
            catch (Exception e) { Logs.FatalError("SendQueue->Dequeue: Occurred error when dequeuing the data {0} ", e.Message); }
            finally
            {
                m_LockFlushAndPending.Exit();
            }

            return sendGram;
        }
Ejemplo n.º 7
0
        /// <summary>
        /// 清除数据
        /// </summary>
        public void Clear()
        {
            SpinLockEx.ReliableEnter(ref m_LockFlushAndPending);
            try
            {
                while (m_PendingBuffer.Count > 0)
                    m_PendingBuffer.Dequeue().Release();

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

                // 清空
                m_WaitSendSize = 0;
            }
            finally
            {
                m_LockFlushAndPending.Exit();
            }
        }