Documentation ¶
Index ¶
- Variables
- func NewDeleteRequest(ctx context.Context, messagePool *pool.Pool, path string, ...) (*pool.Message, error)
- func NewGetRequest(ctx context.Context, messagePool *pool.Pool, path string, ...) (*pool.Message, error)
- func NewObserveRequest(ctx context.Context, messagePool *pool.Pool, path string, ...) (*pool.Message, error)
- func NewPostRequest(ctx context.Context, messagePool *pool.Pool, path string, ...) (*pool.Message, error)
- func NewPutRequest(ctx context.Context, messagePool *pool.Pool, path string, ...) (*pool.Message, error)
- type BlockwiseFactoryFunc
- type BlockwiseOpt
- type ClientConn
- func (cc *ClientConn) AcquireMessage(ctx context.Context) *pool.Message
- func (cc *ClientConn) AddOnClose(f EventFunc)
- func (cc *ClientConn) AsyncPing(receivedPong func()) (func(), error)
- func (cc *ClientConn) CheckExpirations(now time.Time)
- func (cc *ClientConn) Client() *ClientTCP
- func (cc *ClientConn) Close() error
- func (cc *ClientConn) Context() context.Context
- func (cc *ClientConn) Delete(ctx context.Context, path string, opts ...message.Option) (*pool.Message, error)
- func (cc *ClientConn) Do(req *pool.Message) (*pool.Message, error)
- func (cc *ClientConn) Done() <-chan struct{}
- func (cc *ClientConn) Get(ctx context.Context, path string, opts ...message.Option) (*pool.Message, error)
- func (cc *ClientConn) LocalAddr() net.Addr
- func (*ClientConn) Lock()
- func (cc *ClientConn) Observe(ctx context.Context, path string, observeFunc func(req *pool.Message), ...) (*Observation, error)
- func (cc *ClientConn) ObserveRequest(req *pool.Message, observeFunc func(req *pool.Message)) (*Observation, error)
- func (cc *ClientConn) Ping(ctx context.Context) error
- func (cc *ClientConn) Post(ctx context.Context, path string, contentFormat message.MediaType, ...) (*pool.Message, error)
- func (cc *ClientConn) Put(ctx context.Context, path string, contentFormat message.MediaType, ...) (*pool.Message, error)
- func (cc *ClientConn) ReleaseMessage(m *pool.Message)
- func (cc *ClientConn) RemoteAddr() net.Addr
- func (cc *ClientConn) Run() (err error)
- func (cc *ClientConn) Sequence() uint64
- func (cc *ClientConn) Session() *Session
- func (cc *ClientConn) SetContextValue(key interface{}, val interface{})
- func (*ClientConn) Unlock()
- func (cc *ClientConn) WriteMessage(req *pool.Message) error
- type ClientTCP
- func (c *ClientTCP) ClientConn() interface{}
- func (c *ClientTCP) Close() error
- func (c *ClientTCP) Context() context.Context
- func (c *ClientTCP) Delete(ctx context.Context, path string, opts ...message.Option) (*message.Message, error)
- func (c *ClientTCP) Do(req *message.Message) (*message.Message, error)
- func (c *ClientTCP) Done() <-chan struct{}
- func (c *ClientTCP) Get(ctx context.Context, path string, opts ...message.Option) (*message.Message, error)
- func (c *ClientTCP) Observe(ctx context.Context, path string, ...) (mux.Observation, error)
- func (c *ClientTCP) Ping(ctx context.Context) error
- func (c *ClientTCP) Post(ctx context.Context, path string, contentFormat message.MediaType, ...) (*message.Message, error)
- func (c *ClientTCP) Put(ctx context.Context, path string, contentFormat message.MediaType, ...) (*message.Message, error)
- func (c *ClientTCP) RemoteAddr() net.Addr
- func (c *ClientTCP) Sequence() uint64
- func (c *ClientTCP) SetContextValue(key interface{}, val interface{})
- func (c *ClientTCP) WriteMessage(req *message.Message) error
- type CloseSocketOpt
- type ConnectionCacheSizeOpt
- type ContextOpt
- type DialOption
- type DialerOpt
- type DisablePeerTCPSignalMessageCSMsOpt
- type DisableTCPSignalMessageCSMOpt
- type ErrorFunc
- type ErrorsOpt
- type EventFunc
- type GoPoolFunc
- type GoPoolOpt
- type HandlerContainer
- type HandlerFunc
- type HandlerFuncOpt
- type InactivityMonitorOpt
- type KeepAliveOpt
- type Listener
- type MaxMessageSizeOpt
- type MessagePoolOpt
- type NetOpt
- type Notifier
- type Observation
- type OnNewClientConnFunc
- type OnNewClientConnOpt
- type PeriodicRunnerOpt
- type ResponseWriter
- type Server
- type ServerOption
- type Session
- func (s *Session) AddOnClose(f EventFunc)
- func (s *Session) CheckExpirations(now time.Time, cc *ClientConn)
- func (s *Session) Close() error
- func (s *Session) Context() context.Context
- func (s *Session) Done() <-chan struct{}
- func (s *Session) Handle(w *ResponseWriter, r *pool.Message)
- func (s *Session) PeerBlockWiseTransferEnabled() bool
- func (s *Session) PeerMaxMessageSize() uint32
- func (s *Session) Run(cc *ClientConn) (err error)
- func (s *Session) Sequence() uint64
- func (s *Session) SetContextValue(key interface{}, val interface{})
- func (s *Session) TokenHandler() *HandlerContainer
- func (s *Session) WriteMessage(req *pool.Message) error
- type TLSOpt
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( ErrKeyAlreadyExists = errors.New("key already exists") ErrKeyNotExists = errors.New("key does not exist") )
Functions ¶
func NewDeleteRequest ¶
func NewDeleteRequest(ctx context.Context, messagePool *pool.Pool, path string, opts ...message.Option) (*pool.Message, error)
NewDeleteRequest creates delete request.
Use ctx to set timeout.
func NewGetRequest ¶
func NewGetRequest(ctx context.Context, messagePool *pool.Pool, path string, opts ...message.Option) (*pool.Message, error)
NewGetRequest creates get request.
Use ctx to set timeout.
func NewObserveRequest ¶
func NewObserveRequest(ctx context.Context, messagePool *pool.Pool, path string, opts ...message.Option) (*pool.Message, error)
NewObserveRequest creates observe request.
Use ctx to set timeout.
func NewPostRequest ¶
func NewPostRequest(ctx context.Context, messagePool *pool.Pool, path string, contentFormat message.MediaType, payload io.ReadSeeker, opts ...message.Option) (*pool.Message, error)
NewPostRequest creates post request.
Use ctx to set timeout.
An error is returned if by failure to speak COAP (such as a network connectivity problem). Any status code doesn't cause an error.
If payload is nil then content format is not used.
func NewPutRequest ¶
func NewPutRequest(ctx context.Context, messagePool *pool.Pool, path string, contentFormat message.MediaType, payload io.ReadSeeker, opts ...message.Option) (*pool.Message, error)
NewPutRequest creates put request.
Use ctx to set timeout.
If payload is nil then content format is not used.
Types ¶
type BlockwiseFactoryFunc ¶
type BlockwiseOpt ¶
type BlockwiseOpt struct {
// contains filtered or unexported fields
}
BlockwiseOpt network option.
func WithBlockwise ¶
WithBlockwise configure's blockwise transfer.
type ClientConn ¶
type ClientConn struct {
// contains filtered or unexported fields
}
ClientConn represents a virtual connection to a conceptual endpoint, to perform COAPs commands.
func Client ¶
func Client(conn net.Conn, opts ...DialOption) *ClientConn
Client creates client over tcp/tcp-tls connection.
func Dial ¶
func Dial(target string, opts ...DialOption) (*ClientConn, error)
Dial creates a client connection to the given target.
func NewClientConn ¶
func NewClientConn(session *Session, observationTokenHandler *HandlerContainer, observationRequests *observationRequestsMap) *ClientConn
NewClientConn creates connection over session and observation.
func (*ClientConn) AcquireMessage ¶
func (cc *ClientConn) AcquireMessage(ctx context.Context) *pool.Message
func (*ClientConn) AddOnClose ¶
func (cc *ClientConn) AddOnClose(f EventFunc)
AddOnClose calls function on close connection event.
func (*ClientConn) AsyncPing ¶
func (cc *ClientConn) AsyncPing(receivedPong func()) (func(), error)
AsyncPing sends ping and receivedPong will be called when pong arrives. It returns cancellation of ping operation.
func (*ClientConn) CheckExpirations ¶
func (cc *ClientConn) CheckExpirations(now time.Time)
CheckExpirations checks and remove expired items from caches.
func (*ClientConn) Client ¶
func (cc *ClientConn) Client() *ClientTCP
Client get instance which implements mux.Client.
func (*ClientConn) Close ¶
func (cc *ClientConn) Close() error
Close closes connection without wait of ends Run function.
func (*ClientConn) Context ¶
func (cc *ClientConn) Context() context.Context
Context returns the client's context.
If connections was closed context is cancelled.
func (*ClientConn) Delete ¶
func (cc *ClientConn) Delete(ctx context.Context, path string, opts ...message.Option) (*pool.Message, error)
Delete deletes the resource identified by the request path.
Use ctx to set timeout.
func (*ClientConn) Do ¶
Do sends an coap message and returns an coap response.
An error is returned if by failure to speak COAP (such as a network connectivity problem). Any status code doesn't cause an error.
Caller is responsible to release request and response.
func (*ClientConn) Done ¶
func (cc *ClientConn) Done() <-chan struct{}
Done signalizes that connection is not more processed.
func (*ClientConn) Get ¶
func (cc *ClientConn) Get(ctx context.Context, path string, opts ...message.Option) (*pool.Message, error)
Get issues a GET to the specified path.
Use ctx to set timeout.
An error is returned if by failure to speak COAP (such as a network connectivity problem). Any status code doesn't cause an error.
Example ¶
package main import ( "context" "fmt" "io/ioutil" "log" "time" "github.com/plgd-dev/go-coap/v2/tcp" ) func main() { conn, err := tcp.Dial("pluggedin.cloud:5683") if err != nil { log.Fatal(err) } defer conn.Close() ctx, cancel := context.WithTimeout(context.Background(), time.Second) defer cancel() res, err := conn.Get(ctx, "/oic/res") if err != nil { log.Fatal(err) } data, err := ioutil.ReadAll(res.Body()) if err != nil { log.Fatal(err) } fmt.Printf("%v", data) }
Output:
func (*ClientConn) LocalAddr ¶
func (cc *ClientConn) LocalAddr() net.Addr
func (*ClientConn) Observe ¶
func (cc *ClientConn) Observe(ctx context.Context, path string, observeFunc func(req *pool.Message), opts ...message.Option) (*Observation, error)
Observe subscribes for every change of resource on path.
func (*ClientConn) ObserveRequest ¶
func (cc *ClientConn) ObserveRequest(req *pool.Message, observeFunc func(req *pool.Message)) (*Observation, error)
ObserveRequest subscribes for every change of resource on path.
func (*ClientConn) Ping ¶
func (cc *ClientConn) Ping(ctx context.Context) error
Ping issues a PING to the client and waits for PONG response.
Use ctx to set timeout.
func (*ClientConn) Post ¶
func (cc *ClientConn) Post(ctx context.Context, path string, contentFormat message.MediaType, payload io.ReadSeeker, opts ...message.Option) (*pool.Message, error)
Post issues a POST to the specified path.
Use ctx to set timeout.
An error is returned if by failure to speak COAP (such as a network connectivity problem). Any status code doesn't cause an error.
If payload is nil then content format is not used.
func (*ClientConn) Put ¶
func (cc *ClientConn) Put(ctx context.Context, path string, contentFormat message.MediaType, payload io.ReadSeeker, opts ...message.Option) (*pool.Message, error)
Put issues a PUT to the specified path.
Use ctx to set timeout.
An error is returned if by failure to speak COAP (such as a network connectivity problem). Any status code doesn't cause an error.
If payload is nil then content format is not used.
func (*ClientConn) ReleaseMessage ¶
func (cc *ClientConn) ReleaseMessage(m *pool.Message)
func (*ClientConn) RemoteAddr ¶
func (cc *ClientConn) RemoteAddr() net.Addr
RemoteAddr gets remote address.
func (*ClientConn) Run ¶
func (cc *ClientConn) Run() (err error)
Run reads and process requests from a connection, until the connection is not closed.
func (*ClientConn) Sequence ¶
func (cc *ClientConn) Sequence() uint64
Sequence acquires sequence number.
func (*ClientConn) Session ¶
func (cc *ClientConn) Session() *Session
func (*ClientConn) SetContextValue ¶
func (cc *ClientConn) SetContextValue(key interface{}, val interface{})
SetContextValue stores the value associated with key to context of connection.
func (*ClientConn) WriteMessage ¶
func (cc *ClientConn) WriteMessage(req *pool.Message) error
WriteMessage sends an coap message.
type ClientTCP ¶
type ClientTCP struct {
// contains filtered or unexported fields
}
func NewClientTCP ¶
func NewClientTCP(cc *ClientConn) *ClientTCP
func (*ClientTCP) ClientConn ¶
func (c *ClientTCP) ClientConn() interface{}
ClientConn get's underlaying client connection.
func (*ClientTCP) Done ¶
func (c *ClientTCP) Done() <-chan struct{}
Done signalizes that connection is not more processed.
func (*ClientTCP) RemoteAddr ¶
func (*ClientTCP) SetContextValue ¶
func (c *ClientTCP) SetContextValue(key interface{}, val interface{})
type CloseSocketOpt ¶
type CloseSocketOpt struct{}
CloseSocketOpt close socket option.
func WithCloseSocket ¶
func WithCloseSocket() CloseSocketOpt
WithCloseSocket closes socket at the close connection.
type ConnectionCacheSizeOpt ¶
type ConnectionCacheSizeOpt struct {
// contains filtered or unexported fields
}
ConnectionCacheOpt network option.
func WithConnectionCacheSize ¶
func WithConnectionCacheSize(connectionCacheSize uint16) ConnectionCacheSizeOpt
WithConnectionCacheSize configure's maximum size of cache of read buffer.
type ContextOpt ¶
type ContextOpt struct {
// contains filtered or unexported fields
}
ContextOpt handler function option.
func WithContext ¶
func WithContext(ctx context.Context) ContextOpt
WithContext set's parent context of server.
type DialOption ¶
type DialOption interface {
// contains filtered or unexported methods
}
A DialOption sets options such as credentials, keepalive parameters, etc.
type DialerOpt ¶
type DialerOpt struct {
// contains filtered or unexported fields
}
DialerOpt dialer option.
type DisablePeerTCPSignalMessageCSMsOpt ¶
type DisablePeerTCPSignalMessageCSMsOpt struct{}
DisablePeerTCPSignalMessageCSMsOpt coap-tcp csm option.
func WithDisablePeerTCPSignalMessageCSMs ¶
func WithDisablePeerTCPSignalMessageCSMs() DisablePeerTCPSignalMessageCSMsOpt
WithDisablePeerTCPSignalMessageCSMs ignor peer's CSM message.
type DisableTCPSignalMessageCSMOpt ¶
type DisableTCPSignalMessageCSMOpt struct{}
DisableTCPSignalMessageCSMOpt coap-tcp csm option.
func WithDisableTCPSignalMessageCSM ¶
func WithDisableTCPSignalMessageCSM() DisableTCPSignalMessageCSMOpt
WithDisableTCPSignalMessageCSM don't send CSM when client conn is created.
type ErrorsOpt ¶
type ErrorsOpt struct {
// contains filtered or unexported fields
}
ErrorsOpt errors option.
func WithErrors ¶
WithErrors set function for logging error.
type GoPoolFunc ¶
type GoPoolFunc = func(func()) error
type GoPoolOpt ¶
type GoPoolOpt struct {
// contains filtered or unexported fields
}
GoPoolOpt gopool option.
func WithGoPool ¶
func WithGoPool(goPool GoPoolFunc) GoPoolOpt
WithGoPool sets function for managing spawning go routines for handling incoming request's. Eg: https://github.com/panjf2000/ants.
type HandlerContainer ¶
type HandlerContainer struct {
// contains filtered or unexported fields
}
HandlerContainer for regirstration handlers by key
func (*HandlerContainer) Get ¶
func (s *HandlerContainer) Get(key interface{}) (HandlerFunc, error)
Get returns handler for key
func (*HandlerContainer) Insert ¶
func (s *HandlerContainer) Insert(key interface{}, handler HandlerFunc) error
Insert handler for key.
func (*HandlerContainer) Pop ¶
func (s *HandlerContainer) Pop(key interface{}) (HandlerFunc, error)
Pop pops handler for key
type HandlerFunc ¶
type HandlerFunc = func(*ResponseWriter, *pool.Message)
The HandlerFunc type is an adapter to allow the use of ordinary functions as COAP handlers.
func NewObservationHandler ¶
func NewObservationHandler(obsertionTokenHandler *HandlerContainer, next HandlerFunc) HandlerFunc
type HandlerFuncOpt ¶
type HandlerFuncOpt struct {
// contains filtered or unexported fields
}
HandlerFuncOpt handler function option.
func WithHandlerFunc ¶
func WithHandlerFunc(h HandlerFunc) HandlerFuncOpt
WithHandlerFunc set handle for handling request's.
func WithMux ¶
func WithMux(m mux.Handler) HandlerFuncOpt
WithMux set's multiplexer for handle requests.
type InactivityMonitorOpt ¶
type InactivityMonitorOpt struct {
// contains filtered or unexported fields
}
InactivityMonitorOpt notifies when a connection was inactive for a given duration.
func WithInactivityMonitor ¶
func WithInactivityMonitor(duration time.Duration, onInactive inactivity.OnInactiveFunc) InactivityMonitorOpt
WithInactivityMonitor set deadline's for read operations over client connection.
type KeepAliveOpt ¶
type KeepAliveOpt struct {
// contains filtered or unexported fields
}
KeepAliveOpt keepalive option.
func WithKeepAlive ¶
func WithKeepAlive(maxRetries uint32, timeout time.Duration, onInactive inactivity.OnInactiveFunc) KeepAliveOpt
WithKeepAlive monitoring's client connection's.
type MaxMessageSizeOpt ¶
type MaxMessageSizeOpt struct {
// contains filtered or unexported fields
}
MaxMessageSizeOpt handler function option.
func WithMaxMessageSize ¶
func WithMaxMessageSize(maxMessageSize uint32) MaxMessageSizeOpt
WithMaxMessageSize limit size of processed message.
type MessagePoolOpt ¶
type MessagePoolOpt struct {
// contains filtered or unexported fields
}
ConnectionCacheOpt network option.
func WithMessagePool ¶
func WithMessagePool(messagePool *pool.Pool) MessagePoolOpt
WithMessagePool configure's message pool for acquire/releasing coap messages
type NetOpt ¶
type NetOpt struct {
// contains filtered or unexported fields
}
NetOpt network option.
func WithNetwork ¶
WithNetwork define's tcp version (udp4, udp6, tcp) for client.
type Observation ¶
type Observation struct {
// contains filtered or unexported fields
}
Observation represents subscription to resource on the server
func (*Observation) Cancel ¶
func (o *Observation) Cancel(ctx context.Context) error
Cancel remove observation from server. For recreate observation use Observe.
func (*Observation) Canceled ¶
func (o *Observation) Canceled() bool
type OnNewClientConnFunc ¶
type OnNewClientConnFunc = func(cc *ClientConn, tlscon *tls.Conn)
OnNewClientConnFunc is the callback for new connections.
Note: Calling `tlscon.Close()` is forbidden, and `tlscon` should be treated as a "read-only" parameter, mainly used to get the peer certificate from the underlining connection
type OnNewClientConnOpt ¶
type OnNewClientConnOpt struct {
// contains filtered or unexported fields
}
OnNewClientConnOpt network option.
func WithOnNewClientConn ¶
func WithOnNewClientConn(onNewClientConn OnNewClientConnFunc) OnNewClientConnOpt
WithOnNewClientConn server's notify about new client connection.
Note: Calling `tlscon.Close()` is forbidden, and `tlscon` should be treated as a "read-only" parameter, mainly used to get the peer certificate from the underlining connection
type PeriodicRunnerOpt ¶
type PeriodicRunnerOpt struct {
// contains filtered or unexported fields
}
PeriodicRunnerOpt function which is executed in every ticks
func WithPeriodicRunner ¶
func WithPeriodicRunner(periodicRunner periodic.Func) PeriodicRunnerOpt
WithPeriodicRunner set function which is executed in every ticks.
type ResponseWriter ¶
type ResponseWriter struct {
// contains filtered or unexported fields
}
A ResponseWriter interface is used by an CAOP handler to construct an COAP response.
func NewResponseWriter ¶
func NewResponseWriter(response *pool.Message, cc *ClientConn, requestOptions message.Options) *ResponseWriter
func (*ResponseWriter) ClientConn ¶
func (r *ResponseWriter) ClientConn() *ClientConn
func (*ResponseWriter) SetResponse ¶
func (r *ResponseWriter) SetResponse(code codes.Code, contentFormat message.MediaType, d io.ReadSeeker, opts ...message.Option) error
type Server ¶
type Server struct {
// contains filtered or unexported fields
}
func NewServer ¶
func NewServer(opt ...ServerOption) *Server
func (*Server) Serve ¶
Example ¶
package main import ( "log" "github.com/plgd-dev/go-coap/v2/net" "github.com/plgd-dev/go-coap/v2/tcp" ) func main() { l, err := net.NewTCPListener("tcp", "0.0.0.0:5683") if err != nil { log.Fatal(err) } defer l.Close() s := tcp.NewServer() defer s.Stop() log.Fatal(s.Serve(l)) }
Output:
type ServerOption ¶
type ServerOption interface {
// contains filtered or unexported methods
}
A ServerOption sets options such as credentials, codec and keepalive parameters, etc.
type Session ¶
type Session struct {
// contains filtered or unexported fields
}
func NewSession ¶
func NewSession( ctx context.Context, connection *coapNet.Conn, handler HandlerFunc, maxMessageSize uint32, goPool GoPoolFunc, errors ErrorFunc, blockwiseSZX blockwise.SZX, blockWise *blockwise.BlockWise, disablePeerTCPSignalMessageCSMs bool, disableTCPSignalMessageCSM bool, closeSocket bool, inactivityMonitor inactivity.Monitor, connectionCacheSize uint16, messagePool *pool.Pool, ) *Session
func (*Session) AddOnClose ¶
func (*Session) CheckExpirations ¶
func (s *Session) CheckExpirations(now time.Time, cc *ClientConn)
CheckExpirations checks and remove expired items from caches.
func (*Session) Done ¶
func (s *Session) Done() <-chan struct{}
Done signalizes that connection is not more processed.
func (*Session) PeerBlockWiseTransferEnabled ¶
func (*Session) PeerMaxMessageSize ¶
func (*Session) Run ¶
func (s *Session) Run(cc *ClientConn) (err error)
Run reads and process requests from a connection, until the connection is not closed.
func (*Session) SetContextValue ¶
func (s *Session) SetContextValue(key interface{}, val interface{})
SetContextValue stores the value associated with key to context of connection.
func (*Session) TokenHandler ¶
func (s *Session) TokenHandler() *HandlerContainer