代码拉取完成,页面将自动刷新
package msgpackrpc
import (
"errors"
"fmt"
"io"
"github.com/micro/go-micro/codec"
"github.com/tinylib/msgp/msgp"
)
type msgpackCodec struct {
rwc io.ReadWriteCloser
mt codec.MessageType
body bool
}
func (c *msgpackCodec) Close() error {
return c.rwc.Close()
}
func (c *msgpackCodec) String() string {
return "msgpack-rpc"
}
// ReadHeader reads the header from the wire.
func (c *msgpackCodec) ReadHeader(m *codec.Message, mt codec.MessageType) error {
c.mt = mt
switch mt {
case codec.Request:
var h Request
if err := msgp.Decode(c.rwc, &h); err != nil {
return err
}
c.body = h.hasBody
m.Id = uint64(h.ID)
m.Method = h.Method
case codec.Response:
var h Response
if err := msgp.Decode(c.rwc, &h); err != nil {
return err
}
c.body = h.hasBody
m.Id = uint64(h.ID)
m.Error = h.Error
case codec.Publication:
var h Notification
if err := msgp.Decode(c.rwc, &h); err != nil {
return err
}
c.body = h.hasBody
m.Method = h.Method
default:
return errors.New("Unrecognized message type")
}
return nil
}
// ReadBody reads the body of the message. It is assumed the value being
// decoded into is a satisfies the msgp.Decodable interface.
func (c *msgpackCodec) ReadBody(v interface{}) error {
if !c.body {
return nil
}
r := msgp.NewReader(c.rwc)
// Body is present, but no value to decode into.
if v == nil {
return r.Skip()
}
switch c.mt {
case codec.Request, codec.Response, codec.Publication:
return decodeBody(r, v)
default:
return fmt.Errorf("Unrecognized message type: %v", c.mt)
}
return nil
}
// Write writes a message to the wire which contains the header followed by the body.
// The body is assumed to satisfy the msgp.Encodable interface.
func (c *msgpackCodec) Write(m *codec.Message, b interface{}) error {
switch m.Type {
case codec.Request:
h := Request{
ID: uint32(m.Id),
Method: m.Method,
Body: b,
}
return msgp.Encode(c.rwc, &h)
case codec.Response:
h := Response{
ID: uint32(m.Id),
Body: b,
}
h.Error = m.Error
return msgp.Encode(c.rwc, &h)
case codec.Publication:
h := Notification{
Method: m.Method,
Body: b,
}
return msgp.Encode(c.rwc, &h)
default:
return fmt.Errorf("Unrecognized message type: %v", m.Type)
}
return nil
}
func NewCodec(rwc io.ReadWriteCloser) codec.Codec {
return &msgpackCodec{
rwc: rwc,
}
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。