Skip to content

Commit 79a6514

Browse files
Echo on success (#82)
* Handle EOF when DuplexStream is closed, avoiding a panic. Fixes #75 * Additional protection from panics on invalid input buffer lengths. * Only echo a mesh packet back to the PacketRouter if the sending was actually successful. If sending failed, the call to send_* will return an Error as before * Modify comments slightly * cargo fmt
1 parent 5899714 commit 79a6514

1 file changed

Lines changed: 13 additions & 9 deletions

File tree

src/connections/stream_api.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ impl<State> ConnectedStreamApi<State> {
118118
///
119119
/// # Arguments
120120
///
121-
/// * `packet_router` - A generic packet router field that implements the `PacketRouter` trait.
121+
/// * `packet_router` - A struct that implements the `PacketRouter` trait.
122122
/// * `byte_data` - A `Vec<u8>` containing the byte data to send.
123123
/// * `port_num` - A `PortNum` enum that specifies the port number to send the packet on.
124124
/// * `destination` - A `PacketDestination` enum that specifies the destination of the packet.
@@ -210,18 +210,22 @@ impl<State> ConnectedStreamApi<State> {
210210
..Default::default()
211211
};
212212

213+
mesh_packet.rx_time = current_epoch_secs_u32();
214+
215+
let payload_variant = Some(protobufs::to_radio::PayloadVariant::Packet(
216+
mesh_packet.clone(),
217+
));
218+
self.send_to_radio_packet(payload_variant).await?;
219+
220+
// If the sending was successful, echo it back to the client via the `PacketRouter`
213221
if echo_response {
214-
mesh_packet.rx_time = current_epoch_secs_u32();
215-
packet_router
216-
.handle_mesh_packet(mesh_packet.clone())
217-
.map_err(|e| Error::PacketHandlerFailure {
222+
packet_router.handle_mesh_packet(mesh_packet).map_err(|e| {
223+
Error::PacketHandlerFailure {
218224
source: Box::new(e),
219-
})?;
225+
}
226+
})?;
220227
}
221228

222-
let payload_variant = Some(protobufs::to_radio::PayloadVariant::Packet(mesh_packet));
223-
self.send_to_radio_packet(payload_variant).await?;
224-
225229
Ok(())
226230
}
227231

0 commit comments

Comments
 (0)