@@ -506,6 +506,22 @@ impl<I: Channels<S>, S: Service> Deref for WithChannels<I, S> {
506
506
}
507
507
}
508
508
509
+ /// A client to the service `S` using the local message type `M` and the remote
510
+ /// message type `R`.
511
+ ///
512
+ /// `R` is typically a serializable enum with a case for each possible message
513
+ /// type. It can be thought of as the definition of the protocol.
514
+ ///
515
+ /// `M` is typically an enum with a case for each possible message type, where
516
+ /// each case is a `WithChannels` struct that extends the inner protocol message
517
+ /// with a local tx and rx channel as well as a tracing span to allow for
518
+ /// keeping tracing context across async boundaries.
519
+ ///
520
+ /// In some cases, `M` and `R` can be enums for a subset of the protocol. E.g.
521
+ /// if you have a subsystem that only handles a part of the messages.
522
+ ///
523
+ /// The service type `S` provides a scope for the protocol messages. It exists
524
+ /// so you can use the same message with multiple services.
509
525
#[ derive( Debug ) ]
510
526
pub struct Client < M , R , S > ( ClientInner < M > , PhantomData < ( R , S ) > ) ;
511
527
@@ -528,11 +544,16 @@ impl<M, R, S> From<tokio::sync::mpsc::Sender<M>> for Client<M, R, S> {
528
544
}
529
545
530
546
impl < M , R , S > Client < M , R , S > {
547
+ /// Create a new client to a remote service using the given quinn `endpoint`
548
+ /// and a socket `addr` of the remote service.
531
549
#[ cfg( feature = "rpc" ) ]
532
550
pub fn quinn ( endpoint : quinn:: Endpoint , addr : std:: net:: SocketAddr ) -> Self {
533
551
Self :: boxed ( rpc:: QuinnRemoteConnection :: new ( endpoint, addr) )
534
552
}
535
553
554
+ /// Create a new client from a `rpc::RemoteConnection` trait object.
555
+ /// This is used from crates that want to provide other transports than quinn,
556
+ /// such as the iroh transport.
536
557
#[ cfg( feature = "rpc" ) ]
537
558
pub fn boxed ( remote : impl rpc:: RemoteConnection ) -> Self {
538
559
Self ( ClientInner :: Remote ( Box :: new ( remote) ) , PhantomData )
@@ -725,12 +746,17 @@ pub mod rpc {
725
746
BoxedFuture , RequestError , RpcMessage ,
726
747
} ;
727
748
749
+ /// Error that can occur when writing the initial message when doing a
750
+ /// cross-process RPC.
728
751
#[ derive( Debug , thiserror:: Error ) ]
729
752
pub enum WriteError {
730
- #[ error( "error serializing: {0}" ) ]
731
- Io ( #[ from] io:: Error ) ,
753
+ /// Error writing to the stream with quinn
732
754
#[ error( "error writing to stream: {0}" ) ]
733
755
Quinn ( #[ from] quinn:: WriteError ) ,
756
+ /// Generic IO error, e.g. when serializing the message or when using
757
+ /// other transports.
758
+ #[ error( "error serializing: {0}" ) ]
759
+ Io ( #[ from] io:: Error ) ,
734
760
}
735
761
736
762
impl From < WriteError > for io:: Error {
@@ -742,9 +768,21 @@ pub mod rpc {
742
768
}
743
769
}
744
770
771
+ /// Trait to abstract over a client connection to a remote service.
772
+ ///
773
+ /// This isn't really that much abstracted, since the result of open_bi must
774
+ /// still be a quinn::SendStream and quinn::RecvStream. This is just so we
775
+ /// can have different connection implementations for normal quinn connections,
776
+ /// iroh connections, and possibly quinn connections with disabled encryption
777
+ /// for performance.
778
+ ///
779
+ /// This is done as a trait instead of an enum, so we don't need an iroh
780
+ /// dependency in the main crate.
745
781
pub trait RemoteConnection : Send + Sync + Debug + ' static {
782
+ /// Boxed clone so the trait is dynable.
746
783
fn clone_boxed ( & self ) -> Box < dyn RemoteConnection > ;
747
784
785
+ /// Open a bidirectional stream to the remote service.
748
786
fn open_bi (
749
787
& self ,
750
788
) -> BoxedFuture < std:: result:: Result < ( quinn:: SendStream , quinn:: RecvStream ) , RequestError > > ;
@@ -1058,9 +1096,12 @@ pub mod rpc {
1058
1096
}
1059
1097
}
1060
1098
1099
+ /// A request to a service. This can be either local or remote.
1061
1100
#[ derive( Debug ) ]
1062
1101
pub enum Request < L , R > {
1102
+ /// Local request
1063
1103
Local ( L ) ,
1104
+ /// Remote request
1064
1105
Remote ( R ) ,
1065
1106
}
1066
1107
0 commit comments