The current repo belongs to Paused status, and some functions are restricted. For details, please refer to the description of repo status
2 Star 0 Fork 1

JUMEI_ARCH/go-plugins
Paused

Create your Gitee Account
Explore and code with more than 12 million developers,Free private repositories !:)
Sign up
文件
Clone or Download
mercury.go 1.90 KB
Copy Edit Raw Blame History
Asim authored 2016-01-28 18:11 . update to match go-micro
package mercury
import (
"bytes"
"fmt"
"io"
"sync"
"github.com/golang/protobuf/proto"
"github.com/micro/go-micro/codec"
)
type mercuryCodec struct {
sync.Mutex
rwc io.ReadWriteCloser
mt codec.MessageType
buf *bytes.Buffer
}
func (c *mercuryCodec) Close() error {
c.buf.Reset()
return c.rwc.Close()
}
func (c *mercuryCodec) String() string {
return "mercury"
}
func (c *mercuryCodec) Write(m *codec.Message, b interface{}) error {
switch m.Type {
case codec.Request:
data, err := proto.Marshal(b.(proto.Message))
if err != nil {
return err
}
c.rwc.Write(data)
m.Header["Content-Encoding"] = "request"
m.Header["Service"] = m.Target
m.Header["Endpoint"] = m.Method
case codec.Response:
m.Header["Content-Encoding"] = "response"
data, err := proto.Marshal(b.(proto.Message))
if err != nil {
return err
}
c.rwc.Write(data)
case codec.Publication:
data, err := proto.Marshal(b.(proto.Message))
if err != nil {
return err
}
c.rwc.Write(data)
default:
return fmt.Errorf("Unrecognised message type: %v", m.Type)
}
return nil
}
func (c *mercuryCodec) ReadHeader(m *codec.Message, mt codec.MessageType) error {
c.buf.Reset()
c.mt = mt
switch mt {
case codec.Request:
m.Method = m.Header["Endpoint"]
io.Copy(c.buf, c.rwc)
case codec.Response:
io.Copy(c.buf, c.rwc)
case codec.Publication:
io.Copy(c.buf, c.rwc)
default:
return fmt.Errorf("Unrecognised message type: %v", mt)
}
return nil
}
func (c *mercuryCodec) ReadBody(b interface{}) error {
var data []byte
switch c.mt {
case codec.Request, codec.Response:
data = c.buf.Bytes()
case codec.Publication:
data = c.buf.Bytes()
default:
return fmt.Errorf("Unrecognised message type: %v", c.mt)
}
if b != nil {
return proto.Unmarshal(data, b.(proto.Message))
}
return nil
}
func NewCodec(rwc io.ReadWriteCloser) codec.Codec {
return &mercuryCodec{
buf: bytes.NewBuffer(nil),
rwc: rwc,
}
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
Go
1
https://gitee.com/JMArch/go-plugins.git
git@gitee.com:JMArch/go-plugins.git
JMArch
go-plugins
go-plugins
v0.9.3

Search

0d507c66 1850385 C8b1a773 1850385