2017-10-26 07:21:35 +00:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
2018-03-23 03:48:20 +00:00
|
|
|
"encoding/json"
|
2017-10-31 09:25:44 +00:00
|
|
|
|
|
|
|
"git.loafle.net/commons_go/rpc/protocol"
|
2017-10-26 07:21:35 +00:00
|
|
|
)
|
|
|
|
|
2017-10-31 09:25:44 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
// Codec
|
|
|
|
// ----------------------------------------------------------------------------
|
|
|
|
|
|
|
|
// NewClientCodec returns a new JSON Codec.
|
2017-11-26 10:15:51 +00:00
|
|
|
func NewClientCodec() protocol.ClientCodec {
|
2018-03-23 07:43:05 +00:00
|
|
|
return &ClientCodec{}
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// ClientCodec creates a ClientCodecRequest to process each request.
|
|
|
|
type ClientCodec struct {
|
2018-03-23 07:43:05 +00:00
|
|
|
}
|
|
|
|
|
2018-03-23 17:26:41 +00:00
|
|
|
func (cc *ClientCodec) NewRequest(method string, args []interface{}, id interface{}) ([]byte, error) {
|
2018-03-23 13:45:48 +00:00
|
|
|
params, err := convertParamsToStringArray(args)
|
|
|
|
if nil != err {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
req := &clientRequest{
|
|
|
|
Version: Version,
|
|
|
|
Method: method,
|
|
|
|
Params: params,
|
|
|
|
ID: id,
|
|
|
|
}
|
|
|
|
|
|
|
|
return json.Marshal(req)
|
|
|
|
}
|
2018-03-23 17:26:41 +00:00
|
|
|
func (cc *ClientCodec) NewResponse(buf []byte) (protocol.ClientResponseCodec, error) {
|
|
|
|
return newClientResponseCodec(buf)
|
2018-03-23 13:45:48 +00:00
|
|
|
}
|