import "github.com/vmihailenco/msgpack" 有很多限制
Created
July 26, 2014 22:56
-
-
Save changtimwu/0f8fbe78e9a11dbec464 to your computer and use it in GitHub Desktop.
msgpack codec study
smith's binary
4-byte length + msgpack encoded binary
rpc call
ret = apicall(trconn, "pow", &VarList{3, 4})
encoded as
[pow 3 4 map[$:4]]
returns
[4 81]
rpc call
ret = apicall(trconn, "threesum", &VarList{4, 5, 7})
encoded as
[threesum 4 5 7 map[$:5]]
returns
[5 16]
net/rpc call flow backtrace
- client make request
*[github.com/changtimwu/ugorji-go/codec.(*rpcCodec).write]
[github.com/changtimwu/ugorji-go/codec.(*msgpackSpecRpcCodec).WriteRequest]
[net/rpc.(*Client).send]
[net/rpc.(*Client).Go]
[net/rpc.(*Client).Call]
- server reads request reader * 3
*[github.com/changtimwu/ugorji-go/codec.(*rpcCodec).read]
[github.com/changtimwu/ugorji-go/codec.(*msgpackSpecRpcCodec).parseCustomHeader]
[github.com/changtimwu/ugorji-go/codec.(*msgpackSpecRpcCodec).ReadRequestHeader]
[net/rpc.(*Server).readRequestHeader]
[net/rpc.(*Server).readRequest]
[net/rpc.(*Server).ServeCodec]
- serve reads request body
*[github.com/changtimwu/ugorji-go/codec.(*rpcCodec).read]
[github.com/changtimwu/ugorji-go/codec.(*msgpackSpecRpcCodec).ReadRequestBody]
[net/rpc.(*Server).readRequest]
[net/rpc.(*Server).ServeCodec]
- server write response
*[github.com/changtimwu/ugorji-go/codec.(*rpcCodec).write]
[github.com/changtimwu/ugorji-go/codec.(*msgpackSpecRpcCodec).WriteResponse]
[net/rpc.(*Server).sendResponse]
- client reads response header * 3
*[github.com/changtimwu/ugorji-go/codec.(*rpcCodec).read]
[github.com/changtimwu/ugorji-go/codec.(*msgpackSpecRpcCodec).parseCustomHeader]
[github.com/changtimwu/ugorji-go/codec.(*msgpackSpecRpcCodec).ReadResponseHeader]
- client read response body
*[github.com/changtimwu/ugorji-go/codec.(*rpcCodec).read]
[github.com/changtimwu/ugorji-go/codec.(*rpcCodec).ReadResponseBody]
net/rpc
provides- ClientCodec asks
type ClientCodec interface {
// WriteRequest must be safe for concurrent use by multiple goroutines.
WriteRequest(*Request, interface{}) error
ReadResponseHeader(*Response) error
ReadResponseBody(interface{}) error
Close() error
}
- ServerCodec asks
type ServerCodec interface {
ReadRequestHeader(*Request) error
ReadRequestBody(interface{}) error
// WriteResponse must be safe for concurrent use by multiple goroutines.
WriteResponse(*Response, interface{}) error
Close() error
}
rpcCodec
is fundamental struct. It implements- low-level read/write functions which might be used while implementing
ServerCodec
andClientCodec
interfaces.
- low-level read/write functions which might be used while implementing
func (c *rpcCodec) BufferedReader() *bufio.Reader
func (c *rpcCodec) BufferedWriter() *bufio.Writer
func (c *rpcCodec) write(obj1, obj2 interface{}, writeObj2, doFlush bool) (err error)
func (c *rpcCodec) read(obj interface{}) (err error)
- Simple parts of
ServerCodec
andClientCodec
interface like
func (c *rpcCodec) Close() error
func (c *rpcCodec) ReadResponseBody(body interface{}) error
goRpcCodec
inheritsrpcCodec
(has-a
relation) and implementsBINC
specific methods
type goRpcCodec struct {
rpcCodec
}
func (c *goRpcCodec) WriteRequest(r *rpc.Request, body interface{}) error
func (c *goRpcCodec) WriteResponse(r *rpc.Response, body interface{}) error
func (c *goRpcCodec) ReadResponseHeader(r *rpc.Response) error
func (c *goRpcCodec) ReadRequestHeader(r *rpc.Request) error
msgpackSpecRpcCodec
inheritsrpcCodec
(viahas-a
) and implementsmsgpack
specific methods
type msgpackSpecRpcCodec struct {
rpcCodec
}
func (c *msgpackSpecRpcCodec) WriteRequest(r *rpc.Request, body interface{}) error
func (c *msgpackSpecRpcCodec) WriteResponse(r *rpc.Response, body interface{}) error
func (c *msgpackSpecRpcCodec) ReadResponseHeader(r *rpc.Response) error
func (c *msgpackSpecRpcCodec) ReadRequestHeader(r *rpc.Request) error
func (c *msgpackSpecRpcCodec) ReadRequestBody(body interface{}) error
goRpcCodec
implements both rpc.ServerCodec
and rpc.ClientCodec
so the same instance can be passed to both rpc.NewClientWithCodec(cc)
and rpc.NewServer().ServerCodec
msgpackSpecRpcCodec
is the same story.
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
standard msgpack RPC binary
rpc call
generates
which is encoding of the following
rpc call
generates
which is encoding of
call ID is at the second integer. the first integer is always 0 in request and 1 in response.
smith's binary
4-byte length + msgpack encoded binary
[ map[$ Seq] ... ]