transport.go 1.2 KB
Newer Older
Marten Seemann's avatar
Marten Seemann committed
1
2
3
package libp2pquic

import (
Marten Seemann's avatar
Marten Seemann committed
4
5
6
	"sync"

	pstore "github.com/libp2p/go-libp2p-peerstore"
Marten Seemann's avatar
Marten Seemann committed
7
8
9
10
	tpt "github.com/libp2p/go-libp2p-transport"
	ma "github.com/multiformats/go-multiaddr"
)

Marten Seemann's avatar
Marten Seemann committed
11
12
13
14
15
16
17
18
// QuicTransport implements a QUIC Transport
type QuicTransport struct {
	mutex sync.Mutex

	peers pstore.Peerstore

	listeners map[string]tpt.Listener
}
Marten Seemann's avatar
Marten Seemann committed
19

Marten Seemann's avatar
Marten Seemann committed
20
21
22
23
24
25
26
// NewQuicTransport creates a new QUIC Transport
// it tracks dialers and listeners created
func NewQuicTransport(peers pstore.Peerstore) *QuicTransport {
	return &QuicTransport{
		peers:     peers,
		listeners: make(map[string]tpt.Listener),
	}
Marten Seemann's avatar
Marten Seemann committed
27
28
29
30
31
32
}

func (t *QuicTransport) Dialer(laddr ma.Multiaddr, opts ...tpt.DialOpt) (tpt.Dialer, error) {
	panic("not implemented")
}

Marten Seemann's avatar
Marten Seemann committed
33
// Listen starts listening on laddr
Marten Seemann's avatar
Marten Seemann committed
34
func (t *QuicTransport) Listen(laddr ma.Multiaddr) (tpt.Listener, error) {
Marten Seemann's avatar
Marten Seemann committed
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
	// 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
Marten Seemann's avatar
Marten Seemann committed
51
52
53
54
55
56
57
}

func (t *QuicTransport) Matches(ma.Multiaddr) bool {
	panic("not implemented")
}

var _ tpt.Transport = &QuicTransport{}