ing
This commit is contained in:
parent
9f1aa13de1
commit
00cff36194
|
@ -6,10 +6,8 @@ import (
|
||||||
|
|
||||||
// ClientCodec creates a ClientCodecRequest to process each request.
|
// ClientCodec creates a ClientCodecRequest to process each request.
|
||||||
type ClientCodec interface {
|
type ClientCodec interface {
|
||||||
NewDecoder(r io.Reader) interface{}
|
|
||||||
|
|
||||||
WriteRequest(w io.Writer, method string, args []interface{}, id interface{}) error
|
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 {
|
type ClientResponseCodec interface {
|
||||||
|
|
|
@ -20,10 +20,6 @@ func NewClientCodec() protocol.ClientCodec {
|
||||||
type ClientCodec struct {
|
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 {
|
func (cc *ClientCodec) WriteRequest(w io.Writer, method string, args []interface{}, id interface{}) error {
|
||||||
params, err := convertParamsToStringArray(args)
|
params, err := convertParamsToStringArray(args)
|
||||||
if nil != err {
|
if nil != err {
|
||||||
|
@ -46,6 +42,6 @@ func (cc *ClientCodec) WriteRequest(w io.Writer, method string, args []interface
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMessage returns a ClientMessageCodec.
|
// NewMessage returns a ClientMessageCodec.
|
||||||
func (cc *ClientCodec) NewResponse(decoder interface{}) (protocol.ClientResponseCodec, error) {
|
func (cc *ClientCodec) NewResponse(r io.Reader) (protocol.ClientResponseCodec, error) {
|
||||||
return newClientResponseCodec(decoder.(*json.Decoder))
|
return newClientResponseCodec(r)
|
||||||
}
|
}
|
||||||
|
|
|
@ -63,7 +63,11 @@ func (crc *ClientResponseCodec) Notification() (protocol.ClientNotificationCodec
|
||||||
}
|
}
|
||||||
|
|
||||||
// newClientMessageCodec returns a new ClientMessageCodec.
|
// 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()
|
decoder.UseNumber()
|
||||||
|
|
||||||
res := &clientResponse{}
|
res := &clientResponse{}
|
||||||
|
|
|
@ -22,13 +22,9 @@ func NewServerCodec() protocol.ServerCodec {
|
||||||
type ServerCodec struct {
|
type ServerCodec struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (sc *ServerCodec) NewDecoder(r io.Reader) interface{} {
|
|
||||||
return json.NewDecoder(r)
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewRequest returns a ServerRequestCodec.
|
// NewRequest returns a ServerRequestCodec.
|
||||||
func (sc *ServerCodec) NewRequest(decoder interface{}) (protocol.ServerRequestCodec, error) {
|
func (sc *ServerCodec) NewRequest(r io.Reader) (protocol.ServerRequestCodec, error) {
|
||||||
return newServerRequestCodec(decoder.(*json.Decoder))
|
return newServerRequestCodec(r)
|
||||||
}
|
}
|
||||||
|
|
||||||
// WriteNotification send a notification from server to client.
|
// WriteNotification send a notification from server to client.
|
||||||
|
|
|
@ -2,6 +2,7 @@ package json
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"fmt"
|
||||||
"io"
|
"io"
|
||||||
|
|
||||||
"git.loafle.net/commons_go/rpc/protocol"
|
"git.loafle.net/commons_go/rpc/protocol"
|
||||||
|
@ -35,8 +36,13 @@ type serverRequest struct {
|
||||||
// ----------------------------------------------------------------------------
|
// ----------------------------------------------------------------------------
|
||||||
|
|
||||||
// newRequestCodec returns a new ServerRequestCodec.
|
// 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.
|
// 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{}
|
req := &serverRequest{}
|
||||||
err := decoder.Decode(req)
|
err := decoder.Decode(req)
|
||||||
if err == io.ErrUnexpectedEOF || err == io.EOF {
|
if err == io.ErrUnexpectedEOF || err == io.EOF {
|
||||||
|
|
|
@ -6,9 +6,7 @@ import (
|
||||||
|
|
||||||
// ServerCodec creates a ServerRequestCodec to process each request.
|
// ServerCodec creates a ServerRequestCodec to process each request.
|
||||||
type ServerCodec interface {
|
type ServerCodec interface {
|
||||||
NewDecoder(r io.Reader) interface{}
|
NewRequest(r io.Reader) (ServerRequestCodec, error)
|
||||||
|
|
||||||
NewRequest(decoder interface{}) (ServerRequestCodec, error)
|
|
||||||
WriteNotification(w io.Writer, method string, args []interface{}) error
|
WriteNotification(w io.Writer, method string, args []interface{}) error
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user