mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-04 22:17:59 +08:00
42 lines
973 B
Go
42 lines
973 B
Go
package tcp
|
|
|
|
import (
|
|
"io"
|
|
|
|
"github.com/plgd-dev/go-coap/v2/message"
|
|
"github.com/plgd-dev/go-coap/v2/message/codes"
|
|
"github.com/plgd-dev/go-coap/v2/mux"
|
|
"github.com/plgd-dev/go-coap/v2/tcp/message/pool"
|
|
)
|
|
|
|
// WithMux set's multiplexer for handle requests.
|
|
func WithMux(m mux.Handler) HandlerFuncOpt {
|
|
h := func(w *ResponseWriter, r *pool.Message) {
|
|
muxw := &muxResponseWriter{
|
|
w: w,
|
|
}
|
|
muxr, err := pool.ConvertTo(r)
|
|
if err != nil {
|
|
return
|
|
}
|
|
m.ServeCOAP(muxw, &mux.Message{
|
|
Message: muxr,
|
|
SequenceNumber: r.Sequence(),
|
|
RouteParams: new(mux.RouteParams),
|
|
})
|
|
}
|
|
return WithHandlerFunc(h)
|
|
}
|
|
|
|
type muxResponseWriter struct {
|
|
w *ResponseWriter
|
|
}
|
|
|
|
func (w *muxResponseWriter) SetResponse(code codes.Code, contentFormat message.MediaType, d io.ReadSeeker, opts ...message.Option) error {
|
|
return w.w.SetResponse(code, contentFormat, d, opts...)
|
|
}
|
|
|
|
func (w *muxResponseWriter) Client() mux.Client {
|
|
return w.w.ClientConn().Client()
|
|
}
|