-
Notifications
You must be signed in to change notification settings - Fork 0
/
streamer.go
56 lines (50 loc) · 957 Bytes
/
streamer.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
package datastreamer
import (
"bytes"
"encoding/gob"
"log"
"net"
"os"
"runtime"
)
func NewGobStreamer(netstring string, data interface{}, ctrl <-chan bool, counter chan int) {
var netbytes bytes.Buffer
enc := gob.NewEncoder(&netbytes)
err := enc.Encode(data)
var stop = false
if err != nil {
log.Fatal("encode:", err)
}
ln, err := net.Listen("tcp", netstring)
if err != nil {
log.Fatal("listen:", err)
}
go func() {
shutdown := <-ctrl
stop = true
ln.Close()
if !shutdown {
os.Exit(5)
}
}()
go func(l net.Listener, buff bytes.Buffer, count chan int) {
defer l.Close()
for {
conn, _ := l.Accept()
/* if err != nil {
log.Fatal(err)
} */
defer conn.Close()
go func(c net.Conn, b bytes.Buffer, out chan int) {
outbytes := b.Bytes()
for {
n, _ := c.Write(outbytes)
out <- n
if stop {
runtime.Goexit()
}
}
}(conn, buff, count)
}
}(ln, netbytes, counter)
}