summaryrefslogtreecommitdiff
path: root/soundbox/streaming.go
blob: 3eb237d5bce675254fdccbdcfa1dfe1c91988b80 (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
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)
	writers := make([]io.Writer, 0)

	for _, target := range targets {
		conn, err := dialContext(ctx, target)
		if err != nil {
			return err
		}
		conns = append(conns, conn)
		writers = append(writers, conn)
	}

	defer func() {
		for _, conn := range conns {
			conn.Close()
		}
	}()

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

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

	return err
}