Commit 69758dca authored by AlexStocks's avatar AlexStocks

Fix: panic when got notify method

parent 4b3836b3
...@@ -92,7 +92,7 @@ func (h *RpcServerHandler) OnMessage(session getty.Session, pkg interface{}) { ...@@ -92,7 +92,7 @@ func (h *RpcServerHandler) OnMessage(session getty.Session, pkg interface{}) {
} }
if req.header.CallType == CT_OneWay { if req.header.CallType == CT_OneWay {
function := req.methodType.method.Func function := req.methodType.method.Func
function.Call([]reflect.Value{req.service.rcvr, req.argv, req.replyv}) function.Call([]reflect.Value{req.service.rcvr, req.argv})
return return
} }
if req.header.CallType == CT_TwoWayNoReply { if req.header.CallType == CT_TwoWayNoReply {
......
...@@ -76,7 +76,9 @@ func (p *RpcServerPackageHandler) Read(ss getty.Session, data []byte) (interface ...@@ -76,7 +76,9 @@ func (p *RpcServerPackageHandler) Read(ss getty.Session, data []byte) (interface
req.argv = req.argv.Elem() req.argv = req.argv.Elem()
} }
// get reply // get reply
req.replyv = reflect.New(req.methodType.ReplyType.Elem()) if req.methodType.ReplyType != nil {
req.replyv = reflect.New(req.methodType.ReplyType.Elem())
}
return req, length, nil return req, length, nil
} }
......
...@@ -61,9 +61,12 @@ func suitableMethods(typ reflect.Type) map[string]*methodType { ...@@ -61,9 +61,12 @@ func suitableMethods(typ reflect.Type) map[string]*methodType {
if method.PkgPath != "" { if method.PkgPath != "" {
continue continue
} }
// Method needs three ins: receiver, *args, *reply. // service Method needs three ins: receiver, *args, *reply.
if mtype.NumIn() != 3 { // notify Method needs two ins: receiver, *args.
log.Warn("method %s has wrong number of ins %d which should be 3", mname, mtype.NumIn()) mInNum := mtype.NumIn()
if mInNum != 2 && mInNum != 3 {
log.Warn("method %s has wrong number of ins %d which should be " +
"2(notify method) or 3(serive method)", mname, mtype.NumIn())
continue continue
} }
// First arg need not be a pointer. // First arg need not be a pointer.
...@@ -72,16 +75,20 @@ func suitableMethods(typ reflect.Type) map[string]*methodType { ...@@ -72,16 +75,20 @@ func suitableMethods(typ reflect.Type) map[string]*methodType {
log.Error("method{%s} argument type not exported{%v}", mname, argType) log.Error("method{%s} argument type not exported{%v}", mname, argType)
continue continue
} }
// Second arg must be a pointer.
replyType := mtype.In(2) var replyType reflect.Type
if replyType.Kind() != reflect.Ptr { if mInNum == 3 {
log.Error("method{%s} reply type not a pointer{%v}", mname, replyType) // Second arg must be a pointer.
continue replyType = mtype.In(2)
} if replyType.Kind() != reflect.Ptr {
// Reply type must be exported. log.Error("method{%s} reply type not a pointer{%v}", mname, replyType)
if !isExportedOrBuiltinType(replyType) { continue
log.Error("method{%s} reply type not exported{%v}", mname, replyType) }
continue // Reply type must be exported.
if !isExportedOrBuiltinType(replyType) {
log.Error("method{%s} reply type not exported{%v}", mname, replyType)
continue
}
} }
// Method needs one out. // Method needs one out.
if mtype.NumOut() != 1 { if mtype.NumOut() != 1 {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment