@@ -6,6 +6,7 @@ pub use jsonrpcmsg;
66
77// Types re-exported from crate root
88use serde:: { Deserialize , Serialize } ;
9+ use std:: any:: TypeId ;
910use std:: fmt:: Debug ;
1011use std:: panic:: Location ;
1112use std:: pin:: pin;
@@ -19,6 +20,7 @@ mod dynamic_handler;
1920pub ( crate ) mod handlers;
2021mod incoming_actor;
2122mod outgoing_actor;
23+ mod protocol_compat;
2224pub ( crate ) mod run;
2325mod task_actor;
2426mod transport_actor;
@@ -28,6 +30,7 @@ pub use crate::jsonrpc::handlers::NullHandler;
2830use crate :: jsonrpc:: handlers:: { ChainedHandler , NamedHandler } ;
2931use crate :: jsonrpc:: handlers:: { MessageHandler , NotificationHandler , RequestHandler } ;
3032use crate :: jsonrpc:: outgoing_actor:: { OutgoingMessageTx , send_raw_message} ;
33+ use crate :: jsonrpc:: protocol_compat:: { ProtocolCompat , ProtocolMode } ;
3134use crate :: jsonrpc:: run:: SpawnedRun ;
3235use crate :: jsonrpc:: run:: { ChainRun , NullRun , RunWithConnectionTo } ;
3336use crate :: jsonrpc:: task_actor:: { Task , TaskTx } ;
@@ -554,6 +557,21 @@ where
554557
555558 /// Responder for background tasks.
556559 responder : Runner ,
560+
561+ /// Protocol version mode for the public API and wire compatibility layer.
562+ protocol_mode : ProtocolMode ,
563+ }
564+
565+ fn default_protocol_mode < Host : Role > ( ) -> ProtocolMode {
566+ let role = TypeId :: of :: < Host > ( ) ;
567+
568+ if role == TypeId :: of :: < Agent > ( ) {
569+ ProtocolMode :: v1_agent ( )
570+ } else if role == TypeId :: of :: < Client > ( ) {
571+ ProtocolMode :: v1_client ( )
572+ } else {
573+ ProtocolMode :: disabled ( )
574+ }
557575}
558576
559577impl < Host : Role > Builder < Host , NullHandler , NullRun > {
@@ -566,6 +584,7 @@ impl<Host: Role> Builder<Host, NullHandler, NullRun> {
566584 name : None ,
567585 handler : NullHandler ,
568586 responder : NullRun ,
587+ protocol_mode : default_protocol_mode :: < Host > ( ) ,
569588 }
570589 }
571590}
@@ -581,6 +600,7 @@ where
581600 name : None ,
582601 handler,
583602 responder : NullRun ,
603+ protocol_mode : default_protocol_mode :: < Host > ( ) ,
584604 }
585605 }
586606}
@@ -597,6 +617,28 @@ impl<
597617 self
598618 }
599619
620+ pub ( crate ) fn v1_agent ( mut self ) -> Self {
621+ self . protocol_mode = ProtocolMode :: v1_agent ( ) ;
622+ self
623+ }
624+
625+ pub ( crate ) fn v1_client ( mut self ) -> Self {
626+ self . protocol_mode = ProtocolMode :: v1_client ( ) ;
627+ self
628+ }
629+
630+ #[ cfg( feature = "unstable_protocol_v2" ) ]
631+ pub ( crate ) fn v2_agent ( mut self ) -> Self {
632+ self . protocol_mode = ProtocolMode :: v2_agent ( ) ;
633+ self
634+ }
635+
636+ #[ cfg( feature = "unstable_protocol_v2" ) ]
637+ pub ( crate ) fn v2_client ( mut self ) -> Self {
638+ self . protocol_mode = ProtocolMode :: v2_client ( ) ;
639+ self
640+ }
641+
600642 /// Merge another [`Builder`] into this one.
601643 ///
602644 /// Prefer [`Self::on_receive_request`] or [`Self::on_receive_notification`].
@@ -613,14 +655,22 @@ impl<
613655 impl HandleDispatchFrom < Host :: Counterpart > ,
614656 impl RunWithConnectionTo < Host :: Counterpart > ,
615657 > {
658+ let Builder {
659+ name : other_name,
660+ handler : other_handler,
661+ responder : other_responder,
662+ protocol_mode : other_protocol_mode,
663+ host : _,
664+ } = other;
616665 Builder {
617666 host : self . host ,
618667 name : self . name ,
619668 handler : ChainedHandler :: new (
620669 self . handler ,
621- NamedHandler :: new ( other . name , other . handler ) ,
670+ NamedHandler :: new ( other_name , other_handler ) ,
622671 ) ,
623- responder : ChainRun :: new ( self . responder , other. responder ) ,
672+ responder : ChainRun :: new ( self . responder , other_responder) ,
673+ protocol_mode : self . protocol_mode . merge ( other_protocol_mode) ,
624674 }
625675 }
626676
@@ -637,6 +687,7 @@ impl<
637687 name : self . name ,
638688 handler : ChainedHandler :: new ( self . handler , handler) ,
639689 responder : self . responder ,
690+ protocol_mode : self . protocol_mode ,
640691 }
641692 }
642693
@@ -653,6 +704,7 @@ impl<
653704 name : self . name ,
654705 handler : self . handler ,
655706 responder : ChainRun :: new ( self . responder , responder) ,
707+ protocol_mode : self . protocol_mode ,
656708 }
657709 }
658710
@@ -1173,6 +1225,7 @@ impl<
11731225 handler,
11741226 responder,
11751227 host : me,
1228+ protocol_mode,
11761229 } = self ;
11771230
11781231 let ( outgoing_tx, outgoing_rx) = mpsc:: unbounded ( ) ;
@@ -1198,6 +1251,7 @@ impl<
11981251 } = transport_channel;
11991252
12001253 let ( reply_tx, reply_rx) = mpsc:: unbounded ( ) ;
1254+ let protocol_compat = ProtocolCompat :: new ( protocol_mode) ;
12011255
12021256 let future = crate :: util:: instrument_with_connection_name ( name, {
12031257 let connection = connection. clone ( ) ;
@@ -1211,6 +1265,7 @@ impl<
12111265 outgoing_rx,
12121266 reply_tx. clone( ) ,
12131267 transport_outgoing_tx,
1268+ protocol_compat. clone( ) ,
12141269 ) ,
12151270 // Protocol layer: jsonrpcmsg::Message → handler/reply routing
12161271 incoming_actor:: incoming_protocol_actor(
@@ -1220,6 +1275,7 @@ impl<
12201275 dynamic_handler_rx,
12211276 reply_rx,
12221277 handler,
1278+ protocol_compat,
12231279 ) ,
12241280 task_actor:: task_actor( new_task_rx, & connection) ,
12251281 responder. run_with_connection_to( connection. clone( ) ) ,
@@ -1341,6 +1397,9 @@ enum OutgoingMessage {
13411397 Response {
13421398 id : jsonrpcmsg:: Id ,
13431399
1400+ /// Method of the incoming request this response completes.
1401+ method : String ,
1402+
13441403 response : Result < serde_json:: Value , crate :: Error > ,
13451404 } ,
13461405
@@ -1907,6 +1966,7 @@ impl Responder<serde_json::Value> {
19071966 /// The response will be serialized to JSON and sent over the wire.
19081967 fn new ( message_tx : OutgoingMessageTx , method : String , id : jsonrpcmsg:: Id ) -> Self {
19091968 let id_clone = id. clone ( ) ;
1969+ let method_clone = method. clone ( ) ;
19101970 Self {
19111971 method,
19121972 id,
@@ -1915,6 +1975,7 @@ impl Responder<serde_json::Value> {
19151975 & message_tx,
19161976 OutgoingMessage :: Response {
19171977 id : id_clone,
1978+ method : method_clone,
19181979 response,
19191980 } ,
19201981 )
0 commit comments