Intialize stream reader only once.
This commit is contained in:
parent
40431f3d4f
commit
aa46618da7
|
@ -36,14 +36,15 @@ fn main() {
|
||||||
let _ = stream.write_all(encode::serialize(&first_message).as_slice());
|
let _ = stream.write_all(encode::serialize(&first_message).as_slice());
|
||||||
println!("Sent version message");
|
println!("Sent version message");
|
||||||
|
|
||||||
// Wait for reply and close stream
|
// Setup StreamReader
|
||||||
|
let read_stream = stream.try_clone().unwrap();
|
||||||
|
let mut stream_reader = StreamReader::new(read_stream, None);
|
||||||
loop {
|
loop {
|
||||||
let reply = StreamReader::new(&mut stream, None).read_next();
|
// Loop an retrieve new messages
|
||||||
let unwrapped: message::RawNetworkMessage = reply.unwrap();
|
let reply: message::RawNetworkMessage = stream_reader.read_next().unwrap();
|
||||||
match unwrapped.payload {
|
match reply.payload {
|
||||||
message::NetworkMessage::Version(_) => {
|
message::NetworkMessage::Version(_) => {
|
||||||
println!("Received version message: {:?}", unwrapped.payload);
|
println!("Received version message: {:?}", reply.payload);
|
||||||
|
|
||||||
let second_message = message::RawNetworkMessage {
|
let second_message = message::RawNetworkMessage {
|
||||||
magic: constants::Network::Bitcoin.magic(),
|
magic: constants::Network::Bitcoin.magic(),
|
||||||
|
@ -54,11 +55,11 @@ fn main() {
|
||||||
println!("Sent verack message");
|
println!("Sent verack message");
|
||||||
}
|
}
|
||||||
message::NetworkMessage::Verack => {
|
message::NetworkMessage::Verack => {
|
||||||
println!("Received verack message: {:?}", unwrapped.payload);
|
println!("Received verack message: {:?}", reply.payload);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
_ => {
|
_ => {
|
||||||
println!("Received unknown message: {:?}", unwrapped.payload);
|
println!("Received unknown message: {:?}", reply.payload);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue