private void OnMessageReceived(object sender, DuplexChannelMessageEventArgs e) { using (EneterTrace.Entering()) { try { using (ThreadLock.Lock(myResponseReceiverContexts)) { TResponseReceiverContext aResponseReceiver = GetResponseReceiver(e.ResponseReceiverId); if (aResponseReceiver == null) { // Note: the response receiver was just disconnected. return; } aResponseReceiver.LastReceiveTime = DateTime.Now; } // Deserialize the incoming message. MonitorChannelMessage aMessage = mySerializer.Deserialize <MonitorChannelMessage>(e.Message); // if the message is ping, then response. if (aMessage.MessageType == MonitorChannelMessageType.Message) { // Notify the incoming message. DuplexChannelMessageEventArgs aMsg = new DuplexChannelMessageEventArgs(e.ChannelId, aMessage.MessageContent, e.ResponseReceiverId, e.SenderAddress); Dispatcher.Invoke(() => NotifyGeneric(MessageReceived, aMsg, true)); } } catch (Exception err) { EneterTrace.Error(TracedObject + ErrorHandler.FailedToReceiveMessage, err); } } }
private TResponseReceiverContext GetResponseReceiver(string responseReceiverId) { using (EneterTrace.Entering()) { TResponseReceiverContext aResponseReceiverContext = myResponseReceiverContexts.FirstOrDefault(x => x.ResponseReceiverId == responseReceiverId); return(aResponseReceiverContext); } }
private TResponseReceiverContext CreateResponseReceiver(string responseReceiverId, string clientAddress) { using (EneterTrace.Entering()) { TResponseReceiverContext aResponseReceiver = new TResponseReceiverContext(responseReceiverId, clientAddress); myResponseReceiverContexts.Add(aResponseReceiver); if (myResponseReceiverContexts.Count == 1) { myCheckTimer.Change(myPingFrequency); } return(aResponseReceiver); } }
private void OnResponseReceiverConnected(object sender, ResponseReceiverEventArgs e) { using (EneterTrace.Entering()) { using (ThreadLock.Lock(myResponseReceiverContexts)) { TResponseReceiverContext aResponseReceiver = GetResponseReceiver(e.ResponseReceiverId); if (aResponseReceiver != null) { EneterTrace.Warning(TracedObject + "received open connection from already connected response receiver."); return; } else { CreateResponseReceiver(e.ResponseReceiverId, e.SenderAddress); } } // Notify response receiver connected. Dispatcher.Invoke(() => NotifyGeneric(ResponseReceiverConnected, e, false)); } }