@@ -19,6 +19,7 @@ mod dynamic_handler;
1919pub ( crate ) mod handlers;
2020mod incoming_actor;
2121mod outgoing_actor;
22+ mod protocol_compat;
2223pub ( crate ) mod run;
2324mod task_actor;
2425mod transport_actor;
@@ -28,6 +29,7 @@ pub use crate::jsonrpc::handlers::NullHandler;
2829use crate :: jsonrpc:: handlers:: { ChainedHandler , NamedHandler } ;
2930use crate :: jsonrpc:: handlers:: { MessageHandler , NotificationHandler , RequestHandler } ;
3031use crate :: jsonrpc:: outgoing_actor:: { OutgoingMessageTx , send_raw_message} ;
32+ use crate :: jsonrpc:: protocol_compat:: { ProtocolCompat , ProtocolMode } ;
3133use crate :: jsonrpc:: run:: SpawnedRun ;
3234use crate :: jsonrpc:: run:: { ChainRun , NullRun , RunWithConnectionTo } ;
3335use crate :: jsonrpc:: task_actor:: { Task , TaskTx } ;
@@ -554,6 +556,9 @@ where
554556
555557 /// Responder for background tasks.
556558 responder : Runner ,
559+
560+ /// Protocol version mode for the public API and wire compatibility layer.
561+ protocol_mode : ProtocolMode ,
557562}
558563
559564impl < Host : Role > Builder < Host , NullHandler , NullRun > {
@@ -566,6 +571,7 @@ impl<Host: Role> Builder<Host, NullHandler, NullRun> {
566571 name : None ,
567572 handler : NullHandler ,
568573 responder : NullRun ,
574+ protocol_mode : ProtocolMode :: disabled ( ) ,
569575 }
570576 }
571577}
@@ -581,6 +587,7 @@ where
581587 name : None ,
582588 handler,
583589 responder : NullRun ,
590+ protocol_mode : ProtocolMode :: disabled ( ) ,
584591 }
585592 }
586593}
@@ -597,6 +604,28 @@ impl<
597604 self
598605 }
599606
607+ pub ( crate ) fn v1_agent ( mut self ) -> Self {
608+ self . protocol_mode = ProtocolMode :: v1_agent ( ) ;
609+ self
610+ }
611+
612+ pub ( crate ) fn v1_client ( mut self ) -> Self {
613+ self . protocol_mode = ProtocolMode :: v1_client ( ) ;
614+ self
615+ }
616+
617+ #[ cfg( feature = "unstable_protocol_v2" ) ]
618+ pub ( crate ) fn v2_agent ( mut self ) -> Self {
619+ self . protocol_mode = ProtocolMode :: v2_agent ( ) ;
620+ self
621+ }
622+
623+ #[ cfg( feature = "unstable_protocol_v2" ) ]
624+ pub ( crate ) fn v2_client ( mut self ) -> Self {
625+ self . protocol_mode = ProtocolMode :: v2_client ( ) ;
626+ self
627+ }
628+
600629 /// Merge another [`Builder`] into this one.
601630 ///
602631 /// Prefer [`Self::on_receive_request`] or [`Self::on_receive_notification`].
@@ -613,14 +642,22 @@ impl<
613642 impl HandleDispatchFrom < Host :: Counterpart > ,
614643 impl RunWithConnectionTo < Host :: Counterpart > ,
615644 > {
645+ let Builder {
646+ name : other_name,
647+ handler : other_handler,
648+ responder : other_responder,
649+ protocol_mode : other_protocol_mode,
650+ host : _,
651+ } = other;
616652 Builder {
617653 host : self . host ,
618654 name : self . name ,
619655 handler : ChainedHandler :: new (
620656 self . handler ,
621- NamedHandler :: new ( other . name , other . handler ) ,
657+ NamedHandler :: new ( other_name , other_handler ) ,
622658 ) ,
623- responder : ChainRun :: new ( self . responder , other. responder ) ,
659+ responder : ChainRun :: new ( self . responder , other_responder) ,
660+ protocol_mode : self . protocol_mode . merge ( other_protocol_mode) ,
624661 }
625662 }
626663
@@ -637,6 +674,7 @@ impl<
637674 name : self . name ,
638675 handler : ChainedHandler :: new ( self . handler , handler) ,
639676 responder : self . responder ,
677+ protocol_mode : self . protocol_mode ,
640678 }
641679 }
642680
@@ -653,6 +691,7 @@ impl<
653691 name : self . name ,
654692 handler : self . handler ,
655693 responder : ChainRun :: new ( self . responder , responder) ,
694+ protocol_mode : self . protocol_mode ,
656695 }
657696 }
658697
@@ -1173,6 +1212,7 @@ impl<
11731212 handler,
11741213 responder,
11751214 host : me,
1215+ protocol_mode,
11761216 } = self ;
11771217
11781218 let ( outgoing_tx, outgoing_rx) = mpsc:: unbounded ( ) ;
@@ -1198,6 +1238,7 @@ impl<
11981238 } = transport_channel;
11991239
12001240 let ( reply_tx, reply_rx) = mpsc:: unbounded ( ) ;
1241+ let protocol_compat = ProtocolCompat :: new ( protocol_mode) ;
12011242
12021243 let future = crate :: util:: instrument_with_connection_name ( name, {
12031244 let connection = connection. clone ( ) ;
@@ -1211,6 +1252,7 @@ impl<
12111252 outgoing_rx,
12121253 reply_tx. clone( ) ,
12131254 transport_outgoing_tx,
1255+ protocol_compat. clone( ) ,
12141256 ) ,
12151257 // Protocol layer: jsonrpcmsg::Message → handler/reply routing
12161258 incoming_actor:: incoming_protocol_actor(
@@ -1220,6 +1262,7 @@ impl<
12201262 dynamic_handler_rx,
12211263 reply_rx,
12221264 handler,
1265+ protocol_compat,
12231266 ) ,
12241267 task_actor:: task_actor( new_task_rx, & connection) ,
12251268 responder. run_with_connection_to( connection. clone( ) ) ,
@@ -1341,6 +1384,9 @@ enum OutgoingMessage {
13411384 Response {
13421385 id : jsonrpcmsg:: Id ,
13431386
1387+ /// Method of the incoming request this response completes.
1388+ method : String ,
1389+
13441390 response : Result < serde_json:: Value , crate :: Error > ,
13451391 } ,
13461392
@@ -1907,6 +1953,7 @@ impl Responder<serde_json::Value> {
19071953 /// The response will be serialized to JSON and sent over the wire.
19081954 fn new ( message_tx : OutgoingMessageTx , method : String , id : jsonrpcmsg:: Id ) -> Self {
19091955 let id_clone = id. clone ( ) ;
1956+ let method_clone = method. clone ( ) ;
19101957 Self {
19111958 method,
19121959 id,
@@ -1915,6 +1962,7 @@ impl Responder<serde_json::Value> {
19151962 & message_tx,
19161963 OutgoingMessage :: Response {
19171964 id : id_clone,
1965+ method : method_clone,
19181966 response,
19191967 } ,
19201968 )
0 commit comments