Added the ability to set timeouts for live connections.
This commit is contained in:
parent
f5654abd6e
commit
ebab05a6b8
3 changed files with 63 additions and 15 deletions
|
@ -1,7 +1,7 @@
|
||||||
[package]
|
[package]
|
||||||
|
|
||||||
name = "irc"
|
name = "irc"
|
||||||
version = "0.1.2"
|
version = "0.2.0"
|
||||||
description = "A simple, thread-safe IRC client library."
|
description = "A simple, thread-safe IRC client library."
|
||||||
authors = ["Aaron Weiss <aaronweiss74@gmail.com>"]
|
authors = ["Aaron Weiss <aaronweiss74@gmail.com>"]
|
||||||
license = "Unlicense"
|
license = "Unlicense"
|
||||||
|
|
42
src/conn.rs
42
src/conn.rs
|
@ -18,7 +18,23 @@ impl Connection<BufferedStream<TcpStream>> {
|
||||||
/// Creates a thread-safe TCP connection to the specified server.
|
/// Creates a thread-safe TCP connection to the specified server.
|
||||||
#[experimental]
|
#[experimental]
|
||||||
pub fn connect(host: &str, port: u16) -> IoResult<Connection<BufferedStream<NetStream>>> {
|
pub fn connect(host: &str, port: u16) -> IoResult<Connection<BufferedStream<NetStream>>> {
|
||||||
let socket = try!(TcpStream::connect(format!("{}:{}", host, port)[]));
|
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)
|
||||||
|
-> IoResult<Connection<BufferedStream<NetStream>>> {
|
||||||
|
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>)
|
||||||
|
-> IoResult<Connection<BufferedStream<NetStream>>> {
|
||||||
|
let mut socket = try!(TcpStream::connect(format!("{}:{}", host, port)[]));
|
||||||
|
socket.set_timeout(timeout_ms);
|
||||||
Ok(Connection::new(BufferedStream::new(NetStream::UnsecuredTcpStream(socket))))
|
Ok(Connection::new(BufferedStream::new(NetStream::UnsecuredTcpStream(socket))))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,10 +43,14 @@ impl Connection<BufferedStream<TcpStream>> {
|
||||||
#[experimental]
|
#[experimental]
|
||||||
#[cfg(feature = "ssl")]
|
#[cfg(feature = "ssl")]
|
||||||
pub fn connect_ssl(host: &str, port: u16) -> IoResult<Connection<BufferedStream<NetStream>>> {
|
pub fn connect_ssl(host: &str, port: u16) -> IoResult<Connection<BufferedStream<NetStream>>> {
|
||||||
let socket = try!(TcpStream::connect(format!("{}:{}", host, port)[]));
|
Connection::connect_ssl_internal(host, port, None)
|
||||||
let ssl = try!(ssl_to_io(SslContext::new(Tlsv1)));
|
}
|
||||||
let ssl_socket = try!(ssl_to_io(SslStream::new(&ssl, socket)));
|
|
||||||
Ok(Connection::new(BufferedStream::new(NetStream::SslTcpStream(ssl_socket))))
|
#[experimental]
|
||||||
|
#[cfg(feature = "ssl")]
|
||||||
|
pub fn connect_ssl_with_timeout(host: &str, port: u16, timeout_ms: u64)
|
||||||
|
-> IoResult<Connection<BufferedStream<NetStream>>> {
|
||||||
|
Connection::connect_ssl_internal(host, port, Some(timeout_ms))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a thread-safe TCP connection to the specified server over SSL.
|
/// Creates a thread-safe TCP connection to the specified server over SSL.
|
||||||
|
@ -40,6 +60,18 @@ impl Connection<BufferedStream<TcpStream>> {
|
||||||
pub fn connect_ssl(host: &str, port: u16) -> IoResult<Connection<BufferedStream<NetStream>>> {
|
pub fn connect_ssl(host: &str, port: u16) -> IoResult<Connection<BufferedStream<NetStream>>> {
|
||||||
panic!("Cannot connect to {}:{} over SSL without compiling with SSL support.", host, port)
|
panic!("Cannot connect to {}:{} over SSL without compiling with SSL support.", host, port)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// 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>)
|
||||||
|
-> IoResult<Connection<BufferedStream<NetStream>>> {
|
||||||
|
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)));
|
||||||
|
Ok(Connection::new(BufferedStream::new(NetStream::SslTcpStream(ssl_socket))))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Converts a Result<T, SslError> into an IoResult<T>.
|
/// Converts a Result<T, SslError> into an IoResult<T>.
|
||||||
|
|
|
@ -35,17 +35,19 @@ pub struct IrcServer<T> where T: IrcStream {
|
||||||
}
|
}
|
||||||
|
|
||||||
impl IrcServer<BufferedStream<NetStream>> {
|
impl IrcServer<BufferedStream<NetStream>> {
|
||||||
/// Creates a new IRC Server connection from the configuration at the specified path, connecting
|
/// Creates a new IRC Server connection from the configuration at the specified path,
|
||||||
/// immediately.
|
/// connecting immediately.
|
||||||
#[experimental]
|
#[experimental]
|
||||||
pub fn new(config: &str) -> IoResult<IrcServer<BufferedStream<NetStream>>> {
|
pub fn new(config: &str) -> IoResult<IrcServer<BufferedStream<NetStream>>> {
|
||||||
let config = try!(Config::load_utf8(config));
|
IrcServer::from_config(try!(Config::load_utf8(config)))
|
||||||
let conn = try!(if config.use_ssl {
|
}
|
||||||
Connection::connect_ssl(config.server[], config.port)
|
|
||||||
} else {
|
/// Creates a new IRC server connection from the configuration at the specified path with the
|
||||||
Connection::connect(config.server[], config.port)
|
/// specified timeout in milliseconds, connecting immediately.
|
||||||
});
|
#[experimental]
|
||||||
Ok(IrcServer { config: config, conn: conn, chanlists: Mutex::new(HashMap::new()) })
|
pub fn with_timeout(config: &str, timeout_ms: u64)
|
||||||
|
-> IoResult<IrcServer<BufferedStream<NetStream>>> {
|
||||||
|
IrcServer::from_config_with_timeout(try!(Config::load_utf8(config)), timeout_ms)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Creates a new IRC server connection from the specified configuration, connecting
|
/// Creates a new IRC server connection from the specified configuration, connecting
|
||||||
|
@ -59,6 +61,20 @@ impl IrcServer<BufferedStream<NetStream>> {
|
||||||
});
|
});
|
||||||
Ok(IrcServer { config: config, conn: conn, chanlists: Mutex::new(HashMap::new()) })
|
Ok(IrcServer { config: config, conn: conn, chanlists: Mutex::new(HashMap::new()) })
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Creates a new IRC server connection from the specified configuration with the specified
|
||||||
|
/// timeout in milliseconds, connecting immediately.
|
||||||
|
#[experimental]
|
||||||
|
pub fn from_config_with_timeout(config: Config, timeout_ms: u64)
|
||||||
|
-> IoResult<IrcServer<BufferedStream<NetStream>>> {
|
||||||
|
let conn = try!(if config.use_ssl {
|
||||||
|
Connection::connect_ssl_with_timeout(config.server[], config.port, timeout_ms)
|
||||||
|
} else {
|
||||||
|
Connection::connect_with_timeout(config.server[], config.port, timeout_ms)
|
||||||
|
});
|
||||||
|
Ok(IrcServer { config: config, conn: conn, chanlists: Mutex::new(HashMap::new()) })
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'a, T> Server<'a, T> for IrcServer<T> where T: IrcStream {
|
impl<'a, T> Server<'a, T> for IrcServer<T> where T: IrcStream {
|
||||||
|
|
Loading…
Reference in a new issue