public void Listen() { try { if (string.IsNullOrEmpty(ChannelName)) { throw new Exception("Server.Listen 需要设置通道名称。"); } if (_instances.ContainsKey(ChannelName)) { throw new Exception("Server.Listen 无法监听相同的通道名称。(" + ChannelName + ")"); } // 设置Remoting应用程序名 if (_isFirstListen) { RemotingConfiguration.ApplicationName //= "PLDC_MntBll"; //= Process.GetCurrentProcess().ProcessName; = ChannelName; // 设置formatter BinaryServerFormatterSinkProvider formatter; formatter = new BinaryServerFormatterSinkProvider(); formatter.TypeFilterLevel = TypeFilterLevel.Full; // 设置通道名称和端口 IDictionary propertyDic = new Hashtable(); propertyDic["name"] = ChannelName; propertyDic["port"] = Port; // 注册通道 tcpChnl = new TcpChannel(propertyDic, null, formatter); ChannelServices.RegisterChannel(tcpChnl, false); // 注册类型 Type t = typeof(Server); RemotingConfiguration.RegisterWellKnownServiceType( t, "ServerActivated", WellKnownObjectMode.Singleton); _isFirstListen = false; } _instances[ChannelName] = this; } catch (Exception e) { LogMethod.WriteErrLog(e); } }
public void SendStt(DateTime time, string type, string key, object o) { try { if (_vi != null) { new SendSttHandle(_vi.RcvStt).BeginInvoke(time, type, key, o, null, null); //_vi.RcvStt(time, type, key, o); } } catch (Exception e) { LogMethod.WriteErrLog("IClientView", DateTime.Now, e); } }
/// <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 + "."); } } }