Allow arbitrary file sizes
@@ -20,15 +20,20 @@ pub async fn send_message(message: &Message, socket: &mut TcpStream) -> Result<(
/// Send a Vec of bytes. Before the actual bytes are send, the size of the message
/// is transmitted in an header of fixed size (u64).
pub async fn send_bytes(payload: Vec<u8>, socket: &mut TcpStream) -> Result<()> {
- let byte_size = payload.len() as u64;
+ let message_size = payload.len() as u64;
let mut header = vec![];
- header.write_u64::<BigEndian>(byte_size).unwrap();
+ header.write_u64::<BigEndian>(message_size).unwrap();
// Send the request size header first.
// Afterwards send the request.
socket.write_all(&header).await?;
- socket.write_all(&payload).await?;
+
+ // Split the payload into 1.5kbyte chunks (MUT for TCP)
+ let mut iter = payload.chunks(1500);
+ while let Some(chunk) = iter.next() {
+ socket.write_all(chunk).await?;
+ }
Ok(())
}
@@ -36,15 +41,26 @@ pub async fn send_bytes(payload: Vec<u8>, socket: &mut TcpStream) -> Result<()>
/// Receive a byte stream depending on a given header
/// This is the basic protocol beneath all pueue communication
pub async fn receive_bytes(socket: &mut TcpStream) -> Result<Vec<u8>> {
- // Receive the header with the size
+ // Receive the header with the overall message size
let mut header = vec![0; 8];
socket.read(&mut header).await?;
let mut header = Cursor::new(header);
let message_size = header.read_u64::<BigEndian>()? as usize;
- // Receive the payload
- let mut payload_bytes = vec![0; message_size];
- socket.read(&mut payload_bytes).await?;
+ // Buffer for the whole payload
+ let mut payload_bytes = Vec::new();
+
+ // Receive chunks until we reached the expected message size
+ while payload_bytes.len() < message_size {
+ let mut remaining = message_size - payload_bytes.len();
+ if remaining > 1500 {
+ remaining = 1500;
+ }
+ let mut chunk_bytes = vec![0; remaining];
+ socket.read(&mut chunk_bytes).await?;
+ payload_bytes.append(&mut chunk_bytes);
+ }
+
Ok(payload_bytes)
}
