This commit is contained in:
crusader 2018-03-24 02:26:41 +09:00
parent 3e82c65689
commit 0b1486a3bd
6 changed files with 19 additions and 183 deletions

View File

@ -1,16 +1,9 @@
package protocol
import (
"io"
)
// ClientCodec creates a ClientCodecRequest to process each request.
type ClientCodec interface {
NewRequestB(method string, args []interface{}, id interface{}) ([]byte, error)
NewResponseB(buf []byte) (ClientResponseCodec, error)
WriteRequest(w io.Writer, method string, args []interface{}, id interface{}) error
NewResponse(r io.Reader) (ClientResponseCodec, error)
NewRequest(method string, args []interface{}, id interface{}) ([]byte, error)
NewResponse(buf []byte) (ClientResponseCodec, error)
}
type ClientResponseCodec interface {

View File

@ -2,7 +2,6 @@ package json
import (
"encoding/json"
"io"
"git.loafle.net/commons_go/rpc/protocol"
)
@ -20,33 +19,7 @@ func NewClientCodec() protocol.ClientCodec {
type ClientCodec struct {
}
func (cc *ClientCodec) WriteRequest(w io.Writer, method string, args []interface{}, id interface{}) 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) {
func (cc *ClientCodec) NewRequest(method string, args []interface{}, id interface{}) ([]byte, error) {
params, err := convertParamsToStringArray(args)
if nil != err {
return nil, err
@ -61,6 +34,6 @@ func (cc *ClientCodec) NewRequestB(method string, args []interface{}, id interfa
return json.Marshal(req)
}
func (cc *ClientCodec) NewResponseB(buf []byte) (protocol.ClientResponseCodec, error) {
return newClientResponseCodecB(buf)
func (cc *ClientCodec) NewResponse(buf []byte) (protocol.ClientResponseCodec, error) {
return newClientResponseCodec(buf)
}

View File

@ -3,7 +3,6 @@ package json
import (
"encoding/json"
"fmt"
"io"
"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.
func newClientResponseCodec(r io.Reader) (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) {
func newClientResponseCodec(buf []byte) (protocol.ClientResponseCodec, error) {
res := &clientResponse{}
err := json.Unmarshal(buf, res)

View File

@ -2,7 +2,6 @@ package json
import (
"encoding/json"
"io"
"git.loafle.net/commons_go/rpc/protocol"
)
@ -22,11 +21,11 @@ func NewServerCodec() protocol.ServerCodec {
type ServerCodec struct {
}
func (sc *ServerCodec) NewRequestB(buf []byte) (protocol.ServerRequestCodec, error) {
return newServerRequestCodecB(buf)
func (sc *ServerCodec) NewRequest(buf []byte) (protocol.ServerRequestCodec, error) {
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)
if nil != err {
return nil, err
@ -37,26 +36,3 @@ func (sc *ServerCodec) NewNotificationB(method string, args []interface{}) ([]by
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
}

View File

@ -2,8 +2,6 @@ package json
import (
"encoding/json"
"fmt"
"io"
"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.
func newServerRequestCodec(r io.Reader) (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) {
func newServerRequestCodec(buf []byte) (protocol.ServerRequestCodec, error) {
req := &serverRequest{}
err := json.Unmarshal(buf, req)
@ -156,14 +123,11 @@ func (src *ServerRequestCodec) Params() ([]string, error) {
return nil, src.err
}
// WriteResponse encodes the response and writes it to the ResponseWriter.
func (src *ServerRequestCodec) WriteResponse(w io.Writer, reply interface{}) error {
func (src *ServerRequestCodec) NewResponse(reply interface{}) ([]byte, error) {
res := &serverResponse{Version: Version, Result: reply, ID: src.req.ID}
return src.writeServerResponse(w, res)
return src.newServerResponse(res)
}
// WriteError encodes the response and writes it to the ResponseWriter.
func (src *ServerRequestCodec) WriteError(w io.Writer, status int, err error) error {
func (src *ServerRequestCodec) NewError(status int, err error) ([]byte, error) {
jsonErr, ok := err.(*Error)
if !ok {
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}
return src.writeServerResponse(w, res)
return src.newServerResponse(res)
}
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) {
func (src *ServerRequestCodec) newServerResponse(res *serverResponse) ([]byte, error) {
return json.Marshal(res)
}

View File

@ -1,16 +1,9 @@
package protocol
import (
"io"
)
// ServerCodec creates a ServerRequestCodec to process each request.
type ServerCodec interface {
NewRequest(r io.Reader) (ServerRequestCodec, error)
WriteNotification(w io.Writer, method string, args []interface{}) error
NewRequestB(buf []byte) (ServerRequestCodec, error)
NewNotificationB(method string, args []interface{}) ([]byte, error)
NewRequest(buf []byte) (ServerRequestCodec, error)
NewNotification(method string, args []interface{}) ([]byte, error)
}
// ServerRequestCodec decodes a request and encodes a response using a specific
@ -18,9 +11,6 @@ type ServerCodec interface {
type ServerRequestCodec interface {
RegistryCodec
NewResponseB(reply interface{}) ([]byte, error)
NewErrorB(status int, err error) ([]byte, error)
WriteResponse(w io.Writer, reply interface{}) error
WriteError(w io.Writer, status int, err error) error
NewResponse(reply interface{}) ([]byte, error)
NewError(status int, err error) ([]byte, error)
}