public Packet(uint address, RadioPacketType type, int sequence, Bytes body) { this.address = address; this.type = type; this.sequence = sequence % 32; this.body = body; }
private async Task <Packet> ExchangePackets(Packet packet_to_send, RadioPacketType expected_type, int timeout = 10000) { int start_time = 0; bool first = true; Bytes received = null; this.PacketLogger.Log($"SEND PKT {packet_to_send}"); while (start_time == 0 || Environment.TickCount - start_time < timeout) { if (first) { first = false; } else { this.repeated_sends += 1; } if (this.last_packet_timestamp == 0 || (Environment.TickCount - this.last_packet_timestamp) > 2000) { received = await this.PacketRadio.SendAndGetPacket(packet_to_send.get_data(), 0, 0, 300, 1, 300); } else { received = await this.PacketRadio.SendAndGetPacket(packet_to_send.get_data(), 0, 0, 120, 0, 40); } if (start_time == 0) { start_time = Environment.TickCount; } this.PacketLogger.Log($"SEND PKT {packet_to_send}"); if (received == null) { this.receive_timeouts++; this.PacketLogger.Log("RECV PKT None"); this.PacketRadio.TxLevelUp(); continue; } var p = this.GetPacket(received); if (p == null) { this.bad_packets++; this.PacketRadio.TxLevelDown(); continue; } this.PacketLogger.Log($"RECV PKT {p}"); if (p.address != this.Pod.radio_address) { this.bad_packets++; this.PacketLogger.Log("RECV PKT ADDR MISMATCH"); this.PacketRadio.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++; this.PacketLogger.Log("RECV PKT previous"); this.PacketRadio.TxLevelUp(); continue; } this.last_received_packet = p; this.Pod.radio_packet_sequence = (p.sequence + 1) % 32; if (p.type != expected_type) { this.PacketLogger.Log("RECV PKT unexpected type"); this.protocol_errors++; throw new ProtocolException("Unexpected packet type received", p); } if (p.sequence != (packet_to_send.sequence + 1) % 32) { this.Pod.radio_packet_sequence = (p.sequence + 1) % 32; this.PacketLogger.Log("RECV PKT unexpected sequence"); this.last_received_packet = p; this.protocol_errors++; throw new ProtocolException("Incorrect packet sequence received", p); } return(p); } throw new OmnipyTimeoutException("Exceeded timeout while send and receive"); }