ing
This commit is contained in:
parent
3e82c65689
commit
0b1486a3bd
|
@ -1,16 +1,9 @@
|
||||||
package protocol
|
package protocol
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ClientCodec creates a ClientCodecRequest to process each request.
|
// ClientCodec creates a ClientCodecRequest to process each request.
|
||||||
type ClientCodec interface {
|
type ClientCodec interface {
|
||||||
NewRequestB(method string, args []interface{}, id interface{}) ([]byte, error)
|
NewRequest(method string, args []interface{}, id interface{}) ([]byte, error)
|
||||||
NewResponseB(buf []byte) (ClientResponseCodec, error)
|
NewResponse(buf []byte) (ClientResponseCodec, error)
|
||||||
|
|
||||||
WriteRequest(w io.Writer, method string, args []interface{}, id interface{}) error
|
|
||||||
NewResponse(r io.Reader) (ClientResponseCodec, error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type ClientResponseCodec interface {
|
type ClientResponseCodec interface {
|
||||||
|
|
|
@ -2,7 +2,6 @@ package json
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
|
||||||
|
|
||||||
"git.loafle.net/commons_go/rpc/protocol"
|
"git.loafle.net/commons_go/rpc/protocol"
|
||||||
)
|
)
|
||||||
|
@ -20,33 +19,7 @@ func NewClientCodec() protocol.ClientCodec {
|
||||||
type ClientCodec struct {
|
type ClientCodec struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (cc *ClientCodec) WriteRequest(w io.Writer, method string, args []interface{}, id interface{}) error {
|
func (cc *ClientCodec) NewRequest(method string, args []interface{}, id interface{}) ([]byte, 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(r io.Reader) (protocol.ClientResponseCodec, error) {
|
|
||||||
return newClientResponseCodec(r)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (cc *ClientCodec) NewRequestB(method string, args []interface{}, id interface{}) ([]byte, error) {
|
|
||||||
params, err := convertParamsToStringArray(args)
|
params, err := convertParamsToStringArray(args)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -61,6 +34,6 @@ func (cc *ClientCodec) NewRequestB(method string, args []interface{}, id interfa
|
||||||
|
|
||||||
return json.Marshal(req)
|
return json.Marshal(req)
|
||||||
}
|
}
|
||||||
func (cc *ClientCodec) NewResponseB(buf []byte) (protocol.ClientResponseCodec, error) {
|
func (cc *ClientCodec) NewResponse(buf []byte) (protocol.ClientResponseCodec, error) {
|
||||||
return newClientResponseCodecB(buf)
|
return newClientResponseCodec(buf)
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ package json
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"io"
|
|
||||||
|
|
||||||
"git.loafle.net/commons_go/rpc/protocol"
|
"git.loafle.net/commons_go/rpc/protocol"
|
||||||
crp "git.loafle.net/commons_go/rpc/protocol"
|
crp "git.loafle.net/commons_go/rpc/protocol"
|
||||||
|
@ -63,38 +62,7 @@ func (crc *ClientResponseCodec) Notification() (protocol.ClientNotificationCodec
|
||||||
}
|
}
|
||||||
|
|
||||||
// newClientMessageCodec returns a new ClientMessageCodec.
|
// newClientMessageCodec returns a new ClientMessageCodec.
|
||||||
func newClientResponseCodec(r io.Reader) (protocol.ClientResponseCodec, error) {
|
func newClientResponseCodec(buf []byte) (protocol.ClientResponseCodec, error) {
|
||||||
decoder := json.NewDecoder(r)
|
|
||||||
if nil == decoder {
|
|
||||||
return nil, fmt.Errorf("RPC: Cannot create decoder")
|
|
||||||
}
|
|
||||||
decoder.UseNumber()
|
|
||||||
|
|
||||||
res := &clientResponse{}
|
|
||||||
err := decoder.Decode(res)
|
|
||||||
if err != nil {
|
|
||||||
if err == io.ErrUnexpectedEOF || err == io.EOF {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
err = &Error{
|
|
||||||
Code: crp.E_PARSE,
|
|
||||||
Message: err.Error(),
|
|
||||||
Data: res,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if res.Version != Version {
|
|
||||||
err = &Error{
|
|
||||||
Code: crp.E_INVALID_REQ,
|
|
||||||
Message: "jsonrpc must be " + Version,
|
|
||||||
Data: res,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &ClientResponseCodec{res: res, err: err}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// newClientMessageCodec returns a new ClientMessageCodec.
|
|
||||||
func newClientResponseCodecB(buf []byte) (protocol.ClientResponseCodec, error) {
|
|
||||||
|
|
||||||
res := &clientResponse{}
|
res := &clientResponse{}
|
||||||
err := json.Unmarshal(buf, res)
|
err := json.Unmarshal(buf, res)
|
||||||
|
|
|
@ -2,7 +2,6 @@ package json
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"io"
|
|
||||||
|
|
||||||
"git.loafle.net/commons_go/rpc/protocol"
|
"git.loafle.net/commons_go/rpc/protocol"
|
||||||
)
|
)
|
||||||
|
@ -22,11 +21,11 @@ func NewServerCodec() protocol.ServerCodec {
|
||||||
type ServerCodec struct {
|
type ServerCodec struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sc *ServerCodec) NewRequestB(buf []byte) (protocol.ServerRequestCodec, error) {
|
func (sc *ServerCodec) NewRequest(buf []byte) (protocol.ServerRequestCodec, error) {
|
||||||
return newServerRequestCodecB(buf)
|
return newServerRequestCodec(buf)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sc *ServerCodec) NewNotificationB(method string, args []interface{}) ([]byte, error) {
|
func (sc *ServerCodec) NewNotification(method string, args []interface{}) ([]byte, error) {
|
||||||
params, err := convertParamsToStringArray(args)
|
params, err := convertParamsToStringArray(args)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
return nil, err
|
return nil, err
|
||||||
|
@ -37,26 +36,3 @@ func (sc *ServerCodec) NewNotificationB(method string, args []interface{}) ([]by
|
||||||
|
|
||||||
return json.Marshal(res)
|
return json.Marshal(res)
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewRequest returns a ServerRequestCodec.
|
|
||||||
func (sc *ServerCodec) NewRequest(r io.Reader) (protocol.ServerRequestCodec, error) {
|
|
||||||
return newServerRequestCodec(r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// WriteNotification send a notification from server to client.
|
|
||||||
func (sc *ServerCodec) WriteNotification(w io.Writer, method string, args []interface{}) error {
|
|
||||||
params, err := convertParamsToStringArray(args)
|
|
||||||
if nil != err {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
noti := &serverNotification{Method: method, Params: params}
|
|
||||||
res := &serverResponse{Version: Version, Result: noti}
|
|
||||||
|
|
||||||
encoder := json.NewEncoder(w)
|
|
||||||
// Not sure in which case will this happen. But seems harmless.
|
|
||||||
if err := encoder.Encode(res); nil != err {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
|
@ -2,8 +2,6 @@ package json
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"fmt"
|
|
||||||
"io"
|
|
||||||
|
|
||||||
"git.loafle.net/commons_go/rpc/protocol"
|
"git.loafle.net/commons_go/rpc/protocol"
|
||||||
crp "git.loafle.net/commons_go/rpc/protocol"
|
crp "git.loafle.net/commons_go/rpc/protocol"
|
||||||
|
@ -36,38 +34,7 @@ type serverRequest struct {
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
// newRequestCodec returns a new ServerRequestCodec.
|
// newRequestCodec returns a new ServerRequestCodec.
|
||||||
func newServerRequestCodec(r io.Reader) (protocol.ServerRequestCodec, error) {
|
func newServerRequestCodec(buf []byte) (protocol.ServerRequestCodec, error) {
|
||||||
// Decode the request body and check if RPC method is valid.
|
|
||||||
decoder := json.NewDecoder(r)
|
|
||||||
if nil == decoder {
|
|
||||||
return nil, fmt.Errorf("RPC: Cannot create decoder")
|
|
||||||
}
|
|
||||||
|
|
||||||
req := &serverRequest{}
|
|
||||||
err := decoder.Decode(req)
|
|
||||||
if err == io.ErrUnexpectedEOF || err == io.EOF {
|
|
||||||
return nil, err
|
|
||||||
}
|
|
||||||
if err != nil {
|
|
||||||
err = &Error{
|
|
||||||
Code: crp.E_PARSE,
|
|
||||||
Message: err.Error(),
|
|
||||||
Data: req,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if req.Version != Version {
|
|
||||||
err = &Error{
|
|
||||||
Code: crp.E_INVALID_REQ,
|
|
||||||
Message: "jsonrpc must be " + Version,
|
|
||||||
Data: req,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return &ServerRequestCodec{req: req, err: err}, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// newRequestCodec returns a new ServerRequestCodec.
|
|
||||||
func newServerRequestCodecB(buf []byte) (protocol.ServerRequestCodec, error) {
|
|
||||||
|
|
||||||
req := &serverRequest{}
|
req := &serverRequest{}
|
||||||
err := json.Unmarshal(buf, req)
|
err := json.Unmarshal(buf, req)
|
||||||
|
@ -156,14 +123,11 @@ func (src *ServerRequestCodec) Params() ([]string, error) {
|
||||||
return nil, src.err
|
return nil, src.err
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteResponse encodes the response and writes it to the ResponseWriter.
|
func (src *ServerRequestCodec) NewResponse(reply interface{}) ([]byte, error) {
|
||||||
func (src *ServerRequestCodec) WriteResponse(w io.Writer, reply interface{}) error {
|
|
||||||
res := &serverResponse{Version: Version, Result: reply, ID: src.req.ID}
|
res := &serverResponse{Version: Version, Result: reply, ID: src.req.ID}
|
||||||
return src.writeServerResponse(w, res)
|
return src.newServerResponse(res)
|
||||||
}
|
}
|
||||||
|
func (src *ServerRequestCodec) NewError(status int, err error) ([]byte, error) {
|
||||||
// WriteError encodes the response and writes it to the ResponseWriter.
|
|
||||||
func (src *ServerRequestCodec) WriteError(w io.Writer, status int, err error) error {
|
|
||||||
jsonErr, ok := err.(*Error)
|
jsonErr, ok := err.(*Error)
|
||||||
if !ok {
|
if !ok {
|
||||||
jsonErr = &Error{
|
jsonErr = &Error{
|
||||||
|
@ -172,37 +136,9 @@ func (src *ServerRequestCodec) WriteError(w io.Writer, status int, err error) er
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
res := &serverResponse{Version: Version, Error: jsonErr, ID: src.req.ID}
|
res := &serverResponse{Version: Version, Error: jsonErr, ID: src.req.ID}
|
||||||
return src.writeServerResponse(w, res)
|
return src.newServerResponse(res)
|
||||||
}
|
}
|
||||||
|
func (src *ServerRequestCodec) newServerResponse(res *serverResponse) ([]byte, error) {
|
||||||
func (src *ServerRequestCodec) writeServerResponse(w io.Writer, res *serverResponse) error {
|
|
||||||
// ID is null for notifications and they don't have a response.
|
|
||||||
if src.req.ID != nil {
|
|
||||||
encoder := json.NewEncoder(w)
|
|
||||||
// Not sure in which case will this happen. But seems harmless.
|
|
||||||
if err := encoder.Encode(res); nil != err {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func (src *ServerRequestCodec) NewResponseB(reply interface{}) ([]byte, error) {
|
|
||||||
res := &serverResponse{Version: Version, Result: reply, ID: src.req.ID}
|
|
||||||
return src.newServerResponseB(res)
|
|
||||||
}
|
|
||||||
func (src *ServerRequestCodec) NewErrorB(status int, err error) ([]byte, error) {
|
|
||||||
jsonErr, ok := err.(*Error)
|
|
||||||
if !ok {
|
|
||||||
jsonErr = &Error{
|
|
||||||
Code: crp.E_SERVER,
|
|
||||||
Message: err.Error(),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
res := &serverResponse{Version: Version, Error: jsonErr, ID: src.req.ID}
|
|
||||||
return src.newServerResponseB(res)
|
|
||||||
}
|
|
||||||
func (src *ServerRequestCodec) newServerResponseB(res *serverResponse) ([]byte, error) {
|
|
||||||
|
|
||||||
return json.Marshal(res)
|
return json.Marshal(res)
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,16 +1,9 @@
|
||||||
package protocol
|
package protocol
|
||||||
|
|
||||||
import (
|
|
||||||
"io"
|
|
||||||
)
|
|
||||||
|
|
||||||
// ServerCodec creates a ServerRequestCodec to process each request.
|
// ServerCodec creates a ServerRequestCodec to process each request.
|
||||||
type ServerCodec interface {
|
type ServerCodec interface {
|
||||||
NewRequest(r io.Reader) (ServerRequestCodec, error)
|
NewRequest(buf []byte) (ServerRequestCodec, error)
|
||||||
WriteNotification(w io.Writer, method string, args []interface{}) error
|
NewNotification(method string, args []interface{}) ([]byte, error)
|
||||||
|
|
||||||
NewRequestB(buf []byte) (ServerRequestCodec, error)
|
|
||||||
NewNotificationB(method string, args []interface{}) ([]byte, error)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// ServerRequestCodec decodes a request and encodes a response using a specific
|
// ServerRequestCodec decodes a request and encodes a response using a specific
|
||||||
|
@ -18,9 +11,6 @@ type ServerCodec interface {
|
||||||
type ServerRequestCodec interface {
|
type ServerRequestCodec interface {
|
||||||
RegistryCodec
|
RegistryCodec
|
||||||
|
|
||||||
NewResponseB(reply interface{}) ([]byte, error)
|
NewResponse(reply interface{}) ([]byte, error)
|
||||||
NewErrorB(status int, err error) ([]byte, error)
|
NewError(status int, err error) ([]byte, error)
|
||||||
|
|
||||||
WriteResponse(w io.Writer, reply interface{}) error
|
|
||||||
WriteError(w io.Writer, status int, err error) error
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user