简体   繁体   English

MQTT 与 tokio 的连接

[英]MQTT connection with tokio

I'm trying to create an MQTT connection with tokio and the codec provided in the mqtt_v5 crate.我正在尝试使用 tokio 和 mqtt_v5 板条箱中提供的编解码器创建 MQTT 连接。 My code does not compile, and I don't understand why.我的代码无法编译,我不明白为什么。 Here is what I have written so far, the send code might not be correct.这是我到目前为止所写的,发送代码可能不正确。

use tokio::net::TcpStream;
use tokio_util::codec::Framed;
use tokio_util::codec::Decoder;
use std::net::SocketAddrV4;
use mqtt_v5::types::Packet as MqttPacket;
use mqtt_v5::codec::MqttCodec;
use futures_sink::Sink;
use futures_core::stream::Stream;

struct MqttConn {
    inner: Framed<TcpStream, MqttCodec>,
}

impl MqttConn {
    async fn new(addr: SocketAddrV4) -> MqttConn {
        let tcp = TcpStream::connect(addr).await.expect("cannot connect to mqtt");
        MqttConn { inner: MqttCodec::new().framed(tcp) } 
    }

    async fn handle(&self, handler: &dyn Fn(&MqttConn, MqttPacket) -> ()) {
        while let Some(p) = self.inner.next().await {
            handler(self, p)
        }
    }

    async fn send(&self, p: MqttPacket) {
        self.inner.start_send(p);
    }
}

I get these errors from the compiler:我从编译器得到这些错误:

error[E0599]: no method named `framed` found for struct `MqttCodec` in the current scope
  --> src/mqtt.rs:17:44
   |
17 |         MqttConn { inner: MqttCodec::new().framed(tcp) } 
   |                                            ^^^^^^ method not found in `MqttCodec`
   |
   = help: items from traits can only be used if the trait is in scope
   = note: the following trait is implemented but not in scope; perhaps add a `use` for it:
           `use tokio_util::codec::decoder::Decoder;`

error[E0599]: no method named `next` found for struct `Framed<tokio::net::TcpStream, MqttCodec>` in the current scope
  --> src/mqtt.rs:21:40
   |
21 |         while let Some(p) = self.inner.next().await {
   |                                        ^^^^ method not found in `Framed<tokio::net::TcpStream, MqttCodec>`

error[E0599]: no method named `start_send` found for struct `Framed<tokio::net::TcpStream, MqttCodec>` in the current scope
  --> src/mqtt.rs:27:20
   |
27 |         self.inner.start_send(p);
   |                    ^^^^^^^^^^ method not found in `Framed<tokio::net::TcpStream, MqttCodec>`

warning: unused import: `tokio_util::codec::Decoder`
 --> src/mqtt.rs:3:5
  |
3 | use tokio_util::codec::Decoder;
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^

warning: unused import: `futures_sink::Sink`
 --> src/mqtt.rs:7:5
  |
7 | use futures_sink::Sink;
  |     ^^^^^^^^^^^^^^^^^^

The compiler says that the Decoder trait is not in scope, but I use it.编译器说Decoder特性不在 scope 中,但我使用它。 If I try the suggested import I find that the module tokio_util::codec::decoder is private.如果我尝试建议的导入,我发现模块tokio_util::codec::decoder是私有的。 Based on the source tokio_util::codec::decoder::Decoder is reexported as tokio_util::codec::Decoder .基于源tokio_util::codec::decoder::Decoder被重新导出为tokio_util::codec::Decoder Also if I read the docs correctly, Framed should implement Sink and Stream therefore it should have the next and start_send methods.此外,如果我正确阅读了文档, Framed应该实现SinkStream因此它应该有nextstart_send方法。

Relevant lines from Cargo.toml: Cargo.toml 中的相关行:

[dependencies]
tokio = { version = "1.0.1", features = ["full"] }
tokio-util = { version = "0.6", features = ["full"] }
futures-sink = "0.3.9"
futures-core = "0.3.9"

mqtt-v5 = "0.1.1"

How can I get this to compile?我怎样才能让它编译?

You have a number of library incompatibilities that are causing some less-than-obvious error messages.您有许多库不兼容性导致一些不太明显的错误消息。

mqtt-v5 depends on tokio-util^0.3 , which was written for tokio 0.2, not 1.0. mqtt-v5依赖于tokio-util^0.3 ,它是为tokio 0.2 而不是 1.0 编写的。 You'll need to roll back to tokio 0.2 and tokio-util 0.3.您需要回滚到tokio 0.2 和tokio-util 0.3。 This should fix the problem with Decoder and Sink .这应该可以解决DecoderSink的问题。

Additionally, the Stream trait only provides poll_next() , which is the task-level stream method.此外, Stream trait 仅提供poll_next() ,这是任务级 stream 方法。 next() is provided by the StreamExt trait, along with other convenience methods similar to those found in Iterator . next()StreamExt trait 提供,以及与Iterator中类似的其他便利方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM