Skip to content

Instantly share code, notes, and snippets.

@changtimwu
Created July 26, 2014 22:56
Show Gist options
  • Save changtimwu/0f8fbe78e9a11dbec464 to your computer and use it in GitHub Desktop.
Save changtimwu/0f8fbe78e9a11dbec464 to your computer and use it in GitHub Desktop.
msgpack codec study

import "github.com/vmihailenco/msgpack" 有很多限制

@changtimwu
Copy link
Author

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 and ClientCodec interfaces.
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 and ClientCodec interface like
func (c *rpcCodec) Close() error 
func (c *rpcCodec) ReadResponseBody(body interface{}) error
  • goRpcCodec inherits rpcCodec(has-a relation) and implements BINC 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 inherits rpcCodec(via has-a) and implements msgpack 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

@changtimwu
Copy link
Author

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