Trait pea2pea::protocols::Reading [−][src]
pub trait Reading: Pea2Pea where
Self: Clone + Send + Sync + 'static, { type Message: Send; fn read_message(
&self,
source: SocketAddr,
buffer: &[u8]
) -> Result<Option<(Self::Message, usize)>>; 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 [u8],
reader: &'life2 mut R,
carry: usize,
message_sender: &'life3 Sender<Self::Message>
) -> Pin<Box<dyn Future<Output = Result<usize>> + 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_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: Sync + 'async_trait, { ... } }
Expand description
Can be used to specify and enable reading, i.e. receiving inbound messages. If handshaking is enabled too, it goes into force only after the handshake has been concluded.
Associated Types
Required methods
Reads a single message from the given buffer; 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.
Alongside the message it returns the number of bytes the read message occupied in the buffer. An Err
returned here will result in the associated connection being dropped.
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 NodeConfig
, while broken/unreadable messages result in an immediate disconnect (in order to 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 [u8],
reader: &'life2 mut R,
carry: usize,
message_sender: &'life3 Sender<Self::Message>
) -> Pin<Box<dyn Future<Output = Result<usize>> + 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 [u8],
reader: &'life2 mut R,
carry: usize,
message_sender: &'life3 Sender<Self::Message>
) -> Pin<Box<dyn Future<Output = Result<usize>> + 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. Read messages are sent to a message processing task in order to enable
faster reads. Returns the number of pending bytes left in the buffer in case of an incomplete read; they
should be provided to the medthod on the next call as carry
.
Processes an inbound message. Can be used to update state, send replies etc.