Implemented Clone for IrcServer and updated multithreaded examples
accordingly.
This commit is contained in:
parent
a6cd761e51
commit
ea0577bbaf
4 changed files with 10 additions and 6 deletions
|
@ -1,7 +1,6 @@
|
||||||
extern crate irc;
|
extern crate irc;
|
||||||
|
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use std::sync::Arc;
|
|
||||||
use std::thread::spawn;
|
use std::thread::spawn;
|
||||||
use irc::client::prelude::*;
|
use irc::client::prelude::*;
|
||||||
|
|
||||||
|
@ -12,7 +11,7 @@ fn main() {
|
||||||
channels: Some(vec![format!("#vana")]),
|
channels: Some(vec![format!("#vana")]),
|
||||||
.. Default::default()
|
.. Default::default()
|
||||||
};
|
};
|
||||||
let server = Arc::new(IrcServer::from_config(config).unwrap());
|
let server = IrcServer::from_config(config).unwrap();
|
||||||
// FIXME: if set_keepalive is stabilized, this can be readded.
|
// FIXME: if set_keepalive is stabilized, this can be readded.
|
||||||
// server.conn().set_keepalive(Some(5)).unwrap();
|
// server.conn().set_keepalive(Some(5)).unwrap();
|
||||||
let server = server.clone();
|
let server = server.clone();
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
extern crate irc;
|
extern crate irc;
|
||||||
|
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use std::sync::Arc;
|
|
||||||
use std::thread::spawn;
|
use std::thread::spawn;
|
||||||
use irc::client::prelude::*;
|
use irc::client::prelude::*;
|
||||||
|
|
||||||
|
@ -12,7 +11,7 @@ fn main() {
|
||||||
channels: Some(vec![format!("#vana")]),
|
channels: Some(vec![format!("#vana")]),
|
||||||
.. Default::default()
|
.. Default::default()
|
||||||
};
|
};
|
||||||
let server = Arc::new(IrcServer::from_config(config).unwrap());
|
let server = IrcServer::from_config(config).unwrap();
|
||||||
server.identify().unwrap();
|
server.identify().unwrap();
|
||||||
let server = server.clone();
|
let server = server.clone();
|
||||||
let _ = spawn(move || {
|
let _ = spawn(move || {
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
extern crate irc;
|
extern crate irc;
|
||||||
|
|
||||||
use std::default::Default;
|
use std::default::Default;
|
||||||
use std::sync::Arc;
|
|
||||||
use std::thread::{sleep, spawn};
|
use std::thread::{sleep, spawn};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
use irc::client::prelude::*;
|
use irc::client::prelude::*;
|
||||||
|
@ -13,7 +12,7 @@ fn main() {
|
||||||
channels: Some(vec![format!("#vana")]),
|
channels: Some(vec![format!("#vana")]),
|
||||||
.. Default::default()
|
.. Default::default()
|
||||||
};
|
};
|
||||||
let server = Arc::new(IrcServer::from_config(config).unwrap());
|
let server = IrcServer::from_config(config).unwrap();
|
||||||
server.identify().unwrap();
|
server.identify().unwrap();
|
||||||
let server2 = server.clone();
|
let server2 = server.clone();
|
||||||
// Let's set up a loop that just prints the messages.
|
// Let's set up a loop that just prints the messages.
|
||||||
|
|
|
@ -37,6 +37,7 @@ pub struct IrcServer<T: IrcRead, U: IrcWrite> {
|
||||||
state: Arc<ServerState<T, U>>,
|
state: Arc<ServerState<T, U>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Thread-safe internal state for an IRC server connection.
|
||||||
struct ServerState<T: IrcRead, U: IrcWrite> {
|
struct ServerState<T: IrcRead, U: IrcWrite> {
|
||||||
/// The thread-safe IRC connection.
|
/// The thread-safe IRC connection.
|
||||||
conn: Connection<T, U>,
|
conn: Connection<T, U>,
|
||||||
|
@ -81,6 +82,12 @@ impl IrcServer<BufReader<NetStream>, BufWriter<NetStream>> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl<'a, T: IrcRead, U: IrcWrite> Clone for IrcServer<T, U> {
|
||||||
|
fn clone(&self) -> IrcServer<T, U> {
|
||||||
|
IrcServer { state: self.state.clone() }
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl<'a, T: IrcRead, U: IrcWrite> Server<'a, T, U> for IrcServer<T, U> {
|
impl<'a, T: IrcRead, U: IrcWrite> Server<'a, T, U> for IrcServer<T, U> {
|
||||||
fn config(&self) -> &Config {
|
fn config(&self) -> &Config {
|
||||||
&self.state.config
|
&self.state.config
|
||||||
|
|
Loading…
Reference in a new issue