Unverified Commit 05430963 authored by Marten Seemann's avatar Marten Seemann
Browse files

implement the listener

parent 3163c6bb
package libp2pquic
import (
"net"
"time"
tpt "github.com/libp2p/go-libp2p-transport"
ma "github.com/multiformats/go-multiaddr"
)
type conn struct {
quicConn net.Conn
transport tpt.Transport
}
func (c *conn) Read(p []byte) (int, error) {
return c.quicConn.Read(p)
}
func (c *conn) Write(p []byte) (int, error) {
return c.quicConn.Write(p)
}
func (c *conn) Close() error {
return c.quicConn.Close()
}
func (c *conn) LocalAddr() net.Addr {
return c.quicConn.LocalAddr()
}
func (c *conn) RemoteAddr() net.Addr {
return c.quicConn.RemoteAddr()
}
func (c *conn) LocalMultiaddr() ma.Multiaddr {
panic("not implemented")
}
func (c *conn) RemoteMultiaddr() ma.Multiaddr {
panic("not implemented")
}
func (c *conn) Transport() tpt.Transport {
return c.transport
}
func (c *conn) SetDeadline(t time.Time) error {
return nil
}
func (c *conn) SetReadDeadline(t time.Time) error {
return nil
}
func (c *conn) SetWriteDeadline(t time.Time) error {
return nil
}
var _ tpt.Conn = &conn{}
package libp2pquic package libp2pquic
import ( import (
"crypto/tls"
"net" "net"
pstore "github.com/libp2p/go-libp2p-peerstore"
tpt "github.com/libp2p/go-libp2p-transport" tpt "github.com/libp2p/go-libp2p-transport"
quicconn "github.com/marten-seemann/quic-conn"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
manet "github.com/multiformats/go-multiaddr-net"
) )
type listener struct{} type listener struct {
laddr ma.Multiaddr
quicListener net.Listener
transport tpt.Transport
}
func newListener(laddr ma.Multiaddr, peers pstore.Peerstore, transport tpt.Transport) (*listener, error) {
tlsConf := &tls.Config{}
network, host, err := manet.DialArgs(laddr)
if err != nil {
return nil, err
}
qln, err := quicconn.Listen(network, host, tlsConf)
if err != nil {
return nil, err
}
return &listener{
laddr: laddr,
quicListener: qln,
transport: transport,
}, nil
}
func (l *listener) Accept() (tpt.Conn, error) { func (l *listener) Accept() (tpt.Conn, error) {
panic("not implemented") c, err := l.quicListener.Accept()
if err != nil {
return nil, err
}
return &conn{
quicConn: c,
transport: l.transport,
}, nil
} }
func (l *listener) Close() error { func (l *listener) Close() error {
panic("not implemented") return l.quicListener.Close()
} }
func (l *listener) Addr() net.Addr { func (l *listener) Addr() net.Addr {
panic("not implemented") return l.quicListener.Addr()
} }
func (l *listener) Multiaddr() ma.Multiaddr { func (l *listener) Multiaddr() ma.Multiaddr {
panic("not implemented") return l.laddr
} }
var _ tpt.Listener = &listener{} var _ tpt.Listener = &listener{}
package libp2pquic package libp2pquic
import ( import (
"sync"
pstore "github.com/libp2p/go-libp2p-peerstore"
tpt "github.com/libp2p/go-libp2p-transport" tpt "github.com/libp2p/go-libp2p-transport"
ma "github.com/multiformats/go-multiaddr" ma "github.com/multiformats/go-multiaddr"
) )
type QuicTransport struct{} // QuicTransport implements a QUIC Transport
type QuicTransport struct {
mutex sync.Mutex
peers pstore.Peerstore
listeners map[string]tpt.Listener
}
func NewQuicTransport() *QuicTransport { // NewQuicTransport creates a new QUIC Transport
return &QuicTransport{} // it tracks dialers and listeners created
func NewQuicTransport(peers pstore.Peerstore) *QuicTransport {
return &QuicTransport{
peers: peers,
listeners: make(map[string]tpt.Listener),
}
} }
func (t *QuicTransport) Dialer(laddr ma.Multiaddr, opts ...tpt.DialOpt) (tpt.Dialer, error) { func (t *QuicTransport) Dialer(laddr ma.Multiaddr, opts ...tpt.DialOpt) (tpt.Dialer, error) {
panic("not implemented") panic("not implemented")
} }
// Listen starts listening on laddr
func (t *QuicTransport) Listen(laddr ma.Multiaddr) (tpt.Listener, error) { func (t *QuicTransport) Listen(laddr ma.Multiaddr) (tpt.Listener, error) {
panic("not implemented") // TODO: check if laddr is actually a QUIC address
t.mutex.Lock()
defer t.mutex.Unlock()
l, ok := t.listeners[laddr.String()]
if ok {
return l, nil
}
ln, err := newListener(laddr, t.peers, t)
if err != nil {
return nil, err
}
t.listeners[laddr.String()] = ln
return ln, nil
} }
func (t *QuicTransport) Matches(ma.Multiaddr) bool { func (t *QuicTransport) Matches(ma.Multiaddr) bool {
......
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