lotus/lib/cborrpc/rpc.go

31 lines
553 B
Go
Raw Normal View History

2019-07-03 17:39:07 +00:00
package cborrpc
import (
"io"
cbor "github.com/ipfs/go-ipld-cbor"
2019-08-22 01:29:19 +00:00
cbg "github.com/whyrusleeping/cbor-gen"
2019-07-03 17:39:07 +00:00
)
2019-07-03 17:41:54 +00:00
2019-07-03 17:39:07 +00:00
const MessageSizeLimit = 1 << 20
func WriteCborRPC(w io.Writer, obj interface{}) error {
2019-08-22 01:29:19 +00:00
if m, ok := obj.(cbg.CBORMarshaler); ok {
return m.MarshalCBOR(w)
}
2019-07-03 17:39:07 +00:00
data, err := cbor.DumpObject(obj)
if err != nil {
return err
}
_, err = w.Write(data)
return err
}
func ReadCborRPC(r io.Reader, out interface{}) error {
2019-08-22 01:29:19 +00:00
if um, ok := out.(cbg.CBORUnmarshaler); ok {
return um.UnmarshalCBOR(r)
}
2019-07-26 00:49:27 +00:00
return cbor.DecodeReader(r, out)
2019-07-03 17:41:54 +00:00
}