-
Notifications
You must be signed in to change notification settings - Fork 6
/
broadcast_room.go
69 lines (52 loc) · 1.17 KB
/
broadcast_room.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
package main
import "sync"
type BroadcastRoom struct {
members map[string]*BroadcastSession
lock sync.RWMutex
}
func (r *BroadcastRoom) Add(session *BroadcastSession) bool {
r.lock.Lock()
defer r.lock.Unlock()
if _, ok := r.members[session.SourceID]; ok {
return false
}
r.members[session.SourceID] = session
return true
}
func (r *BroadcastRoom) Remove(sourceId string) {
r.lock.Lock()
defer r.lock.Unlock()
_, ok := r.members[sourceId]
if !ok {
return
}
delete(r.members, sourceId)
}
func (r *BroadcastRoom) Exist(sourceId string) bool {
r.lock.RLock()
defer r.lock.RUnlock()
_, ok := r.members[sourceId]
return ok
}
func (r *BroadcastRoom) Find(sourceId string) *BroadcastSession {
r.lock.RLock()
defer r.lock.RUnlock()
session, _ := r.members[sourceId]
return session
}
func (r *BroadcastRoom) PopAll() []*BroadcastSession {
r.lock.Lock()
defer r.lock.Unlock()
var members []*BroadcastSession
for _, session := range r.members {
members = append(members, session)
}
return members
}
func (r *BroadcastRoom) DispatchRtpPacket(data []byte) {
r.lock.RLock()
defer r.lock.RUnlock()
for _, session := range r.members {
session.Write(data)
}
}