blob: 2686f4733da27656323b18eaf76acf4ab9a3ed9d (
plain)
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
|
package soundbox
import (
"context"
"errors"
"io"
"net"
"time"
)
const bufferSize = 20
const writeTimeout = 1 * time.Second
func streamContext(ctx context.Context, r io.Reader, targets []net.HardwareAddr) error {
conns := make([]net.Conn, 0)
for _, target := range targets {
conn, err := dialContext(ctx, target)
if err != nil {
return err
}
conns = append(conns, conn)
}
defer func() {
for _, conn := range conns {
conn.Close()
}
}()
for {
buffer := make([]byte, bufferSize)
i, readErr := r.Read(buffer)
for _, conn := range conns {
conn.SetDeadline(time.Now().Add(writeTimeout))
_, writeErr := conn.Write(buffer[:i])
if writeErr != nil {
return writeErr
}
}
if readErr != nil {
if errors.Is(readErr, io.EOF) {
break
} else {
return readErr
}
}
}
return nil
}
|