Commit 5340ac83 authored by Janos Guljas's avatar Janos Guljas

pkg/p2p: add IncompatibleStreamError

parent 00a7f60d
...@@ -25,6 +25,7 @@ require ( ...@@ -25,6 +25,7 @@ require (
github.com/libp2p/go-libp2p-transport v0.1.0 // indirect github.com/libp2p/go-libp2p-transport v0.1.0 // indirect
github.com/libp2p/go-stream-muxer v0.1.0 // indirect github.com/libp2p/go-stream-muxer v0.1.0 // indirect
github.com/multiformats/go-multiaddr v0.2.0 github.com/multiformats/go-multiaddr v0.2.0
github.com/multiformats/go-multistream v0.1.0
github.com/whyrusleeping/go-smux-multiplex v3.0.16+incompatible // indirect github.com/whyrusleeping/go-smux-multiplex v3.0.16+incompatible // indirect
github.com/whyrusleeping/go-smux-multistream v2.0.2+incompatible // indirect github.com/whyrusleeping/go-smux-multistream v2.0.2+incompatible // indirect
github.com/whyrusleeping/go-smux-yamux v2.0.9+incompatible // indirect github.com/whyrusleeping/go-smux-yamux v2.0.9+incompatible // indirect
......
...@@ -5,6 +5,8 @@ import ( ...@@ -5,6 +5,8 @@ import (
"fmt" "fmt"
"time" "time"
"github.com/multiformats/go-multistream"
"github.com/janos/bee/pkg/p2p" "github.com/janos/bee/pkg/p2p"
"github.com/libp2p/go-libp2p" "github.com/libp2p/go-libp2p"
...@@ -20,7 +22,6 @@ import ( ...@@ -20,7 +22,6 @@ import (
libp2pquic "github.com/libp2p/go-libp2p-quic-transport" libp2pquic "github.com/libp2p/go-libp2p-quic-transport"
secio "github.com/libp2p/go-libp2p-secio" secio "github.com/libp2p/go-libp2p-secio"
libp2ptls "github.com/libp2p/go-libp2p-tls" libp2ptls "github.com/libp2p/go-libp2p-tls"
"github.com/multiformats/go-multiaddr"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
) )
...@@ -129,7 +130,7 @@ func (s *Service) Addresses() (addrs []string, err error) { ...@@ -129,7 +130,7 @@ func (s *Service) Addresses() (addrs []string, err error) {
return addrs, nil return addrs, nil
} }
func (s *Service) Connect(ctx context.Context, addr multiaddr.Multiaddr) (peerID string, err error) { func (s *Service) Connect(ctx context.Context, addr ma.Multiaddr) (peerID string, err error) {
// Extract the peer ID from the multiaddr. // Extract the peer ID from the multiaddr.
info, err := peer.AddrInfoFromP2pAddr(addr) info, err := peer.AddrInfoFromP2pAddr(addr)
if err != nil { if err != nil {
...@@ -149,6 +150,9 @@ func (s *Service) NewStream(ctx context.Context, peerID, protocolName, streamNam ...@@ -149,6 +150,9 @@ func (s *Service) NewStream(ctx context.Context, peerID, protocolName, streamNam
swarmStreamName := p2p.NewSwarmStreamName(protocolName, streamName, version) swarmStreamName := p2p.NewSwarmStreamName(protocolName, streamName, version)
st, err := s.host.NewStream(ctx, id, protocol.ID(swarmStreamName)) st, err := s.host.NewStream(ctx, id, protocol.ID(swarmStreamName))
if err != nil { if err != nil {
if err == multistream.ErrNotSupported || err == multistream.ErrIncorrectVersion {
return nil, p2p.NewIncompatibleStreamError(err)
}
return nil, fmt.Errorf("create stream %q to %q: %w", swarmStreamName, peerID, err) return nil, fmt.Errorf("create stream %q to %q: %w", swarmStreamName, peerID, err)
} }
return stream{st}, nil return stream{st}, nil
......
...@@ -2,6 +2,7 @@ package p2p ...@@ -2,6 +2,7 @@ package p2p
import ( import (
"context" "context"
"fmt"
"io" "io"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
...@@ -44,6 +45,20 @@ type StreamSpec struct { ...@@ -44,6 +45,20 @@ type StreamSpec struct {
Handler func(Peer) Handler func(Peer)
} }
type IncompatibleStreamError struct {
err error
}
func NewIncompatibleStreamError(err error) *IncompatibleStreamError {
return &IncompatibleStreamError{err: err}
}
func (e *IncompatibleStreamError) Unwrap() error { return e.err }
func (e *IncompatibleStreamError) Error() string {
return fmt.Sprintf("incompatible stream: %v", e.err)
}
func NewSwarmStreamName(protocol, stream, version string) string { func NewSwarmStreamName(protocol, stream, version string) string {
return "/swarm/" + protocol + "/" + stream + "/" + version return "/swarm/" + protocol + "/" + stream + "/" + version
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment