rpc/protocol/json/client.go
crusader 0b1486a3bd ing
2018-03-24 02:26:41 +09:00

40 lines
914 B
Go

package json
import (
"encoding/json"
"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) NewRequest(method string, args []interface{}, id interface{}) ([]byte, error) {
params, err := convertParamsToStringArray(args)
if nil != err {
return nil, err
}
req := &clientRequest{
Version: Version,
Method: method,
Params: params,
ID: id,
}
return json.Marshal(req)
}
func (cc *ClientCodec) NewResponse(buf []byte) (protocol.ClientResponseCodec, error) {
return newClientResponseCodec(buf)
}