Initial QSfera import
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
package proto
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"github.com/oxtoacart/bpool"
|
||||
"go-micro.dev/v4/codec"
|
||||
)
|
||||
|
||||
// create buffer pool with 16 instances each preallocated with 256 bytes.
|
||||
var bufferPool = bpool.NewSizedBufferPool(16, 256)
|
||||
|
||||
type Marshaler struct{}
|
||||
|
||||
func (Marshaler) Marshal(v interface{}) ([]byte, error) {
|
||||
pb, ok := v.(proto.Message)
|
||||
if !ok {
|
||||
return nil, codec.ErrInvalidMessage
|
||||
}
|
||||
|
||||
// looks not good, but allows to reuse underlining bytes
|
||||
buf := bufferPool.Get()
|
||||
pbuf := proto.NewBuffer(buf.Bytes())
|
||||
defer func() {
|
||||
bufferPool.Put(bytes.NewBuffer(pbuf.Bytes()))
|
||||
}()
|
||||
|
||||
if err := pbuf.Marshal(pb); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return pbuf.Bytes(), nil
|
||||
}
|
||||
|
||||
func (Marshaler) Unmarshal(data []byte, v interface{}) error {
|
||||
pb, ok := v.(proto.Message)
|
||||
if !ok {
|
||||
return codec.ErrInvalidMessage
|
||||
}
|
||||
|
||||
return proto.Unmarshal(data, pb)
|
||||
}
|
||||
|
||||
func (Marshaler) String() string {
|
||||
return "proto"
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package proto
|
||||
|
||||
type Message struct {
|
||||
Data []byte
|
||||
}
|
||||
|
||||
func (m *Message) MarshalJSON() ([]byte, error) {
|
||||
return m.Data, nil
|
||||
}
|
||||
|
||||
func (m *Message) UnmarshalJSON(data []byte) error {
|
||||
m.Data = data
|
||||
return nil
|
||||
}
|
||||
|
||||
func (m *Message) ProtoMessage() {}
|
||||
|
||||
func (m *Message) Reset() {
|
||||
*m = Message{}
|
||||
}
|
||||
|
||||
func (m *Message) String() string {
|
||||
return string(m.Data)
|
||||
}
|
||||
|
||||
func (m *Message) Marshal() ([]byte, error) {
|
||||
return m.Data, nil
|
||||
}
|
||||
|
||||
func (m *Message) Unmarshal(data []byte) error {
|
||||
m.Data = data
|
||||
return nil
|
||||
}
|
||||
|
||||
func NewMessage(data []byte) *Message {
|
||||
return &Message{data}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
// Package proto provides a proto codec
|
||||
package proto
|
||||
|
||||
import (
|
||||
"io"
|
||||
|
||||
"github.com/golang/protobuf/proto"
|
||||
"go-micro.dev/v4/codec"
|
||||
)
|
||||
|
||||
type Codec struct {
|
||||
Conn io.ReadWriteCloser
|
||||
}
|
||||
|
||||
func (c *Codec) ReadHeader(m *codec.Message, t codec.MessageType) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func (c *Codec) ReadBody(b interface{}) error {
|
||||
if b == nil {
|
||||
return nil
|
||||
}
|
||||
buf, err := io.ReadAll(c.Conn)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
m, ok := b.(proto.Message)
|
||||
if !ok {
|
||||
return codec.ErrInvalidMessage
|
||||
}
|
||||
return proto.Unmarshal(buf, m)
|
||||
}
|
||||
|
||||
func (c *Codec) Write(m *codec.Message, b interface{}) error {
|
||||
if b == nil {
|
||||
// Nothing to write
|
||||
return nil
|
||||
}
|
||||
p, ok := b.(proto.Message)
|
||||
if !ok {
|
||||
return codec.ErrInvalidMessage
|
||||
}
|
||||
buf, err := proto.Marshal(p)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err = c.Conn.Write(buf)
|
||||
return err
|
||||
}
|
||||
|
||||
func (c *Codec) Close() error {
|
||||
return c.Conn.Close()
|
||||
}
|
||||
|
||||
func (c *Codec) String() string {
|
||||
return "proto"
|
||||
}
|
||||
|
||||
func NewCodec(c io.ReadWriteCloser) codec.Codec {
|
||||
return &Codec{
|
||||
Conn: c,
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user