ab56a9bcf7
SVN-Revision: r240
64 lines
2.0 KiB
C#
64 lines
2.0 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading;
|
|
|
|
namespace Lskj.SocketService
|
|
{
|
|
class DaemonThread : Object
|
|
{
|
|
private Thread _thread;
|
|
private AsyncSocketServer _asyncSocketServer;
|
|
|
|
public DaemonThread(AsyncSocketServer asyncSocketServer)
|
|
{
|
|
_asyncSocketServer = asyncSocketServer;
|
|
_thread = new Thread(DaemonThreadStart);
|
|
_thread.Start();
|
|
}
|
|
|
|
public void DaemonThreadStart()
|
|
{
|
|
while (_thread.IsAlive)
|
|
{
|
|
AsyncSocketUserToken[] userTokenArray = null;
|
|
_asyncSocketServer.AsyncSocketUserTokenList.CopyList(ref userTokenArray);
|
|
for (int i = 0; i < userTokenArray.Length; i++)
|
|
{
|
|
if (!_thread.IsAlive)
|
|
break;
|
|
try
|
|
{
|
|
if ((DateTime.Now - userTokenArray[i].ActiveDateTime).Milliseconds > _asyncSocketServer.SocketTimeOutMS) //超时Socket断开
|
|
{
|
|
lock (userTokenArray[i])
|
|
{
|
|
_asyncSocketServer.CloseClientSocket(userTokenArray[i]);
|
|
}
|
|
}
|
|
}
|
|
catch (Exception E)
|
|
{
|
|
Program.Logger.ErrorFormat("Daemon thread check timeout socket error, message: {0}", E.Message);
|
|
Program.Logger.Error(E.StackTrace);
|
|
}
|
|
}
|
|
|
|
for (int i = 0; i < 60 * 1000 / 10; i++) //每分钟检测一次
|
|
{
|
|
if (!_thread.IsAlive)
|
|
break;
|
|
Thread.Sleep(10);
|
|
}
|
|
}
|
|
}
|
|
|
|
public void Close()
|
|
{
|
|
_thread.Abort();
|
|
_thread.Join();
|
|
}
|
|
}
|
|
}
|