2014-09-24 02:11:13 +02:00
|
|
|
use std::io::{BufferedWriter, IoResult, TcpStream};
|
|
|
|
use data::Message;
|
|
|
|
|
|
|
|
pub struct Connection(pub TcpStream);
|
|
|
|
|
|
|
|
pub fn connect(host: &str, port: u16) -> IoResult<Connection> {
|
|
|
|
let socket = try!(TcpStream::connect(host, port));
|
|
|
|
Ok(Connection(socket))
|
|
|
|
}
|
|
|
|
|
|
|
|
fn send_internal(conn: &Connection, msg: &str) -> IoResult<()> {
|
|
|
|
let &Connection(ref tcp) = conn;
|
|
|
|
let mut writer = BufferedWriter::new(tcp.clone());
|
2014-10-06 22:33:37 +02:00
|
|
|
writer.write_str(msg)
|
2014-09-24 02:11:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
pub fn send(conn: &Connection, msg: Message) -> IoResult<()> {
|
2014-10-06 22:37:29 +02:00
|
|
|
let mut send = msg.command.to_string();
|
|
|
|
send.push_str(" ");
|
|
|
|
send.push_str(msg.args.init().connect(" ").as_slice());
|
|
|
|
send.push_str(" :");
|
|
|
|
send.push_str(*msg.args.last().unwrap());
|
|
|
|
send.push_str("\r\n");
|
|
|
|
send_internal(conn, send.as_slice())
|
2014-09-24 02:11:13 +02:00
|
|
|
}
|