summaryrefslogtreecommitdiff
path: root/soundbox/streaming.go
blob: 357fa30399778d933b0a5f2f277f96d20b6eda39 (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
package soundbox

import (
	"context"
	"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()
		}
	}()

	writers := make([]io.Writer, 0)
	for _, conn := range conns {
		writers = append(writers, conn)
	}

	mw := io.MultiWriter(writers...)

	buf := make([]byte, bufferSize)
	_, err := io.CopyBuffer(mw, r, buf)

	return err
}