I have two programes , the client programe will send data to server programe over TCP. Since TCP is stream-based and my data is defined as frame, it occurs that two frames are in the same TCP package.
I am trying to find a solution for this. Here are some ideas, but I'm not sure if they fit my situation.
Implement encoding and decoding by some crate. tokio_util::codec seems to be the best solution. But the question is , the server programe is async (implemented by tokio), the client is sync (just use std::net). The tokio_util can't be used in sync code, is there any other crate could work?
Similar to length_delimited, Send the data length before data. The server read the length first, then read the data of specified length from stream.
let len = stream.read_u64().await.unwrap() as usize; let mut buf = Vec::with_capacity(len); stream.read_exact(&mut buf).await.unwrap();
If so,buf needs to be dynamically allocated every time data reach.I'm worried this will have performance issues.
- Similar to #2 , Copy all data to a buffer when recieved, then read the length and data form the buffer.
But if the frame data is divided into two TCP packages, I need some extra code to solve it.(maybe a state machine is needed)
This seems like a simple question, but I can't find a graceful and simple solution. I really need some advice about it..