This commit is contained in:
crusader 2017-10-27 12:06:50 +09:00
parent 529bab00a6
commit 445bab7d74
2 changed files with 34 additions and 15 deletions

View File

@ -116,9 +116,14 @@ func (rr *rpcRegistry) Invoke(contentType string, r io.Reader, w io.Writer, befo
} }
// Call the service method. // Call the service method.
reply := reflect.New(methodSpec.replyType) reply := reflect.New(methodSpec.replyType)
// errValue := methodSpec.method.Func.Call([]reflect.Value{
// serviceSpec.rcvr,
// reflect.ValueOf(r),
// args,
// reply,
// })
errValue := methodSpec.method.Func.Call([]reflect.Value{ errValue := methodSpec.method.Func.Call([]reflect.Value{
serviceSpec.rcvr, serviceSpec.rcvr,
reflect.ValueOf(r),
args, args,
reply, reply,
}) })

View File

@ -3,7 +3,6 @@ package rpc
import ( import (
"fmt" "fmt"
"log" "log"
"net/http"
"reflect" "reflect"
"strings" "strings"
"sync" "sync"
@ -14,7 +13,7 @@ import (
var ( var (
// Precompute the reflect.Type of error and http.Request // Precompute the reflect.Type of error and http.Request
typeOfError = reflect.TypeOf((*error)(nil)).Elem() typeOfError = reflect.TypeOf((*error)(nil)).Elem()
typeOfRequest = reflect.TypeOf((*http.Request)(nil)).Elem() // typeOfRequest = reflect.TypeOf((*http.Request)(nil)).Elem()
) )
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
@ -72,25 +71,40 @@ func (m *serviceMap) register(rcvr interface{}, name string) error {
if method.PkgPath != "" { if method.PkgPath != "" {
continue continue
} }
// Method needs four ins: receiver, *http.Request, *args, *reply. // // Method needs four ins: receiver, *http.Request, *args, *reply.
if mtype.NumIn() != 4 { // if mtype.NumIn() != 4 {
// continue
// }
// // First argument must be a pointer and must be http.Request.
// reqType := mtype.In(1)
// if reqType.Kind() != reflect.Ptr || reqType.Elem() != typeOfRequest {
// continue
// }
// // Second argument must be a pointer and must be exported.
// args := mtype.In(2)
// if args.Kind() != reflect.Ptr || !isExportedOrBuiltin(args) {
// continue
// }
// // Third argument must be a pointer and must be exported.
// reply := mtype.In(3)
// if reply.Kind() != reflect.Ptr || !isExportedOrBuiltin(reply) {
// continue
// }
// Method needs four ins: receiver, *args, *reply.
if mtype.NumIn() != 3 {
continue continue
} }
// First argument must be a pointer and must be http.Request. // First argument must be a pointer and must be exported.
reqType := mtype.In(1) args := mtype.In(1)
if reqType.Kind() != reflect.Ptr || reqType.Elem() != typeOfRequest {
continue
}
// Second argument must be a pointer and must be exported.
args := mtype.In(2)
if args.Kind() != reflect.Ptr || !isExportedOrBuiltin(args) { if args.Kind() != reflect.Ptr || !isExportedOrBuiltin(args) {
continue continue
} }
// Third argument must be a pointer and must be exported. // Second argument must be a pointer and must be exported.
reply := mtype.In(3) reply := mtype.In(2)
if reply.Kind() != reflect.Ptr || !isExportedOrBuiltin(reply) { if reply.Kind() != reflect.Ptr || !isExportedOrBuiltin(reply) {
continue continue
} }
// Method needs one out: error. // Method needs one out: error.
if mtype.NumOut() != 1 { if mtype.NumOut() != 1 {
continue continue