-
Notifications
You must be signed in to change notification settings - Fork 295
Expand file tree
/
Copy pathsocket_task.go
More file actions
322 lines (289 loc) · 8 KB
/
Copy pathsocket_task.go
File metadata and controls
322 lines (289 loc) · 8 KB
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
/*
SPDX-FileCopyrightText: 2026 M5Stack Technology CO LTD
SPDX-License-Identifier: MIT
*/
package web_socket
import (
"context"
"math/rand"
"stackChan/internal/model"
"sync"
"time"
"github.com/gorilla/websocket"
)
var (
randMu sync.Mutex
rander = rand.New(rand.NewSource(time.Now().UnixNano()))
)
const (
ClientExpireTimeout = 15 * time.Second
)
// StartPingTime sends Ping messages to all connected clients for heartbeat detection
func StartPingTime(ctx context.Context) {
// Global panic recovery, prevent entire heartbeat detection logic from crashing
defer func() {
if r := recover(); r != nil {
logger.Errorf(ctx, "StartPingTime panic recovered: %v", r)
}
}()
message := createMessage(ping, nil)
messageType := websocket.BinaryMessage
// Iterate over StackChanClientPool
stackChanClientPool.Range(func(_, value any) bool {
if value == nil {
return true
}
client, ok := value.(*model.StackChanClient)
if !ok {
logger.Warningf(ctx, "StartPingTime: invalid type in StackChanClientPool, skip")
return true
}
if client == nil {
return true
}
func() {
defer func() {
if r := recover(); r != nil {
logger.Errorf(ctx, "panic in StartPingTime StackChanClientPool forwardMessage: %v", r)
}
}()
if client.GetConn() == nil {
logger.Debugf(ctx, "StartPingTime: StackChanClient %s has nil conn, skip ping", client.GetMac())
return
}
stackChanSendMessage(ctx, client, &messageType, message)
}()
return true // continue iteration
})
// Iterate over AppClientPool
appClientPool.Range(func(_, value any) bool {
if value == nil {
return true
}
clients, ok := value.([]*model.AppClient)
if !ok {
logger.Warningf(ctx, "StartPingTime: invalid type in AppClientPool, skip")
return true
}
if len(clients) == 0 {
return true
}
for _, client := range clients {
func() {
defer func() {
if r := recover(); r != nil {
logger.Errorf(ctx, "panic in StartPingTime AppClientPool forwardMessage: %v", r)
}
}()
if client == nil {
return
}
if client.GetConn() == nil {
logger.Debugf(ctx, "StartPingTime: AppClient %s (deviceId: %s) has nil conn, skip ping", client.GetMac(), client.GetDeviceId())
return
}
appSendMessage(ctx, client, &messageType, message)
}()
}
return true // continue iteration
})
}
// CheckExpiredLinks checks and cleans up App client connections that have been inactive for over 60 seconds
func CheckExpiredLinks(ctx context.Context) {
defer func() {
if r := recover(); r != nil {
logger.Errorf(ctx, "CheckExpiredLinks panic recovered: %v", r)
}
}()
now := time.Now()
var expiredClients []*model.AppClient
// 1. Clean up expired AppClient
appClientPool.Range(func(mac, value any) bool {
if mac == nil || value == nil {
return true
}
clients, ok := value.([]*model.AppClient)
if !ok {
logger.Warningf(ctx, "AppClientPool invalid type for mac: %v, delete invalid entry", mac)
appClientPool.Delete(mac)
return true
}
newClients := clients[:0]
for _, client := range clients {
if client == nil {
continue
}
if now.Sub(client.GetLastTime()) > ClientExpireTimeout {
stackChanClientPool.Range(func(_, scValue any) bool {
defer func() {
if r := recover(); r != nil {
logger.Errorf(ctx, "Clean StackChanClient panic: %v", r)
}
}()
stackChanClient, ok := scValue.(*model.StackChanClient)
if !ok || stackChanClient == nil {
return true
}
if stackChanClient.GetCallAppClient() == client {
stackChanClient.SetCallAppClient(nil)
}
//Remove camera subscription
newCamera := make([]*model.AppClient, 0, len(stackChanClient.GetCameraSubscriptionList()))
removedCamera := false
for _, sub := range stackChanClient.GetCameraSubscriptionList() {
if sub != nil && sub != client {
newCamera = append(newCamera, sub)
} else if sub == client {
removedCamera = true
}
}
stackChanClient.SetCameraSubscriptionList(newCamera)
if removedCamera && len(newCamera) == 0 && stackChanClient.GetConn() != nil {
msg := createMessage(OffCamera, nil)
msgType := websocket.BinaryMessage
stackChanSendMessage(ctx, stackChanClient, &msgType, msg)
}
//Remove audio subscription
newAudio := make([]*model.AppClient, 0, len(stackChanClient.GetAudioSubscriptionList()))
removedAudio := false
for _, sub := range stackChanClient.GetAudioSubscriptionList() {
if sub != nil && sub != client {
newAudio = append(newAudio, sub)
} else if sub == client {
removedAudio = true
}
}
stackChanClient.SetAudioSubscriptionList(newAudio)
if removedAudio && len(newAudio) == 0 && stackChanClient.GetConn() != nil {
msg := createMessage(OffAudio, nil)
msgType := websocket.BinaryMessage
stackChanSendMessage(ctx, stackChanClient, &msgType, msg)
}
return true
})
expiredClients = append(expiredClients, client)
} else {
newClients = append(newClients, client)
}
}
if len(newClients) == 0 {
appClientPool.Delete(mac)
} else {
appClientPool.Store(mac, newClients)
}
return true
})
for _, client := range expiredClients {
if client == nil {
continue
}
logger.Infof(ctx, "Kicked out expired App client: %s", client.GetMac())
func() {
defer func() {
if r := recover(); r != nil {
logger.Errorf(ctx, "Close AppClient conn panic: %v", r)
}
}()
client.CloseWriterCoroutine()
if client.GetConn() != nil {
_ = client.GetConn().Close()
client.SetConn(nil)
}
}()
}
var expiredStackChanKeys []string
stackChanClientPool.Range(func(mac, value any) bool {
if mac == nil || value == nil {
return true
}
macStr, ok := mac.(string)
if !ok {
return true
}
stackChanClient, ok := value.(*model.StackChanClient)
if !ok || stackChanClient == nil {
logger.Warningf(ctx, "StackChanClientPool invalid type for mac: %v, delete invalid entry", macStr)
stackChanClientPool.Delete(mac)
return true
}
if now.Sub(stackChanClient.GetLastTime()) > ClientExpireTimeout {
expiredStackChanKeys = append(expiredStackChanKeys, macStr)
}
return true
})
for _, mac := range expiredStackChanKeys {
val, ok := stackChanClientPool.Load(mac)
if !ok {
continue
}
stackChanClient, ok := val.(*model.StackChanClient)
if !ok || stackChanClient == nil {
stackChanClientPool.Delete(mac)
continue
}
stackChanClientPool.Delete(mac)
offlineMsg := createStringMessage(DeviceOffline, "Your StackChan is offline.")
msgType := websocket.BinaryMessage
appClients := getAppClients(stackChanClient.GetMac())
if appClients != nil {
for _, appClient := range appClients {
if appClient == nil {
continue
}
func() {
defer func() {
if r := recover(); r != nil {
logger.Errorf(ctx, "Notify AppClient offline panic: %v", r)
}
}()
appSendMessage(ctx, appClient, &msgType, offlineMsg)
}()
}
}
logger.Infof(ctx, "Kicked out expired StackChan client: %s", mac)
stackChanClient.CloseWriterCoroutine()
conn := stackChanClient.GetConn()
stackChanClient.SetConn(nil)
if conn != nil {
func() {
defer func() {
if r := recover(); r != nil {
logger.Errorf(ctx, "Close StackChan conn panic: %v", r)
}
}()
_ = conn.Close()
}()
}
}
}
// GetRandomStackChanDevice get Random StackChan Device list
func GetRandomStackChanDevice(userMac string, maxLength int) (list []string) {
if maxLength <= 0 {
return []string{}
}
var macs []string
stackChanClientPool.Range(func(key, value interface{}) bool {
mac := key.(string)
client := value.(*model.StackChanClient)
if mac == userMac {
return true
}
online := client.GetConn() != nil
if online {
macs = append(macs, mac)
}
return true
})
if len(macs) == 0 {
return []string{}
}
randMu.Lock()
rander.Shuffle(len(macs), func(i, j int) {
macs[i], macs[j] = macs[j], macs[i]
})
randMu.Unlock()
if len(macs) > maxLength {
macs = macs[:maxLength]
}
return macs
}