Skip to content

Commit

Permalink
Merge pull request #8 from marten-seemann/master
Browse files Browse the repository at this point in the history
Compatibility with the new transport interfaces
  • Loading branch information
Stebalien authored Sep 7, 2017
2 parents 53da0c0 + 21fd167 commit 3c94e56
Showing 1 changed file with 22 additions and 7 deletions.
29 changes: 22 additions & 7 deletions p2p/transport/tcp/tcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ import (
"fmt"
"net"
"sync"
"time"

logging "github.com/ipfs/go-log"
reuseport "github.com/jbenet/go-reuseport"
Expand All @@ -25,6 +24,8 @@ type TcpTransport struct {
listeners map[string]tpt.Listener
}

var _ tpt.Transport = &TcpTransport{}

// NewTCPTransport creates a tcp transport object that tracks dialers and listeners
// created. It represents an entire tcp stack (though it might not necessarily be)
func NewTCPTransport() *TcpTransport {
Expand Down Expand Up @@ -54,8 +55,6 @@ func (t *TcpTransport) Dialer(laddr ma.Multiaddr, opts ...tpt.DialOpt) (tpt.Dial
var doReuse bool
for _, o := range opts {
switch o := o.(type) {
case tpt.TimeoutOpt:
base.Timeout = time.Duration(o)
case tpt.ReuseportOpt:
doReuse = bool(o)
default:
Expand Down Expand Up @@ -135,6 +134,8 @@ type tcpDialer struct {
transport tpt.Transport
}

var _ tpt.Dialer = &tcpDialer{}

func (t *TcpTransport) newTcpDialer(base manet.Dialer, laddr ma.Multiaddr, doReuse bool) (*tcpDialer, error) {
// get the local net.Addr manually
la, err := manet.ToNetAddr(laddr)
Expand Down Expand Up @@ -195,9 +196,9 @@ func (d *tcpDialer) DialContext(ctx context.Context, raddr ma.Multiaddr) (tpt.Co
return nil, err
}

return &tpt.ConnWrap{
return &tcpConn{
Conn: c,
Tpt: d.transport,
t: d.transport,
}, nil
}

Expand Down Expand Up @@ -238,15 +239,17 @@ type tcpListener struct {
transport tpt.Transport
}

var _ tpt.Listener = &tcpListener{}

func (d *tcpListener) Accept() (tpt.Conn, error) {
c, err := d.list.Accept()
if err != nil {
return nil, err
}

return &tpt.ConnWrap{
return &tcpConn{
Conn: c,
Tpt: d.transport,
t: d.transport,
}, nil
}

Expand All @@ -265,3 +268,15 @@ func (t *tcpListener) NetListener() net.Listener {
func (d *tcpListener) Close() error {
return d.list.Close()
}

type tcpConn struct {
manet.Conn
t tpt.Transport
}

var _ tpt.Conn = &tcpConn{}
var _ tpt.DuplexConn = &tcpConn{}

func (c *tcpConn) Transport() tpt.Transport {
return c.t
}

0 comments on commit 3c94e56

Please sign in to comment.