Go语言 使用libp2p进行编程,以使用TCP、QUIC或两者进行连接

3lxsmp7m  于 2023-03-10  发布在  Go
关注(0)|答案(1)|浏览(211)

我正在编写一个P2P应用程序,我想决定使用TCP还是QUIC向对等体发送消息,这是为了测试协议,也是为了让用户决定(我想要pefed的quic协议)。但是我很难做到这一点。目前我将远程主机地址分为quic和tcp地址,然后向每个地址发出连接,但是有时tcp地址连接到udp,反之亦然。我不知道这是否是最好的方法
我已经尝试了切分的地址,使用TCP连接,QUIC,也从分布式哈希表会合收到的地址。

quicaddr := selectAddrs(peeraddr.Addrs, "quic")
        tcpaddr := selectAddrs(peeraddr.Addrs, "tcp")

        fmt.Println("[*] Using TCP protocol")
        var peeraddrTCP peer.AddrInfo
        peeraddrTCP.ID = peeraddr.ID
        peeraddrTCP.Addrs = tcpaddr

        fmt.Println(peeraddrTCP.Addrs)
        err = Host.Connect(ctx, peeraddrTCP)
        conn2 := Host.Network().ConnsToPeer(peeraddr.ID)
        for _, c := range conn2 {
            fmt.Println("Using: ", c.ConnState().Transport)
            fmt.Println(c.RemoteMultiaddr())
            available_add = append(available_add, c.RemoteMultiaddr())
            c.Close()

        }

        fmt.Println("[*] Using QUIC protocol")
        var peeraddrQUIC peer.AddrInfo
        peeraddrQUIC.ID = peeraddr.ID
        peeraddrQUIC.Addrs = quicaddr
        fmt.Println(peeraddrQUIC.Addrs)

        err = Host.Connect(ctx, peeraddrQUIC)

        conn1 := Host.Network().ConnsToPeer(peeraddrQUIC.ID)
        for _, c := range conn1 {
            fmt.Println("Using: ", c.ConnState().Transport)
            fmt.Println(c.RemoteMultiaddr())
            available_add = append(available_add, c.RemoteMultiaddr())
            c.Close()
        }

基本上,我想知道是否有可能有一个连接与tcp和quic。对不起,我的英语。

aij0ehis

aij0ehis1#

我做了一个功能,以改变为QUIC或TCP我张贴它的情况下,任何人都有兴趣,在任何人有更好的选择请分享,谢谢!
func setTransport(ctx上下文.上下文,对等体ID对等体.ID,首选QUIC bool)bool {

// get peer addrinfo from id
peeraddr := Host.Peerstore().PeerInfo(peerid)
var addrs []multiaddr.Multiaddr

conn := Host.Network().ConnsToPeer(peeraddr.ID)
for _, c := range conn {
    fmt.Println("Currently Using : ", c.ConnState().Transport)

    // if we want to use quic and the connection is quic, return
    if c.ConnState().Transport == "quic" && preferQUIC {
        fmt.Println("[*] Using QUIC protocol")
        return true
    }
    // if we want to use tcp and the connection is tcp, return
    if c.ConnState().Transport == "tcp" && !preferQUIC {
        fmt.Println("[*] Using TCP protocol")

        return true
    }
    //conn is tcp and we want quic
    if c.ConnState().Transport == "tcp" {
        fmt.Println("[*] Changing to QUIC protocol")
        addrs = selectAddrs(peeraddr.Addrs, true, false)

    }
    //conn is quic and we want tcp
    if c.ConnState().Transport == "quic" {

        fmt.Println("[*] Changing to TCP protocol")
        addrs = selectAddrs(peeraddr.Addrs, false, true)

    }

}
if len(addrs) == 0 {

    fmt.Print("Not Supported", peeraddr.Addrs, preferQUIC, "current transport", conn)
    return false

}

Host.Peerstore().ClearAddrs(peeraddr.ID) //maybe this inst needed ???
Host.Peerstore().AddAddrs(peeraddr.ID, addrs, time.Hour*1) //maybe this inst needed ???
closeConns(peeraddr.ID) // need to close conns, else the previous transport will not change

var NewPeer peer.AddrInfo
NewPeer.ID = peeraddr.ID
NewPeer.Addrs = addrs //selected addr containing desired trp

err := Host.Connect(ctx, NewPeer)

if err != nil {

    fmt.Println("Error connecting to ", addrs, err)
    return false
}

singleconn := Host.Network().ConnsToPeer(peeraddr.ID)[0]
if preferQUIC && singleconn.ConnState().Transport == "quic" {
    fmt.Println("[*] Succesfully changed to QUIC")
    return true

} else if !preferQUIC && singleconn.ConnState().Transport == "tcp" {
    fmt.Println("[*] Succesfully changed to TCP")
    return true

} else {
    fmt.Println("Error changing transport")
    return false
}

}

相关问题