1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use bytebuffer::ByteBuffer;
use std::net::{SocketAddr, TcpStream};
use std::{
io::{self, Read, Write},
net::Shutdown,
};
pub trait Streamer: Read + Write + Send + Sync {
fn shutdown(&mut self, how: Shutdown) -> io::Result<()>;
fn connect(&mut self, addr: &SocketAddr);
}
#[derive(Debug)]
pub struct TcpStreamer {
pub(crate) stream: TcpStream,
}
impl TcpStreamer {
pub fn new(stream: TcpStream) -> Self {
Self { stream: stream }
}
}
impl Streamer for TcpStreamer {
fn shutdown(&mut self, how: Shutdown) -> io::Result<()> {
self.stream.shutdown(how)
}
fn connect(&mut self, addr: &SocketAddr) {
self.stream = TcpStream::connect(addr).expect("Cannot connect!!");
}
}
impl Clone for TcpStreamer {
fn clone(&self) -> Self {
TcpStreamer::new(self.stream.try_clone().unwrap())
}
}
impl Read for TcpStreamer {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.stream.read(buf)
}
}
impl Write for TcpStreamer {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.stream.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.stream.flush()
}
}
pub struct TestStreamer {
stream: ByteBuffer,
}
impl TestStreamer {
pub fn new() -> Self {
TestStreamer {
stream: ByteBuffer::new(),
}
}
}
impl Streamer for TestStreamer {
fn shutdown(&mut self, _how: Shutdown) -> io::Result<()> {
Ok(())
}
fn connect(&mut self, _addr: &SocketAddr) {}
}
impl Read for TestStreamer {
fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
self.stream.read(buf)
}
fn read_to_end(&mut self, allbuf: &mut Vec<u8>) -> io::Result<usize> {
let mut cont = true;
const NUM_BYTES: usize = 4096;
while cont {
let mut buf: [u8; NUM_BYTES] = [0; NUM_BYTES];
let bytes_read = self
.stream
.read(&mut buf)
.expect("Couldnt read from reader...");
allbuf.extend_from_slice(&buf[0..bytes_read]);
if bytes_read < NUM_BYTES {
cont = false;
}
}
Ok(allbuf.len())
}
}
impl Write for TestStreamer {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.stream.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.stream.flush()
}
}