This commit is contained in:
crusader 2018-03-23 18:53:47 +09:00
parent 9f1aa13de1
commit 00cff36194
6 changed files with 18 additions and 20 deletions

View File

@ -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 {

View File

@ -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)
}

View File

@ -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{}

View File

@ -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.

View File

@ -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 {

View File

@ -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
}