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>
64 lines
1.7 KiB
Go
64 lines
1.7 KiB
Go
// Package coap provides a CoAP client and server.
|
|
package coap
|
|
|
|
import (
|
|
"crypto/tls"
|
|
"fmt"
|
|
|
|
piondtls "github.com/pion/dtls/v2"
|
|
"github.com/plgd-dev/go-coap/v2/dtls"
|
|
"github.com/plgd-dev/go-coap/v2/mux"
|
|
"github.com/plgd-dev/go-coap/v2/net"
|
|
"github.com/plgd-dev/go-coap/v2/tcp"
|
|
"github.com/plgd-dev/go-coap/v2/udp"
|
|
)
|
|
|
|
// ListenAndServe Starts a server on address and network specified Invoke handler
|
|
// for incoming queries.
|
|
func ListenAndServe(network string, addr string, handler mux.Handler) error {
|
|
switch network {
|
|
case "udp", "udp4", "udp6", "":
|
|
l, err := net.NewListenUDP(network, addr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer l.Close()
|
|
s := udp.NewServer(udp.WithMux(handler))
|
|
return s.Serve(l)
|
|
case "tcp", "tcp4", "tcp6":
|
|
l, err := net.NewTCPListener(network, addr)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer l.Close()
|
|
s := tcp.NewServer(tcp.WithMux(handler))
|
|
return s.Serve(l)
|
|
default:
|
|
return fmt.Errorf("invalid network (%v)", network)
|
|
}
|
|
}
|
|
|
|
// ListenAndServeTCPTLS Starts a server on address and network over TLS specified Invoke handler
|
|
// for incoming queries.
|
|
func ListenAndServeTCPTLS(network, addr string, config *tls.Config, handler mux.Handler) error {
|
|
l, err := net.NewTLSListener(network, addr, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer l.Close()
|
|
s := tcp.NewServer(tcp.WithMux(handler))
|
|
return s.Serve(l)
|
|
}
|
|
|
|
// ListenAndServeDTLS Starts a server on address and network over DTLS specified Invoke handler
|
|
// for incoming queries.
|
|
func ListenAndServeDTLS(network string, addr string, config *piondtls.Config, handler mux.Handler) error {
|
|
l, err := net.NewDTLSListener(network, addr, config)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer l.Close()
|
|
s := dtls.NewServer(dtls.WithMux(handler))
|
|
return s.Serve(l)
|
|
}
|