Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
67 changes: 58 additions & 9 deletions network/src/p2p/handler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use super::connection::{
use super::listener::Listener;
use super::{NegotiationMessage, NetworkMessage};
use crate::client::Client;
use crate::p2p::connection::Error as P2PConnectionError;
use crate::session::Session;
use crate::stream::Stream;
use crate::{FiltersControl, NodeId, RoutingTable, SocketAddr};
Expand Down Expand Up @@ -670,7 +671,13 @@ impl IoHandler<Message> for Handler {
io.update_registration(stream_token);
}
});
match con.receive()? {
let received = con.receive();
if let Err(P2PConnectionError::IoError(ioerr)) = &received {
if ioerr.kind() == std::io::ErrorKind::ConnectionAborted {
io.deregister_stream(stream_token);
}
};
match received? {
Some(NetworkMessage::Extension(msg)) => {
let remote_node_id = *self.remote_node_ids.read().get(&stream_token).unwrap_or_else(|| {
unreachable!("Node id for {}:{} must exist", stream_token, con.peer_addr())
Expand Down Expand Up @@ -739,7 +746,13 @@ impl IoHandler<Message> for Handler {
io.update_registration(stream_token);
}
});
match con.receive()? {
let received = con.receive();
if let Err(P2PConnectionError::IoError(ioerr)) = &received {
if ioerr.kind() == std::io::ErrorKind::ConnectionAborted {
io.deregister_stream(stream_token);
}
};
match received? {
Some(NetworkMessage::Extension(msg)) => {
let remote_node_id = *self.remote_node_ids.read().get(&stream_token).unwrap_or_else(|| {
unreachable!("Node id for {}:{} must exist", stream_token, con.peer_addr())
Expand Down Expand Up @@ -784,7 +797,13 @@ impl IoHandler<Message> for Handler {
io.update_registration(stream_token);
}
});
match con.receive()? {
let received = con.receive();
if let Err(P2PConnectionError::IoError(ioerr)) = &received {
if ioerr.kind() == std::io::ErrorKind::ConnectionAborted {
io.deregister_stream(stream_token);
}
};
match received? {
Some(OutgoingMessage::Sync1 {
initiator_pub_key,
network_id,
Expand Down Expand Up @@ -879,7 +898,13 @@ impl IoHandler<Message> for Handler {
}
});
let from = *con.peer_addr();
match con.receive()? {
let received = con.receive();
if let Err(P2PConnectionError::IoError(ioerr)) = &received {
if ioerr.kind() == std::io::ErrorKind::ConnectionAborted {
io.deregister_stream(stream_token);
}
};
match received? {
Some(IncomingMessage::Ack {
recipient_pub_key,
encrypted_nonce,
Expand Down Expand Up @@ -916,32 +941,56 @@ impl IoHandler<Message> for Handler {
Ok(())
}

fn stream_writable(&self, _io: &IoContext<Message>, stream: StreamToken) -> IoHandlerResult<()> {
fn stream_writable(&self, io: &IoContext<Message>, stream: StreamToken) -> IoHandlerResult<()> {
match stream {
FIRST_INBOUND..=LAST_INBOUND => {
if let Some(con) = self.inbound_connections.write().get_mut(&stream) {
con.flush()?;
let flush_result = con.flush();
if let Err(P2PConnectionError::IoError(io_error)) = &flush_result {
if io_error.kind() == std::io::ErrorKind::BrokenPipe {
io.deregister_stream(stream);
}
}
flush_result?;
} else {
cdebug!(NETWORK, "Invalid inbound token({}) on write", stream);
}
}
FIRST_OUTBOUND..=LAST_OUTBOUND => {
if let Some(con) = self.outbound_connections.write().get_mut(&stream) {
con.flush()?;
let flush_result = con.flush();
if let Err(P2PConnectionError::IoError(io_error)) = &flush_result {
if io_error.kind() == std::io::ErrorKind::BrokenPipe {
io.deregister_stream(stream);
}
}
flush_result?;
} else {
cdebug!(NETWORK, "Invalid outbound token({}) on write", stream);
}
}
FIRST_INCOMING..=LAST_INCOMING => {
if let Some(con) = self.incoming_connections.write().get_mut(&stream) {
con.flush()?;
let flush_result = con.flush();
if let Err(P2PConnectionError::IoError(io_error)) = &flush_result {
if io_error.kind() == std::io::ErrorKind::BrokenPipe {
io.deregister_stream(stream);
}
}
flush_result?;
} else {
cdebug!(NETWORK, "Invalid incoming token({}) on write", stream);
}
}
FIRST_OUTGOING..=LAST_OUTGOING => {
if let Some(con) = self.outgoing_connections.write().get_mut(&stream) {
con.flush()?;
let flush_result = con.flush();
if let Err(P2PConnectionError::IoError(io_error)) = &flush_result {
if io_error.kind() == std::io::ErrorKind::BrokenPipe {
io.deregister_stream(stream);
}
}
flush_result?;
} else {
cdebug!(NETWORK, "Invalid outgoing token({}) on write", stream);
}
Expand Down
8 changes: 7 additions & 1 deletion network/src/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,9 @@ impl<Stream: TryRead + TryWrite + PeerAddr + Shutdown> TryStream<Stream> {
assert!(read_size < len_of_len, "{} should be less than {}", read_size, len_of_len);

if let Some(new_read_size) = self.stream.try_read(&mut bytes[(1 + read_size)..=len_of_len])? {
if new_read_size == 0 {
return Err(io::Error::new(io::ErrorKind::ConnectionAborted, "EOF"))
}
read_size += new_read_size;
};
if len_of_len == read_size {
Expand All @@ -128,7 +131,7 @@ impl<Stream: TryRead + TryWrite + PeerAddr + Shutdown> TryStream<Stream> {

if let Some(read_size) = self.stream.try_read(&mut bytes)? {
if read_size == 0 {
return Ok(None)
return Err(io::Error::new(io::ErrorKind::ConnectionAborted, "EOF").into())
}
debug_assert_eq!(1, read_size);
if 0xf8 <= bytes[0] {
Expand Down Expand Up @@ -184,6 +187,9 @@ impl<Stream: TryRead + TryWrite + PeerAddr + Shutdown> TryStream<Stream> {
while remain_length != 0 {
let to_be_read = ::std::cmp::min(remain_length, 1024);
if let Some(read_size) = self.stream.try_read(&mut bytes[0..to_be_read])? {
if read_size == 0 {
return Err(io::Error::new(io::ErrorKind::ConnectionAborted, "EOF").into())
}
result.extend_from_slice(&bytes[..read_size]);
debug_assert!(remain_length >= read_size);
remain_length -= read_size;
Expand Down