当前仓库属于暂停状态,部分功能使用受限,详情请查阅 仓库状态说明
2 Star 0 Fork 1

JUMEI_ARCH / go-plugins
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
bsonrpc.go 1.66 KB
一键复制 编辑 原始数据 按行查看 历史
Asim Aslam 提交于 2018-04-10 15:39 . move bson to asim
// Package bsonrpc provides a bson-rpc codec
package bsonrpc
import (
"bytes"
"fmt"
"io"
"github.com/asim/go-bson"
"github.com/micro/go-micro/codec"
)
type bsonCodec struct {
buf *bytes.Buffer
mt codec.MessageType
rwc io.ReadWriteCloser
c *clientCodec
s *serverCodec
}
func (b *bsonCodec) Close() error {
b.buf.Reset()
return b.rwc.Close()
}
func (b *bsonCodec) String() string {
return "bson-rpc"
}
func (b *bsonCodec) Write(m *codec.Message, body interface{}) error {
switch m.Type {
case codec.Request:
return b.c.Write(m, body)
case codec.Response:
return b.s.Write(m, body)
case codec.Publication:
data, err := bson.Marshal(body)
if err != nil {
return err
}
_, err = b.rwc.Write(data)
return err
default:
return fmt.Errorf("Unrecognised message type: %v", m.Type)
}
}
func (b *bsonCodec) ReadHeader(m *codec.Message, mt codec.MessageType) error {
b.buf.Reset()
b.mt = mt
switch mt {
case codec.Request:
return b.s.ReadHeader(m)
case codec.Response:
return b.c.ReadHeader(m)
case codec.Publication:
io.Copy(b.buf, b.rwc)
default:
return fmt.Errorf("Unrecognised message type: %v", mt)
}
return nil
}
func (b *bsonCodec) ReadBody(body interface{}) error {
switch b.mt {
case codec.Request:
return b.s.ReadBody(body)
case codec.Response:
return b.c.ReadBody(body)
case codec.Publication:
if body != nil {
return bson.Unmarshal(b.buf.Bytes(), body)
}
default:
return fmt.Errorf("Unrecognised message type: %v", b.mt)
}
return nil
}
func NewCodec(rwc io.ReadWriteCloser) codec.Codec {
return &bsonCodec{
buf: bytes.NewBuffer(nil),
rwc: rwc,
c: newClientCodec(rwc),
s: newServerCodec(rwc),
}
}
Go
1
https://gitee.com/JMArch/go-plugins.git
git@gitee.com:JMArch/go-plugins.git
JMArch
go-plugins
go-plugins
v0.14.1

搜索帮助