~fnux/yggdrasil-go-coap

yggdrasil-go-coap/networksession.go -rw-r--r-- 1.8 KiB
614f652bTimothée Floure Add syntax highlighting to code snippets in README 1 year, 6 months ago
                                                                                
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
package coap

import (
	"context"
	"net"
)

// A networkSession interface is used by an COAP handler to
// server data in session.
type networkSession interface {
	// LocalAddr returns the net.Addr of the server
	LocalAddr() net.Addr
	// RemoteAddr returns the net.Addr of the client that sent the current request.
	RemoteAddr() net.Addr
	// WriteContextMsg writes a reply back to the client.
	WriteMsgWithContext(ctx context.Context, resp Message) error
	// Close closes the connection.
	Close() error
	// Return type of network
	IsTCP() bool
	// Create message for response via writter
	NewMessage(params MessageParams) Message
	// ExchangeContext writes message and wait for response - paired by token and msgid
	// it is safe to use in goroutines
	ExchangeWithContext(ctx context.Context, req Message) (Message, error)
	// Send ping to peer and wait for pong
	PingWithContext(ctx context.Context) error
	// Sequence discontinuously unique growing number for connection.
	Sequence() uint64

	// handlePairMsg Message was handled by pair
	handlePairMsg(w ResponseWriter, r *Request) bool

	// handleSignals Message below to signals
	handleSignals(w ResponseWriter, r *Request) bool

	// sendPong create pong by m and send it
	sendPong(w ResponseWriter, r *Request) error

	// close session with error
	closeWithError(err error) error

	TokenHandler() *TokenHandler

	// BlockWiseTransferEnabled
	blockWiseEnabled() bool
	// BlockWiseTransferSzx
	blockWiseSzx() BlockWiseSzx
	// MaxPayloadSize
	blockWiseMaxPayloadSize(peer BlockWiseSzx) (int, BlockWiseSzx)

	blockWiseIsValid(szx BlockWiseSzx) bool
}

func handleSignalMsg(w ResponseWriter, r *Request, next HandlerFunc) {
	if !r.Client.networkSession().handleSignals(w, r) {
		next(w, r)
	}
}

func handlePairMsg(w ResponseWriter, r *Request, next HandlerFunc) {
	if !r.Client.networkSession().handlePairMsg(w, r) {
		next(w, r)
	}
}