github.com/trigonella/mattermost-server@v5.11.1+incompatible/plugin/io_rpc.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package plugin
     5  
     6  import (
     7  	"bufio"
     8  	"encoding/binary"
     9  	"io"
    10  )
    11  
    12  type remoteIOReader struct {
    13  	conn io.ReadWriteCloser
    14  }
    15  
    16  func (r *remoteIOReader) Read(b []byte) (int, error) {
    17  	var buf [10]byte
    18  	n := binary.PutVarint(buf[:], int64(len(b)))
    19  	if _, err := r.conn.Write(buf[:n]); err != nil {
    20  		return 0, err
    21  	}
    22  	return r.conn.Read(b)
    23  }
    24  
    25  func (r *remoteIOReader) Close() error {
    26  	return r.conn.Close()
    27  }
    28  
    29  func connectIOReader(conn io.ReadWriteCloser) io.ReadCloser {
    30  	return &remoteIOReader{conn}
    31  }
    32  
    33  func serveIOReader(r io.Reader, conn io.ReadWriteCloser) {
    34  	cr := bufio.NewReader(conn)
    35  	defer conn.Close()
    36  	buf := make([]byte, 32*1024)
    37  	for {
    38  		n, err := binary.ReadVarint(cr)
    39  		if err != nil {
    40  			break
    41  		}
    42  		if written, err := io.CopyBuffer(conn, io.LimitReader(r, n), buf); err != nil || written < n {
    43  			break
    44  		}
    45  	}
    46  }