-
Notifications
You must be signed in to change notification settings - Fork 3
/
client.go
101 lines (87 loc) · 2.15 KB
/
client.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
package rudp
import (
"context"
"net"
"time"
dcep "github.com/pion/datachannel"
"github.com/pion/logging"
"github.com/pion/sctp"
)
// DialConfig ...
type DialConfig struct {
Network string
LocalAddr *net.UDPAddr
RemoteAddr *net.UDPAddr
BufferSize int
LoggerFactory logging.LoggerFactory
}
// Client ...
type Client struct {
conn *net.UDPConn
assoc *sctp.Association
loggerFactory logging.LoggerFactory
log logging.LeveledLogger
}
// Dial ...
func Dial(config *DialConfig) (*Client, error) {
log := config.LoggerFactory.NewLogger("rudp-c")
conn, err := net.DialUDP(config.Network, config.LocalAddr, config.RemoteAddr)
if err != nil {
return nil, err
}
if config.BufferSize > 0 {
log.Debugf("setting buffer size to %d\n", config.BufferSize)
err = conn.SetReadBuffer(config.BufferSize)
if err != nil {
return nil, err
}
err = conn.SetWriteBuffer(config.BufferSize)
if err != nil {
return nil, err
}
}
log.Debug("instantiating SCTP client")
assoc, err := sctp.Client(sctp.Config{
LoggerFactory: config.LoggerFactory,
MaxReceiveBufferSize: uint32(config.BufferSize), // 0: defaults to 1MB
NetConn: conn,
})
if err != nil {
return nil, err
}
log.Debug("creating new client")
c := &Client{
conn: conn,
assoc: assoc,
loggerFactory: config.LoggerFactory,
log: log,
}
return c, nil
}
// OpenChannel ...
func (c *Client) OpenChannel(chID uint16, cfg Config) (Channel, error) {
c.log.Debugf("opening channel %d", chID)
dcepCh, err := dcep.Dial(c.assoc, chID, &dcep.Config{
ChannelType: cfg.ChannelType,
Negotiated: cfg.Negotiated,
Priority: cfg.Priority,
ReliabilityParameter: cfg.ReliabilityParameter,
Label: cfg.Label,
Protocol: cfg.Protocol,
LoggerFactory: c.loggerFactory,
})
if err != nil {
return nil, err
}
dc := &dataChannel{
dc: dcepCh,
config: cfg,
}
return dc, nil
}
// Close ...
func (c *Client) Close() error {
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Second)
defer cancel()
return c.assoc.Shutdown(ctx)
}