-
Notifications
You must be signed in to change notification settings - Fork 35
/
conn_config.go
98 lines (81 loc) · 2.89 KB
/
conn_config.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
package voice
import (
"log/slog"
)
// DefaultConnConfig returns a ConnConfig with sensible defaults.
func DefaultConnConfig() *ConnConfig {
return &ConnConfig{
Logger: slog.Default(),
GatewayCreateFunc: NewGateway,
UDPConnCreateFunc: NewUDPConn,
AudioSenderCreateFunc: NewAudioSender,
AudioReceiverCreateFunc: NewAudioReceiver,
}
}
// ConnConfig is used to configure a Conn.
type ConnConfig struct {
Logger *slog.Logger
GatewayCreateFunc GatewayCreateFunc
GatewayConfigOpts []GatewayConfigOpt
UDPConnCreateFunc UDPConnCreateFunc
UDPConnConfigOpts []UDPConnConfigOpt
AudioSenderCreateFunc AudioSenderCreateFunc
AudioReceiverCreateFunc AudioReceiverCreateFunc
EventHandlerFunc EventHandlerFunc
}
// ConnConfigOpt is used to functionally configure a ConnConfig.
type ConnConfigOpt func(connConfig *ConnConfig)
// Apply applies the ConnConfigOpt(s) to the ConnConfig.
func (c *ConnConfig) Apply(opts []ConnConfigOpt) {
for _, opt := range opts {
opt(c)
}
}
// WithConnLogger sets the Conn(s) used Logger.
func WithConnLogger(logger *slog.Logger) ConnConfigOpt {
return func(config *ConnConfig) {
config.Logger = logger
}
}
// WithConnGatewayCreateFunc sets the Conn(s) used GatewayCreateFunc.
func WithConnGatewayCreateFunc(gatewayCreateFunc GatewayCreateFunc) ConnConfigOpt {
return func(config *ConnConfig) {
config.GatewayCreateFunc = gatewayCreateFunc
}
}
// WithConnGatewayConfigOpts sets the Conn(s) used GatewayConfigOpt(s).
func WithConnGatewayConfigOpts(opts ...GatewayConfigOpt) ConnConfigOpt {
return func(config *ConnConfig) {
config.GatewayConfigOpts = append(config.GatewayConfigOpts, opts...)
}
}
// WithUDPConnCreateFunc sets the Conn(s) used UDPConnCreateFunc.
func WithUDPConnCreateFunc(udpConnCreateFunc UDPConnCreateFunc) ConnConfigOpt {
return func(config *ConnConfig) {
config.UDPConnCreateFunc = udpConnCreateFunc
}
}
// WithUDPConnConfigOpts sets the Conn(s) used UDPConnConfigOpt(s).
func WithUDPConnConfigOpts(opts ...UDPConnConfigOpt) ConnConfigOpt {
return func(config *ConnConfig) {
config.UDPConnConfigOpts = append(config.UDPConnConfigOpts, opts...)
}
}
// WithConnAudioSenderCreateFunc sets the Conn(s) used AudioSenderCreateFunc.
func WithConnAudioSenderCreateFunc(audioSenderCreateFunc AudioSenderCreateFunc) ConnConfigOpt {
return func(config *ConnConfig) {
config.AudioSenderCreateFunc = audioSenderCreateFunc
}
}
// WithConnAudioReceiverCreateFunc sets the Conn(s) used AudioReceiverCreateFunc.
func WithConnAudioReceiverCreateFunc(audioReceiverCreateFunc AudioReceiverCreateFunc) ConnConfigOpt {
return func(config *ConnConfig) {
config.AudioReceiverCreateFunc = audioReceiverCreateFunc
}
}
// WithConnEventHandlerFunc sets the Conn(s) used EventHandlerFunc.
func WithConnEventHandlerFunc(eventHandlerFunc EventHandlerFunc) ConnConfigOpt {
return func(config *ConnConfig) {
config.EventHandlerFunc = eventHandlerFunc
}
}