Browse Source

types: add note about ReadMessage having no cap

pull/1780/head
Ethan Buchman 7 years ago
parent
commit
9272756c49
3 changed files with 53 additions and 27 deletions
  1. +0
    -2
      types/application.go
  2. +25
    -25
      types/messages.go
  3. +28
    -0
      types/messages_test.go

+ 0
- 2
types/application.go View File

@ -75,8 +75,6 @@ func (BaseApplication) EndBlock(req RequestEndBlock) ResponseEndBlock {
//------------------------------------------------------- //-------------------------------------------------------
var _ Application = (*GRPCApplication)(nil)
// GRPCApplication is a GRPC wrapper for Application // GRPCApplication is a GRPC wrapper for Application
type GRPCApplication struct { type GRPCApplication struct {
app Application app Application


+ 25
- 25
types/messages.go View File

@ -7,6 +7,31 @@ import (
wire "github.com/tendermint/go-wire" wire "github.com/tendermint/go-wire"
) )
// WriteMessage writes a length-delimited protobuf message.
func WriteMessage(msg proto.Message, w io.Writer) error {
bz, err := proto.Marshal(msg)
if err != nil {
return err
}
var n int
wire.WriteByteSlice(bz, w, &n, &err)
return err
}
// ReadMessage reads a length delimited protobuf message.
func ReadMessage(r io.Reader, msg proto.Message) error {
var n int
var err error
bz := wire.ReadByteSlice(r, 0, &n, &err) //XXX: no max
if err != nil {
return err
}
err = proto.Unmarshal(bz, msg)
return err
}
//----------------------------------------
func ToRequestEcho(message string) *Request { func ToRequestEcho(message string) *Request {
return &Request{ return &Request{
Value: &Request_Echo{&RequestEcho{message}}, Value: &Request_Echo{&RequestEcho{message}},
@ -146,28 +171,3 @@ func ToResponseEndBlock(res ResponseEndBlock) *Response {
Value: &Response_EndBlock{&res}, Value: &Response_EndBlock{&res},
} }
} }
//----------------------------------------
// Write proto message, length delimited
func WriteMessage(msg proto.Message, w io.Writer) error {
bz, err := proto.Marshal(msg)
if err != nil {
return err
}
var n int
wire.WriteByteSlice(bz, w, &n, &err)
return err
}
// Read proto message, length delimited
func ReadMessage(r io.Reader, msg proto.Message) error {
var n int
var err error
bz := wire.ReadByteSlice(r, 0, &n, &err)
if err != nil {
return err
}
err = proto.Unmarshal(bz, msg)
return err
}

+ 28
- 0
types/messages_test.go View File

@ -0,0 +1,28 @@
package types
import (
"bytes"
"testing"
"github.com/gogo/protobuf/proto"
"github.com/stretchr/testify/assert"
)
func TestWriteReadMessage(t *testing.T) {
cases := []proto.Message{
&RequestEcho{"hello"},
// TODO: add the rest
}
for _, c := range cases {
buf := new(bytes.Buffer)
err := WriteMessage(c, buf)
assert.Nil(t, err)
msg := new(RequestEcho)
err = ReadMessage(buf, msg)
assert.Nil(t, err)
assert.Equal(t, c, msg)
}
}

Loading…
Cancel
Save