outbound chunking

This commit is contained in:
2026-03-20 17:32:26 +07:00
parent 5aed291375
commit 53c1bbbc3c
+36 -13
View File
@@ -117,22 +117,45 @@ impl TunnelEngine {
codec: &mut Codec, codec: &mut Codec,
msg: MuxMessage, msg: MuxMessage,
) -> Result<(), String> { ) -> Result<(), String> {
match codec.encrypt_data(msg.stream_id, msg.frame_type, msg.data) { const MAX_CHUNK_SIZE: usize = 16000;
Ok(pkt) => {
outbound let mut data = msg.data;
.write_all(&pkt) let stream_id = msg.stream_id;
.await let frame_type = msg.frame_type;
.map_err(|e| {
error!(stream_id = msg.stream_id, error = %e, "Failed to write encrypted data to network"); if data.is_empty() {
match codec.encrypt_data(stream_id, frame_type.clone(), Bytes::new()) {
Ok(pkt) => {
outbound.write_all(&pkt).await.map_err(|e| e.to_string())?;
}
Err(e) => {
error!(stream_id, error = ?e, "Encryption failed for empty message");
return Err(format!("Encryption error: {:?}", e));
}
}
return Ok(());
}
while !data.is_empty() {
let chunk_size = std::cmp::min(data.len(), MAX_CHUNK_SIZE);
let chunk = data.split_to(chunk_size);
match codec.encrypt_data(stream_id, frame_type.clone(), chunk) {
Ok(pkt) => {
outbound.write_all(&pkt).await.map_err(|e| {
error!(stream_id, error = %e, "Failed to write encrypted data to network");
e.to_string() e.to_string()
})?; })?;
debug!(stream_id = msg.stream_id, "Outbound packet sent"); }
Ok(()) Err(e) => {
} error!(stream_id, error = ?e, "Encryption failed for chunked message");
Err(e) => { return Err(format!("Encryption error: {:?}", e));
error!(stream_id = msg.stream_id, error = ?e, "Encryption failed for outbound message"); }
Err(format!("Encryption error: {:?}", e))
} }
} }
debug!(stream_id, "Outbound packet sent successfully");
Ok(())
} }
} }