This repository has been archived by the owner on Jun 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
client_permission.go
108 lines (95 loc) · 2.35 KB
/
client_permission.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package turnc
import (
"context"
"errors"
"net"
"sync"
"time"
"go.uber.org/zap"
"gortc.io/turn"
)
// Permission implements net.PacketConn.
type Permission struct {
log *zap.Logger
mux sync.RWMutex
ip net.IP
client *Client
ctx context.Context
cancel func()
wg sync.WaitGroup
refreshRate time.Duration
conn []*Connection
}
var (
// ErrAlreadyBound means that selected permission already has bound channel number.
ErrAlreadyBound = errors.New("channel already bound")
// ErrNotBound means that selected permission already has no channel number.
ErrNotBound = errors.New("channel is not bound")
)
func (p *Permission) refresh() error {
return p.client.alloc.allocate(turn.PeerAddress{IP: p.ip})
}
func (p *Permission) startLoop(f func()) {
if p.refreshRate == 0 {
return
}
p.wg.Add(1)
go func() {
ticker := time.NewTicker(p.refreshRate)
defer p.wg.Done()
for {
select {
case <-ticker.C:
f()
case <-p.ctx.Done():
return
}
}
}()
}
func (p *Permission) startRefreshLoop() {
p.startLoop(func() {
if err := p.refresh(); err != nil {
p.log.Error("failed to refresh permission", zap.Error(err))
}
p.log.Debug("permission refreshed")
})
}
// WriteTo writes packet b to addr.
func (p *Permission) WriteTo(b []byte, addr net.Addr) (n int, err error) {
return 0, ErrNotImplemented
}
// Close stops all refreshing loops for permission and removes it from
// allocation.
func (p *Permission) Close() error {
p.mux.Lock()
cancel := p.cancel
p.mux.Unlock()
cancel()
p.wg.Wait()
p.client.alloc.removePermission(p)
return nil
}
// ErrNotImplemented means that functionality is not currently implemented,
// but it will be (eventually).
var ErrNotImplemented = errors.New("functionality not implemented")
func (p *Permission) removeConn(connection *Connection) {}
// CreateUDP creates new UDP Permission to peer with provided addr.
func (p *Permission) CreateUDP(addr *net.UDPAddr) (*Connection, error) {
peer := turn.PeerAddress{
IP: addr.IP,
Port: addr.Port,
}
c := &Connection{
log: p.log,
peerAddr: peer,
client: p.client,
refreshRate: p.client.refreshRate,
}
c.ctx, c.cancel = context.WithCancel(context.Background())
c.peerL, c.peerR = net.Pipe()
p.client.mux.Lock()
p.conn = append(p.conn, c)
p.client.mux.Unlock()
return c, nil
}