|
14 | 14 |
|
15 | 15 | //! Stream reader.
|
16 | 16 | //!
|
17 |
| -//! This module defines the `StreamReader` struct and its implementation which |
18 |
| -//! is used for parsing an incoming stream into separate `RawNetworkMessage`s, |
19 |
| -//! handling assembling messages from multiple packets, or dealing with partial |
20 |
| -//! or multiple messages in the stream (e.g. when reading from a TCP socket). |
| 17 | +//! Deprecated |
| 18 | +//! |
| 19 | +//! This module defines `StreamReader` struct and its implementation which is used |
| 20 | +//! for parsing incoming stream into separate `Decodable`s, handling assembling |
| 21 | +//! messages from multiple packets or dealing with partial or multiple messages in the stream |
| 22 | +//! (like can happen with reading from TCP socket) |
21 | 23 | //!
|
22 |
| -
|
23 |
| -use prelude::*; |
24 | 24 |
|
25 | 25 | use core::fmt;
|
26 |
| -use io::{self, Read}; |
| 26 | +use io::Read; |
27 | 27 |
|
28 | 28 | use consensus::{encode, Decodable};
|
29 | 29 |
|
30 | 30 | /// Struct used to configure stream reader function
|
31 | 31 | pub struct StreamReader<R: Read> {
|
32 | 32 | /// Stream to read from
|
33 | 33 | pub stream: R,
|
34 |
| - /// I/O buffer |
35 |
| - data: Vec<u8>, |
36 |
| - /// Buffer containing unparsed message part |
37 |
| - unparsed: Vec<u8> |
38 | 34 | }
|
39 | 35 |
|
40 | 36 | impl<R: Read> fmt::Debug for StreamReader<R> {
|
41 | 37 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
42 |
| - write!(f, "StreamReader with buffer_size={} and unparsed content {:?}", |
43 |
| - self.data.capacity(), self.unparsed) |
| 38 | + write!(f, "StreamReader") |
44 | 39 | }
|
45 | 40 | }
|
46 | 41 |
|
47 | 42 | impl<R: Read> StreamReader<R> {
|
48 |
| - /// Constructs new stream reader for a given input stream `stream` with |
49 |
| - /// optional parameter `buffer_size` determining reading buffer size |
50 |
| - pub fn new(stream: R, buffer_size: Option<usize>) -> StreamReader<R> { |
| 43 | + /// Constructs new stream reader for a given input stream `stream` |
| 44 | + #[deprecated(since="0.28.0", note="wrap you stream into a buffered reader if necessary and use consensus_encode directly")] |
| 45 | + pub fn new(stream: R, _buffer_size: Option<usize>) -> StreamReader<R> { |
51 | 46 | StreamReader {
|
52 | 47 | stream,
|
53 |
| - data: vec![0u8; buffer_size.unwrap_or(64 * 1024)], |
54 |
| - unparsed: vec![] |
55 | 48 | }
|
56 | 49 | }
|
57 | 50 |
|
58 |
| - /// Reads stream and parses next message from its current input, |
59 |
| - /// also taking into account previously unparsed partial message (if there was such). |
| 51 | + /// Reads stream and parses next message from its current input |
| 52 | + #[deprecated(since="0.28.0", note="wrap you stream into a buffered reader if necessary and use consensus_encode directly")] |
60 | 53 | pub fn read_next<D: Decodable>(&mut self) -> Result<D, encode::Error> {
|
61 |
| - loop { |
62 |
| - match encode::deserialize_partial::<D>(&self.unparsed) { |
63 |
| - // In this case we just have an incomplete data, so we need to read more |
64 |
| - Err(encode::Error::Io(ref err)) if err.kind () == io::ErrorKind::UnexpectedEof => { |
65 |
| - let count = self.stream.read(&mut self.data)?; |
66 |
| - if count > 0 { |
67 |
| - self.unparsed.extend(self.data[0..count].iter()); |
68 |
| - } |
69 |
| - else { |
70 |
| - return Err(encode::Error::Io(io::Error::from(io::ErrorKind::UnexpectedEof))); |
71 |
| - } |
72 |
| - }, |
73 |
| - Err(err) => return Err(err), |
74 |
| - // We have successfully read from the buffer |
75 |
| - Ok((message, index)) => { |
76 |
| - self.unparsed.drain(..index); |
77 |
| - return Ok(message) |
78 |
| - }, |
79 |
| - } |
80 |
| - } |
| 54 | + Decodable::consensus_decode(&mut self.stream) |
81 | 55 | }
|
82 | 56 | }
|
83 | 57 |
|
| 58 | +#[allow(deprecated)] |
84 | 59 | #[cfg(test)]
|
85 | 60 | mod test {
|
86 | 61 | use std::thread;
|
87 | 62 | use std::time::Duration;
|
88 |
| - use io::{self, BufReader, Write}; |
| 63 | + use io::{BufReader, Write}; |
89 | 64 | use std::net::{TcpListener, TcpStream, Shutdown};
|
90 | 65 | use std::thread::JoinHandle;
|
91 | 66 | use network::constants::ServiceFlags;
|
@@ -193,22 +168,6 @@ mod test {
|
193 | 168 | }
|
194 | 169 | }
|
195 | 170 |
|
196 |
| - #[test] |
197 |
| - fn parse_multipartmsg_test() { |
198 |
| - let stream = io::empty(); |
199 |
| - let mut reader = StreamReader::new(stream, None); |
200 |
| - reader.unparsed = MSG_ALERT[..24].to_vec(); |
201 |
| - let message: Result<RawNetworkMessage, _> = reader.read_next(); |
202 |
| - assert!(message.is_err()); |
203 |
| - assert_eq!(reader.unparsed.len(), 24); |
204 |
| - |
205 |
| - reader.unparsed = MSG_ALERT.to_vec(); |
206 |
| - let message = reader.read_next().unwrap(); |
207 |
| - assert_eq!(reader.unparsed.len(), 0); |
208 |
| - |
209 |
| - check_alert_msg(&message); |
210 |
| - } |
211 |
| - |
212 | 171 | #[test]
|
213 | 172 | fn read_singlemsg_test() {
|
214 | 173 | let stream = MSG_VERSION[..].to_vec();
|
@@ -346,4 +305,17 @@ mod test {
|
346 | 305 | // should be also ok for a non-witness block as commitment is optional in that case
|
347 | 306 | assert!(block.check_witness_commitment());
|
348 | 307 | }
|
| 308 | + |
| 309 | + #[test] |
| 310 | + fn parse_multipartmsg_test() { |
| 311 | + let mut multi = MSG_ALERT.to_vec(); |
| 312 | + multi.extend(&MSG_ALERT[..]); |
| 313 | + let mut reader = StreamReader::new(&multi[..], None); |
| 314 | + let message: Result<RawNetworkMessage, _> = reader.read_next(); |
| 315 | + assert!(message.is_ok()); |
| 316 | + check_alert_msg(&message.unwrap()); |
| 317 | + let message: Result<RawNetworkMessage, _> = reader.read_next(); |
| 318 | + assert!(message.is_ok()); |
| 319 | + check_alert_msg(&message.unwrap()); |
| 320 | + } |
349 | 321 | }
|
0 commit comments