This repository has been archived by the owner on Nov 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
gateway.go
103 lines (87 loc) · 1.89 KB
/
gateway.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
package dummyGateway
import (
"github.com/ronaksoft/rony"
"github.com/ronaksoft/rony/internal/gateway"
"sync"
"sync/atomic"
)
/*
Creation Time: 2020 - Oct - 02
Created by: (ehsan)
Maintainers:
1. Ehsan N. Moosa (E2)
Auditor: Ehsan N. Moosa (E2)
Copyright Ronak Software Group 2020
*/
type Config struct {
Exposer func(gw *Gateway)
}
type Gateway struct {
gateway.ConnectHandler
gateway.MessageHandler
gateway.CloseHandler
conns map[uint64]*Conn
connsMtx sync.RWMutex
connsTotal int32
}
func New(config Config) (*Gateway, error) {
g := &Gateway{
conns: make(map[uint64]*Conn, 8192),
}
// Call the exposer make caller have access to this gateway object
config.Exposer(g)
return g, nil
}
func (g *Gateway) OpenConn(connID uint64, onReceiveMessage func(connID uint64, streamID int64, data []byte), kvs ...*rony.KeyValue) {
dConn := &Conn{
id: connID,
kv: make(map[string]interface{}),
onMessage: onReceiveMessage,
}
g.connsMtx.Lock()
g.conns[connID] = dConn
g.connsMtx.Unlock()
g.ConnectHandler(dConn, kvs...)
atomic.AddInt32(&g.connsTotal, 1)
return
}
func (g *Gateway) CloseConn(connID uint64) {
g.connsMtx.Lock()
c := g.conns[connID]
delete(g.conns, connID)
g.connsMtx.Unlock()
if c != nil {
g.CloseHandler(c)
atomic.AddInt32(&g.connsTotal, -1)
}
}
func (g *Gateway) SendToConn(connID uint64, streamID int64, data []byte) {
g.connsMtx.RLock()
conn := g.conns[connID]
g.connsMtx.RUnlock()
if conn == nil {
return
}
g.MessageHandler(conn, streamID, data)
}
func (g *Gateway) Start() {
// Do nothing
}
func (g *Gateway) Run() {
// Do nothing
}
func (g *Gateway) Shutdown() {
// Do nothing
}
func (g *Gateway) GetConn(connID uint64) rony.Conn {
g.connsMtx.RLock()
conn := g.conns[connID]
g.connsMtx.RUnlock()
if conn == nil {
return nil
}
return conn
}
func (g *Gateway) Addr() []string {
return []string{"TEST"}
}