-
Notifications
You must be signed in to change notification settings - Fork 0
/
handle_ack.go
70 lines (61 loc) · 1.61 KB
/
handle_ack.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
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package coap
type pendingEntry struct {
c chan *Message
}
func (s *Server) pendingSave(msg *Message) chan *Message {
pe := &pendingEntry{}
pe.c = make(chan *Message, 1)
if len(msg.Token) == 0 {
msg.Token = []byte(randomString(8))
}
s.pendingMux.Lock()
msg.MessageID = s.pendingMsgId
s.pendingMsgId = s.pendingMsgId + 1
s.pendingMap[string(msg.Token)] = pe
s.pendingMidMap[msg.MessageID] = pe
s.pendingMux.Unlock()
return pe.c
}
func (s *Server) handleAcknowledgement(req *Message) bool {
if req.Code == CodeEmpty {
// delayed response
s.pendingMux.Lock()
pe, found := s.pendingMidMap[req.MessageID]
if found {
delete(s.pendingMidMap, req.MessageID)
}
s.pendingMux.Unlock()
if !found {
return false
}
select {
case pe.c <- req:
//logDebug(req, nil, "ack found (removed from pending list)")
default:
logDebug(req, nil, "empty ack on closed channel (removed from pending list)")
}
//logDebug(req, nil, "empty ack")
return true
}
s.pendingMux.Lock()
pe, found := s.pendingMap[string(req.Token)]
if found {
delete(s.pendingMap, string(req.Token))
delete(s.pendingMidMap, req.MessageID)
}
s.pendingMux.Unlock()
if found {
select {
case pe.c <- req:
//logDebug(req, nil, "ack found (removed from pending list)")
default:
logDebug(req, nil, "ack on closed channel (removed from pending list)")
}
return true
}
//logDebug(req, nil, "ack not found")
return false
}