public bool ResponseMessage(MessageListener lstn, int id, string message) { try { lstn.Response(id, message); return(true); } catch (Exception ex) { OnNotifyError(lstn, ex); } return(false); }
private void OnNotifyError(MessageListener listener, Exception error) { if (this.NotifyError == null || listener == null) { return; } MessageNotifyErrorEventArgs args = new MessageNotifyErrorEventArgs(listener, error); ThreadPool.QueueUserWorkItem(delegate(object state) { this.NotifyError(this, state as MessageNotifyErrorEventArgs); }, args); }
/// <summary> /// 线程安全的获取当前监听器对应的原有监听器,如果没有,返回空 /// </summary> /// <param name="newListener"></param> /// <returns></returns> private MessageListener GetExistsListener(MessageListener newListener) { lock (_syncLock) { if (_listeners.Count > 0) { int index = _listeners.IndexOf(newListener); if (index >= 0) { return(_listeners[index]); } } return(null); } }
public MessageListener GetListener(string ip, int port) { MessageListener lsn = null; lock (_syncLock) { lsn = _listeners.FirstOrDefault(p => p.FromIP == ip && p.FromPort == port); } //由主程序定期执行 CheckListeners 方法,故此注释下面的代码 dth,2012.5.3 //if (lsn != null && DateTime.Now.Second % 12 == 0) //{ // //检查连接是否还有效 // if (!this.NotifyOneMessage(lsn, 0, "heart beat")) // lsn = null; //} return(lsn); }
public override bool Equals(object obj) { bool eq = base.Equals(obj); if (!eq) { MessageListener lstn = obj as MessageListener; #if (MONO) //下面一行用于mono,FromIP 是sessionid if (this.FromIP == lstn.FromIP && this.FromPort == lstn.FromPort) { eq = true; } #else if (lstn._innerListener.Equals(this._innerListener)) { eq = true; } #endif } return(eq); }
public void AcceptMessage(MessageListener listener) { if (_listeners.Contains(listener)) { if (ListenerAcceptMessage != null) { //由于此方法相关的服务接口特性为 IsOneWay = true ,listener 将很快过期 MessageListener currLsn = this.GetListener(listener.FromIP, listener.FromPort); if (currLsn != null) { currLsn.FromMessage = listener.FromMessage; currLsn.MessageID = listener.MessageID; //string showMsg = currLsn.FromMessage.Length > 1000 ? currLsn.FromMessage.Substring(0, 1000) : currLsn.FromMessage; //Console.WriteLine("MessageListener FromMessage:{0},MessageID:{1}", showMsg, currLsn.MessageID); this.ListenerAcceptMessage(this, new MessageListenerEventArgs(currLsn)); } } } else { OnNotifyError(listener, new Exception("监听器未被注册。")); } }
public string RequestMessage(MessageListener listener) { if (_listeners.Contains(listener)) { if (ListenerRequestMessage != null) { //依赖于外部对消息进行处理,取得ResultText MessageListener currLsn = this.GetListener(listener); currLsn.FromMessage = listener.FromMessage; MessageRequestEventArgs args = new MessageRequestEventArgs(currLsn); ListenerRequestMessage(this, args); return(args.ResultText); } else { return(""); } } else { OnNotifyError(listener, new Exception("监听器未被注册。")); } return(""); }
public void AddListener(MessageListener listener) { AddListener(listener, ""); }
public MessageNotifyErrorEventArgs(MessageListener listener, Exception error) { this.Listener = listener; this.Error = error; }
public MessageRequestEventArgs(MessageListener listener, string messageText) : base(listener, messageText) { this.MessageText = listener.FromMessage; }
public MessageRequestEventArgs(MessageListener listener) : base(listener) { this.MessageText = listener.FromMessage; }
public MessageListenerEventArgs(MessageListener listener, string messageText) { this.Listener = listener; this.MessageText = messageText; }
public MessageListenerEventArgs(MessageListener listener) { this.Listener = listener; }