-
Notifications
You must be signed in to change notification settings - Fork 58
/
session.go
66 lines (56 loc) · 1.51 KB
/
session.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
package router
import (
"sync"
"github.com/gammazero/nexus/wamp"
)
// session is a wrapper around a wamp.Session to provide the router with a
// lockable killable session.
type session struct {
wamp.Session
killChan chan *wamp.Goodbye
rwlock sync.RWMutex
}
// newSession created a new lockable session.
func newSession(peer wamp.Peer, sid wamp.ID, details wamp.Dict) *session {
return &session{
Session: wamp.Session{
Peer: peer,
ID: sid,
Details: details,
},
killChan: make(chan *wamp.Goodbye),
}
}
func (s *session) rLock() { s.rwlock.RLock() }
func (s *session) rUnlock() { s.rwlock.RUnlock() }
func (s *session) lock() { s.rwlock.Lock() }
func (s *session) unlock() { s.rwlock.Unlock() }
func (s *session) kill(goodbye *wamp.Goodbye) bool {
if s.killChan == nil {
return false
}
if goodbye == nil {
close(s.killChan)
} else {
s.killChan <- goodbye
}
s.killChan = nil // prevent subsequent kill from using chan
return true
}
// String returns the session ID as a string.
func (s *session) String() string { return s.Session.String() }
// HasRole returns true if the session supports the specified role.
func (s *session) HasRole(role string) bool {
s.rwlock.RLock()
ok := s.Session.HasRole(role)
s.rwlock.RUnlock()
return ok
}
// HasFeature returns true if the session has the specified feature for the
// specified role.
func (s *session) HasFeature(role, feature string) bool {
s.rwlock.RLock()
ok := s.Session.HasFeature(role, feature)
s.rwlock.RUnlock()
return ok
}