forked from juneym/gor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
emitter.go
53 lines (44 loc) · 786 Bytes
/
emitter.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
package main
import (
"io"
)
func Start(stop chan int) {
for _, in := range Plugins.Inputs {
go CopyMulty(in, Plugins.Outputs...)
}
select {
case <-stop:
return
}
}
// Copy from 1 reader to multiple writers
func CopyMulty(src io.Reader, writers ...io.Writer) (err error) {
buf := make([]byte, 32*1024)
wIndex := 0
for {
nr, er := src.Read(buf)
if nr > 0 && len(buf) > nr{
Debug("Sending", src, ": ", string(buf[0:nr]))
if Settings.splitOutput {
// Simple round robin
writers[wIndex].Write(buf[0:nr])
wIndex++
if wIndex >= len(writers) {
wIndex = 0
}
} else {
for _, dst := range writers {
dst.Write(buf[0:nr])
}
}
}
if er == io.EOF {
break
}
if er != nil {
err = er
break
}
}
return err
}