3
3
//! This module contains definitions of all the complex data structures that are returned by calls
4
4
5
5
use std:: collections:: { BTreeMap , VecDeque } ;
6
- #[ cfg( test) ]
7
- use std:: fs:: File ;
8
6
use std:: io:: { self , BufRead , BufReader , Read , Write } ;
9
7
use std:: net:: { TcpStream , ToSocketAddrs } ;
10
8
@@ -31,8 +29,6 @@ use socks::{Socks5Stream, ToTargetAddr};
31
29
use stream:: ClonableStream ;
32
30
33
31
use batch:: Batch ;
34
- #[ cfg( test) ]
35
- use test_stream:: TestStream ;
36
32
use types:: * ;
37
33
38
34
macro_rules! impl_batch_call {
@@ -72,8 +68,8 @@ pub struct Client<S>
72
68
where
73
69
S : Read + Write ,
74
70
{
75
- stream : S ,
76
- buf_reader : BufReader < S > ,
71
+ stream : ClonableStream < S > ,
72
+ buf_reader : BufReader < ClonableStream < S > > ,
77
73
78
74
headers : VecDeque < HeaderNotification > ,
79
75
script_notifications : BTreeMap < ScriptHash , VecDeque < ScriptStatus > > ,
82
78
calls : usize ,
83
79
}
84
80
85
- impl < S > From < S > for Client < ClonableStream < S > >
81
+ impl < S > From < S > for Client < S >
86
82
where
87
83
S : Read + Write ,
88
84
{
@@ -107,23 +103,14 @@ impl Client<ElectrumPlaintextStream> {
107
103
/// Creates a new plaintext client and tries to connect to `socket_addr`.
108
104
pub fn new < A : ToSocketAddrs > ( socket_addr : A ) -> Result < Self , Error > {
109
105
let stream = TcpStream :: connect ( socket_addr) ?;
110
- let buf_reader = BufReader :: new ( stream. try_clone ( ) ?) ;
111
-
112
- Ok ( Self {
113
- stream,
114
- buf_reader,
115
- headers : VecDeque :: new ( ) ,
116
- script_notifications : BTreeMap :: new ( ) ,
117
106
118
- #[ cfg( feature = "debug-calls" ) ]
119
- calls : 0 ,
120
- } )
107
+ Ok ( stream. into ( ) )
121
108
}
122
109
}
123
110
124
111
#[ cfg( feature = "use-openssl" ) ]
125
112
/// Transport type used to establish an OpenSSL TLS encrypted/authenticated connection with the server
126
- pub type ElectrumSslStream = ClonableStream < SslStream < TcpStream > > ;
113
+ pub type ElectrumSslStream = SslStream < TcpStream > ;
127
114
#[ cfg( feature = "use-openssl" ) ]
128
115
impl Client < ElectrumSslStream > {
129
116
/// Creates a new SSL client and tries to connect to `socket_addr`. Optionally, if `domain` is not
@@ -174,7 +161,7 @@ mod danger {
174
161
not( feature = "use-openssl" )
175
162
) ) ]
176
163
/// Transport type used to establish a Rustls TLS encrypted/authenticated connection with the server
177
- pub type ElectrumSslStream = ClonableStream < StreamOwned < ClientSession , TcpStream > > ;
164
+ pub type ElectrumSslStream = StreamOwned < ClientSession , TcpStream > ;
178
165
#[ cfg( all(
179
166
any( feature = "default" , feature = "use-rustls" ) ,
180
167
not( feature = "use-openssl" )
@@ -209,7 +196,7 @@ impl Client<ElectrumSslStream> {
209
196
210
197
#[ cfg( any( feature = "default" , feature = "proxy" ) ) ]
211
198
/// Transport type used to establish a connection to a server through a socks proxy
212
- pub type ElectrumProxyStream = ClonableStream < Socks5Stream > ;
199
+ pub type ElectrumProxyStream = Socks5Stream ;
213
200
#[ cfg( any( feature = "default" , feature = "proxy" ) ) ]
214
201
impl Client < ElectrumProxyStream > {
215
202
/// Creates a new socks client and tries to connect to `target_addr` using `proxy_addr` as an
@@ -226,21 +213,6 @@ impl Client<ElectrumProxyStream> {
226
213
}
227
214
}
228
215
229
- #[ cfg( test) ]
230
- impl Client < TestStream > {
231
- pub fn new_test ( file : File ) -> Self {
232
- let stream = TestStream :: new_out ( ) ;
233
- let buf_reader = BufReader :: new ( TestStream :: new_in ( file) ) ;
234
-
235
- Self {
236
- stream,
237
- buf_reader,
238
- headers : VecDeque :: new ( ) ,
239
- script_notifications : BTreeMap :: new ( ) ,
240
- }
241
- }
242
- }
243
-
244
216
impl < S : Read + Write > Client < S > {
245
217
fn call ( & mut self , req : Request ) -> Result < serde_json:: Value , Error > {
246
218
let mut raw = serde_json:: to_vec ( & req) ?;
@@ -650,9 +622,16 @@ impl<S: Read + Write> Client<S> {
650
622
mod test {
651
623
use std:: fs:: File ;
652
624
use std:: io:: Read ;
625
+ use test_stream:: TestStream ;
653
626
654
627
use client:: Client ;
655
628
629
+ impl Client < TestStream > {
630
+ pub fn new_test ( file : File ) -> Self {
631
+ TestStream :: new ( file) . into ( )
632
+ }
633
+ }
634
+
656
635
macro_rules! impl_test_prelude {
657
636
( $testcase: expr ) => { {
658
637
let data_in = File :: open( format!( "./test_data/{}.in" , $testcase) ) . unwrap( ) ;
@@ -665,7 +644,7 @@ mod test {
665
644
let mut data_out = File :: open( format!( "./test_data/{}.out" , $testcase) ) . unwrap( ) ;
666
645
let mut buffer = Vec :: new( ) ;
667
646
data_out. read_to_end( & mut buffer) . unwrap( ) ;
668
- let stream_buffer: Vec < u8 > = $stream. into ( ) ;
647
+ let stream_buffer = $stream. stream ( ) . lock ( ) . unwrap ( ) . buffer . clone ( ) ;
669
648
670
649
assert_eq!(
671
650
stream_buffer,
0 commit comments