Esempio n. 1
0
        /// <summary>
        /// 发送数据(在多线程中主要实现了顺序的发送)
        /// </summary>
        /// <param name="packet">需要发送的数据包</param>
        public void Send(Packet packet)
        {
            if (Running == false)
            {
                return;
            }

            switch (packet.PacketPriority)
            {
            case PacketPriority.Highest:
            case PacketPriority.AboveNormal:

                m_PacketSendQueue_Highest.Enqueue(packet);
                break;

            case PacketPriority.Normal:

                m_PacketSendQueue_Normal.Enqueue(packet);
                break;

            case PacketPriority.BelowNormal:
            case PacketPriority.Lowest:

                m_PacketSendQueue_Lowest.Enqueue(packet);
                break;

            default:

                m_PacketSendQueue_Normal.Enqueue(packet);
                break;
            }

            // XG
            SanGuo.TcpMonitor monitor = SanGuo.SanGuoMonitor.tcpMonitor;
            ++monitor.front.c11.runCount;                             // 消息数量
            monitor.front.c12.runCount += packet.WriterStream.Length; // 消息长度

            // 防止发送的顺序出错
            if (m_LockSend.InLock() == false)
            {
                return;
            }

            Packet sendPacket = null;

            do
            {
                sendPacket = null;

                if (m_PacketSendQueue_Highest.TryDequeue(out sendPacket) == false)
                {
                    if (m_PacketSendQueue_Normal.TryDequeue(out sendPacket) == false)
                    {
                        if (m_PacketSendQueue_Lowest.TryDequeue(out sendPacket) == false)
                        {
                            break;
                        }
                    }
                }

                if (sendPacket != null)
                {
                    InternalSend(sendPacket);
                }
            } while (sendPacket != null);

            m_LockSend.OutLock();

            // 如果没有错误且需要等待发送,则放在全局的处理中。。。
            if (m_SendQueue.IsEmpty == false)
            {
                m_World.FlushNetStates(this);
            }
        }