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. // EncodeClientRequest encodes parameters for a JSON-RPC client request.
func EncodeClientRequest(method string, args interface{}) ([]byte, error) { func EncodeClientRequest(method string, args interface{}) ([]byte, error) {
c := &clientRequest{ c := &clientRequest{
Version: "2.0", Version: Version,
Method: method, Method: method,
Params: args, Params: args,
ID: uint64(rand.Int63()), 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. // EncodeClientNotify encodes parameters for a JSON-RPC client notification.
func EncodeClientNotify(method string, args interface{}) ([]byte, error) { func EncodeClientNotify(method string, args interface{}) ([]byte, error) {
c := &clientRequest{ c := &clientRequest{
Version: "2.0", Version: Version,
Method: method, Method: method,
Params: args, Params: args,
} }

View File

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

View File

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