]> Sergey Matveev's repositories - btrtrc.git/blob - webrtc.go
Merge branch 'http-proxying' into crawshaw
[btrtrc.git] / webrtc.go
1 package torrent
2
3 import (
4         "fmt"
5         "net"
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         // Probably makes sense to return the IP:port expected of most net.Addrs. I'm not sure if
34         // Address would be quoted for IPv6 already. If not, net.JoinHostPort might be appropriate.
35         return fmt.Sprintf("%s:%d", me.Address, me.Port)
36 }
37
38 func (me webrtcNetConn) LocalAddr() net.Addr {
39         // I'm not sure if this evolves over time. It might also be unavailable if the PeerConnection is
40         // closed or closes itself. The same concern applies to RemoteAddr.
41         pair, err := me.DataChannelContext.GetSelectedIceCandidatePair()
42         if err != nil {
43                 panic(err)
44         }
45         return webrtcNetAddr{pair.Local}
46 }
47
48 func (me webrtcNetConn) RemoteAddr() net.Addr {
49         // See comments on LocalAddr.
50         pair, err := me.DataChannelContext.GetSelectedIceCandidatePair()
51         if err != nil {
52                 panic(err)
53         }
54         return webrtcNetAddr{pair.Remote}
55 }
56
57 // Do we need these for WebRTC connections exposed as net.Conns? Can we set them somewhere inside
58 // PeerConnection or on the channel or some transport?
59
60 func (w webrtcNetConn) SetDeadline(t time.Time) error {
61         return nil
62 }
63
64 func (w webrtcNetConn) SetReadDeadline(t time.Time) error {
65         return nil
66 }
67
68 func (w webrtcNetConn) SetWriteDeadline(t time.Time) error {
69         return nil
70 }