old_flsystem/类库/Chat.Framework/PCRobotSDK/PCRobotPool.cs

317 lines
9.5 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 PCRobot.Pack;
using SuperSocket.SocketBase;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace Chat.Framework.PCRobotSDK
{
/// <summary>
/// 获取消息之心跳类
/// </summary>
public class PackHist : IRunable
{
public PackHist()
{
ThreadExcutor.RegisterIntervalObject(this, this, 1000 * 60, false);
}
class Pack
{
public DateTime CreateTime { get; set; }
public BaseMsg BaseMsg { 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 BaseMsg _GetResultMsg(string msgId)
{
try
{
lock (mPackDic)
{
//Thread.Sleep(10);
BaseMsg buf = null;
try
{
if (mPackDic.ContainsKey(msgId))
{
buf = mPackDic[msgId].BaseMsg;
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(BaseMsg msg)
{
try
{
lock (mPackDic)
{
Console.WriteLine("add" + msg.MsgId);
if (mPackDic.ContainsKey(msg.MsgId)) mPackDic.Add(msg.MsgId, new Pack() { BaseMsg = msg });
else mPackDic[msg.MsgId] = new Pack() { BaseMsg = msg };
}
}
catch (Exception)
{ }
}
/// <summary>
/// 获取接收数据包
/// </summary>
/// <param name="_msg_id">msgId</param>
/// <returns></returns>
public Task<BaseMsg> GetResult(string _msg_id)
{
var msg_id = _msg_id;
return Task.Factory.StartNew<BaseMsg>(delegate ()
{
try
{
Console.WriteLine($"get {msg_id}");
DateTime end_time = DateTime.Now.AddSeconds(60);
while (end_time > DateTime.Now)
{
var r = _GetResultMsg(msg_id);
if (r != null) return r;
Thread.Sleep(20);
}
}
catch (Exception)
{
}
return null;
});
}
}
/// <summary>
/// PC机器人(易转发)管理端
/// </summary>
public class PCRobotPool
{
public class AppSessionInfo
{
public AppSession appSession { get; set; }
public string device { get; set; }
}
#region superSocket 1.0
/*
private object _lock = new object();
private Dictionary<string, SupperSocketSession> AppSessions { get; set; }
public PackHist PackHist { get; private set; }
public PCRobotPool()
{
AppSessions = new Dictionary<string, SupperSocketSession>();
PackHist = new PackHist();
}
public void Update(string username, SupperSocketSession session)
{
lock (_lock)
{
AppSessions[username] = session;
}
}
public string Remove(SupperSocketSession session)
{
lock (_lock)
{
try
{
var removeName = string.Empty;
foreach (var item in AppSessions)
{
if (item.Value == session)
{
removeName = item.Key;
break;
}
else if (item.Value.RemoteEndPoint.Address == session.RemoteEndPoint.Address && item.Value.RemoteEndPoint.Port == session.RemoteEndPoint.Port)
{
removeName = item.Key;
break;
}
}
if (!string.IsNullOrEmpty(removeName))
{
this.AppSessions.Remove(removeName);
return removeName;
}
}
catch (Exception)
{
}
}
return string.Empty;
}
public SupperSocketSession GetSession(string robotname)
{
if (AppSessions.ContainsKey(robotname)) return AppSessions[robotname];
return null;
}
*/
#endregion
#region superSocket 2.0
private object _lock = new object();
private Dictionary<string, AppSession> AppSessions { get; set; }
/// <summary>
/// 客户端链接集合
/// </summary>
//public Dictionary<string, AppSession> ClientSessions { get; set; }
public Dictionary<string, AppSessionInfo> ClientSessions { get; set; }
public PackHist PackHist { get; private set; }
public PCRobotPool()
{
AppSessions = new Dictionary<string, AppSession>();
//ClientSessions = new Dictionary<string, AppSession>();
ClientSessions = new Dictionary<string, AppSessionInfo>();
PackHist = new PackHist();
}
/// <summary>
/// 更新连接
/// </summary>
/// <param name="username"></param>
/// <param name="session"></param>
public void Update(string username, AppSession session)
{
lock (_lock)
{
AppSessions[username] = session;
}
}
/// <summary>
/// 删除连接
/// </summary>
/// <param name="session"></param>
/// <returns></returns>
public string Remove(AppSession session)
{
lock (_lock)
{
try
{
var removeName = string.Empty;
foreach (var item in AppSessions)
{
if (item.Value == session)
{
removeName = item.Key;
break;
}
else if (item.Value.RemoteEndPoint.Address == session.RemoteEndPoint.Address && item.Value.RemoteEndPoint.Port == session.RemoteEndPoint.Port)
{
removeName = item.Key;
break;
}
}
if (!string.IsNullOrEmpty(removeName))
{
this.AppSessions.Remove(removeName);
return removeName;
}
}
catch (Exception)
{
}
}
return string.Empty;
}
/// <summary>
/// 获取连接
/// </summary>
/// <param name="robotname"></param>
/// <returns></returns>
public AppSession GetSession(string robotname)
{
if (AppSessions.ContainsKey(robotname)) return AppSessions[robotname];
return null;
}
public void SendAppUpdate()
{
try
{
if (ClientSessions.Count == 0) return;
var apps = new List<AppSession>();
var appSessionList = ClientSessions.Values.ToList();
for (int i = 0; i < appSessionList.Count; i++)
{
if (apps.FirstOrDefault(f => f.RemoteEndPoint == appSessionList[i].appSession.RemoteEndPoint) == null)
{
apps.Add(appSessionList[i].appSession);
var need_send = $"{PCRobotCMD.updateApp} {PackTool.CompressString("A")}\r\n";
Thread.Sleep(70);
appSessionList[i].appSession.TrySend(need_send);
}
}
}
catch (Exception ex)
{
}
}
#endregion
}
}