-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathconnmgr.go
104 lines (90 loc) · 2.34 KB
/
connmgr.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
/*
socket长连接管理相关方法和函数
*/
package main
import (
"log"
"net"
"sync"
"time"
)
var (
// OnlineClient 客户端网络地址和连接句柄映射
OnlineClient = make(map[string]*TerminalInfo)
// ClientAddrToken 客户端令牌和网络地址映射
ClientAddrToken = make(map[string]string)
connTableLock sync.Mutex
)
// TerminalInfo 终端连接信息
type TerminalInfo struct {
Addr string
TimeStamp int64
Conn net.Conn
CarNumber string
}
// addClient 添加已连接设备
func addClient(token string, t *TerminalInfo) {
connTableLock.Lock()
defer func() {
log.Println("新的客户端已添加:", token)
connTableLock.Unlock()
}()
OnlineClient[token] = t
ClientAddrToken[t.Addr] = token
}
// removeClient 移除已连接客户端
func removeClient(token string) {
connTableLock.Lock()
defer func() {
connTableLock.Unlock()
}()
term, ok := OnlineClient[token]
if !ok || term == nil {
return
}
conn := term.Conn
log.Printf("[GPS Server] 连接: %s 车牌号: %s 连接断开...", conn.RemoteAddr().String(), term.CarNumber)
delete(ClientAddrToken, conn.RemoteAddr().String())
_ = conn.Close()
delete(OnlineClient, token)
}
// updateClientState 更新客户端的时间
func updateClientState(token string) {
connTableLock.Lock()
defer func() {
connTableLock.Unlock()
}()
OnlineClient[token].TimeStamp = time.Now().Unix()
}
// removeTimeoutClient 移除超时的客户端
func removeTimeoutClient() {
log.Println("[GPS Server]心跳 监控 进程启动...")
var count = 0
for range time.Tick(time.Duration(1) * time.Second) {
nowTime := time.Now().Unix()
if count >= 60 {
count = 0
}
// 检测已连接客户端是否超时
if count%30 == 0 {
// log.Println("[GPS Server] 开始检测客户端连接状态...")
for token, val := range OnlineClient {
if int(nowTime-val.TimeStamp) > 70 {
// log.Println("关闭的客户端:", token, int(nowTime-val.TimeStamp))
removeClient(token)
}
}
}
// 删除超时车辆缓存
if count%60 == 0 {
// log.Println("[GPS Server] 开始清除车辆缓存超时数据...")
for key, cache := range cacheVehicleLocationData {
if int(nowTime-cache.TimeStamp) > 86400 {
// log.Println("删除的车辆:", key, nowTime, cache.TimeStamp, 86400)
delete(cacheVehicleLocationData, key)
}
}
}
count++
}
}