using System;
using System.Collections.Concurrent;
using MrWu.RabbitMQ;
using Server.Pack;
using GameData;

namespace Server.MQ {
    /// <summary>
    /// 消息池
    /// </summary>
    public class WaitBeMsgPool {
        static WaitBeMsgPool() {
            instace = new WaitBeMsgPool();
        }

        private WaitBeMsgPool() {
        }

        private static WaitBeMsgPool instace;

        /// <summary>
        /// 所有的消息
        /// </summary>
        private ConcurrentQueue<WaitBeMsg> msgs = new ConcurrentQueue<WaitBeMsg>();

        /// <summary>
        /// 获取消息
        /// </summary>
        /// <returns></returns>
        public static WaitBeMsg GetWaiteBeMsg(Message msg) {
            WaitBeMsg tmp = null;
            if (!instace.msgs.TryDequeue(out tmp)) {
                tmp = new WaitBeMsg();
            }

            tmp.message = msg;
            tmp.AutoAck = true;
            //ServerHead head = null;
            tmp.jsondata = JsonPack.GetPackHead(msg.bodystr, out  tmp.head);
            //tmp.head = head;
            return tmp;
        }

        /// <summary>
        /// 处理消息
        /// </summary>
        /// <returns></returns>
        public static void PutWaiteBeMsg(ref WaitBeMsg msg) {
            if (msg == null)
                return;
            msg.Clear();
            instace.msgs.Enqueue(msg);
            msg = null;
        }


    }
}
