1
我想知道如何使用tokio-proto
箱,特别是在建立连接时进行的握手。我已经得到了例如从official documentation工作:如何从tokio-proto连接握手中检索信息?
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
实现应如何查看该密钥?它应该存储在哪里?
我想我一定会错过一些东西 - 是不是你的变量握手你正在寻找的“钥匙”?添加它已经从这个函数返回,所以你只是... *使用*它? – Shepmaster
从'bind_transport()'返回的'handshake'变量是包含尚未执行的握手过程的未来。我需要在握手期间阅读一些关键值,并将其用于稍后从此客户端发出的请求中。基本上,我需要在握手未来完成后检索并存储一些价值。除了为我的协议实现我自己的'BindClient'特性之外,我没有看到任何方法来做到这一点,我不知道这是正确的方式。 –