我正在弄清楚如何使用tokio-proto
包,特别是在建立连接时进行的握手。我从official documentation工作中得到了例子:
impl<T: AsyncRead + AsyncWrite + 'static> ClientProto<T> for ClientLineProto {
type Request = String;
type Response = String;
/// `Framed<T, LineCodec>` is the return value of `io.framed(LineCodec)`
type Transport = Framed<T, line::LineCodec>;
type BindTransport = Box<Future<Item = Self::Transport, Error = io::Error>>;
fn bind_transport(&self, io: T) -> Self::BindTransport {
// Construct the line-based transport
let transport = io.framed(line::LineCodec);
// Send the handshake frame to the server.
let handshake = transport.send("You ready?".to_string())
// Wait for a response from the server, if the transport errors out,
// we don't care about the transport handle anymore, just the error
.and_then(|transport| transport.into_future().map_err(|(e, _)| e))
.and_then(|(line, transport)| {
// The server sent back a line, check to see if it is the
// expected handshake line.
match line {
Some(ref msg) if msg == "Bring it!" => {
println!("CLIENT: received server handshake");
Ok(transport)
}
Some(ref msg) if msg == "No! Go away!" => {
// At this point, the server is at capacity. There are a
// few things that we could do. Set a backoff timer and
// try again in a bit. Or we could try a different
// remote server. However, we're just going to error out
// the connection.
println!("CLIENT: server is at capacity");
let err = io::Error::new(io::ErrorKind::Other, "server at capacity");
Err(err)
}
_ => {
println!("CLIENT: server handshake INVALID");
let err = io::Error::new(io::ErrorKind::Other, "invalid handshake");
Err(err)
}
}
});
Box::new(handshake)
}
}
但官方文件只提到没有有状态信息的握手。是否有一种通用的方法来从握手中检索和存储有用的数据?
例如,如果在握手期间(在建立连接后的第一条消息中)服务器发送了一些应该由客户端稍后使用的密钥,那么ClientProto
实现应如何查看该密钥?它应该存放在哪里?
答案 0 :(得分:1)
您可以向ClientLineProto
添加字段,因此这应该有效:
pub struct ClientLineProto {
handshakes: Arc<Mutex<HashMap<String, String>>>
}
然后您可以根据需要引用它并存储数据:
let mut handshakes = self.handshakes.lock();
handshakes.insert(handshake_key, "Blah blah handshake data")
这种访问在bind_transport()
中可用于存储内容。然后,当您在Arc::Mutex::HashMap
函数中创建main()
时,您也可以访问serve()
方法中的所有内容,这意味着您可以将其传递给Service对象实例化然后在call()
期间可以使用握手。