]> Sergey Matveev's repositories - btrtrc.git/blob - webrtc.go
Fix webrtcNetAddr.String for IPv6
[btrtrc.git] / webrtc.go
1 package torrent
2
3 import (
4         "net"
5         "strconv"
6         "time"
7
8         "github.com/anacrolix/torrent/webtorrent"
9         "github.com/pion/datachannel"
10         "github.com/pion/webrtc/v3"
11 )
12
13 const webrtcNetwork = "webrtc"
14
15 type webrtcNetConn struct {
16         datachannel.ReadWriteCloser
17         webtorrent.DataChannelContext
18 }
19
20 type webrtcNetAddr struct {
21         *webrtc.ICECandidate
22 }
23
24 var _ net.Addr = webrtcNetAddr{}
25
26 func (webrtcNetAddr) Network() string {
27         // Now that we have the ICE candidate, we can tell if it's over udp or tcp. But should we use
28         // that for the network?
29         return webrtcNetwork
30 }
31
32 func (me webrtcNetAddr) String() string {
33         return net.JoinHostPort(me.Address, strconv.FormatUint(uint64(me.Port), 10))
34 }
35
36 func (me webrtcNetConn) LocalAddr() net.Addr {
37         // I'm not sure if this evolves over time. It might also be unavailable if the PeerConnection is
38         // closed or closes itself. The same concern applies to RemoteAddr.
39         pair, err := me.DataChannelContext.GetSelectedIceCandidatePair()
40         if err != nil {
41                 panic(err)
42         }
43         return webrtcNetAddr{pair.Local}
44 }
45
46 func (me webrtcNetConn) RemoteAddr() net.Addr {
47         // See comments on LocalAddr.
48         pair, err := me.DataChannelContext.GetSelectedIceCandidatePair()
49         if err != nil {
50                 panic(err)
51         }
52         return webrtcNetAddr{pair.Remote}
53 }
54
55 // Do we need these for WebRTC connections exposed as net.Conns? Can we set them somewhere inside
56 // PeerConnection or on the channel or some transport?
57
58 func (w webrtcNetConn) SetDeadline(t time.Time) error {
59         return nil
60 }
61
62 func (w webrtcNetConn) SetReadDeadline(t time.Time) error {
63         return nil
64 }
65
66 func (w webrtcNetConn) SetWriteDeadline(t time.Time) error {
67         return nil
68 }