forked from roadrunner-server/goridge
/
pipe.go
86 lines (68 loc) · 1.88 KB
/
pipe.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
package goridge
import (
"io"
"sync"
"errors"
)
// PipeRelay communicate with underlying process using standard streams (STDIN, STDOUT). Attention, use TCP alternative for
// Windows as more reliable option. This relay closes automatically with the process.
type PipeRelay struct {
// How many bytes to write/read at once.
BufferSize uint64
mur sync.Mutex // concurrent read
in io.ReadCloser
muw sync.Mutex // concurrent write
out io.WriteCloser
}
// NewPipeRelay creates new pipe based data relay.
func NewPipeRelay(in io.ReadCloser, out io.WriteCloser) *PipeRelay {
return &PipeRelay{BufferSize: BufferSize, in: in, out: out}
}
// Send signed (prefixed) data to underlying process.
func (rl *PipeRelay) Send(data []byte, flags byte) (err error) {
rl.muw.Lock()
defer rl.muw.Unlock()
prefix := NewPrefix().WithFlags(flags).WithSize(uint64(len(data)))
if _, err := rl.out.Write(append(prefix[:], data...)); err != nil {
return err
}
return nil
}
// Receive data from the underlying process and returns associated prefix or error.
func (rl *PipeRelay) Receive() (data []byte, p Prefix, err error) {
rl.mur.Lock()
defer rl.mur.Unlock()
defer func() {
if rErr, ok := recover().(error); ok {
err = rErr
}
}()
if _, err := rl.in.Read(p[:]); err != nil {
return nil, p, err
}
if !p.Valid() {
return nil, p, errors.New("invalid data found in the buffer (possible echo)")
}
if !p.HasPayload() {
return nil, p, nil
}
data = make([]byte, 0, p.Size())
leftBytes := p.Size()
buffer := make([]byte, min(uint64(cap(data)), rl.BufferSize))
for {
if n, err := rl.in.Read(buffer); err == nil {
data = append(data, buffer[:n]...)
leftBytes -= uint64(n)
} else {
return nil, p, err
}
if leftBytes == 0 {
break
}
}
return
}
// Close the connection. Pipes are closed automatically with the underlying process.
func (rl *PipeRelay) Close() error {
return nil
}