mirror of
https://github.com/mainflux/mainflux.git
synced 2025-05-04 22:17:59 +08:00

* Switch CoAP lib Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Revert removed adapter code Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * WIP CoAP refactor Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add auth key Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix observers map Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix reading message body Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix subtopic parsing Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix error handling Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Fix multi-protocol communication Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Separate client from observer Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Remove unused config Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Remove TCP option Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Inline error check Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Add logging client errors Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Replace RWMutex since we're not using RLock Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Inline error handling Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com> * Inline error handling Signed-off-by: dusanb94 <dusan.borovcanin@mainflux.com>
60 lines
1.4 KiB
Go
60 lines
1.4 KiB
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/message/noresponse"
|
|
"github.com/plgd-dev/go-coap/v2/tcp/message/pool"
|
|
)
|
|
|
|
// A ResponseWriter interface is used by an CAOP handler to construct an COAP response.
|
|
type ResponseWriter struct {
|
|
noResponseValue *uint32
|
|
response *pool.Message
|
|
cc *ClientConn
|
|
}
|
|
|
|
func NewResponseWriter(response *pool.Message, cc *ClientConn, requestOptions message.Options) *ResponseWriter {
|
|
var noResponseValue *uint32
|
|
v, err := requestOptions.GetUint32(message.NoResponse)
|
|
if err == nil {
|
|
noResponseValue = &v
|
|
}
|
|
|
|
return &ResponseWriter{
|
|
response: response,
|
|
cc: cc,
|
|
noResponseValue: noResponseValue,
|
|
}
|
|
}
|
|
|
|
func (r *ResponseWriter) SetResponse(code codes.Code, contentFormat message.MediaType, d io.ReadSeeker, opts ...message.Option) error {
|
|
if r.noResponseValue != nil {
|
|
err := noresponse.IsNoResponseCode(code, *r.noResponseValue)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
r.response.SetCode(code)
|
|
r.response.ResetOptionsTo(opts)
|
|
if d != nil {
|
|
r.response.SetContentFormat(contentFormat)
|
|
r.response.SetBody(d)
|
|
if !r.response.HasOption(message.ETag) {
|
|
etag, err := message.GetETag(d)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
r.response.SetOptionBytes(message.ETag, etag)
|
|
}
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (r *ResponseWriter) ClientConn() *ClientConn {
|
|
return r.cc
|
|
}
|