old_flsystem/类库/Api.Framework/Cps/CefSharp1/DouyinSendOrder.cs

139 lines
3.9 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using Chat.Framework.WXSdk.Implement;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Api.Framework.Cps.CefSharp1
{
class DouyinSendOrder
{
}
public class DouyinPackHist : IRunable
{
public DouyinPackHist()
{
ThreadExcutor.RegisterIntervalObject(this, this, 1000 * 60, false);
}
class Pack
{
public DateTime CreateTime { get; set; }
public BaseResult BaseResult { get; set; }
}
//private object mPackDicObj = new object();
private Dictionary<string, Pack> mPackDic = new Dictionary<string, Pack>();
public bool IsRunning { get; set; }
public RegisteredWaitHandle RegisterdHandler { get; set; }
public WaitHandle WaitHandler { get; set; }
private BaseResult _GetResultMsg(string msgId)
{
try
{
lock (mPackDic)
{
//Thread.Sleep(10);
BaseResult buf = null;
try
{
if (mPackDic.ContainsKey(msgId))
{
buf = mPackDic[msgId].BaseResult;
mPackDic.Remove(msgId);
return buf;
}
}
catch (Exception ee)
{ }
return buf;
}
}
catch (Exception)
{
}
return null;
}
public void Run(object state, bool timedOut)
{
if (IsRunning) return;
try
{
IsRunning = true;
var t = DateTime.Now.AddMinutes(-5);
var list = mPackDic.ToList().Where(f => f.Value.CreateTime < t);//5分钟前的包直接忽略
foreach (var item in list) if (mPackDic.ContainsKey(item.Key)) mPackDic.Remove(item.Key);
}
catch (Exception)
{ }
finally
{
IsRunning = false;
}
}
public void Dispose()
{
}
/// <summary>
/// 设置接收数据包
/// </summary>
/// <param name="msg_id"></param>
/// <param name="msg"></param>
public void SetResult(BaseResult msg)
{
try
{
lock (mPackDic)
{
Console.WriteLine("add" + msg.MsgId);
if (mPackDic.ContainsKey(msg.MsgId)) mPackDic.Add(msg.MsgId, new Pack() { BaseResult = msg });
else mPackDic[msg.MsgId] = new Pack() { BaseResult = msg };
}
}
catch (Exception)
{ }
}
/// <summary>
/// 获取接收数据包
/// </summary>
/// <param name="_msg_id">msgId</param>
/// <returns></returns>
public Task<BaseResult> GetResult(string _msg_id)
{
var msg_id = _msg_id;
return Task.Factory.StartNew<BaseResult>(delegate ()
{
try
{
Console.WriteLine($"get {msg_id}");
DateTime end_time = DateTime.Now.AddSeconds(5);
while (end_time > DateTime.Now)
{
var r = _GetResultMsg(msg_id);
if (r != null) return r;
Thread.Sleep(20);
}
}
catch (Exception)
{
}
return null;
});
}
}
public class DouyinPool
{
public static DouyinPackHist PackHist { get; private set; } = new DouyinPackHist();
}
}