Trait pea2pea::protocols::Reading [−][src]
pub trait Reading: Pea2Pea where
Self: Clone + Send + Sync + 'static, {
type Message: Send;
fn read_message<R: Read>(
&self,
source: SocketAddr,
reader: &mut R
) -> Result<Option<Self::Message>>;
fn process_message<'life0, 'async_trait>(
&'life0 self,
source: SocketAddr,
message: Self::Message
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where
'life0: 'async_trait,
Self: 'async_trait;
fn enable_reading(&self) { ... }
fn read_from_stream<'life0, 'life1, 'life2, 'life3, 'async_trait, R: AsyncRead + Unpin + Send>(
&'life0 self,
addr: SocketAddr,
buffer: &'life1 mut Vec<u8>,
reader: &'life2 mut R,
message_sender: &'life3 Sender<Self::Message>
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>>
where
R: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Self: Sync + 'async_trait,
{ ... }
fn process_buffer(
&self,
addr: SocketAddr,
buffer: &mut Vec<u8>,
left: usize,
message_sender: &Sender<Self::Message>
) -> Result<()> { ... }
}
Expand description
Can be used to specify and enable reading, i.e. receiving inbound messages.
If Handshake
is enabled too, it goes into force only after the handshake has been concluded.
Associated Types
Required methods
fn read_message<R: Read>(
&self,
source: SocketAddr,
reader: &mut R
) -> Result<Option<Self::Message>>
fn read_message<R: Read>(
&self,
source: SocketAddr,
reader: &mut R
) -> Result<Option<Self::Message>>
Reads a single message from the given reader; Ok(None)
indicates that the message is incomplete,
i.e. further reads from the stream must be performed in order to produce the whole message. An Err
returned here indicates an invalid message which, depending on the configured list of fatal errors,
can cause the related connection to be dropped.
fn process_message<'life0, 'async_trait>(
&'life0 self,
source: SocketAddr,
message: Self::Message
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where
'life0: 'async_trait,
Self: 'async_trait,
fn process_message<'life0, 'async_trait>(
&'life0 self,
source: SocketAddr,
message: Self::Message
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where
'life0: 'async_trait,
Self: 'async_trait,
Processes an inbound message. Can be used to update state, send replies etc.
Provided methods
fn enable_reading(&self)
fn enable_reading(&self)
Prepares the node to receive messages; failures to read from a connection’s stream are penalized by a timeout
defined in Config
, while the configured fatal errors result in an immediate disconnect (in order to e.g. avoid
accidentally reading “borked” messages).
fn read_from_stream<'life0, 'life1, 'life2, 'life3, 'async_trait, R: AsyncRead + Unpin + Send>(
&'life0 self,
addr: SocketAddr,
buffer: &'life1 mut Vec<u8>,
reader: &'life2 mut R,
message_sender: &'life3 Sender<Self::Message>
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where
R: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Self: Sync + 'async_trait,
fn read_from_stream<'life0, 'life1, 'life2, 'life3, 'async_trait, R: AsyncRead + Unpin + Send>(
&'life0 self,
addr: SocketAddr,
buffer: &'life1 mut Vec<u8>,
reader: &'life2 mut R,
message_sender: &'life3 Sender<Self::Message>
) -> Pin<Box<dyn Future<Output = Result<()>> + Send + 'async_trait>> where
R: 'async_trait,
'life0: 'async_trait,
'life1: 'async_trait,
'life2: 'async_trait,
'life3: 'async_trait,
Self: Sync + 'async_trait,
Performs a read from the given reader. The default implementation is buffered; it sacrifices a bit of simplicity for better performance.
fn process_buffer(
&self,
addr: SocketAddr,
buffer: &mut Vec<u8>,
left: usize,
message_sender: &Sender<Self::Message>
) -> Result<()>
fn process_buffer(
&self,
addr: SocketAddr,
buffer: &mut Vec<u8>,
left: usize,
message_sender: &Sender<Self::Message>
) -> Result<()>
Attempts to isolate full messages from the connection’s read buffer using Reading::read_message
. Once
no more messages can be extracted, it preserves any leftover bytes and moves them to the beginning of the
buffer, and further reads from the stream are appended to them. Read messages are sent to a separate message
processing task in order not to block further reads.