forked from plgd-dev/go-coap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsessionbase.go
172 lines (148 loc) · 4.24 KB
/
sessionbase.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
package coap
import (
"context"
"fmt"
"log"
"sync"
"sync/atomic"
)
type sessionResp struct {
ch chan *Request // channel must have size 1 for non-blocking write to channel
}
type sessionBase struct {
srv *Server
handler *TokenHandler
sequence uint64
blockWiseTransfer bool
blockWiseTransferSzx uint32 //BlockWiseSzx
mapPairs map[[MaxTokenSize]byte]map[uint16]*sessionResp //storage of channel Message
mapPairsLock sync.Mutex //to sync add remove token
done chan struct{}
doneMutex sync.Mutex
}
func newBaseSession(blockWiseTransfer bool, blockWiseTransferSzx BlockWiseSzx, srv *Server) *sessionBase {
return &sessionBase{
srv: srv,
handler: &TokenHandler{tokenHandlers: make(map[[MaxTokenSize]byte]HandlerFunc)},
blockWiseTransfer: blockWiseTransfer,
blockWiseTransferSzx: uint32(blockWiseTransferSzx),
mapPairs: make(map[[MaxTokenSize]byte]map[uint16](*sessionResp)),
done: make(chan struct{}),
}
}
func (s *sessionBase) blockWiseSzx() BlockWiseSzx {
return BlockWiseSzx(atomic.LoadUint32(&s.blockWiseTransferSzx))
}
func (s *sessionBase) setBlockWiseSzx(szx BlockWiseSzx) {
atomic.StoreUint32(&s.blockWiseTransferSzx, uint32(szx))
}
func (s *sessionBase) Sequence() uint64 {
return atomic.AddUint64(&s.sequence, 1)
}
func (s *sessionBase) blockWiseMaxPayloadSize(peer BlockWiseSzx) (int, BlockWiseSzx) {
szx := s.blockWiseSzx()
if peer < szx {
return szxToBytes[peer], peer
}
return szxToBytes[szx], szx
}
func (s *sessionBase) TokenHandler() *TokenHandler {
return s.handler
}
func (s *sessionBase) newSessionResp(token []byte, messageID uint16) (*sessionResp, error) {
var pairToken [MaxTokenSize]byte
copy(pairToken[:], token)
//register msgid to token
pairChan := &sessionResp{make(chan *Request, 1)}
s.mapPairsLock.Lock()
defer s.mapPairsLock.Unlock()
if s.mapPairs[pairToken] == nil {
s.mapPairs[pairToken] = make(map[uint16]*sessionResp)
}
if s.mapPairs[pairToken][messageID] != nil {
return nil, ErrTokenAlreadyExist
}
s.mapPairs[pairToken][messageID] = pairChan
return pairChan, nil
}
func (s *sessionBase) getSessionResp(token []byte, messageID uint16) *sessionResp {
var pairToken [MaxTokenSize]byte
copy(pairToken[:], token)
s.mapPairsLock.Lock()
defer s.mapPairsLock.Unlock()
if m, ok := s.mapPairs[pairToken]; ok {
if p, ok := m[messageID]; ok {
return p
}
}
return nil
}
func (s *sessionBase) removeSessionResp(token []byte, messageID uint16) {
var pairToken [MaxTokenSize]byte
copy(pairToken[:], token)
s.mapPairsLock.Lock()
defer s.mapPairsLock.Unlock()
delete(s.mapPairs[pairToken], messageID)
if len(s.mapPairs[pairToken]) == 0 {
delete(s.mapPairs, pairToken)
}
}
func (s *sessionBase) exchangeWithContext(ctx context.Context, req Message, writeMsgWithContext func(context.Context, Message) error) (Message, error) {
if err := validateMsg(req); err != nil {
return nil, err
}
//register msgid to token
pairChan, err := s.newSessionResp(req.Token(), req.MessageID())
if err != nil {
return nil, err
}
defer s.removeSessionResp(req.Token(), req.MessageID())
err = writeMsgWithContext(ctx, req)
if err != nil {
return nil, err
}
select {
case request := <-pairChan.ch:
return request.Msg, nil
case <-s.done:
return nil, ErrConnectionClosed
case <-ctx.Done():
return nil, ctx.Err()
}
}
func validateMsg(msg Message) error {
if msg.Payload() != nil && msg.Option(ContentFormat) == nil {
return ErrContentFormatNotSet
}
if msg.Payload() == nil && msg.Option(ContentFormat) != nil {
return ErrInvalidPayload
}
return nil
}
func (s *sessionBase) handlePairMsg(w ResponseWriter, r *Request) bool {
//validate token
pair := s.getSessionResp(r.Msg.Token(), r.Msg.MessageID())
if pair != nil {
select {
case pair.ch <- r:
default:
log.Fatal("Exactly one message can be send to pair. This is second message.")
}
return true
}
return false
}
func (s *sessionBase) Done() <-chan struct{} {
return s.done
}
func (s *sessionBase) Close() error {
s.doneMutex.Lock()
defer s.doneMutex.Unlock()
select {
case <-s.done:
return fmt.Errorf("already closed")
default:
}
close(s.done)
return nil
}