introduce packet protocol v1

This commit is contained in:
Max 2026-06-13 13:57:56 +02:00
parent 8996d8369f
commit cc771bbea6
3 changed files with 27 additions and 11 deletions

View File

@ -345,12 +345,16 @@ func (node *Node) streamHandler(stream network.Stream) {
}
var streamLock = new(sync.Mutex)
inserted := node.insertActiveStream(remotePeerID, SharedStream{
Stream: &stream,
Lock: streamLock,
})
if inserted {
defer node.expireActiveStream(remotePeerID)
// Version 0 nodes don't read from this stream, so we can't reuse it.
if stream.Protocol() != p2p.ProtocolV0 {
inserted := node.insertActiveStream(remotePeerID, SharedStream{
Stream: &stream,
Lock: streamLock,
})
if inserted {
defer node.expireActiveStream(remotePeerID)
}
}
var packet = make([]byte, 1420)
@ -419,7 +423,7 @@ func (node *Node) sendPacket(dst peer.ID, packet []byte, plen int) {
}
}
stream, err := node.p2p.NewStream(node.ctx, dst, p2p.Protocol)
stream, err := node.p2p.NewStream(node.ctx, dst, p2p.Protocols...)
if err != nil {
logger.With(zap.String("destination", dst.String()), zap.Error(err)).Error("Failed to open stream")
go p2p.Rediscover()

View File

@ -21,6 +21,7 @@ import (
"github.com/libp2p/go-libp2p/core/network"
"github.com/libp2p/go-libp2p/core/peer"
"github.com/libp2p/go-libp2p/core/pnet"
"github.com/libp2p/go-libp2p/core/protocol"
"github.com/libp2p/go-libp2p/core/routing"
"github.com/libp2p/go-libp2p/p2p/discovery/backoff"
"github.com/libp2p/go-libp2p/p2p/host/autorelay"
@ -48,8 +49,16 @@ func (c *httpRoutingWrapper) Bootstrap(ctx context.Context) error {
return nil
}
// Protocol is a descriptor for the Hyprspace P2P Protocol.
const Protocol = "/hyprspace/0.0.1"
// Version 0
const ProtocolV0 = "/hyprspace/0.0.1"
// Version 1: Bidirectional streams
const ProtocolV1 = "/hyprspace/1"
var Protocols = []protocol.ID{
ProtocolV1,
ProtocolV0,
}
func getExtraPeers(addr ma.Multiaddr) (nodesList []string) {
nodesList = []string{}
@ -281,7 +290,9 @@ func CreateNode(ctx context.Context, privateKey crypto.PrivKey, listenAddreses [
node = routedhost.Wrap(basicHost, pr)
// Setup Hyprspace Stream Handler
node.SetStreamHandler(Protocol, handler)
for _, proto := range Protocols {
node.SetStreamHandler(proto, handler)
}
if err != nil {
return node, nil, err

View File

@ -7,6 +7,7 @@ import (
"net"
"net/rpc"
"os"
"slices"
"sync"
"syscall"
@ -82,7 +83,7 @@ func (hsr *HyprspaceRPC) Route(args *RouteArgs, reply *RouteReply) error {
ConnLoop:
for _, c := range hsr.host.Network().ConnsToPeer(rte.Target.ID) {
for _, s := range c.GetStreams() {
if s.Protocol() == p2p.Protocol {
if slices.Contains(p2p.Protocols, s.Protocol()) {
if _, err := c.RemoteMultiaddr().ValueForProtocol(multiaddr.P_CIRCUIT); err == nil {
relay = true
if ra, err := c.RemoteMultiaddr().ValueForProtocol(multiaddr.P_P2P); err == nil {