高性能 TCP Socket连接关闭释放集中调度类

7 篇文章 0 订阅
/// <summary>
    /// Socket关闭调度类,此类提供了静态访问,但是此类可以实例化
    /// </summary>
    /// <remarks>
    /// Socket连接关闭类
    /// 3个队列
    /// 1.shotdown 关闭队列
    /// 步骤1
    /// 2.close 队列 间隔2秒
    /// 步骤2
    /// 3.dispose 队列 间隔1秒
    /// 步骤3
    /// </remarks>
    public class CloseSoketDispather
    {
        /// <summary>
        /// 静态单例
        /// </summary>
        private static readonly CloseSoketDispather instance = new CloseSoketDispather();


        /// <summary>
        /// 创建一个 Socket关闭调度实例
        /// </summary>
        public   CloseSoketDispather()
        {
            Start();
        }


        /// <summary>
        /// 运行状态
        /// </summary>
        public bool IsRun { get; protected set; }

        /// <summary>
        /// 用于单例访问的实例
        /// </summary>
        public static CloseSoketDispather Instance
        {
            get
            {
                return instance;
            }
        }

        /// <summary>
        /// 开始初始化
        /// </summary>
        protected virtual void Start()
        {
            if (!IsRun)
            {
                //Shutdown
                Task.Factory.StartNew(CycleShutdown);
                //Close
                Task.Factory.StartNew(CycleClose);
                //Dispose
                Task.Factory.StartNew(CycleDispose);
            }
            IsRun = true;
        }


        /// <summary>
        /// 关闭队列
        /// </summary>
        protected ConcurrentQueue<SocketcloseInfo> ShutdownQueue = new ConcurrentQueue<SocketcloseInfo>();
        /// <summary>
        /// 关闭队列
        /// </summary>
        protected ConcurrentQueue<SocketcloseInfo> CloseQueue = new ConcurrentQueue<SocketcloseInfo>();
        /// <summary>
        /// 释放队列
        /// </summary>
        protected ConcurrentQueue<SocketcloseInfo> DisposeQueue = new ConcurrentQueue<SocketcloseInfo>();


        /// <summary>
        /// 关闭对像池
        /// </summary>
        protected BasePool<SocketcloseInfo> Closepool = new BasePool<SocketcloseInfo>();

        /// <summary>
        /// 添加到需要关闭队列.
        /// </summary>
        /// <param name="socket"></param>
        public void Push(Socket socket)
        {
            if (socket != null)
            {
                SocketcloseInfo info = Closepool.Pop();
                info.Socket = socket;
                ShutdownQueue.Enqueue(info);
            }
        }

        #region 步骤1 Shutdown
        /// <summary>
        /// 循环关闭
        /// </summary>
        protected void CycleShutdown()
        {
            while (true)
            {
                Shutdown();
            }
        }

        /// <summary>
        /// 循环关闭socket
        /// </summary>
        protected void Shutdown()
        {
            SocketcloseInfo info;
            if (ShutdownQueue.TryDequeue(out info))
            {

                try
                {
                    if (info.Socket != null)
                    {
                        info.Socket.Shutdown(SocketShutdown.Both);
                    }
                    //增加
                    info.ShutdownTime = DateTime.Now;

                }
                catch (Exception ex)
                {

                    Logs.Write(LogType.Error, string.Format("关闭sokcet对像时出现异常:{0}", ex));

                }
                finally
                {
                    if (info != null)
                    {
                        CloseQueue.Enqueue(info);
                    }
                }
            }
            else
            {
                //暂停100毫秒
                System.Threading.Thread.Sleep(100);
            }
        }

        #endregion

        #region 步骤2Close
        /// <summary>
        /// 循环关闭
        /// </summary>
        protected void CycleClose()
        {
            while (true)
            {
                Closes();
            }
        }

        /// <summary>
        /// 关闭符合条件的数据
        /// </summary>
        protected void Closes()
        {
            //临时变量
            var tmp = new List<SocketcloseInfo>();
            SocketcloseInfo info;
            while (CloseQueue.TryDequeue(out info))
            {
                //关闭shout发生的时间
                if (info.ShutdownTime.AddSeconds(1.01) < DateTime.Now)
                {
                    Close(info);
                }
                else
                {
                    tmp.Add(info);
                }
            }
            //将没有结束的数据添加到集合中进行下次检查
            foreach (var item in tmp)
            {
                //添加到集合中进行下次检查
                CloseQueue.Enqueue(item);
            }
            //下次再循环
            System.Threading.Thread.Sleep(100);
        }

        /// <summary>
        /// 循环关闭socket
        /// </summary>
        protected void Close(SocketcloseInfo info)
        {
            try
            {
                if (info.Socket != null)
                {
                    info.Socket.Close();
                }
                //增加
                info.CloseTime = DateTime.Now;
            }
            catch (Exception ex)
            {
                Logs.Write(LogType.Error, string.Format("关闭sokcet对像时出现异常:{0}", ex));
            }
            finally
            {
                //添加到释放队列
                if (info != null)
                {
                    //添加到队列
                    DisposeQueue.Enqueue(info);
                }
            }
        }

        #endregion

        #region 步骤3Dispose
        /// <summary>
        /// 循环关闭
        /// </summary>
        protected void CycleDispose()
        {
            while (true)
            {
                Disposes();
            }
        }

        /// <summary>
        /// 关闭符合条件的数据
        /// </summary>
        protected void Disposes()
        {
            //临时变量
            var tmp = new List<SocketcloseInfo>();
            SocketcloseInfo info;
            while (DisposeQueue.TryDequeue(out info))
            {
                //关闭shout发生的时间
                if (info.ShutdownTime.AddSeconds(1.01) < DateTime.Now)
                {
                    SocketDispose(info);
                }
                else
                {
                    tmp.Add(info);
                }
            }
            //将没有结束的数据添加到集合中进行下次检查
            foreach (var item in tmp)
            {
                //添加到集合中进行下次检查
                DisposeQueue.Enqueue(item);
            }
            //下次再循环
            System.Threading.Thread.Sleep(100);
        }

        /// <summary>
        /// 循环关闭socket
        /// </summary>
        protected void SocketDispose(SocketcloseInfo info)
        {
            try
            {
                if (info.Socket != null)
                {
                    info.Socket.Dispose();
                }
            }
            catch (Exception ex)
            {
                Logs.Write(LogType.Error, string.Format("关闭sokcet对像时出现异常:{0}", ex));
            }
            finally
            {
                //还池
                GaveBack(info);
            }
        }
        #endregion


        /// <summary>
        /// 还池
        /// </summary>
        /// <param name="obj">池中的数据对像</param>
        protected void GaveBack(SocketcloseInfo obj)
        {

            obj.Socket = null;
            obj.ShutdownTime = DateTime.MinValue;
            obj.CloseTime = DateTime.MinValue;

            //还到池中.
            Closepool.GaveBack(obj);

        }


        /// <summary>
        /// socket关闭信息
        /// </summary>
        protected class SocketcloseInfo
        {
            /// <summary>
            /// 对应Socket对像
            /// </summary>
            public Socket Socket { get; set; }

            /// <summary>
            /// 停止时间
            /// </summary>
            public DateTime ShutdownTime { get; set; }

            /// <summary>
            /// close时间
            /// </summary>
            public DateTime CloseTime { get; set; }

        }
    }

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值