2014-11-08 23:21:55 -05:00
|
|
|
//! Thread-safe connections on IrcStreams.
|
2014-11-03 00:52:15 -05:00
|
|
|
#![experimental]
|
2014-11-03 14:12:23 -05:00
|
|
|
use std::sync::{Mutex, MutexGuard};
|
2014-12-01 18:17:03 -05:00
|
|
|
use std::io::{BufferedReader, BufferedWriter, IoResult, TcpStream};
|
2014-11-30 03:30:10 -05:00
|
|
|
#[cfg(any(feature = "encode", feature = "ssl"))] use std::io::{IoError, IoErrorKind};
|
|
|
|
#[cfg(feature = "encode")] use encoding::{DecoderTrap, EncoderTrap, Encoding};
|
|
|
|
#[cfg(feature = "encode")] use encoding::label::encoding_from_whatwg_label;
|
2014-12-01 18:17:03 -05:00
|
|
|
use data::kinds::{IrcReader, IrcWriter};
|
2014-11-02 17:25:45 -05:00
|
|
|
use data::message::Message;
|
2014-11-08 17:35:19 -05:00
|
|
|
#[cfg(feature = "ssl")] use openssl::ssl::{SslContext, SslStream, Tlsv1};
|
|
|
|
#[cfg(feature = "ssl")] use openssl::ssl::error::SslError;
|
2014-11-02 17:25:45 -05:00
|
|
|
|
2014-11-06 15:23:02 -05:00
|
|
|
/// A thread-safe connection.
|
2014-11-03 00:52:15 -05:00
|
|
|
#[experimental]
|
2014-12-01 18:17:03 -05:00
|
|
|
pub struct Connection<T: IrcReader, U: IrcWriter> {
|
|
|
|
reader: Mutex<T>,
|
|
|
|
writer: Mutex<U>,
|
2014-11-02 17:25:45 -05:00
|
|
|
}
|
|
|
|
|
2014-12-01 18:17:03 -05:00
|
|
|
/// A Connection over a buffered NetStream.
|
|
|
|
pub type NetConnection = Connection<BufferedReader<NetStream>, BufferedWriter<NetStream>>;
|
|
|
|
|
|
|
|
impl Connection<BufferedReader<NetStream>, BufferedWriter<NetStream>> {
|
2014-11-06 15:23:02 -05:00
|
|
|
/// Creates a thread-safe TCP connection to the specified server.
|
2014-11-03 00:52:15 -05:00
|
|
|
#[experimental]
|
2014-12-01 18:17:03 -05:00
|
|
|
pub fn connect(host: &str, port: u16) -> IoResult<NetConnection> {
|
2014-11-29 04:52:50 -05:00
|
|
|
Connection::connect_internal(host, port, None)
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a thread-safe TCP connection to the specified server with a given timeout in
|
|
|
|
/// milliseconds.
|
|
|
|
#[experimental]
|
|
|
|
pub fn connect_with_timeout(host: &str, port: u16, timeout_ms: u64)
|
2014-12-01 18:17:03 -05:00
|
|
|
-> IoResult<NetConnection> {
|
2014-11-29 04:52:50 -05:00
|
|
|
Connection::connect_internal(host, port, Some(timeout_ms))
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a thread-safe TCP connection with an optional timeout.
|
|
|
|
#[experimental]
|
|
|
|
fn connect_internal(host: &str, port: u16, timeout_ms: Option<u64>)
|
2014-12-01 18:17:03 -05:00
|
|
|
-> IoResult<NetConnection> {
|
2014-11-29 04:52:50 -05:00
|
|
|
let mut socket = try!(TcpStream::connect(format!("{}:{}", host, port)[]));
|
|
|
|
socket.set_timeout(timeout_ms);
|
2014-12-01 18:17:03 -05:00
|
|
|
Ok(Connection::new(
|
|
|
|
BufferedReader::new(NetStream::UnsecuredTcpStream(socket.clone())),
|
|
|
|
BufferedWriter::new(NetStream::UnsecuredTcpStream(socket))
|
|
|
|
))
|
2014-11-08 17:35:19 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/// Creates a thread-safe TCP connection to the specified server over SSL.
|
|
|
|
/// If the library is compiled without SSL support, this method panics.
|
|
|
|
#[experimental]
|
2014-12-01 18:17:03 -05:00
|
|
|
pub fn connect_ssl(host: &str, port: u16) -> IoResult<NetConnection> {
|
2014-11-29 04:52:50 -05:00
|
|
|
Connection::connect_ssl_internal(host, port, None)
|
|
|
|
}
|
|
|
|
|
2014-11-29 04:57:01 -05:00
|
|
|
/// Creates a thread-safe TCP connection to the specificed server over SSL with a given timeout
|
|
|
|
/// in milliseconds. If the library is compiled without SSL support, this method panics.
|
2014-11-29 04:52:50 -05:00
|
|
|
#[experimental]
|
|
|
|
pub fn connect_ssl_with_timeout(host: &str, port: u16, timeout_ms: u64)
|
2014-12-01 18:17:03 -05:00
|
|
|
-> IoResult<NetConnection> {
|
2014-11-29 04:52:50 -05:00
|
|
|
Connection::connect_ssl_internal(host, port, Some(timeout_ms))
|
2014-11-08 17:35:19 -05:00
|
|
|
}
|
|
|
|
|
2014-11-29 04:57:01 -05:00
|
|
|
/// Panics because SSL support was not included at compilation.
|
2014-11-08 17:35:19 -05:00
|
|
|
#[experimental]
|
|
|
|
#[cfg(not(feature = "ssl"))]
|
2014-11-29 04:57:38 -05:00
|
|
|
fn connect_ssl_internal(host: &str, port: u16, _: Option<u64>)
|
2014-12-01 18:17:03 -05:00
|
|
|
-> IoResult<NetConnection> {
|
2014-11-08 17:35:19 -05:00
|
|
|
panic!("Cannot connect to {}:{} over SSL without compiling with SSL support.", host, port)
|
|
|
|
}
|
2014-11-29 04:52:50 -05:00
|
|
|
|
|
|
|
/// Creates a thread-safe TCP connection over SSL with an optional timeout.
|
|
|
|
#[experimental]
|
|
|
|
#[cfg(feature = "ssl")]
|
|
|
|
fn connect_ssl_internal(host: &str, port: u16, timeout_ms: Option<u64>)
|
2014-12-01 18:17:03 -05:00
|
|
|
-> IoResult<NetConnection> {
|
2014-11-29 04:52:50 -05:00
|
|
|
let mut socket = try!(TcpStream::connect(format!("{}:{}", host, port)[]));
|
|
|
|
socket.set_timeout(timeout_ms);
|
|
|
|
let ssl = try!(ssl_to_io(SslContext::new(Tlsv1)));
|
|
|
|
let ssl_socket = try!(ssl_to_io(SslStream::new(&ssl, socket)));
|
2014-12-01 18:17:03 -05:00
|
|
|
Ok(Connection::new(
|
|
|
|
BufferedReader::new(NetStream::SslTcpStream(ssl_socket.clone())),
|
|
|
|
BufferedWriter::new(NetStream::SslTcpSteram(ssl_socket)),
|
|
|
|
))
|
2014-11-29 04:52:50 -05:00
|
|
|
}
|
2014-11-08 17:35:19 -05:00
|
|
|
}
|
|
|
|
|
2014-12-01 18:17:03 -05:00
|
|
|
impl<T: IrcReader, U: IrcWriter> Connection<T, U> {
|
|
|
|
/// Creates a new connection from an IrcReader and an IrcWriter.
|
2014-11-30 01:29:38 -05:00
|
|
|
#[experimental]
|
2014-12-01 18:17:03 -05:00
|
|
|
pub fn new(reader: T, writer: U) -> Connection<T, U> {
|
2014-11-30 01:29:38 -05:00
|
|
|
Connection {
|
2014-12-01 18:17:03 -05:00
|
|
|
reader: Mutex::new(reader),
|
|
|
|
writer: Mutex::new(writer),
|
2014-11-30 01:29:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sends a Message over this connection.
|
|
|
|
#[experimental]
|
2014-11-30 03:30:10 -05:00
|
|
|
#[cfg(feature = "encode")]
|
2014-11-30 01:29:38 -05:00
|
|
|
pub fn send(&self, message: Message, encoding: &str) -> IoResult<()> {
|
|
|
|
let encoding = match encoding_from_whatwg_label(encoding) {
|
|
|
|
Some(enc) => enc,
|
|
|
|
None => return Err(IoError {
|
|
|
|
kind: IoErrorKind::InvalidInput,
|
|
|
|
desc: "Failed to find decoder.",
|
|
|
|
detail: Some(format!("Invalid decoder: {}", encoding))
|
|
|
|
})
|
|
|
|
};
|
|
|
|
let data = match encoding.encode(message.into_string()[], EncoderTrap::Strict) {
|
|
|
|
Ok(data) => data,
|
|
|
|
Err(data) => return Err(IoError {
|
|
|
|
kind: IoErrorKind::InvalidInput,
|
|
|
|
desc: "Failed to decode message.",
|
|
|
|
detail: Some(format!("Failed to decode {} as {}.", data, encoding.name())),
|
|
|
|
})
|
|
|
|
};
|
2014-12-01 18:17:03 -05:00
|
|
|
let mut writer = self.writer.lock();
|
|
|
|
try!(writer.write(data[]));
|
|
|
|
writer.flush()
|
2014-11-30 01:29:38 -05:00
|
|
|
}
|
|
|
|
|
2014-11-30 03:30:10 -05:00
|
|
|
/// Sends a message over this connection.
|
|
|
|
#[experimental]
|
|
|
|
#[cfg(not(feature = "encode"))]
|
|
|
|
pub fn send(&self, message: Message) -> IoResult<()> {
|
2014-12-01 18:17:03 -05:00
|
|
|
let mut writer = self.writer.lock();
|
|
|
|
try!(writer.write_str(message.into_string()[]));
|
|
|
|
writer.flush()
|
2014-11-30 03:30:10 -05:00
|
|
|
}
|
|
|
|
|
2014-11-30 01:29:38 -05:00
|
|
|
/// Receives a single line from this connection.
|
|
|
|
#[experimental]
|
2014-11-30 03:30:10 -05:00
|
|
|
#[cfg(feature = "encoding")]
|
2014-11-30 01:29:38 -05:00
|
|
|
pub fn recv(&self, encoding: &str) -> IoResult<String> {
|
|
|
|
let encoding = match encoding_from_whatwg_label(encoding) {
|
|
|
|
Some(enc) => enc,
|
|
|
|
None => return Err(IoError {
|
|
|
|
kind: IoErrorKind::InvalidInput,
|
|
|
|
desc: "Failed to find decoder.",
|
|
|
|
detail: Some(format!("Invalid decoder: {}", encoding))
|
|
|
|
})
|
|
|
|
};
|
2014-12-01 18:17:03 -05:00
|
|
|
self.reader.lock().read_until(b'\n').and_then(|line|
|
2014-11-30 01:29:38 -05:00
|
|
|
match encoding.decode(line[], DecoderTrap::Strict) {
|
|
|
|
Ok(data) => Ok(data),
|
|
|
|
Err(data) => Err(IoError {
|
|
|
|
kind: IoErrorKind::InvalidInput,
|
|
|
|
desc: "Failed to decode message.",
|
|
|
|
detail: Some(format!("Failed to decode {} as {}.", data, encoding.name())),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2014-11-30 03:30:10 -05:00
|
|
|
/// Receives a single line from this connection.
|
|
|
|
#[experimental]
|
|
|
|
#[cfg(not(feature = "encoding"))]
|
|
|
|
pub fn recv(&self) -> IoResult<String> {
|
2014-12-01 18:17:03 -05:00
|
|
|
self.reader.lock().read_line()
|
2014-11-30 03:30:10 -05:00
|
|
|
}
|
|
|
|
|
2014-12-01 18:17:03 -05:00
|
|
|
/// Acquires the Reader lock.
|
|
|
|
#[experimental]
|
|
|
|
pub fn reader<'a>(&'a self) -> MutexGuard<'a, T> {
|
|
|
|
self.reader.lock()
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Acquires the Writer lock.
|
2014-11-30 01:29:38 -05:00
|
|
|
#[experimental]
|
2014-12-01 18:17:03 -05:00
|
|
|
pub fn writer<'a>(&'a self) -> MutexGuard<'a, U> {
|
|
|
|
self.writer.lock()
|
2014-11-30 01:29:38 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-08 23:07:57 -05:00
|
|
|
/// Converts a Result<T, SslError> into an IoResult<T>.
|
|
|
|
#[cfg(feature = "ssl")]
|
|
|
|
fn ssl_to_io<T>(res: Result<T, SslError>) -> IoResult<T> {
|
|
|
|
match res {
|
|
|
|
Ok(x) => Ok(x),
|
|
|
|
Err(e) => Err(IoError {
|
2014-11-30 01:32:53 -05:00
|
|
|
kind: IoErrorKind::OtherIoError,
|
2014-11-08 23:07:57 -05:00
|
|
|
desc: "An SSL error occurred.",
|
|
|
|
detail: Some(format!("{}", e)),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-08 17:35:19 -05:00
|
|
|
/// An abstraction over different networked streams.
|
|
|
|
#[experimental]
|
|
|
|
pub enum NetStream {
|
|
|
|
/// An unsecured TcpStream.
|
|
|
|
UnsecuredTcpStream(TcpStream),
|
|
|
|
/// An SSL-secured TcpStream.
|
|
|
|
/// This is only available when compiled with SSL support.
|
|
|
|
#[cfg(feature = "ssl")]
|
|
|
|
SslTcpStream(SslStream<TcpStream>),
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Reader for NetStream {
|
|
|
|
fn read(&mut self, buf: &mut [u8]) -> IoResult<uint> {
|
|
|
|
match self {
|
2014-11-18 19:00:18 -05:00
|
|
|
&NetStream::UnsecuredTcpStream(ref mut stream) => stream.read(buf),
|
2014-11-08 17:35:19 -05:00
|
|
|
#[cfg(feature = "ssl")]
|
2014-11-18 19:00:18 -05:00
|
|
|
&NetStream::SslTcpStream(ref mut stream) => stream.read(buf),
|
2014-11-08 17:35:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Writer for NetStream {
|
|
|
|
fn write(&mut self, buf: &[u8]) -> IoResult<()> {
|
|
|
|
match self {
|
2014-11-18 19:00:18 -05:00
|
|
|
&NetStream::UnsecuredTcpStream(ref mut stream) => stream.write(buf),
|
2014-11-08 17:35:19 -05:00
|
|
|
#[cfg(feature = "ssl")]
|
2014-11-18 19:00:18 -05:00
|
|
|
&NetStream::SslTcpStream(ref mut stream) => stream.write(buf),
|
2014-11-08 17:35:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-05 01:45:17 -05:00
|
|
|
#[cfg(test)]
|
|
|
|
mod test {
|
2014-12-01 18:17:03 -05:00
|
|
|
use super::Connection;
|
2014-11-05 01:45:17 -05:00
|
|
|
use std::io::{MemReader, MemWriter};
|
|
|
|
use std::io::util::{NullReader, NullWriter};
|
|
|
|
use data::message::Message;
|
2014-11-30 03:30:10 -05:00
|
|
|
#[cfg(feature = "encode")] use encoding::{DecoderTrap, Encoding};
|
|
|
|
#[cfg(feature = "encode")] use encoding::all::ISO_8859_15;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(not(feature = "encode"))]
|
|
|
|
fn send() {
|
2014-12-01 18:17:03 -05:00
|
|
|
let conn = Connection::new(NullReader, MemWriter::new());
|
2014-11-30 03:30:10 -05:00
|
|
|
assert!(conn.send(
|
|
|
|
Message::new(None, "PRIVMSG", Some(vec!["test"]), Some("Testing!"))
|
|
|
|
).is_ok());
|
2014-12-01 18:17:03 -05:00
|
|
|
let data = String::from_utf8(conn.writer().get_ref().to_vec()).unwrap();
|
2014-11-30 03:30:10 -05:00
|
|
|
assert_eq!(data[], "PRIVMSG test :Testing!\r\n");
|
|
|
|
}
|
2014-11-05 01:45:17 -05:00
|
|
|
|
|
|
|
#[test]
|
2014-11-30 03:30:10 -05:00
|
|
|
#[cfg(feature = "encode")]
|
2014-11-30 01:29:38 -05:00
|
|
|
fn send_utf8() {
|
2014-12-01 18:17:03 -05:00
|
|
|
let conn = Connection::new(NullReader, MemWriter::new());
|
2014-11-18 19:15:29 -05:00
|
|
|
assert!(conn.send(
|
2014-11-30 01:29:38 -05:00
|
|
|
Message::new(None, "PRIVMSG", Some(vec!["test"]), Some("€ŠšŽžŒœŸ")), "l9"
|
2014-11-18 19:15:29 -05:00
|
|
|
).is_ok());
|
2014-11-30 01:29:38 -05:00
|
|
|
let data = ISO_8859_15.decode(conn.stream().value()[], DecoderTrap::Strict).unwrap();
|
|
|
|
assert_eq!(data[], "PRIVMSG test :€ŠšŽžŒœŸ\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2014-11-30 03:30:10 -05:00
|
|
|
#[cfg(feature = "encode")]
|
2014-11-30 01:29:38 -05:00
|
|
|
fn send_iso885915() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2014-11-30 03:30:10 -05:00
|
|
|
#[cfg(not(feature = "encode"))]
|
|
|
|
fn recv() {
|
2014-12-01 18:17:03 -05:00
|
|
|
let conn = Connection::new(
|
|
|
|
MemReader::new("PRIVMSG test :Testing!\r\n".as_bytes().to_vec()), NullWriter
|
|
|
|
);
|
2014-11-30 03:30:10 -05:00
|
|
|
assert_eq!(conn.recv().unwrap()[], "PRIVMSG test :Testing!\r\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
#[cfg(feature = "encode")]
|
2014-11-30 01:29:38 -05:00
|
|
|
fn recv_utf8() {
|
2014-12-01 18:17:03 -05:00
|
|
|
let conn = Connection::new(
|
|
|
|
MemReader::new(b"PRIVMSG test :Testing!\r\n".to_vec()), NullWriter
|
|
|
|
);
|
2014-11-30 01:29:38 -05:00
|
|
|
assert_eq!(conn.recv("UTF-8").unwrap()[], "PRIVMSG test :Testing!\r\n");
|
2014-11-05 01:45:17 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
2014-11-30 03:30:10 -05:00
|
|
|
#[cfg(feature = "encode")]
|
2014-11-30 01:29:38 -05:00
|
|
|
fn recv_iso885915() {
|
2014-12-01 18:17:03 -05:00
|
|
|
let conn = Connection::new(
|
|
|
|
MemReader::new({
|
2014-11-30 01:29:38 -05:00
|
|
|
let mut vec = Vec::new();
|
|
|
|
vec.push_all(b"PRIVMSG test :");
|
|
|
|
vec.push_all(&[0xA4, 0xA6, 0xA8, 0xB4, 0xB8, 0xBC, 0xBD, 0xBE]);
|
|
|
|
vec.push_all(b"\r\n");
|
|
|
|
vec
|
2014-12-01 18:17:03 -05:00
|
|
|
}), NullWriter
|
|
|
|
);
|
2014-11-30 01:29:38 -05:00
|
|
|
assert_eq!(conn.recv("l9").unwrap()[], "PRIVMSG test :€ŠšŽžŒœŸ\r\n");
|
2014-11-05 01:45:17 -05:00
|
|
|
}
|
|
|
|
}
|