-
-
Notifications
You must be signed in to change notification settings - Fork 114
/
muxResponseWriter.go
47 lines (39 loc) · 1.4 KB
/
muxResponseWriter.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
package mux
import (
"io"
"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/responsewriter"
)
// ToHandler converts mux handler to udp/dtls/tcp handler.
func ToHandler[C Conn](m Handler) func(w *responsewriter.ResponseWriter[C], r *pool.Message) {
return func(w *responsewriter.ResponseWriter[C], r *pool.Message) {
muxw := &muxResponseWriter[C]{
w: w,
}
m.ServeCOAP(muxw, &Message{
Message: r,
RouteParams: new(RouteParams),
})
}
}
type muxResponseWriter[C Conn] struct {
w *responsewriter.ResponseWriter[C]
}
// SetResponse simplifies the setup of the response for the request. ETags must be set via options. For advanced setup, use Message().
func (w *muxResponseWriter[C]) SetResponse(code codes.Code, contentFormat message.MediaType, d io.ReadSeeker, opts ...message.Option) error {
return w.w.SetResponse(code, contentFormat, d, opts...)
}
// Conn peer connection.
func (w *muxResponseWriter[C]) Conn() Conn {
return w.w.Conn()
}
// Message direct access to the response.
func (w *muxResponseWriter[C]) Message() *pool.Message {
return w.w.Message()
}
// SetMessage replaces the response message. Ensure that Token, MessageID(udp), and Type(udp) messages are paired correctly.
func (w *muxResponseWriter[C]) SetMessage(msg *pool.Message) {
w.w.SetMessage(msg)
}