/
client.go
323 lines (273 loc) · 7.78 KB
/
client.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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
/**
* Copyright (c) 2014-2015, GoBelieve
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package main
import "net"
import "time"
import "sync/atomic"
import log "github.com/golang/glog"
type Client struct {
Connection//必须放在结构体首部
*IMClient
*RoomClient
*VOIPClient
*CSClient
public_ip int32
}
func NewClient(conn interface{}) *Client {
client := new(Client)
//初始化Connection
client.conn = conn // conn is net.Conn or engineio.Conn
if net_conn, ok := conn.(net.Conn); ok {
addr := net_conn.LocalAddr()
if taddr, ok := addr.(*net.TCPAddr); ok {
ip4 := taddr.IP.To4()
client.public_ip = int32(ip4[0]) << 24 | int32(ip4[1]) << 16 | int32(ip4[2]) << 8 | int32(ip4[3])
}
}
client.wt = make(chan *Message, 100)
client.ewt = make(chan *EMessage, 100)
client.owt = make(chan *EMessage, 100)
client.unacks = make(map[int]int64)
client.unackMessages = make(map[int]*EMessage)
atomic.AddInt64(&server_summary.nconnections, 1)
client.IMClient = &IMClient{&client.Connection}
client.RoomClient = &RoomClient{Connection:&client.Connection}
client.VOIPClient = &VOIPClient{Connection:&client.Connection}
client.CSClient = NewCSClient(&client.Connection)
return client
}
func (client *Client) Read() {
for {
tc := atomic.LoadInt32(&client.tc)
if tc > 0 {
log.Infof("quit read goroutine, client:%d write goroutine blocked", client.uid)
client.HandleClientClosed()
break
}
t1 := time.Now().Unix()
msg := client.read()
t2 := time.Now().Unix()
if t2 - t1 > 6*60 {
log.Infof("client:%d socket read timeout:%d %d", client.uid, t1, t2)
}
if msg == nil {
client.HandleClientClosed()
break
}
client.HandleMessage(msg)
t3 := time.Now().Unix()
if t3 - t2 > 2 {
log.Infof("client:%d handle message is too slow:%d %d", client.uid, t2, t3)
}
}
}
func (client *Client) RemoveClient() {
route := app_route.FindRoute(client.appid)
if route == nil {
log.Warning("can't find app route")
return
}
route.RemoveClient(client)
if client.room_id > 0 {
route.RemoveRoomClient(client.room_id, client)
}
}
func (client *Client) HandleClientClosed() {
atomic.AddInt64(&server_summary.nconnections, -1)
if client.uid > 0 {
atomic.AddInt64(&server_summary.nclients, -1)
}
atomic.StoreInt32(&client.closed, 1)
client.RemoveClient()
//quit when write goroutine received
client.wt <- nil
client.RoomClient.Logout()
client.IMClient.Logout()
}
func (client *Client) HandleMessage(msg *Message) {
log.Info("msg cmd:", Command(msg.cmd))
switch msg.cmd {
case MSG_AUTH_TOKEN:
client.HandleAuthToken(msg.body.(*AuthenticationToken), msg.version)
case MSG_HEARTBEAT:
// nothing to do
case MSG_PING:
client.HandlePing()
}
client.IMClient.HandleMessage(msg)
client.RoomClient.HandleMessage(msg)
client.VOIPClient.HandleMessage(msg)
client.CSClient.HandleMessage(msg)
}
func (client *Client) AuthToken(token string) (int64, int64, int, error) {
appid, uid, err := LoadUserAccessToken(token)
if err != nil {
return 0, 0, 0, err
}
forbidden, err := GetUserForbidden(appid, uid)
if err != nil {
return appid, uid, 0, nil
} else {
return appid, uid, forbidden, nil
}
}
func (client *Client) HandleAuthToken(login *AuthenticationToken, version int) {
if client.uid > 0 {
log.Info("repeat login")
return
}
var err error
appid, uid, fb, err := client.AuthToken(login.token)
if err != nil {
log.Infof("auth token:%s err:%s", login.token, err)
msg := &Message{cmd: MSG_AUTH_STATUS, version:version, body: &AuthenticationStatus{1, 0}}
client.EnqueueMessage(msg)
return
}
if uid == 0 {
log.Info("auth token uid==0")
msg := &Message{cmd: MSG_AUTH_STATUS, version:version, body: &AuthenticationStatus{1, 0}}
client.EnqueueMessage(msg)
return
}
if login.platform_id != PLATFORM_WEB && len(login.device_id) > 0{
client.device_ID, err = GetDeviceID(login.device_id, int(login.platform_id))
if err != nil {
log.Info("auth token uid==0")
msg := &Message{cmd: MSG_AUTH_STATUS, version:version, body: &AuthenticationStatus{1, 0}}
client.EnqueueMessage(msg)
return
}
}
client.appid = appid
client.uid = uid
client.forbidden = int32(fb)
client.version = version
client.device_id = login.device_id
client.platform_id = login.platform_id
client.tm = time.Now()
log.Infof("auth token:%s appid:%d uid:%d device id:%s:%d forbidden:%d",
login.token, client.appid, client.uid, client.device_id, client.device_ID, client.forbidden)
msg := &Message{cmd: MSG_AUTH_STATUS, version:version, body: &AuthenticationStatus{0, client.public_ip}}
client.EnqueueMessage(msg)
client.AddClient()
client.IMClient.Login()
close(client.owt)
log.Infof("offline loaded:%d", client.uid)
CountDAU(client.appid, client.uid)
atomic.AddInt64(&server_summary.nclients, 1)
}
func (client *Client) AddClient() {
route := app_route.FindOrAddRoute(client.appid)
route.AddClient(client)
}
func (client *Client) HandlePing() {
m := &Message{cmd: MSG_PONG}
client.EnqueueMessage(m)
if client.uid == 0 {
log.Warning("client has't been authenticated")
return
}
}
func (client *Client) Write() {
seq := 0
running := true
loaded := false
//发送离线消息
for running && !loaded {
select {
case msg := <-client.wt:
if msg == nil {
client.close()
running = false
log.Infof("client:%d socket closed", client.uid)
break
}
if msg.cmd == MSG_RT {
atomic.AddInt64(&server_summary.out_message_count, 1)
}
seq++
//以当前客户端所用版本号发送消息
vmsg := &Message{msg.cmd, seq, client.version, msg.body}
client.send(vmsg)
case emsg, ok := <- client.owt:
if !ok {
//离线消息读取完毕
loaded = true
break
}
seq++
emsg.msg.seq = seq
client.AddUnAckMessage(emsg)
//以当前客户端所用版本号发送消息
msg := &Message{emsg.msg.cmd, seq, client.version, emsg.msg.body}
if msg.cmd == MSG_IM || msg.cmd == MSG_GROUP_IM {
atomic.AddInt64(&server_summary.out_message_count, 1)
}
client.send(msg)
}
}
//发送在线消息
for running {
select {
case msg := <-client.wt:
if msg == nil {
client.close()
running = false
log.Infof("client:%d socket closed", client.uid)
break
}
if msg.cmd == MSG_RT {
atomic.AddInt64(&server_summary.out_message_count, 1)
}
seq++
//以当前客户端所用版本号发送消息
vmsg := &Message{msg.cmd, seq, client.version, msg.body}
client.send(vmsg)
case emsg := <- client.ewt:
seq++
emsg.msg.seq = seq
client.AddUnAckMessage(emsg)
//以当前客户端所用版本号发送消息
msg := &Message{cmd:emsg.msg.cmd, seq:seq, version:client.version, body:emsg.msg.body}
if msg.cmd == MSG_IM || msg.cmd == MSG_GROUP_IM {
atomic.AddInt64(&server_summary.out_message_count, 1)
}
client.send(msg)
}
}
//等待200ms,避免发送者阻塞
t := time.After(200 * time.Millisecond)
running = true
for running {
select {
case <- t:
running = false
case <- client.wt:
log.Warning("msg is dropped")
case <- client.ewt:
log.Warning("emsg is dropped")
}
}
log.Info("write goroutine exit")
}
func (client *Client) Run() {
go client.Write()
go client.Read()
}