Skip to content
This repository has been archived by the owner on May 26, 2022. It is now read-only.

Commit

Permalink
Support QListener.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jorropo committed Feb 29, 2020
1 parent 764a610 commit 83882e9
Showing 1 changed file with 36 additions and 2 deletions.
38 changes: 36 additions & 2 deletions swarm_transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ func (s *Swarm) AddTransport(ot transport.BaseTransport) error {
return nil
}

var _ transport.QTransport = transportUpgrader{}

// Used to upgrade `transport.Transport` to `transport.QTransport`.
// Only use it with `BaseTransport` castable to `transport.Transport`.
type transportUpgrader struct {
Expand All @@ -132,7 +134,17 @@ func (t transportUpgrader) Dial(ctx context.Context, raddr ma.Multiaddr, p peer.
if err != nil {
return nil, err
}
return upgradedCapableConn{CapableConn: conn}, nil
return upgradedCapableConn{
listenedUpgradedCapableConn{BaseCapableConn: conn, t: t},
}, nil
}

func (t transportUpgrader) Listen(laddr ma.Multiaddr) (transport.QListener, error) {
l, err := t.BaseTransport.(transport.Transport).Listen(laddr)
if err != nil {
return nil, err
}
return upgradedListener{BaseListener: l, t: t}, nil
}

func (t transportUpgrader) Score(raddr ma.Multiaddr, _ peer.ID) (transport.Score, error) {
Expand Down Expand Up @@ -183,7 +195,7 @@ func (t transportUpgrader) Score(raddr ma.Multiaddr, _ peer.ID) (transport.Score

// Used to upgrade `transport.CapableConn` to `transport.QCapableConn`.
type upgradedCapableConn struct {
transport.CapableConn
listenedUpgradedCapableConn
}

func (c upgradedCapableConn) Quality() uint32 {
Expand All @@ -205,3 +217,25 @@ func (c upgradedCapableConn) Quality() uint32 {
}
return defaultNonProxyQuality
}

type listenedUpgradedCapableConn struct {
transport.BaseCapableConn
t transport.QTransport
}

func (c listenedUpgradedCapableConn) Transport() transport.QTransport {
return c.t
}

type upgradedListener struct {
transport.BaseListener
t transport.QTransport
}

func (l upgradedListener) Accept() (transport.ListenedQCapableConn, error) {
c, err := l.BaseListener.(transport.Listener).Accept()
if err != nil {
return nil, err
}
return listenedUpgradedCapableConn{BaseCapableConn: c, t: l.t}, nil
}

0 comments on commit 83882e9

Please sign in to comment.