forked from mattermost/mattermost
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io_rpc.go
46 lines (39 loc) · 933 Bytes
/
io_rpc.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
// Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
// See LICENSE.txt for license information.
package plugin
import (
"bufio"
"encoding/binary"
"io"
)
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
}
}
}