rpc/protocol/json/client.go
crusader 08688dfb2f ing
2018-03-23 16:43:05 +09:00

52 lines
1.1 KiB
Go

package json
import (
"encoding/json"
"io"
"git.loafle.net/commons_go/rpc/protocol"
)
// ----------------------------------------------------------------------------
// Codec
// ----------------------------------------------------------------------------
// NewClientCodec returns a new JSON Codec.
func NewClientCodec() protocol.ClientCodec {
return &ClientCodec{}
}
// ClientCodec creates a ClientCodecRequest to process each request.
type ClientCodec struct {
}
func (cc *ClientCodec) NewDecoder(r io.Reader) interface{} {
return json.NewDecoder(r)
}
func (cc *ClientCodec) WriteRequest(w io.Writer, method string, args []interface{}, id interface{}) error {
params, err := convertParamsToStringArray(args)
if nil != err {
return err
}
req := &clientRequest{
Version: Version,
Method: method,
Params: params,
ID: id,
}
encoder := json.NewEncoder(w)
if err := encoder.Encode(req); nil != err {
return err
}
return nil
}
// NewMessage returns a ClientMessageCodec.
func (cc *ClientCodec) NewResponse(decoder interface{}) (protocol.ClientResponseCodec, error) {
return newClientResponseCodec(decoder.(*json.Decoder))
}