-
-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathsender.go
105 lines (86 loc) · 2.03 KB
/
sender.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
//
// obs-teleport. OBS Studio plugin.
// Copyright (C) 2021-2024 Florian Zwoch <[email protected]>
//
// This file is part of obs-teleport.
//
// obs-teleport 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.
//
// obs-teleport 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 obs-teleport. If not, see <http://www.gnu.org/licenses/>.
//
package main
//
// #include <obs-module.h>
//
import "C"
import (
"net"
"strconv"
"sync"
)
type Sender struct {
sync.Mutex
sync.WaitGroup
conns map[net.Conn]chan []byte
}
func (s *Sender) SenderAdd(c net.Conn) {
s.Lock()
defer s.Unlock()
blog(C.LOG_INFO, "connect: "+c.RemoteAddr().String())
if s.conns == nil {
s.conns = make(map[net.Conn]chan []byte)
}
ch := make(chan []byte, 1000)
s.conns[c] = ch
s.Add(1)
go func() {
defer s.Done()
defer c.Close()
for b := range ch {
_, err := c.Write(b)
if err != nil {
blog(C.LOG_INFO, "disconnect: "+c.RemoteAddr().String())
s.Lock()
delete(s.conns, c)
s.Unlock()
break
}
}
}()
}
func (s *Sender) SenderGetNumConns() int {
s.Lock()
defer s.Unlock()
return len(s.conns)
}
func (s *Sender) SenderSend(b []byte) {
s.Lock()
defer s.Unlock()
for c, ch := range s.conns {
if len(ch) > 800 {
blog(C.LOG_WARNING, "send queue exceeded ["+c.RemoteAddr().String()+"] "+strconv.Itoa(len(ch)))
continue
} else if len(ch) > 100 {
blog(C.LOG_WARNING, "send queue high ["+c.RemoteAddr().String()+"] "+strconv.Itoa(len(ch)))
}
ch <- b
}
}
func (s *Sender) SenderClose() {
s.Lock()
for _, ch := range s.conns {
close(ch)
}
s.conns = nil
s.Unlock()
s.Wait()
}