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
use std::io::prelude::*;
use std::io::{BufReader, Lines};
use std::net::{TcpStream, ToSocketAddrs};
pub struct CandidConnection {
stream: BufReader<TcpStream>,
}
impl CandidConnection {
pub fn new<A: ToSocketAddrs>(addr: A) -> Result<CandidConnection, std::io::Error> {
let stream = TcpStream::connect(addr)?;
Ok(CandidConnection {
stream: BufReader::new(stream),
})
}
pub fn read_frame(&mut self) -> Result<String, std::io::Error> {
let mut buffer = String::new();
self.stream.read_line(&mut buffer)?;
Ok(buffer)
}
pub fn into_raw_stream(self) -> TcpStream {
self.stream.into_inner()
}
pub fn into_buffer(self) -> BufReader<TcpStream> {
self.stream
}
pub fn lines(self) -> Lines<BufReader<TcpStream>> {
self.stream.lines()
}
}