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

JUMEI_ARCH/go-plugins
暂停

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
bsonrpc.go 1.66 KB
一键复制 编辑 原始数据 按行查看 历史
Asim Aslam 提交于 2017-10-03 11:53 . Fix vetting code
// Package bsonrpc provides a bson-rpc codec
package bsonrpc
import (
"bytes"
"fmt"
"io"
"github.com/micro/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),
}
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/JMArch/go-plugins.git
git@gitee.com:JMArch/go-plugins.git
JMArch
go-plugins
go-plugins
v0.9.2

搜索帮助

0d507c66 1850385 C8b1a773 1850385