133 lines
3.9 KiB
C#
133 lines
3.9 KiB
C#
|
using System;
|
|||
|
using System.Collections.Generic;
|
|||
|
using System.Linq;
|
|||
|
using System.Text;
|
|||
|
using System.Threading;
|
|||
|
using System.Threading.Tasks;
|
|||
|
using Chat.Framework.WXSdk.IPAD;
|
|||
|
|
|||
|
namespace Chat.Framework.WXSdk.Implement
|
|||
|
{
|
|||
|
public interface IRunable : IDisposable
|
|||
|
{
|
|||
|
void Run(object state, bool timedOut);
|
|||
|
|
|||
|
bool IsRunning { get; }
|
|||
|
|
|||
|
RegisteredWaitHandle RegisterdHandler { get; set; }
|
|||
|
|
|||
|
WaitHandle WaitHandler { get; set; }
|
|||
|
}
|
|||
|
public interface ICallable
|
|||
|
{
|
|||
|
|
|||
|
void Call(object state);
|
|||
|
|
|||
|
bool IsRunning { get; }
|
|||
|
}
|
|||
|
public class ThreadExcutor
|
|||
|
{
|
|||
|
public static void RegisterIntervalObject(IRunable runnable, object state, long interval, bool onlyOnce)
|
|||
|
{
|
|||
|
runnable.WaitHandler = new AutoResetEvent(false);
|
|||
|
runnable.RegisterdHandler = ThreadPool.RegisterWaitForSingleObject(runnable.WaitHandler, new WaitOrTimerCallback(runnable.Run), state, interval, onlyOnce);
|
|||
|
}
|
|||
|
|
|||
|
public static void Submit(ICallable callable, object state)
|
|||
|
{
|
|||
|
if (!callable.IsRunning)
|
|||
|
{
|
|||
|
ThreadPool.QueueUserWorkItem(new WaitCallback(callable.Call), state);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
public static void UnRegisterIntervaluObject(IRunable runnable)
|
|||
|
{
|
|||
|
if (runnable != null)
|
|||
|
{
|
|||
|
if ((runnable.RegisterdHandler != null) && (runnable.WaitHandler != null))
|
|||
|
{
|
|||
|
runnable.RegisterdHandler.Unregister(runnable.WaitHandler);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
internal class HeadPack : IRunable
|
|||
|
{
|
|||
|
WXClientImpl_IPAD client;
|
|||
|
public HeadPack(WXClientImpl_IPAD client)
|
|||
|
{
|
|||
|
ThreadExcutor.RegisterIntervalObject(this, this, 1000, false);
|
|||
|
this.client = client;
|
|||
|
}
|
|||
|
public bool IsRunning { get; set; }
|
|||
|
|
|||
|
public RegisteredWaitHandle RegisterdHandler { get; set; }
|
|||
|
public WaitHandle WaitHandler { get; set; }
|
|||
|
|
|||
|
public void Dispose()
|
|||
|
{
|
|||
|
|
|||
|
}
|
|||
|
public int count { get; set; }
|
|||
|
public void Run(object state, bool timedOut)
|
|||
|
{
|
|||
|
//ni轮训获取消息?
|
|||
|
|
|||
|
if (IsRunning) return;
|
|||
|
var isnext = false;
|
|||
|
Next:
|
|||
|
try
|
|||
|
{
|
|||
|
IsRunning = true;
|
|||
|
if (client.Status == WxStatus.在线)
|
|||
|
{
|
|||
|
count++;
|
|||
|
if (count % 172800 == 0)//2天重新登录一次
|
|||
|
{
|
|||
|
if (client.FastLogin() == null)
|
|||
|
{
|
|||
|
if (!client.AsyncSyncMessage())
|
|||
|
client.ResetConnection();
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
client.ResetConnection();
|
|||
|
}
|
|||
|
}
|
|||
|
else if (count % 300 == 0)//10分钟一次
|
|||
|
{
|
|||
|
client.Socket.ClearPack();
|
|||
|
//20*30
|
|||
|
if (!client.AsyncSyncMessage()) client.ResetConnection();
|
|||
|
}
|
|||
|
else if (count % 240 == 0)
|
|||
|
{
|
|||
|
WechatPack send = new WechatPack(this.client);
|
|||
|
var data = send.Send(WechatCmd.心跳验证包);
|
|||
|
var _data = send.SendWx(data);
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
if (count % 100 == 0) client.CloseGRPC(true);
|
|||
|
}
|
|||
|
catch (Exception ex)
|
|||
|
{
|
|||
|
if (!isnext)
|
|||
|
{
|
|||
|
try
|
|||
|
{
|
|||
|
isnext = true;
|
|||
|
client.ResetConnection();
|
|||
|
goto Next;
|
|||
|
}
|
|||
|
catch { }
|
|||
|
}
|
|||
|
client.WriteLog("心跳线程异常:" + ex.Message);
|
|||
|
}
|
|||
|
IsRunning = false;
|
|||
|
|
|||
|
}
|
|||
|
}
|
|||
|
}
|