@ -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..])
|
bincode::deserialize::<IpEchoServerMessage>(&data[4..])
|
||||||
.map(Some)
|
.map(Some)
|
||||||
.or_else(|err| {
|
.or_else(|err| {
|
||||||
|
@ -29,14 +29,15 @@ fn ip_echo_server_request(
|
|||||||
let timeout = Duration::new(5, 0);
|
let timeout = Duration::new(5, 0);
|
||||||
TcpStream::connect_timeout(ip_echo_server_addr, timeout)
|
TcpStream::connect_timeout(ip_echo_server_addr, timeout)
|
||||||
.and_then(|mut stream| {
|
.and_then(|mut stream| {
|
||||||
let msg = bincode::serialize(&msg).expect("serialize IpEchoServerMessage");
|
let mut bytes = vec![0; 4]; // Start with 4 null bytes to avoid looking like an HTTP GET/POST request
|
||||||
// Start with 4 null bytes to avoid looking like an HTTP GET/POST request
|
|
||||||
stream.write_all(&[0; 4])?;
|
|
||||||
|
|
||||||
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
|
// End with '\n' to make this request look HTTP-ish and tickle an error response back
|
||||||
stream.write_all(b"\n")?;
|
// from an HTTP server
|
||||||
|
bytes.push(b'\n');
|
||||||
|
|
||||||
|
stream.write_all(&bytes)?;
|
||||||
stream.shutdown(std::net::Shutdown::Write)?;
|
stream.shutdown(std::net::Shutdown::Write)?;
|
||||||
stream
|
stream
|
||||||
.set_read_timeout(Some(Duration::new(10, 0)))
|
.set_read_timeout(Some(Duration::new(10, 0)))
|
||||||
|
Reference in New Issue
Block a user