使用Tokio的frame_delimited时为什么会出现FrameTooBig错误?

时间:2018-04-23 12:50:02

标签: rust rust-tokio

我试图用Tokio弄湿我的脚。当我从Telnet连接发送消息时,我得到Custom { kind: InvalidData, error: FrameTooBig }。我不了解这个问题,也不了​​解如何克服它。

extern crate tokio;
extern crate tokio_io;

use tokio::io;
use tokio::net::TcpListener;
use tokio::prelude::*;
use tokio_io::codec::length_delimited;

fn main() {
    let addr = "127.0.0.1:12345".parse().unwrap();
    let listener = TcpListener::bind(&addr).unwrap();

    let server = listener
        .incoming()
        .for_each(|socket| {
            let transport = length_delimited::Builder::new().new_read(socket);
            let msg_proccessing = transport
                .for_each(|msg| {
                    // Note: This part is never actually executed
                    println!("{:?}", msg);
                    Ok(())
                })
                .map_err(|e| println!("waaaaaaaaaaaaaaaaa {:?}", e));
            tokio::spawn(msg_proccessing);
            Ok(())
        })
        .map_err(|_| {});
    tokio::run(server);
}

客户方:

▶ telnet localhost 12345                                     
Trying 127.0.0.1...
Connected to localhost.
Escape character is '^]'.
\x00\x00\x00\x0bhello world
Connection closed by foreign host.

1 个答案:

答案 0 :(得分:1)

问题出在客户端,这与Telnet的工作原理有关。使用Telnet发送十六进制数据并不简单,所以我尝试了这个并运行良好:

echo '\x00\x00\x00\x0bhello world' | nc localhost 12345  #WORKS!

但是,这些都不起作用:

  1. echo '\x00\x00\x00\x0bhello world' | telnet localhost
    
  2. ▶ telnet localhost 12345                                     
    Trying 127.0.0.1...
    Connected to localhost.
    Escape character is '^]'.
    \x00\x00\x00\x0bhello world
    Connection closed by foreign host.
    
  3. 似乎FrameTooBig错误是由于telnet发送的消息比服务器预期的大。我无法使用十六进制对框架进行正确编码,然后长度与收到的长度标头不匹配,从而导致错误。