Browse Source

Cleanup

master
johnl 4 years ago
parent
commit
e76f49241b
1 changed files with 6 additions and 33 deletions
  1. +6
    -33
      serverobj.go

+ 6
- 33
serverobj.go View File

@ -2,7 +2,6 @@ package rpc
import (
"io"
"log"
"reflect"
)
@ -145,7 +144,7 @@ func (s *serverObj) Handle(
// Decode the request.
token, fn, encArgs, err := DecodeReq(codec, rawReq)
if err != nil {
s.writeError(err, codec, w)
WriteError(codec, w, err)
return
}
@ -160,13 +159,13 @@ func (s *serverObj) Handle(
errVal := s.isAuthorizedV.Call(
[]reflect.Value{sessionV, reflect.ValueOf(fn)})[0]
if !errVal.IsNil() { // if err != nil
s.writeError(errVal.Interface().(error), codec, w)
WriteError(codec, w, errVal.Interface().(error))
return
}
m, ok := s.methods[fn]
if !ok {
s.writeError(newError(ErrCodeNotFound, "Function: "+fn), codec, w)
WriteError(codec, w, newError(ErrCodeNotFound, "Function: "+fn))
return
}
@ -175,7 +174,7 @@ func (s *serverObj) Handle(
// Unmarshal arguments.
if err := DecodeArgs(codec, encArgs, argsV.Interface()); err != nil {
s.writeError(err, codec, w)
WriteError(codec, w, err)
return
}
@ -183,35 +182,9 @@ func (s *serverObj) Handle(
fnArgs := []reflect.Value{s.objV, sessionV, argsV, respV}
errVal = m.method.Func.Call(fnArgs)[0]
if !errVal.IsNil() { // if err != nil
s.writeError(errVal.Interface().(error), codec, w)
WriteError(codec, w, errVal.Interface().(error))
return
}
s.writeResp(respV.Interface(), codec, w)
}
func (s *serverObj) writeError(err error, codec Codec, w io.Writer) {
le := lineError{}
if e, ok := err.(Error); ok {
le.Code = e.Code()
le.Msg = e.Msg()
} else {
le.Code = ErrCodeUnexpected
le.Msg = err.Error()
}
b, err := EncodeResp(codec, false, le)
if err != nil {
log.Printf("Failed to encode error response: %v", err)
panic(err) // Nothing else we can do.
}
w.Write(b)
}
func (s *serverObj) writeResp(resp interface{}, codec Codec, w io.Writer) {
b, err := EncodeResp(codec, true, resp)
if err != nil {
log.Printf("Failed to encode response: %v", err)
panic(err) // Nothing else we can do.
}
w.Write(b)
WriteResp(codec, w, respV.Interface())
}

Loading…
Cancel
Save