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
|
use std::{io, str};
use message::Message;
pub struct Client {
nick: String,
host: String,
port: u16,
connection: Option<io::BufferedStream<io::TcpStream>>,
}
impl Client {
pub fn new (nick: &str, host: &str, port: u16) -> Client {
Client {
nick: nick.to_string(),
host: host.to_string(),
port: port,
connection: None,
}
}
pub fn connect (&mut self) {
let mut stream = io::TcpStream::connect(self.host.as_slice(), self.port);
self.connection = Some(io::BufferedStream::new(stream.unwrap()));
}
pub fn read (&mut self) -> Message {
// \n isn't valid inside a message, so this should be fine. if the \n
// we find isn't preceded by a \r, this will be caught by the message
// parser.
match self.connection {
Some(ref mut conn) => {
let buf = conn.read_until(b'\n');
// XXX handle different encodings
// XXX proper error handling
Message::parse(str::from_utf8(buf.unwrap().as_slice()).unwrap()).unwrap()
},
None => fail!(),
}
}
}
|