summaryrefslogtreecommitdiffstats
path: root/plugin/rpcplugin/io.go
blob: 21d79ab0bc9947c36f9946b808ea75a7b7af1d72 (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
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package rpcplugin

import (
	"bufio"
	"bytes"
	"encoding/binary"
	"io"
	"sync"
)

type asyncRead struct {
	b   []byte
	err error
}

type asyncReadCloser struct {
	io.ReadCloser
	buffer    bytes.Buffer
	read      chan struct{}
	reads     chan asyncRead
	close     chan struct{}
	closeOnce sync.Once
}

// NewAsyncReadCloser returns a ReadCloser that supports Close during Read.
func NewAsyncReadCloser(r io.ReadCloser) io.ReadCloser {
	ret := &asyncReadCloser{
		ReadCloser: r,
		read:       make(chan struct{}),
		reads:      make(chan asyncRead),
		close:      make(chan struct{}),
	}
	go ret.loop()
	return ret
}

func (r *asyncReadCloser) loop() {
	buf := make([]byte, 1024*8)
	var n int
	var err error
	for {
		select {
		case <-r.read:
			n = 0
			if err == nil {
				n, err = r.ReadCloser.Read(buf)
			}
			select {
			case r.reads <- asyncRead{buf[:n], err}:
			case <-r.close:
			}
		case <-r.close:
			r.ReadCloser.Close()
			return
		}
	}
}

func (r *asyncReadCloser) Read(b []byte) (int, error) {
	if r.buffer.Len() > 0 {
		return r.buffer.Read(b)
	}
	select {
	case r.read <- struct{}{}:
	case <-r.close:
	}
	select {
	case read := <-r.reads:
		if read.err != nil {
			return 0, read.err
		}
		n := copy(b, read.b)
		if n < len(read.b) {
			r.buffer.Write(read.b[n:])
		}
		return n, nil
	case <-r.close:
		return 0, io.EOF
	}
}

func (r *asyncReadCloser) Close() error {
	r.closeOnce.Do(func() {
		close(r.close)
	})
	return nil
}

type asyncWrite struct {
	n   int
	err error
}

type asyncWriteCloser struct {
	io.WriteCloser
	writeBuffer bytes.Buffer
	write       chan struct{}
	writes      chan asyncWrite
	close       chan struct{}
	closeOnce   sync.Once
}

// NewAsyncWriteCloser returns a WriteCloser that supports Close during Write.
func NewAsyncWriteCloser(w io.WriteCloser) io.WriteCloser {
	ret := &asyncWriteCloser{
		WriteCloser: w,
		write:       make(chan struct{}),
		writes:      make(chan asyncWrite),
		close:       make(chan struct{}),
	}
	go ret.loop()
	return ret
}

func (w *asyncWriteCloser) loop() {
	var n int64
	var err error
	for {
		select {
		case <-w.write:
			n = 0
			if err == nil {
				n, err = w.writeBuffer.WriteTo(w.WriteCloser)
			}
			select {
			case w.writes <- asyncWrite{int(n), err}:
			case <-w.close:
			}
		case <-w.close:
			w.WriteCloser.Close()
			return
		}
	}
}

func (w *asyncWriteCloser) Write(b []byte) (int, error) {
	if n, err := w.writeBuffer.Write(b); err != nil {
		return n, err
	}
	select {
	case w.write <- struct{}{}:
	case <-w.close:
	}
	select {
	case write := <-w.writes:
		return write.n, write.err
	case <-w.close:
		return 0, io.EOF
	}
}

func (w *asyncWriteCloser) Close() error {
	w.closeOnce.Do(func() {
		close(w.close)
	})
	return nil
}

type rwc struct {
	io.ReadCloser
	io.WriteCloser
}

func (rwc *rwc) Close() (err error) {
	err = rwc.WriteCloser.Close()
	if rerr := rwc.ReadCloser.Close(); err == nil {
		err = rerr
	}
	return
}

func NewReadWriteCloser(r io.ReadCloser, w io.WriteCloser) io.ReadWriteCloser {
	return &rwc{r, w}
}

type RemoteIOReader struct {
	conn io.ReadWriteCloser
}

func (r *RemoteIOReader) Read(b []byte) (int, error) {
	var buf [10]byte
	n := binary.PutVarint(buf[:], int64(len(b)))
	if _, err := r.conn.Write(buf[:n]); err != nil {
		return 0, err
	}
	return r.conn.Read(b)
}

func (r *RemoteIOReader) Close() error {
	return r.conn.Close()
}

func ConnectIOReader(conn io.ReadWriteCloser) io.ReadCloser {
	return &RemoteIOReader{conn}
}

func ServeIOReader(r io.Reader, conn io.ReadWriteCloser) {
	cr := bufio.NewReader(conn)
	defer conn.Close()
	buf := make([]byte, 32*1024)
	for {
		n, err := binary.ReadVarint(cr)
		if err != nil {
			break
		}
		if written, err := io.CopyBuffer(conn, io.LimitReader(r, n), buf); err != nil || written < n {
			break
		}
	}
}