conn.go 1.83 KB
Newer Older
1
2
3
package libp2pquic

import (
4
5
	ic "github.com/libp2p/go-libp2p-crypto"
	peer "github.com/libp2p/go-libp2p-peer"
6
	tpt "github.com/libp2p/go-libp2p-transport"
7
	smux "github.com/libp2p/go-stream-muxer"
8
9
10
11
	quic "github.com/lucas-clemente/quic-go"
	ma "github.com/multiformats/go-multiaddr"
)

12
type conn struct {
13
14
15
	sess      quic.Session
	transport tpt.Transport

16
17
18
	localPeer      peer.ID
	privKey        ic.PrivKey
	localMultiaddr ma.Multiaddr
19

20
21
22
	remotePeerID    peer.ID
	remotePubKey    ic.PubKey
	remoteMultiaddr ma.Multiaddr
23
24
}

25
var _ tpt.Conn = &conn{}
26

27
28
func (c *conn) Close() error {
	return c.sess.Close(nil)
29
30
}

31
32
33
// IsClosed returns whether a connection is fully closed.
func (c *conn) IsClosed() bool {
	return c.sess.Context().Err() != nil
34
35
}

36
37
38
39
// OpenStream creates a new stream.
func (c *conn) OpenStream() (smux.Stream, error) {
	qstr, err := c.sess.OpenStreamSync()
	return &stream{Stream: qstr}, err
40
41
}

42
43
44
45
// AcceptStream accepts a stream opened by the other side.
func (c *conn) AcceptStream() (smux.Stream, error) {
	qstr, err := c.sess.AcceptStream()
	return &stream{Stream: qstr}, err
46
47
}

48
49
50
// LocalPeer returns our peer ID
func (c *conn) LocalPeer() peer.ID {
	return c.localPeer
51
52
}

53
54
55
// LocalPrivateKey returns our private key
func (c *conn) LocalPrivateKey() ic.PrivKey {
	return c.privKey
56
57
}

58
59
60
// RemotePeer returns the peer ID of the remote peer.
func (c *conn) RemotePeer() peer.ID {
	return c.remotePeerID
61
62
}

63
64
65
// RemotePublicKey returns the public key of the remote peer.
func (c *conn) RemotePublicKey() ic.PubKey {
	return c.remotePubKey
66
67
}

68
69
70
// LocalMultiaddr returns the local Multiaddr associated
func (c *conn) LocalMultiaddr() ma.Multiaddr {
	return c.localMultiaddr
71
72
}

73
74
75
// RemoteMultiaddr returns the remote Multiaddr associated
func (c *conn) RemoteMultiaddr() ma.Multiaddr {
	return c.remoteMultiaddr
76
77
}

78
func (c *conn) Transport() tpt.Transport {
79
80
	return c.transport
}