mirror of https://github.com/fatedier/frp
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
32 lines
605 B
32 lines
605 B
package rpc
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
"errors"
|
|
"io"
|
|
)
|
|
|
|
func WriteBytes(w io.Writer, buf []byte) (int, error) {
|
|
out := bytes.NewBuffer(nil)
|
|
binary.Write(out, binary.BigEndian, int64(len(buf)))
|
|
out.Write(buf)
|
|
return w.Write(out.Bytes())
|
|
}
|
|
|
|
func ReadBytes(r io.Reader) ([]byte, error) {
|
|
var length int64
|
|
if err := binary.Read(r, binary.BigEndian, &length); err != nil {
|
|
return nil, err
|
|
}
|
|
buffer := make([]byte, length)
|
|
n, err := io.ReadFull(r, buffer)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
if int64(n) != length {
|
|
return nil, errors.New("invalid length")
|
|
}
|
|
return buffer, nil
|
|
}
|