简体   繁体   English

Rust 中的 P2P 通信

[英]P2P communications in Rust

I am trying to write a P2P node.我正在尝试编写一个 P2P 节点。 A node contains a list of peers, opens a listening port to let other nodes establish a communication channel and, at the same time, actively tries to establish the connection with some other chosen nodes.一个节点包含一个对等点列表,打开一个监听端口让其他节点建立通信通道,同时主动尝试与其他一些选择的节点建立连接。 This choice is as such that there is a single connection between each pair of nodes (not implemented in the following snippet).这种选择是这样的,即每对节点之间只有一个连接(未在以下代码段中实现)。

I am trying to achieve this with Tokio using futures combinators.我正在尝试使用futures组合器通过 Tokio 实现这一目标。

main.rs

use failure::Error;

pub mod networking {
    use failure::Error;

    use std::net::SocketAddr;
    use tokio::net::{TcpListener, TcpStream};
    use tokio::prelude::*;

    use crate::Config;
    use futures::Future;

    pub fn start(cfg: &Config) -> Result<(), Error> {
        let myself = cfg.myself.parse::<SocketAddr>()?;
        let others = cfg
            .others
            .iter()
            .filter_map(|s| s.parse().ok())
            .collect::<Vec<SocketAddr>>();

        let server = TcpListener::bind(&myself)?
            .incoming()
            .for_each(|socket| {
                println!("Got a socket: {:?}", socket);
                future::ok(())
            })
            .map_err(|e| eprintln!("Error connecting: {:?}", e));

        let client = TcpStream::connect(&others[0])
            .map(|socket| {
                println!("Got a socket: {:?}", socket);
            })
            .map_err(|e| eprintln!("Error connecting: {:?}", e));

        let future = server.join(client);

        tokio::run(future);

        Ok(())
    }
}

struct Config {
    myself: String,
    others: Vec<String>,
}

fn main() -> Result<(), Error> {
    let config = Config {
        myself: "127.0.0.1:2501".to_string(),
        others: vec!["127.0.0.1:2502".to_string(), "127.0.0.1:2503".to_string()],
    };
    networking::start(&config)
}

( playground ) 操场

This does not work:这不起作用:

error[E0271]: type mismatch resolving `<futures::future::join::Join<futures::future::map_err::MapErr<futures::stream::for_each::ForEach<tokio_tcp::incoming::Incoming, [closure@src/main.rs:23:23: 26:14], futures::future::result_::FutureResult<(), std::io::Error>>, [closure@src/main.rs:27:22: 27:64]>, futures::future::map_err::MapErr<futures::future::map::Map<tokio_tcp::stream::ConnectFuture, [closure@src/main.rs:30:18: 32:14]>, [closure@src/main.rs:33:22: 33:64]>> as futures::future::Future>::Item == ()`
  --> src/main.rs:37:9
   |
37 |         tokio::run(future);
   |         ^^^^^^^^^^ expected tuple, found ()
   |
   = note: expected type `((), ())`
              found type `()`
   = note: required by `tokio::runtime::threadpool::run`

I understand what the compiler says, but I have no clue what exactly I have to correct.我明白编译器说什么,但我不知道我到底要纠正什么。 What should I correct to get the types right?我应该更正什么才能使类型正确?

thanks for the hints,感谢您的提示,

let future = server.join(client);

has to be corrected to必须纠正为

let future = server.join(client).map(|_| ())

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

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