2017-10-26 07:21:35 +00:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
2018-03-23 03:48:20 +00:00
|
|
|
"encoding/json"
|
2017-10-26 07:21:35 +00:00
|
|
|
"io"
|
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-20 06:31:54 +00:00
|
|
|
func (cc *ClientCodec) WriteRequest(w io.Writer, method string, args []interface{}, id interface{}) error {
|
|
|
|
params, err := convertParamsToStringArray(args)
|
|
|
|
if nil != err {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2017-11-28 16:19:03 +00:00
|
|
|
req := &clientRequest{
|
|
|
|
Version: Version,
|
|
|
|
Method: method,
|
2018-03-20 06:31:54 +00:00
|
|
|
Params: params,
|
2017-11-28 16:19:03 +00:00
|
|
|
ID: id,
|
|
|
|
}
|
2017-10-31 09:25:44 +00:00
|
|
|
|
2018-03-23 07:43:05 +00:00
|
|
|
encoder := json.NewEncoder(w)
|
2017-10-31 09:25:44 +00:00
|
|
|
if err := encoder.Encode(req); nil != err {
|
2017-10-26 07:21:35 +00:00
|
|
|
return err
|
|
|
|
}
|
2017-10-31 09:25:44 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2017-11-26 10:15:51 +00:00
|
|
|
// NewMessage returns a ClientMessageCodec.
|
2018-03-23 09:53:47 +00:00
|
|
|
func (cc *ClientCodec) NewResponse(r io.Reader) (protocol.ClientResponseCodec, error) {
|
|
|
|
return newClientResponseCodec(r)
|
2017-10-26 07:21:35 +00:00
|
|
|
}
|