private void OnAnswerReceived(object?sender, GameIdEventArgs e) { if (string.Equals(e.GameId, GameId, StringComparison.Ordinal)) { AnswerReceived?.Invoke(this, EventArgs.Empty); } }
/// <summary> /// Called by the MulticastClient when a DNS message is received. /// </summary> /// <param name="sender"> /// The <see cref="MulticastClient"/> that got the message. /// </param> /// <param name="result"> /// The received message <see cref="UdpReceiveResult"/>. /// </param> /// <remarks> /// Decodes the <paramref name="result"/> and then raises /// either the <see cref="QueryReceived"/> or <see cref="AnswerReceived"/> event. /// <para> /// Multicast DNS messages received with an OPCODE or RCODE other than zero /// are silently ignored. /// </para> /// <para> /// If the message cannot be decoded, then the <see cref="MalformedMessage"/> /// event is raised. /// </para> /// </remarks> public void OnDnsMessage(object sender, UdpReceiveResult result) { // If recently received, then ignore. // A message has been recently received, if its content, as well as the remote endpoint(!) match, otherwise messages from different senders can be considered equal. var receivedMessage = new byte[result.Buffer.Length + result.RemoteEndPoint.Address.GetAddressBytes().Length]; result.Buffer.CopyTo(receivedMessage, 0); result.RemoteEndPoint.Address.GetAddressBytes().CopyTo(receivedMessage, result.Buffer.Length); if (IgnoreDuplicateMessages && !receivedMessages.TryAdd(receivedMessage)) { // Not continuing here because it looks like a duplicate message return; } var msg = new Message(); try { msg.Read(result.Buffer, 0, result.Buffer.Length); } catch (Exception e) { log.Warn("Received malformed message", e); MalformedMessage?.Invoke(this, result.Buffer); return; // eat the exception } if (msg.Opcode != MessageOperation.Query || msg.Status != MessageStatus.NoError) { return; } // Dispatch the message. try { if (msg.IsQuery && msg.Questions.Count > 0) { QueryReceived?.Invoke(this, new MessageEventArgs { Message = msg, RemoteEndPoint = result.RemoteEndPoint }); } else if (msg.IsResponse && msg.Answers.Count > 0) { AnswerReceived?.Invoke(this, new MessageEventArgs { Message = msg, RemoteEndPoint = result.RemoteEndPoint }); } } catch (Exception e) { log.Error("Receive handler failed", e); // eat the exception } }
/// <summary> /// Called by the MulticastClient when a DNS message is received. /// </summary> /// <param name="sender"> /// The <see cref="MulticastClient"/> that got the message. /// </param> /// <param name="result"> /// The received message <see cref="UdpReceiveResult"/>. /// </param> /// <remarks> /// Decodes the <paramref name="result"/> and then raises /// either the <see cref="QueryReceived"/> or <see cref="AnswerReceived"/> event. /// <para> /// Multicast DNS messages received with an OPCODE or RCODE other than zero /// are silently ignored. /// </para> /// <para> /// If the message cannot be decoded, then the <see cref="MalformedMessage"/> /// event is raised. /// </para> /// </remarks> public void OnDnsMessage(object sender, UdpReceiveResult result) { // If recently received, then ignore. if (IgnoreDuplicateMessages && !_receivedMessages.TryAdd(result.Buffer)) { return; } if (GetLinkLocalAddresses().Any(a => a == result.RemoteEndPoint.Address)) { return; } var msg = new Message(); try { msg.Read(result.Buffer, 0, result.Buffer.Length); } catch (Exception e) { Log.LogWarning("Received malformed message", e); MalformedMessage?.Invoke(this, result.Buffer); return; // eat the exception } if (msg.Opcode != MessageOperation.Query || msg.Status != MessageStatus.NoError) { return; } // Dispatch the message. try { if (msg.IsQuery && msg.Questions.Count > 0) { QueryReceived?.Invoke(this, new MessageEventArgs { Message = msg, RemoteEndPoint = result.RemoteEndPoint }); } else if (msg.IsResponse && msg.Answers.Count > 0) { AnswerReceived?.Invoke(this, new MessageEventArgs { Message = msg, RemoteEndPoint = result.RemoteEndPoint }); } } catch (Exception e) { Log.LogError("Receive handler failed", e); // eat the exception } }
/// <summary> /// Called by the MulticastClient when a DNS message is received. /// </summary> /// <param name="sender"> /// The <see cref="MulticastClient"/> that got the message. /// </param> /// <param name="result"> /// The received message <see cref="UdpReceiveResult"/>. /// </param> /// <remarks> /// Decodes the <paramref name="result"/> and then raises /// either the <see cref="QueryReceived"/> or <see cref="AnswerReceived"/> event. /// <para> /// Multicast DNS messages received with an OPCODE or RCODE other than zero /// are silently ignored. /// </para> /// <para> /// If the message cannot be decoded, then the <see cref="MalformedMessage"/> /// event is raised. /// </para> /// </remarks> public void OnDnsMessage(object sender, UdpReceiveResult result) { // If recently received, then ignore. if (IgnoreDuplicateMessages && !receivedMessages.TryAdd(result.Buffer)) { return; } var msg = new Message(); try { msg.Read(result.Buffer, 0, result.Buffer.Length); } catch (Exception e) { System.Diagnostics.Trace.TraceWarning($"Received malformed message : {e.GetBaseException().Message}"); MalformedMessage?.Invoke(this, result.Buffer); return; // eat the exception } if (msg.Opcode != MessageOperation.Query || msg.Status != MessageStatus.NoError) { return; } // Dispatch the message. try { if (msg.IsQuery && msg.Questions.Count > 0) { QueryReceived?.Invoke(this, new MessageEventArgs { Message = msg, RemoteEndPoint = result.RemoteEndPoint }); } else if (msg.IsResponse && msg.Answers.Count > 0) { AnswerReceived?.Invoke(this, new MessageEventArgs { Message = msg, RemoteEndPoint = result.RemoteEndPoint }); } } catch (Exception e) { System.Diagnostics.Trace.TraceError($"Receive handler failed {e.GetBaseException().Message}"); // eat the exception } }
/// <summary> /// Called by the listener when a DNS message is received. /// </summary> /// <param name="datagram"> /// The received message. /// </param> /// <param name="length"> /// The length of the messages. /// </param> /// <remarks> /// Decodes the <paramref name="datagram"/> and then raises /// either the <see cref="QueryReceived"/> or <see cref="AnswerReceived"/> event. /// <para> /// Multicast DNS messages received with an OPCODE or RCODE other than zero /// are silently ignored. /// </para> /// </remarks> void OnDnsMessage(byte[] datagram, int length) { var msg = new Message(); try { msg.Read(datagram, 0, length); } catch (Exception e) { log.Warn("Received malformed message", e); return; // eat the exception } if (msg.Opcode != MessageOperation.Query || msg.Status != MessageStatus.NoError) { return; } // Dispatch the message. try { if (msg.IsQuery && msg.Questions.Count > 0) { QueryReceived?.Invoke(this, new MessageEventArgs { Message = msg }); } else if (msg.IsResponse && msg.Answers.Count > 0) { AnswerReceived?.Invoke(this, new MessageEventArgs { Message = msg }); } } catch (Exception e) { log.Error("Receive handler failed", e); // eat the exception } }
public async Task <EnOceanPacket> SendTelegram(EnOceanTelegram telegram) { _stream.Pause(); telegram.SetIdBase(_idBase.Span); var packet = telegram.ToPacket(); PacketSent?.Invoke(this, new PacketSentEventArgs(packet, telegram)); _stream.WriteFrame(packet); var p = await _stream.ReadFrame(); if (p != null) { AnswerReceived?.Invoke(this, new AnswerReceviedEventArgs(p)); } _stream.Continue(); return(p); }
protected virtual void OnAnswerReceived(QuestionResponseBto answer) => AnswerReceived?.Invoke(this, answer);
public void SubmitAnswer(string gameId, Answer answer) { UpdateGameState(gameId, gs => new AnswerSubmittedGameState(answer, gs)); AnswerReceived?.Invoke(this, new GameIdEventArgs(gameId)); }