This commit is contained in:
crusader 2017-10-27 14:18:11 +09:00
parent 10cadfc5b5
commit f22f37a6dc
3 changed files with 9 additions and 5 deletions

View File

@ -10,7 +10,7 @@ import (
// Codec creates a CodecRequest to process each request.
type Codec interface {
NewRequest(rc io.Reader) CodecRequest
NewRequest(rc io.Reader) (CodecRequest, error)
}
// CodecRequest decodes a request and encodes a response using a specific

View File

@ -72,7 +72,7 @@ type Codec struct {
}
// NewRequest returns a CodecRequest.
func (c *Codec) NewRequest(r io.Reader) protocol.CodecRequest {
func (c *Codec) NewRequest(r io.Reader) (protocol.CodecRequest, error) {
return newCodecRequest(r, c.encSel.Select(r))
}
@ -81,12 +81,13 @@ func (c *Codec) NewRequest(r io.Reader) protocol.CodecRequest {
// ----------------------------------------------------------------------------
// newCodecRequest returns a new CodecRequest.
func newCodecRequest(r io.Reader, encoder encode.Encoder) protocol.CodecRequest {
func newCodecRequest(r io.Reader, encoder encode.Encoder) (protocol.CodecRequest, error) {
// Decode the request body and check if RPC method is valid.
req := new(serverRequest)
err := json.NewDecoder(r).Decode(req)
if err == io.ErrUnexpectedEOF || err == io.EOF {
log.Printf("NewRequest err: %v", err)
return nil, err
}
if err != nil {
err = &Error{
@ -103,7 +104,7 @@ func newCodecRequest(r io.Reader, encoder encode.Encoder) protocol.CodecRequest
}
}
return &CodecRequest{request: req, err: err, encoder: encoder}
return &CodecRequest{request: req, err: err, encoder: encoder}, nil
}
// CodecRequest decodes and encodes a single request.

View File

@ -101,7 +101,10 @@ func (rr *rpcRegistry) Invoke(contentType string, r io.Reader, w io.Writer, befo
log.Print("codec.NewRequest")
// Create a new codec request.
codecReq := codec.NewRequest(r)
codecReq, errNew := codec.NewRequest(r)
if nil != errNew {
return errNew
}
// Get service method to be called.
log.Printf("codecReq.Method: %v", codecReq)
method, errMethod := codecReq.Method()