-
Notifications
You must be signed in to change notification settings - Fork 2
/
router.go
65 lines (51 loc) · 1.29 KB
/
router.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
package xconn
import (
"fmt"
"github.com/xconnio/wampproto-go/messages"
"github.com/xconnio/xconn-go/internal"
)
type Router struct {
realms internal.Map[string, *Realm]
}
func NewRouter() *Router {
return &Router{
realms: internal.Map[string, *Realm]{},
}
}
func (r *Router) AddRealm(name string) {
r.realms.Store(name, NewRealm())
}
func (r *Router) RemoveRealm(name string) {
r.realms.Delete(name)
}
func (r *Router) HasRealm(name string) bool {
_, exists := r.realms.Load(name)
return exists
}
func (r *Router) AttachClient(base BaseSession) error {
realm, ok := r.realms.Load(base.Realm())
if !ok {
return fmt.Errorf("xconn: could not find realm: %s", base.Realm())
}
return realm.AttachClient(base)
}
func (r *Router) DetachClient(base BaseSession) error {
realm, ok := r.realms.Load(base.Realm())
if !ok {
return fmt.Errorf("xconn: could not find realm: %s", base.Realm())
}
return realm.DetachClient(base)
}
func (r *Router) ReceiveMessage(base BaseSession, msg messages.Message) error {
realm, ok := r.realms.Load(base.Realm())
if !ok {
return fmt.Errorf("xconn: could not find realm: %s", base.Realm())
}
return realm.ReceiveMessage(base.ID(), msg)
}
func (r *Router) Close() {
r.realms.Range(func(name string, realm *Realm) bool {
realm.Close()
return true
})
}