mirror of
https://github.com/anyproto/any-sync.git
synced 2025-06-08 05:57:03 +09:00
drpc vt proto encoding
This commit is contained in:
parent
bb760b9391
commit
f7c3a85db4
7 changed files with 47 additions and 14 deletions
1
go.mod
1
go.mod
|
@ -15,6 +15,7 @@ require (
|
|||
github.com/cheggaaa/mb/v3 v3.0.2
|
||||
github.com/gobwas/glob v0.2.3
|
||||
github.com/goccy/go-graphviz v0.2.9
|
||||
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db
|
||||
github.com/google/uuid v1.6.0
|
||||
github.com/hashicorp/yamux v0.1.2
|
||||
github.com/huandu/skiplist v1.2.1
|
||||
|
|
2
go.sum
2
go.sum
|
@ -94,6 +94,8 @@ github.com/goccy/go-graphviz v0.2.9/go.mod h1:hssjl/qbvUXGmloY81BwXt2nqoApKo7DFg
|
|||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0 h1:DACJavvAHhabrF08vX0COfcOBJRhZ8lUbR+ZWIs0Y5g=
|
||||
github.com/golang/freetype v0.0.0-20170609003504-e2365dfdc4a0/go.mod h1:E/TSTwGwJL78qG/PmXZO1EjYhfJinVAhrmmHX6Z8B9k=
|
||||
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
|
||||
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db h1:woRePGFeVFfLKN/pOkfl+p/TAqKOfFu+7KPlMVpok/w=
|
||||
github.com/golang/snappy v0.0.0-20180518054509-2e65f85255db/go.mod h1:/XxbfmMg8lxefKM7IXC3fBNl/7bRcc72aCRzEWrmP2Q=
|
||||
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
|
||||
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
|
||||
github.com/google/gopacket v1.1.19 h1:ves8RnFZPGiFnTS0uPQStjwru6uO6h+nlr9j6fL7kF8=
|
||||
|
|
|
@ -4,19 +4,20 @@ import (
|
|||
"context"
|
||||
"errors"
|
||||
|
||||
"github.com/anyproto/protobuf/proto"
|
||||
"storj.io/drpc"
|
||||
|
||||
"github.com/anyproto/any-sync/protobuf"
|
||||
)
|
||||
|
||||
var ErrNotAProtoMessage = errors.New("encoding: not a proto message")
|
||||
|
||||
type ProtoMessageGettable interface {
|
||||
ProtoMessage() (proto.Message, error)
|
||||
ProtoMessage() (protobuf.Message, error)
|
||||
}
|
||||
|
||||
type ProtoMessageSettable interface {
|
||||
ProtoMessageGettable
|
||||
SetProtoMessage(proto.Message) error
|
||||
SetProtoMessage(protobuf.Message) error
|
||||
}
|
||||
|
||||
// WrapConnEncoding wraps the drpc connection and replace an encoding
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
package encoding
|
||||
|
||||
import (
|
||||
"github.com/anyproto/protobuf/proto"
|
||||
"storj.io/drpc"
|
||||
|
||||
"github.com/anyproto/any-sync/protobuf"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -16,7 +17,7 @@ func (b protoEncoding) Marshal(msg drpc.Message) ([]byte, error) {
|
|||
}
|
||||
|
||||
func (b protoEncoding) MarshalAppend(buf []byte, msg drpc.Message) (res []byte, err error) {
|
||||
protoMessage, ok := msg.(proto.Message)
|
||||
protoMessage, ok := msg.(protobuf.Message)
|
||||
if !ok {
|
||||
if protoMessageGettable, ok := msg.(ProtoMessageGettable); ok {
|
||||
protoMessage, err = protoMessageGettable.ProtoMessage()
|
||||
|
@ -27,12 +28,12 @@ func (b protoEncoding) MarshalAppend(buf []byte, msg drpc.Message) (res []byte,
|
|||
return nil, ErrNotAProtoMessage
|
||||
}
|
||||
}
|
||||
return proto.MarshalAppend(buf, protoMessage)
|
||||
return protobuf.MarshalAppend(buf, protoMessage)
|
||||
}
|
||||
|
||||
func (b protoEncoding) Unmarshal(buf []byte, msg drpc.Message) (err error) {
|
||||
var protoMessageSettable ProtoMessageSettable
|
||||
protoMessage, ok := msg.(proto.Message)
|
||||
protoMessage, ok := msg.(protobuf.Message)
|
||||
if !ok {
|
||||
if protoMessageSettable, ok = msg.(ProtoMessageSettable); ok {
|
||||
protoMessage, err = protoMessageSettable.ProtoMessage()
|
||||
|
@ -43,7 +44,7 @@ func (b protoEncoding) Unmarshal(buf []byte, msg drpc.Message) (err error) {
|
|||
return ErrNotAProtoMessage
|
||||
}
|
||||
}
|
||||
err = proto.Unmarshal(buf, protoMessage)
|
||||
err = protoMessage.UnmarshalVT(buf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -7,9 +7,10 @@ import (
|
|||
"sync/atomic"
|
||||
"time"
|
||||
|
||||
"github.com/anyproto/protobuf/proto"
|
||||
"github.com/golang/snappy"
|
||||
"storj.io/drpc"
|
||||
|
||||
"github.com/anyproto/any-sync/protobuf"
|
||||
)
|
||||
|
||||
var (
|
||||
|
@ -42,7 +43,7 @@ func (se *snappyEncoding) Marshal(msg drpc.Message) ([]byte, error) {
|
|||
}
|
||||
|
||||
func (se *snappyEncoding) MarshalAppend(buf []byte, msg drpc.Message) (res []byte, err error) {
|
||||
protoMessage, ok := msg.(proto.Message)
|
||||
protoMessage, ok := msg.(protobuf.Message)
|
||||
if !ok {
|
||||
if protoMessageGettable, ok := msg.(ProtoMessageGettable); ok {
|
||||
protoMessage, err = protoMessageGettable.ProtoMessage()
|
||||
|
@ -53,7 +54,7 @@ func (se *snappyEncoding) MarshalAppend(buf []byte, msg drpc.Message) (res []byt
|
|||
return nil, ErrNotAProtoMessage
|
||||
}
|
||||
}
|
||||
if se.marshalBuf, err = proto.MarshalAppend(se.marshalBuf[:0], protoMessage); err != nil {
|
||||
if se.marshalBuf, err = protobuf.MarshalAppend(se.marshalBuf[:0], protoMessage); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
|
@ -80,7 +81,7 @@ func (se *snappyEncoding) Unmarshal(buf []byte, msg drpc.Message) (err error) {
|
|||
rawBytes.Add(uint64(len(se.unmarshalBuf)))
|
||||
|
||||
var protoMessageSettable ProtoMessageSettable
|
||||
protoMessage, ok := msg.(proto.Message)
|
||||
protoMessage, ok := msg.(protobuf.Message)
|
||||
if !ok {
|
||||
if protoMessageSettable, ok = msg.(ProtoMessageSettable); ok {
|
||||
protoMessage, err = protoMessageSettable.ProtoMessage()
|
||||
|
@ -91,7 +92,7 @@ func (se *snappyEncoding) Unmarshal(buf []byte, msg drpc.Message) (err error) {
|
|||
return ErrNotAProtoMessage
|
||||
}
|
||||
}
|
||||
err = proto.Unmarshal(se.unmarshalBuf, protoMessage)
|
||||
err = protoMessage.UnmarshalVT(se.unmarshalBuf)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
|
|
@ -3,9 +3,9 @@ package encoding
|
|||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/anyproto/protobuf/proto"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"google.golang.org/protobuf/reflect/protoreflect"
|
||||
)
|
||||
|
||||
func TestSnappyEncoding_Marshal(t *testing.T) {
|
||||
|
@ -19,3 +19,29 @@ func TestSnappyEncoding_Marshal(t *testing.T) {
|
|||
assert.Equal(t, msg, msg2)
|
||||
t.Log(len(data), len(msg.data))
|
||||
}
|
||||
|
||||
type message struct {
|
||||
data []byte
|
||||
}
|
||||
|
||||
func (m *message) SizeVT() (n int) {
|
||||
return len(m.data)
|
||||
}
|
||||
|
||||
func (m *message) MarshalVT() (dAtA []byte, err error) {
|
||||
return m.data, nil
|
||||
}
|
||||
|
||||
func (m *message) UnmarshalVT(dAtA []byte) error {
|
||||
m.data = dAtA
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *message) MarshalToSizedBufferVT(dAtA []byte) (int, error) {
|
||||
copy(dAtA, m.data)
|
||||
return len(m.data), nil
|
||||
}
|
||||
|
||||
func (m *message) ProtoReflect() protoreflect.Message {
|
||||
return nil
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ type Message interface {
|
|||
MarshalVT() (dAtA []byte, err error)
|
||||
UnmarshalVT(dAtA []byte) error
|
||||
MarshalToSizedBufferVT(dAtA []byte) (int, error)
|
||||
proto.Message
|
||||
}
|
||||
|
||||
func MarshalAppend(buf []byte, pb proto.Message) ([]byte, error) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue