forked from pion/ice
-
Notifications
You must be signed in to change notification settings - Fork 0
/
transport.go
134 lines (111 loc) · 3.02 KB
/
transport.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
package ice
import (
"context"
"errors"
"net"
"sync/atomic"
"time"
"github.com/pion/stun"
)
// Dial connects to the remote agent, acting as the controlling ice agent.
// Dial blocks until at least one ice candidate pair has successfully connected.
func (a *Agent) Dial(ctx context.Context, remoteUfrag, remotePwd string) (*Conn, error) {
return a.connect(ctx, true, remoteUfrag, remotePwd)
}
// Accept connects to the remote agent, acting as the controlled ice agent.
// Accept blocks until at least one ice candidate pair has successfully connected.
func (a *Agent) Accept(ctx context.Context, remoteUfrag, remotePwd string) (*Conn, error) {
return a.connect(ctx, false, remoteUfrag, remotePwd)
}
// Conn represents the ICE connection.
// At the moment the lifetime of the Conn is equal to the Agent.
type Conn struct {
bytesReceived uint64
bytesSent uint64
agent *Agent
}
// BytesSent returns the number of bytes sent
func (c *Conn) BytesSent() uint64 {
return atomic.LoadUint64(&c.bytesSent)
}
// BytesReceived returns the number of bytes received
func (c *Conn) BytesReceived() uint64 {
return atomic.LoadUint64(&c.bytesReceived)
}
func (a *Agent) connect(ctx context.Context, isControlling bool, remoteUfrag, remotePwd string) (*Conn, error) {
err := a.ok()
if err != nil {
return nil, err
}
if a.opened {
return nil, errors.New("a connection is already opened")
}
err = a.startConnectivityChecks(isControlling, remoteUfrag, remotePwd)
if err != nil {
return nil, err
}
// block until pair selected
select {
case <-a.done:
return nil, a.getErr()
case <-ctx.Done():
// TODO: Stop connectivity checks?
return nil, ErrCanceledByCaller
case <-a.onConnected:
}
return &Conn{
agent: a,
}, nil
}
// Read implements the Conn Read method.
func (c *Conn) Read(p []byte) (int, error) {
err := c.agent.ok()
if err != nil {
return 0, err
}
n, err := c.agent.buffer.Read(p)
atomic.AddUint64(&c.bytesReceived, uint64(n))
return n, err
}
// Write implements the Conn Write method.
func (c *Conn) Write(p []byte) (int, error) {
err := c.agent.ok()
if err != nil {
return 0, err
}
if stun.IsMessage(p) {
return 0, errors.New("the ICE conn can't write STUN messages")
}
pair, err := c.agent.getSelectedPair()
if err != nil {
return 0, err
}
atomic.AddUint64(&c.bytesSent, uint64(len(p)))
return pair.Write(p)
}
// Close implements the Conn Close method. It is used to close
// the connection. Any calls to Read and Write will be unblocked and return an error.
func (c *Conn) Close() error {
return c.agent.Close()
}
// TODO: Maybe just switch to using io.ReadWriteCloser?
// LocalAddr is a stub
func (c *Conn) LocalAddr() net.Addr {
return nil
}
// RemoteAddr is a stub
func (c *Conn) RemoteAddr() net.Addr {
return nil
}
// SetDeadline is a stub
func (c *Conn) SetDeadline(t time.Time) error {
return nil
}
// SetReadDeadline is a stub
func (c *Conn) SetReadDeadline(t time.Time) error {
return nil
}
// SetWriteDeadline is a stub
func (c *Conn) SetWriteDeadline(t time.Time) error {
return nil
}