/
config.go
59 lines (54 loc) · 1.81 KB
/
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
package client
import (
"fmt"
"net"
"time"
"github.com/plgd-dev/go-coap/v3/message"
"github.com/plgd-dev/go-coap/v3/message/codes"
"github.com/plgd-dev/go-coap/v3/message/pool"
"github.com/plgd-dev/go-coap/v3/net/monitor/inactivity"
"github.com/plgd-dev/go-coap/v3/net/responsewriter"
"github.com/plgd-dev/go-coap/v3/options/config"
)
const DefaultMTU = 1472
var DefaultConfig = func() Config {
opts := Config{
Common: config.NewCommon[*Conn](),
CreateInactivityMonitor: func() InactivityMonitor {
return inactivity.NewNilMonitor[*Conn]()
},
RequestMonitor: func(*Conn, *pool.Message) (bool, error) {
return false, nil
},
Dialer: &net.Dialer{Timeout: time.Second * 3},
Net: "udp",
TransmissionNStart: 1,
TransmissionAcknowledgeTimeout: time.Second * 2,
TransmissionMaxRetransmit: 4,
GetMID: message.GetMID,
MTU: DefaultMTU,
}
opts.Handler = func(w *responsewriter.ResponseWriter[*Conn], r *pool.Message) {
switch r.Code() {
case codes.POST, codes.PUT, codes.GET, codes.DELETE:
if err := w.SetResponse(codes.NotFound, message.TextPlain, nil); err != nil {
opts.Errors(fmt.Errorf("udp client: cannot set response: %w", err))
}
}
}
return opts
}()
type Config struct {
config.Common[*Conn]
CreateInactivityMonitor CreateInactivityMonitorFunc
RequestMonitor RequestMonitorFunc
Net string
GetMID GetMIDFunc
Handler HandlerFunc
Dialer *net.Dialer
TransmissionNStart uint32
TransmissionAcknowledgeTimeout time.Duration
TransmissionMaxRetransmit uint32
CloseSocket bool
MTU uint16
}