/// <summary> 移除(注销)客户端 </summary> public void RmvCLT(string id) { lock (_cltL) { if (_cltL.ContainsKey(id)) { var temp = _cltL[id]; _cltL.Remove(id); LogMethod.WriteRunLog(MSG_SIGN, DateTime.Now, "Rmv client " + temp.CltInfo + "."); } } }
/// <summary> 发送状态 </summary> public void SendStt(DateTime time, string type, string key, object vlu) { var stt_ = new SttP() { Tim = time, Typ = type, Key = key, Vlu = vlu }; #region - 添加一类 状态 - lock (_sttC) { if (!_sttC.ContainsKey(type)) { _sttC[type] = new Dictionary <string, SttP>(); } } #endregion #region - 添加一个 状态 - lock (_sttC[type]) { _sttC[type][key] = stt_; } #endregion #region - 获得 客户列表 - CltShell[] cls_ = null; lock (_cltL) { if (_cltL.Count > 0) { cls_ = new CltShell[_cltL.Count]; _cltL.Values.CopyTo(cls_, 0); } } #endregion #region - 发送 状态 - if (cls_ != null && cls_.Length > 0) { for (int i = 0; i < cls_.Length; i++) { try { cls_[i].Clt.SendStt(time, type, key, vlu); cls_[i] = null; } catch (Exception e) { LogMethod.WriteErrLog(e); LogMethod.WriteRunLog(MSG_SIGN, DateTime.Now, "Invoke client err - " + cls_[i].CltInfo + ". " + e.Message); } } } #endregion #region - 移除 无效客户 - if (cls_ != null && cls_.Length > 0) { for (int i = 0; i < cls_.Length; i++) { if (cls_[i] != null) { RmvCLT(cls_[i].ID); } } } #endregion }
/// <summary> 发送消息 </summary> public void SendMsg(DateTime time, string type, object vlu) { var msg_ = new MsgP() { Tim = time, Typ = type, Vlu = vlu }; #region - 添加一类 消息 - lock (_msgC) { if (!_msgC.ContainsKey(type)) { _msgC[type] = new List <MsgP>(500); } } #endregion #region - 维护 消息列表 - var msl_ = _msgC[type]; lock (msl_) { if (msl_.Count == 500) { msl_.RemoveAt(0); } msl_.Add(msg_); } #endregion #region - 获得 客户列表 - CltShell[] cls_ = null; lock (_cltL) { if (_cltL.Count > 0) { cls_ = new CltShell[_cltL.Count]; _cltL.Values.CopyTo(cls_, 0); } } #endregion #region - 发送 消息 - if (cls_ != null && cls_.Length > 0) { for (int i = 0; i < cls_.Length; i++) { try { cls_[i].Clt.SendMsg(time, type, vlu); cls_[i] = null; } catch (Exception e) { LogMethod.WriteErrLog(e); LogMethod.WriteRunLog(MSG_SIGN, DateTime.Now, "Invoke client err - " + cls_[i].CltInfo + ". " + e.Message); } } } #endregion #region - 移除 无效客户 - if (cls_ != null && cls_.Length > 0) { for (int i = 0; i < cls_.Length; i++) { if (cls_[i] != null) { RmvCLT(cls_[i].ID); } } } #endregion }
/// <summary> 添加(注册)客户端 </summary> public void AddCLT(Client clt) { CltShell clt_ = null; try { clt_ = new CltShell() { ID = clt.ID, CltInfo = clt.LocalInfo, Clt = clt }; } catch (Exception e) { LogMethod.WriteErrLog(MSG_SIGN, DateTime.Now, e); LogMethod.WriteRunLog(MSG_SIGN, DateTime.Now, "客户端注册失败。" + e.Message); return; } lock (_cltL) { if (!_cltL.ContainsKey(clt.ID)) { lock (_msgC) { foreach (var d in _msgC) { foreach (var l in d.Value) { try { clt.SendMsg(l.Tim, l.Typ, l.Vlu); } catch (Exception e) { LogMethod.WriteErrLog(MSG_SIGN, DateTime.Now, e); LogMethod.WriteRunLog(MSG_SIGN, DateTime.Now, "客户端" + clt_.CltInfo + "注册失败。" + e.Message); return; } } } } lock (_sttC) { foreach (var t in _sttC) { foreach (var k in t.Value) { try { clt.SendStt(k.Value.Tim, k.Value.Typ, k.Value.Key, k.Value.Vlu); } catch (Exception e) { LogMethod.WriteErrLog(MSG_SIGN, DateTime.Now, e); LogMethod.WriteRunLog(MSG_SIGN, DateTime.Now, "客户端" + clt_.CltInfo + "注册失败。" + e.Message); return; } } } } _cltL.Add(clt_.ID, clt_); LogMethod.WriteRunLog(MSG_SIGN, DateTime.Now, "Add client " + clt_.CltInfo + "."); } } }