Added documentation everywhere.
This commit is contained in:
parent
63f4ca5097
commit
5bbde7e96c
7 changed files with 141 additions and 53 deletions
12
src/conn.rs
12
src/conn.rs
|
@ -1,14 +1,20 @@
|
|||
//! Thread-safe connections on any IrcWriters and IrcReaders
|
||||
#![experimental]
|
||||
use std::sync::Mutex;
|
||||
use std::io::{BufferedReader, BufferedWriter, IoResult, TcpStream};
|
||||
use data::kinds::{IrcWriter, IrcReader};
|
||||
use data::message::Message;
|
||||
|
||||
/// A thread-safe connection
|
||||
#[experimental]
|
||||
pub struct Connection<T, U> where T: IrcWriter, U: IrcReader {
|
||||
writer: Mutex<T>,
|
||||
reader: Mutex<U>,
|
||||
}
|
||||
|
||||
impl Connection<BufferedWriter<TcpStream>, BufferedReader<TcpStream>> {
|
||||
/// Creates a thread-safe TCP connection to the specified server
|
||||
#[experimental]
|
||||
pub fn connect(host: &str, port: u16) -> IoResult<Connection<BufferedWriter<TcpStream>, BufferedReader<TcpStream>>> {
|
||||
let socket = try!(TcpStream::connect(host, port));
|
||||
Ok(Connection::new(BufferedWriter::new(socket.clone()), BufferedReader::new(socket)))
|
||||
|
@ -16,6 +22,8 @@ impl Connection<BufferedWriter<TcpStream>, BufferedReader<TcpStream>> {
|
|||
}
|
||||
|
||||
impl<T, U> Connection<T, U> where T: IrcWriter, U: IrcReader {
|
||||
/// Creates a new connection from any arbitrary IrcWriter and IrcReader
|
||||
#[experimental]
|
||||
pub fn new(writer: T, reader: U) -> Connection<T, U> {
|
||||
Connection {
|
||||
writer: Mutex::new(writer),
|
||||
|
@ -23,12 +31,16 @@ impl<T, U> Connection<T, U> where T: IrcWriter, U: IrcReader {
|
|||
}
|
||||
}
|
||||
|
||||
/// Sends a Message over this connection
|
||||
#[experimental]
|
||||
pub fn send(&self, message: Message) -> IoResult<()> {
|
||||
let mut send = self.writer.lock();
|
||||
try!(send.write_str(message.into_string()[]));
|
||||
send.flush()
|
||||
}
|
||||
|
||||
/// Receives a single line from this connection
|
||||
#[experimental]
|
||||
pub fn recv(&self) -> IoResult<String> {
|
||||
self.reader.lock().read_line()
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue