-
Notifications
You must be signed in to change notification settings - Fork 0
/
states.go
executable file
·199 lines (175 loc) · 4.45 KB
/
states.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
/* MIT License
*
* Copyright (c) 2018 Mike Taghavi <mitghi[at]gmail.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package server
import (
"net"
"sync"
"sync/atomic"
"time"
"github.com/mitghi/protox/protobase"
)
type cstflg byte
const (
CLConnected cstflg = iota
CLDisconnected
CLSent
CLRecv
CLReject
CLFault
)
type connstats struct {
sent uint64
recv uint64
connect uint64
disconnect uint64
reject uint64
fault uint64
}
type serverState struct {
sync.RWMutex
clients map[string]*connection
mode byte
// TODO
// conns map[net.Conn]*connection
}
type connection struct {
sync.RWMutex
connstats
conninfo
typ SConnType
conn *net.Conn
uid string
ip string
hasSession bool
persist bool
// TODO
// add callbacks
}
type conninfo struct {
start *time.Time
end *time.Time
proto protobase.ProtoConnection
client protobase.ClientInterface
msgstore protobase.MessageStorage
authsys protobase.AuthInterface
}
func newConnection(typ SConnType, uid string, conn net.Conn, session bool, persist bool) *connection {
return &connection{
conn: &conn,
uid: uid,
hasSession: session,
persist: persist,
// ip: conn.RemoteAddr().String(),
}
}
func newServerState(mode byte) *serverState {
ret := &serverState{
clients: make(map[string]*connection),
mode: mode,
// conns: make(map[net.Conn]*connection),
}
return ret
}
func (c *connection) Started() {
t := time.Now()
c.ip = (*c.conn).RemoteAddr().String()
c.conninfo.start = &t
c.conninfo.end = nil
}
func (c *connection) Ended() {
t := time.Now()
c.conninfo.end = &t
}
func (c *connection) setInfo(conn net.Conn, proto protobase.ProtoConnection, client protobase.ClientInterface, msgstore protobase.MessageStorage, authsys protobase.AuthInterface) {
c.conn = &conn
c.conninfo.proto = proto
c.conninfo.client = client
c.conninfo.msgstore = msgstore
c.conninfo.authsys = authsys
}
func (c *connection) Inc(statics cstflg) {
switch statics {
case CLConnected:
atomic.AddUint64(&c.connect, 1)
case CLDisconnected:
atomic.AddUint64(&c.disconnect, 1)
case CLSent:
atomic.AddUint64(&c.sent, 1)
case CLRecv:
atomic.AddUint64(&c.recv, 1)
case CLReject:
atomic.AddUint64(&c.reject, 1)
case CLFault:
atomic.AddUint64(&c.fault, 1)
default:
// invalid
}
}
func (c *connection) update() {
t := time.Now()
c.start = &t
c.ip = (*c.conn).RemoteAddr().String()
}
func (c *connection) Status() uint32 {
return c.proto.GetStatus()
}
func (c *connection) Statics() (sent, recv, connect, disconnect, reject, fault uint64) {
sent = atomic.LoadUint64(&c.sent)
recv = atomic.LoadUint64(&c.recv)
connect = atomic.LoadUint64(&c.connect)
disconnect = atomic.LoadUint64(&c.disconnect)
reject = atomic.LoadUint64(&c.reject)
fault = atomic.LoadUint64(&c.fault)
return sent, recv, connect, disconnect, reject, fault
}
func (c *connection) ResetStatics() {
c.Lock()
c.sent = 0
c.recv = 0
c.connect = 0
c.disconnect = 0
c.reject = 0
c.fault = 0
c.Unlock()
}
func (s *serverState) get(cid string) (val *connection) {
s.RLock()
defer s.RUnlock()
if val, ok := s.clients[cid]; ok {
return val
}
return nil
}
func (s *serverState) set(cid string, info *connection) {
s.Lock()
s.clients[cid] = info
s.Unlock()
}
func (s *serverState) pruneByCid(cid string) (val *connection) {
s.Lock()
defer s.Unlock()
if val, ok := s.clients[cid]; ok {
delete(s.clients, cid)
return val
}
return nil
}