我想使用 mio 实现 tcp 客户端。这是我的代码:
pub struct Client<'a> {
pub connected: bool,
connection: Option<&'a TcpStream>,
auth_handler: auth::AuthHandler<'a>,
}
impl Client<'_> {
pub fn connect(&mut self, host: &str, port: i16) {
let addr_list = format!("{}:{}", host, port)
.to_socket_addrs()
.unwrap();
let addr = addr_list.last().unwrap();
match TcpStream::connect(addr) {
Ok(stream) => {
self.connected = true;
self.connection = Some(&stream);
self.auth_handler.init(self.connection.unwrap());
self.auth_handler.authenticate("login".to_string(), "password".to_string());
println!("Connected to {}:{}", host, port);
}
Err(..) => {
println!("Cannot connect !");
}
}
}
pub fn listen(&mut self) {
let mut connection = self.connection.as_mut().unwrap();
let mut poll = Poll::new().unwrap();
let mut events = Events::with_capacity(256);
poll.registry().register(
connection,
CLIENT,
Interest::READABLE | Interest::WRITABLE
);
loop {
poll.poll(&mut events, None).unwrap();
for event in events.iter() {
match event.token() {
CLIENT => {
if event.is_writable() {
// ...
}
if event.is_readable() {
println!("Data")
}
},
_ => (),
}
}
}
}
pub fn new<'a>() -> Client<'a> {
Client {
connected: false,
connection: None,
auth_handler: auth::AuthHandler {
connection: None::<&'a TcpStream>,
},
}
}
}
和我的身份验证处理程序的代码:
pub struct AuthHandler<'a> {
pub connection: Option<&'a TcpStream>,
}
impl AuthHandler {
pub fn authenticate(&self, login: String, password: String) {
// ...
}
pub fn new<'a>() -> AuthHandler<'a> {
AuthHandler {
connection: None::<&'a TcpStream>,
}
}
pub fn init(&mut self, connection: &TcpStream) {
self.connection = Some(&connection); // error here see ERRORS below
}
}
编译时出现错误“错误[E0621]:连接类型需要显式生命周期”:
self.connection = Some(&connection);
^^^^^^^^^^^^^^^^^ lifetime `'static` required
如何解决?从我的角度来看,我不确定static
生命周期是否可以,因为我想在经过身份验证和登录后破坏连接。