-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
common.go
61 lines (56 loc) · 1.99 KB
/
common.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
package config
import (
"context"
"fmt"
"time"
"github.com/plgd-dev/go-coap/v3/message"
"github.com/plgd-dev/go-coap/v3/message/pool"
"github.com/plgd-dev/go-coap/v3/net/blockwise"
"github.com/plgd-dev/go-coap/v3/net/client"
"github.com/plgd-dev/go-coap/v3/net/responsewriter"
"github.com/plgd-dev/go-coap/v3/pkg/runner/periodic"
)
type (
ErrorFunc = func(error)
HandlerFunc[C responsewriter.Client] func(w *responsewriter.ResponseWriter[C], r *pool.Message)
ProcessReceivedMessageFunc[C responsewriter.Client] func(req *pool.Message, cc C, handler HandlerFunc[C])
)
type Common[C responsewriter.Client] struct {
LimitClientParallelRequests int64
LimitClientEndpointParallelRequests int64
Ctx context.Context
Errors ErrorFunc
PeriodicRunner periodic.Func
MessagePool *pool.Pool
GetToken client.GetTokenFunc
MaxMessageSize uint32
BlockwiseTransferTimeout time.Duration
BlockwiseSZX blockwise.SZX
BlockwiseEnable bool
ProcessReceivedMessage ProcessReceivedMessageFunc[C]
ReceivedMessageQueueSize int
}
func NewCommon[C responsewriter.Client]() Common[C] {
return Common[C]{
Ctx: context.Background(),
MaxMessageSize: 64 * 1024,
Errors: func(err error) {
fmt.Println(err)
},
BlockwiseSZX: blockwise.SZX1024,
BlockwiseEnable: true,
BlockwiseTransferTimeout: time.Second * 3,
PeriodicRunner: func(f func(now time.Time) bool) {
go func() {
for f(time.Now()) {
time.Sleep(4 * time.Second)
}
}()
},
MessagePool: pool.New(1024, 2048),
GetToken: message.GetToken,
LimitClientParallelRequests: 1,
LimitClientEndpointParallelRequests: 1,
ReceivedMessageQueueSize: 16,
}
}