2017-10-31 09:25:44 +00:00
|
|
|
package json
|
|
|
|
|
|
|
|
import (
|
2018-03-23 03:22:16 +00:00
|
|
|
"encoding/json"
|
2017-10-31 09:25:44 +00:00
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"git.loafle.net/commons_go/rpc/protocol"
|
2018-03-20 06:31:54 +00:00
|
|
|
crp "git.loafle.net/commons_go/rpc/protocol"
|
2017-10-31 09:25:44 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// ----------------------------------------------------------------------------
|
2017-11-26 10:15:51 +00:00
|
|
|
// ClientResponseCodec
|
2017-10-31 09:25:44 +00:00
|
|
|
// ----------------------------------------------------------------------------
|
2017-11-26 10:15:51 +00:00
|
|
|
// clientResponse represents a JSON-RPC response returned to a client.
|
|
|
|
type clientResponse struct {
|
2017-11-28 16:19:03 +00:00
|
|
|
Version string `json:"jsonrpc"`
|
|
|
|
Result *json.RawMessage `json:"result,omitempty"`
|
2018-03-27 11:23:43 +00:00
|
|
|
Error *json.RawMessage `json:"error,omitempty"`
|
2017-11-28 16:19:03 +00:00
|
|
|
ID interface{} `json:"id,omitempty"`
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|
|
|
|
|
2017-11-26 10:15:51 +00:00
|
|
|
// ClientResponseCodec decodes and encodes a single request.
|
|
|
|
type ClientResponseCodec struct {
|
2018-03-23 07:43:05 +00:00
|
|
|
res *clientResponse
|
|
|
|
err error
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 16:19:03 +00:00
|
|
|
func (crc *ClientResponseCodec) ID() interface{} {
|
|
|
|
return crc.res.ID
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 16:19:03 +00:00
|
|
|
func (crc *ClientResponseCodec) Result(result interface{}) error {
|
|
|
|
if nil == crc.err && nil != crc.res.Result {
|
2018-03-23 03:48:20 +00:00
|
|
|
if err := json.Unmarshal(*crc.res.Result, result); nil != err {
|
2018-03-27 11:23:43 +00:00
|
|
|
crc.err = &crp.Error{
|
2018-03-20 06:31:54 +00:00
|
|
|
Code: crp.E_PARSE,
|
2017-11-28 16:19:03 +00:00
|
|
|
Message: err.Error(),
|
|
|
|
Data: crc.res.Result,
|
2017-11-01 09:58:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-10-31 09:25:44 +00:00
|
|
|
|
2017-11-28 16:19:03 +00:00
|
|
|
return crc.err
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|
|
|
|
|
2018-03-27 11:23:43 +00:00
|
|
|
func (crc *ClientResponseCodec) Error() *crp.Error {
|
|
|
|
protocolError := &crp.Error{}
|
|
|
|
err := json.Unmarshal(*crc.res.Error, protocolError)
|
|
|
|
if nil != err {
|
|
|
|
return &crp.Error{
|
|
|
|
Code: crp.E_PARSE,
|
|
|
|
Message: err.Error(),
|
|
|
|
Data: crc.res.Error,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return protocolError
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 16:19:03 +00:00
|
|
|
func (crc *ClientResponseCodec) Notification() (protocol.ClientNotificationCodec, error) {
|
|
|
|
if nil != crc.res.ID || nil == crc.res.Result {
|
|
|
|
return nil, fmt.Errorf("RPC[JSON]: This is not notification")
|
|
|
|
}
|
2017-10-31 09:25:44 +00:00
|
|
|
|
2017-11-28 16:19:03 +00:00
|
|
|
noti := &clientNotification{}
|
2018-03-23 03:48:20 +00:00
|
|
|
err := json.Unmarshal(*crc.res.Result, noti)
|
2017-11-28 16:19:03 +00:00
|
|
|
if nil != err {
|
|
|
|
return nil, err
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|
2017-11-28 16:19:03 +00:00
|
|
|
|
2018-03-23 07:43:05 +00:00
|
|
|
return &ClientNotificationCodec{noti: noti, err: err}, nil
|
2017-10-31 09:25:44 +00:00
|
|
|
}
|
|
|
|
|
2017-11-28 16:19:03 +00:00
|
|
|
// newClientMessageCodec returns a new ClientMessageCodec.
|
2018-03-23 17:26:41 +00:00
|
|
|
func newClientResponseCodec(buf []byte) (protocol.ClientResponseCodec, error) {
|
2018-03-23 13:45:48 +00:00
|
|
|
|
|
|
|
res := &clientResponse{}
|
|
|
|
err := json.Unmarshal(buf, res)
|
|
|
|
|
|
|
|
if err != nil {
|
2018-03-27 11:23:43 +00:00
|
|
|
err = &crp.Error{
|
2018-03-23 13:45:48 +00:00
|
|
|
Code: crp.E_PARSE,
|
|
|
|
Message: err.Error(),
|
|
|
|
Data: res,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if res.Version != Version {
|
2018-03-27 11:23:43 +00:00
|
|
|
err = &crp.Error{
|
2018-03-23 13:45:48 +00:00
|
|
|
Code: crp.E_INVALID_REQ,
|
|
|
|
Message: "jsonrpc must be " + Version,
|
|
|
|
Data: res,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ClientResponseCodec{res: res, err: err}, nil
|
|
|
|
}
|