This commit is contained in:
crusader 2017-10-27 13:00:30 +09:00
parent 445bab7d74
commit cbd9ce0150
3 changed files with 10 additions and 34 deletions

View File

@ -36,7 +36,7 @@ type clientResponse struct {
// EncodeClientRequest encodes parameters for a JSON-RPC client request.
func EncodeClientRequest(method string, args interface{}) ([]byte, error) {
c := &clientRequest{
Version: "2.0",
Version: Version,
Method: method,
Params: args,
ID: uint64(rand.Int63()),
@ -47,7 +47,7 @@ func EncodeClientRequest(method string, args interface{}) ([]byte, error) {
// EncodeClientNotify encodes parameters for a JSON-RPC client notification.
func EncodeClientNotify(method string, args interface{}) ([]byte, error) {
c := &clientRequest{
Version: "2.0",
Version: Version,
Method: method,
Params: args,
}

View File

@ -3,6 +3,7 @@ package rpc
import (
"fmt"
"io"
"log"
"reflect"
"strings"
@ -61,9 +62,9 @@ func (rr *rpcRegistry) RegisterCodec(codec protocol.Codec, contentType string) {
// - The receiver is exported (begins with an upper case letter) or local
// (defined in the package registering the service).
// - The method name is exported.
// - The method has three arguments: *http.Request, *args, *reply.
// - All three arguments are pointers.
// - The second and third arguments are exported or local.
// - The method has two arguments: *args, *reply.
// - All two arguments are pointers.
// - The first and second arguments are exported or local.
// - The method has return type error.
//
// All other methods are ignored.
@ -98,13 +99,16 @@ func (rr *rpcRegistry) Invoke(contentType string, r io.Reader, w io.Writer, befo
return fmt.Errorf("Unrecognized Content-Type: %s", contentType)
}
log.Print("codec.NewRequest")
// Create a new codec request.
codecReq := codec.NewRequest(r)
// Get service method to be called.
log.Print("codecReq.Method")
method, errMethod := codecReq.Method()
if errMethod != nil {
return write(codecReq, w, beforeWrite, afterWrite, nil, errMethod)
}
log.Print("rr.services.get")
serviceSpec, methodSpec, errGet := rr.services.get(method)
if errGet != nil {
return write(codecReq, w, beforeWrite, afterWrite, nil, errGet)
@ -116,12 +120,6 @@ func (rr *rpcRegistry) Invoke(contentType string, r io.Reader, w io.Writer, befo
}
// Call the service method.
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{
serviceSpec.rcvr,
args,

View File

@ -2,7 +2,6 @@ package rpc
import (
"fmt"
"log"
"reflect"
"strings"
"sync"
@ -13,7 +12,6 @@ import (
var (
// Precompute the reflect.Type of error and http.Request
typeOfError = reflect.TypeOf((*error)(nil)).Elem()
// typeOfRequest = reflect.TypeOf((*http.Request)(nil)).Elem()
)
// ----------------------------------------------------------------------------
@ -62,7 +60,7 @@ func (m *serviceMap) register(rcvr interface{}, name string) error {
return fmt.Errorf("rpc: no service name for type %q",
s.rcvrType.String())
}
log.Printf("method count of service[%s] is %d", s.name, s.rcvrType.NumMethod())
// Setup methods.
for i := 0; i < s.rcvrType.NumMethod(); i++ {
method := s.rcvrType.Method(i)
@ -71,25 +69,6 @@ func (m *serviceMap) register(rcvr interface{}, name string) error {
if method.PkgPath != "" {
continue
}
// // Method needs four ins: receiver, *http.Request, *args, *reply.
// 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
@ -117,7 +96,6 @@ func (m *serviceMap) register(rcvr interface{}, name string) error {
argsType: args.Elem(),
replyType: reply.Elem(),
}
log.Printf("The name of service[%s] method is %s", s.name, method.Name)
}
if len(s.methods) == 0 {
return fmt.Errorf("rpc: %q has no exported methods of suitable type",