parent
5ac435325b
commit
d38f3f664f
|
@ -70,6 +70,19 @@ pub fn ip_echo_server(tcp: std::net::TcpListener) -> IpEchoServer {
|
|||
));
|
||||
}
|
||||
|
||||
let expected_len =
|
||||
bincode::serialized_size(&IpEchoServerMessage::default()).unwrap() as usize;
|
||||
let actual_len = data[4..].len();
|
||||
if actual_len < expected_len {
|
||||
return Err(io::Error::new(
|
||||
io::ErrorKind::Other,
|
||||
format!(
|
||||
"Request too short, actual {} < expected {}",
|
||||
actual_len, expected_len
|
||||
),
|
||||
));
|
||||
}
|
||||
|
||||
bincode::deserialize::<IpEchoServerMessage>(&data[4..])
|
||||
.map(Some)
|
||||
.or_else(|err| {
|
||||
|
|
|
@ -29,14 +29,15 @@ fn ip_echo_server_request(
|
|||
let timeout = Duration::new(5, 0);
|
||||
TcpStream::connect_timeout(ip_echo_server_addr, timeout)
|
||||
.and_then(|mut stream| {
|
||||
let msg = bincode::serialize(&msg).expect("serialize IpEchoServerMessage");
|
||||
// Start with 4 null bytes to avoid looking like an HTTP GET/POST request
|
||||
stream.write_all(&[0; 4])?;
|
||||
let mut bytes = vec![0; 4]; // Start with 4 null bytes to avoid looking like an HTTP GET/POST request
|
||||
|
||||
stream.write_all(&msg)?;
|
||||
bytes.append(&mut bincode::serialize(&msg).expect("serialize IpEchoServerMessage"));
|
||||
|
||||
// Send a '\n' to make this request look HTTP-ish and tickle an error response back from an HTTP server
|
||||
stream.write_all(b"\n")?;
|
||||
// End with '\n' to make this request look HTTP-ish and tickle an error response back
|
||||
// from an HTTP server
|
||||
bytes.push(b'\n');
|
||||
|
||||
stream.write_all(&bytes)?;
|
||||
stream.shutdown(std::net::Shutdown::Write)?;
|
||||
stream
|
||||
.set_read_timeout(Some(Duration::new(10, 0)))
|
||||
|
|
Loading…
Reference in New Issue