From 0b1486a3bd5b81e04b70a854e7507ae0d4412628 Mon Sep 17 00:00:00 2001 From: crusader Date: Sat, 24 Mar 2018 02:26:41 +0900 Subject: [PATCH] ing --- protocol/client_codec.go | 11 +---- protocol/json/client.go | 33 ++------------ protocol/json/client_response.go | 34 +------------- protocol/json/server.go | 30 ++----------- protocol/json/server_request.go | 76 +++----------------------------- protocol/server_codec.go | 18 ++------ 6 files changed, 19 insertions(+), 183 deletions(-) diff --git a/protocol/client_codec.go b/protocol/client_codec.go index 2be739d..43dfd48 100644 --- a/protocol/client_codec.go +++ b/protocol/client_codec.go @@ -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 { diff --git a/protocol/json/client.go b/protocol/json/client.go index 9550936..44d8490 100644 --- a/protocol/json/client.go +++ b/protocol/json/client.go @@ -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) } diff --git a/protocol/json/client_response.go b/protocol/json/client_response.go index 2787cc4..f5423ea 100644 --- a/protocol/json/client_response.go +++ b/protocol/json/client_response.go @@ -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) diff --git a/protocol/json/server.go b/protocol/json/server.go index 244ef55..d5d29df 100644 --- a/protocol/json/server.go +++ b/protocol/json/server.go @@ -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 -} diff --git a/protocol/json/server_request.go b/protocol/json/server_request.go index baa313a..3362642 100644 --- a/protocol/json/server_request.go +++ b/protocol/json/server_request.go @@ -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) } diff --git a/protocol/server_codec.go b/protocol/server_codec.go index 3e22301..2181669 100644 --- a/protocol/server_codec.go +++ b/protocol/server_codec.go @@ -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) }