From 00cff3619498f703c9d8f39f73be581d854cd071 Mon Sep 17 00:00:00 2001 From: crusader Date: Fri, 23 Mar 2018 18:53:47 +0900 Subject: [PATCH] ing --- protocol/client_codec.go | 4 +--- protocol/json/client.go | 8 ++------ protocol/json/client_response.go | 6 +++++- protocol/json/server.go | 8 ++------ protocol/json/server_request.go | 8 +++++++- protocol/server_codec.go | 4 +--- 6 files changed, 18 insertions(+), 20 deletions(-) diff --git a/protocol/client_codec.go b/protocol/client_codec.go index db3b291..7f1a27e 100644 --- a/protocol/client_codec.go +++ b/protocol/client_codec.go @@ -6,10 +6,8 @@ import ( // ClientCodec creates a ClientCodecRequest to process each request. type ClientCodec interface { - NewDecoder(r io.Reader) interface{} - WriteRequest(w io.Writer, method string, args []interface{}, id interface{}) error - NewResponse(decoder interface{}) (ClientResponseCodec, error) + NewResponse(r io.Reader) (ClientResponseCodec, error) } type ClientResponseCodec interface { diff --git a/protocol/json/client.go b/protocol/json/client.go index 1a8f1c0..7365066 100644 --- a/protocol/json/client.go +++ b/protocol/json/client.go @@ -20,10 +20,6 @@ func NewClientCodec() protocol.ClientCodec { type ClientCodec struct { } -func (cc *ClientCodec) NewDecoder(r io.Reader) interface{} { - return json.NewDecoder(r) -} - func (cc *ClientCodec) WriteRequest(w io.Writer, method string, args []interface{}, id interface{}) error { params, err := convertParamsToStringArray(args) if nil != err { @@ -46,6 +42,6 @@ func (cc *ClientCodec) WriteRequest(w io.Writer, method string, args []interface } // NewMessage returns a ClientMessageCodec. -func (cc *ClientCodec) NewResponse(decoder interface{}) (protocol.ClientResponseCodec, error) { - return newClientResponseCodec(decoder.(*json.Decoder)) +func (cc *ClientCodec) NewResponse(r io.Reader) (protocol.ClientResponseCodec, error) { + return newClientResponseCodec(r) } diff --git a/protocol/json/client_response.go b/protocol/json/client_response.go index 2ec41d3..1101007 100644 --- a/protocol/json/client_response.go +++ b/protocol/json/client_response.go @@ -63,7 +63,11 @@ func (crc *ClientResponseCodec) Notification() (protocol.ClientNotificationCodec } // newClientMessageCodec returns a new ClientMessageCodec. -func newClientResponseCodec(decoder *json.Decoder) (protocol.ClientResponseCodec, error) { +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{} diff --git a/protocol/json/server.go b/protocol/json/server.go index 1eb5fb2..39e44b9 100644 --- a/protocol/json/server.go +++ b/protocol/json/server.go @@ -22,13 +22,9 @@ func NewServerCodec() protocol.ServerCodec { type ServerCodec struct { } -func (sc *ServerCodec) NewDecoder(r io.Reader) interface{} { - return json.NewDecoder(r) -} - // NewRequest returns a ServerRequestCodec. -func (sc *ServerCodec) NewRequest(decoder interface{}) (protocol.ServerRequestCodec, error) { - return newServerRequestCodec(decoder.(*json.Decoder)) +func (sc *ServerCodec) NewRequest(r io.Reader) (protocol.ServerRequestCodec, error) { + return newServerRequestCodec(r) } // WriteNotification send a notification from server to client. diff --git a/protocol/json/server_request.go b/protocol/json/server_request.go index 1361527..5ca81e0 100644 --- a/protocol/json/server_request.go +++ b/protocol/json/server_request.go @@ -2,6 +2,7 @@ package json import ( "encoding/json" + "fmt" "io" "git.loafle.net/commons_go/rpc/protocol" @@ -35,8 +36,13 @@ type serverRequest struct { // ---------------------------------------------------------------------------- // newRequestCodec returns a new ServerRequestCodec. -func newServerRequestCodec(decoder *json.Decoder) (protocol.ServerRequestCodec, error) { +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 { diff --git a/protocol/server_codec.go b/protocol/server_codec.go index 15d74b7..92f98de 100644 --- a/protocol/server_codec.go +++ b/protocol/server_codec.go @@ -6,9 +6,7 @@ import ( // ServerCodec creates a ServerRequestCodec to process each request. type ServerCodec interface { - NewDecoder(r io.Reader) interface{} - - NewRequest(decoder interface{}) (ServerRequestCodec, error) + NewRequest(r io.Reader) (ServerRequestCodec, error) WriteNotification(w io.Writer, method string, args []interface{}) error }