/// <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.SpinWait(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; }
}
}
原文地址:http://blog.csdn.net/daonidedie/article/details/44241253