2727use std:: time:: Duration ;
2828
2929use jsonrpsee_types:: { ErrorCode , ErrorObject , Id , InvalidRequest , Response , ResponsePayload } ;
30+ use serde_json:: value:: RawValue ;
3031use tokio:: sync:: mpsc;
3132
32- use super :: { DisconnectError , SendTimeoutError , SubscriptionMessage , TrySendError } ;
33+ use super :: { DisconnectError , SendTimeoutError , TrySendError } ;
3334
3435/// Sink that is used to send back the result to the server for a specific method.
3536#[ derive( Clone , Debug ) ]
3637pub struct MethodSink {
3738 /// Channel sender.
38- tx : mpsc:: Sender < String > ,
39+ tx : mpsc:: Sender < Box < RawValue > > ,
3940 /// Max response size in bytes for a executed call.
4041 max_response_size : u32 ,
4142}
4243
4344impl MethodSink {
4445 /// Create a new `MethodSink` with unlimited response size.
45- pub fn new ( tx : mpsc:: Sender < String > ) -> Self {
46+ pub fn new ( tx : mpsc:: Sender < Box < RawValue > > ) -> Self {
4647 MethodSink { tx, max_response_size : u32:: MAX }
4748 }
4849
4950 /// Create a new `MethodSink` with a limited response size.
50- pub fn new_with_limit ( tx : mpsc:: Sender < String > , max_response_size : u32 ) -> Self {
51+ pub fn new_with_limit ( tx : mpsc:: Sender < Box < RawValue > > , max_response_size : u32 ) -> Self {
5152 MethodSink { tx, max_response_size }
5253 }
5354
@@ -74,25 +75,25 @@ impl MethodSink {
7475 /// connection has been closed or if the message buffer is full.
7576 ///
7677 /// Returns the message if the send fails such that either can be thrown away or re-sent later.
77- pub fn try_send ( & mut self , msg : String ) -> Result < ( ) , TrySendError > {
78+ pub fn try_send ( & mut self , msg : Box < RawValue > ) -> Result < ( ) , TrySendError > {
7879 self . tx . try_send ( msg) . map_err ( Into :: into)
7980 }
8081
8182 /// Async send which will wait until there is space in channel buffer or that the subscription is disconnected.
82- pub async fn send ( & self , msg : String ) -> Result < ( ) , DisconnectError > {
83+ pub async fn send ( & self , msg : Box < RawValue > ) -> Result < ( ) , DisconnectError > {
8384 self . tx . send ( msg) . await . map_err ( Into :: into)
8485 }
8586
8687 /// Send a JSON-RPC error to the client
8788 pub async fn send_error < ' a > ( & self , id : Id < ' a > , err : ErrorObject < ' a > ) -> Result < ( ) , DisconnectError > {
8889 let payload = ResponsePayload :: < ( ) > :: error_borrowed ( err) ;
89- let json = serde_json:: to_string ( & Response :: new ( payload, id) ) . expect ( "valid JSON; qed" ) ;
90+ let json = serde_json:: value :: to_raw_value ( & Response :: new ( payload, id) ) . expect ( "valid JSON; qed" ) ;
9091
9192 self . send ( json) . await
9293 }
9394
9495 /// Similar to `MethodSink::send` but only waits for a limited time.
95- pub async fn send_timeout ( & self , msg : String , timeout : Duration ) -> Result < ( ) , SendTimeoutError > {
96+ pub async fn send_timeout ( & self , msg : Box < RawValue > , timeout : Duration ) -> Result < ( ) , SendTimeoutError > {
9697 self . tx . send_timeout ( msg, timeout) . await . map_err ( Into :: into)
9798 }
9899
@@ -112,7 +113,7 @@ impl MethodSink {
112113 // The permit is thrown away here because it's just
113114 // a way to ensure that the return buffer has space.
114115 Ok ( _) => Ok ( ( ) ) ,
115- Err ( _) => Err ( DisconnectError ( SubscriptionMessage :: empty ( ) ) ) ,
116+ Err ( _) => Err ( DisconnectError ( RawValue :: NULL . to_owned ( ) . into ( ) ) ) ,
116117 }
117118 }
118119}
0 commit comments