-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreader.go
59 lines (48 loc) · 896 Bytes
/
reader.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
57
58
59
package main
import (
"bufio"
"bytes"
"errors"
"io"
"net"
log "github.com/sirupsen/logrus"
)
func connReader(conn io.Reader) <-chan []byte {
recvLine := make(chan []byte)
recv := bufio.NewReader(conn)
// Abandon this goroutine on termination
// since conn.Read() blocks everything.
go func() {
defer close(recvLine)
for {
temp, isPrefix, err := recv.ReadLine()
if err != nil {
if err != io.EOF && !errors.Is(err, net.ErrClosed) {
log.Error(err)
}
return
}
buf := bytes.NewBuffer([]byte{})
buf.Write(temp)
if isPrefix {
for {
b, cont, err := recv.ReadLine()
if err != nil {
if err != io.EOF {
log.Fatal(err)
}
break
}
if _, err := buf.Write(b); err != nil {
log.Fatal(err)
}
if !cont {
break
}
}
}
recvLine <- buf.Bytes()
}
}()
return recvLine
}