private async Task SendPacket(RadioPacket packet_to_send, int timeout = 25000) { int start_time = 0; this.unique_packets++; Bytes received = null; while (start_time == 0 || Environment.TickCount - start_time < timeout) { try { Debug.WriteLine($"SEND PKT {packet_to_send}"); try { received = await RileyLink.SendAndGetPacket(packet_to_send.get_data(), 0, 0, 300, 3, 300); } catch (OmniCoreTimeoutException) { Debug.WriteLine("Silence fell."); this.Pod.PacketSequence = (this.Pod.PacketSequence + 1) % 32; return; } if (start_time == 0) { start_time = Environment.TickCount; } var p = this.GetPacket(received); if (p == null) { this.bad_packets++; RileyLink.TxLevelDown(); continue; } if (p.address != this.Pod.RadioAddress) { this.bad_packets++; Debug.WriteLine("RECV PKT ADDR MISMATCH"); RileyLink.TxLevelDown(); continue; } this.last_packet_timestamp = Environment.TickCount; if (this.last_received_packet != null && p.type == this.last_received_packet.type && p.sequence == this.last_received_packet.sequence) { this.repeated_receives++; Debug.WriteLine("RECV PKT previous"); RileyLink.TxLevelUp(); continue; } Debug.WriteLine($"RECV PKT {p}"); Debug.WriteLine($"RECEIVED unexpected packet"); this.protocol_errors++; this.last_received_packet = p; this.Pod.PacketSequence = (p.sequence + 1) % 32; packet_to_send.with_sequence(this.Pod.PacketSequence); start_time = Environment.TickCount; continue; } catch (PacketRadioException pre) { this.radio_errors++; Debug.WriteLine($"Radio error during send, retrying {pre}"); await RileyLink.Reset(); start_time = Environment.TickCount; } catch (Exception) { throw; } } Debug.WriteLine("Exceeded timeout while waiting for silence to fall"); }
private async Task <RadioPacket> ExchangePackets(RadioPacket packet_to_send, PacketType expected_type, int timeout = 10000) { int start_time = 0; Bytes received = null; Debug.WriteLine($"SEND PKT {packet_to_send}"); while (start_time == 0 || Environment.TickCount - start_time < timeout) { try { if (this.last_packet_timestamp == 0 || (Environment.TickCount - this.last_packet_timestamp) > 2000) { received = await RileyLink.SendAndGetPacket(packet_to_send.get_data(), 0, 0, 300, 1, 300); } else { received = await RileyLink.SendAndGetPacket(packet_to_send.get_data(), 0, 0, 120, 0, 40); } } catch (OmniCoreTimeoutException) { received = null; } finally { if (start_time == 0) { start_time = Environment.TickCount; } else { this.repeated_sends += 1; } } if (received == null) { this.receive_timeouts++; Debug.WriteLine("RECV PKT None"); RileyLink.TxLevelUp(); continue; } var p = this.GetPacket(received); if (p == null) { this.bad_packets++; RileyLink.TxLevelDown(); continue; } Debug.WriteLine($"RECV PKT {p}"); if (p.address != this.Pod.RadioAddress) { this.bad_packets++; Debug.WriteLine("RECV PKT ADDR MISMATCH"); RileyLink.TxLevelDown(); continue; } this.last_packet_timestamp = Environment.TickCount; if (this.last_received_packet != null && p.sequence == this.last_received_packet.sequence && p.type == this.last_received_packet.type) { this.repeated_receives++; Debug.WriteLine("RECV PKT previous"); RileyLink.TxLevelUp(); continue; } this.last_received_packet = p; this.Pod.PacketSequence = (p.sequence + 1) % 32; if (p.type != expected_type) { Debug.WriteLine("RECV PKT unexpected type"); this.protocol_errors++; throw new ErosProtocolException("Unexpected packet type received", p); } if (p.sequence != (packet_to_send.sequence + 1) % 32) { this.Pod.PacketSequence = (p.sequence + 1) % 32; Debug.WriteLine("RECV PKT unexpected sequence"); this.last_received_packet = p; this.protocol_errors++; throw new ErosProtocolException("Incorrect packet sequence received", p); } return(p); } throw new OmniCoreTimeoutException("Exceeded timeout while send and receive"); }