using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Text;

namespace WsSubDemoNetCore
{
    public class Ret
    {
        public bool IsValid()
        {
            return !string.IsNullOrEmpty(Cmd);
        }

        public string Cmd { get; set; }
		public int Code { get; set; }

        public string Msg { get; set; }

        //...
	}

	/// <summary>
	/// 程序缓存队列，接收数据马上进入
	/// </summary>
	public class QueueObj: ConcurrentQueue<string>
    {
        bool _clearflag = false;
        int _maxlen = 5000;
        public QueueObj(int maxlen)
        {
            _maxlen = maxlen;
        }

        public new int Enqueue(string item)
        {
            if (_clearflag)
                return 1;

            if (Count > _maxlen)
            {
                //Log4net.WarnFormat("queue overflow:{0}", _maxlen);
                return 1;
            }

            base.Enqueue(item);

            return 0;
        }

		/// <summary>
		/// 检查队列数量，避免数据溢出
		/// </summary>
		public int CheckMaxMiss()
        {
            int count = 0;
            if (Count > _maxlen)
            {
                _clearflag = true;
                try
                {
                    string tempmsg = "";
                    while (Count > _maxlen * 2 / 3)
                    {
                        count++;
                        if (!TryDequeue(out tempmsg))
                            break;
                    }
                }
                finally
                {
                    _clearflag = false;
                }
            }

            return count;
        }
    }
}
