-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Closed
Labels
Description
Writing a simple "request-response" protocol (where each request opens a new substream) is way too complicated at the moment, despite the OneShotHandler.
Instead, in my opinion there should be some sort of helper structs.
Here is an API sketch:
impl<T: OutboundUpgrade> Request<T> {
pub fn new(protocol: T) -> Self { ... }
pub fn start_request(&mut self, destination: &PeerId) -> Result<..., ...>;
pub fn in_progress(&self) -> impl Iterator<Item = InProgress> { ... }
}
impl<T: OutboundUpgrade> NetworkBehaviour for Request<T> { ... }
impl<T: InboundUpgrade> Respond<T> {
pub fn pending(&self) -> impl Iterator<Item = Pending> { ... }
}
impl<T: InboundUpgrade> NetworkBehaviour for Respond<T> { ... }
/// `Pending` borrows an entry within the `Respond`
impl<'a> Pending<'a> {
pub fn source(&self) -> &PeerId { ... }
pub fn request(&self) -> &... { ... }
pub fn respond(self, response: ...) { ... }
}Thanks to this, it would be possible to write just an implementation of OutboundUpgrade and InboundUpgrade.
This could also be combined with libp2p_core::upgrade::from_fn to create protocols in one line of code, provided Rust gets existential types.
Related: #1530