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 { /// /// 获取消息之心跳类 /// 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 mPackDic = new Dictionary(); 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() { } /// /// 设置接收数据包 /// /// /// 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) { } } /// /// 获取接收数据包 /// /// msgId /// public Task GetResult(string _msg_id) { var msg_id = _msg_id; return Task.Factory.StartNew(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; }); } } /// /// PC机器人(易转发)管理端 /// 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 AppSessions { get; set; } public PackHist PackHist { get; private set; } public PCRobotPool() { AppSessions = new Dictionary(); 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 AppSessions { get; set; } /// /// 客户端链接集合 /// //public Dictionary ClientSessions { get; set; } public Dictionary ClientSessions { get; set; } public PackHist PackHist { get; private set; } public PCRobotPool() { AppSessions = new Dictionary(); //ClientSessions = new Dictionary(); ClientSessions = new Dictionary(); PackHist = new PackHist(); } /// /// 更新连接 /// /// /// public void Update(string username, AppSession session) { lock (_lock) { AppSessions[username] = session; } } /// /// 删除连接 /// /// /// 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; } /// /// 获取连接 /// /// /// 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(); 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 } }