mirror of
https://github.com/duanhf2012/origin.git
synced 2026-02-04 06:54:45 +08:00
优化原始RPC,支持interface{}参数
This commit is contained in:
@@ -147,11 +147,15 @@ func (handler *RpcHandler) suitableMethods(method reflect.Method) error {
|
|||||||
return fmt.Errorf("%s Unsupported parameter types!",method.Name)
|
return fmt.Errorf("%s Unsupported parameter types!",method.Name)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
a := typ.In(parIdx).Kind()
|
||||||
rpcMethodInfo.inParamValue = reflect.New(typ.In(parIdx).Elem()) //append(rpcMethodInfo.iparam,)
|
if a == reflect.Interface {
|
||||||
rpcMethodInfo.inParam = reflect.New(typ.In(parIdx).Elem()).Interface()
|
rpcMethodInfo.inParam = nil
|
||||||
pt,_ := GetProcessorType(rpcMethodInfo.inParamValue.Interface())
|
}else{
|
||||||
rpcMethodInfo.rpcProcessorType = pt
|
rpcMethodInfo.inParamValue = reflect.New(typ.In(parIdx).Elem()) //append(rpcMethodInfo.iparam,)
|
||||||
|
rpcMethodInfo.inParam = reflect.New(typ.In(parIdx).Elem()).Interface()
|
||||||
|
pt,_ := GetProcessorType(rpcMethodInfo.inParamValue.Interface())
|
||||||
|
rpcMethodInfo.rpcProcessorType = pt
|
||||||
|
}
|
||||||
|
|
||||||
parIdx++
|
parIdx++
|
||||||
if parIdx< typ.NumIn() {
|
if parIdx< typ.NumIn() {
|
||||||
@@ -251,17 +255,23 @@ func (handler *RpcHandler) HandlerRpcRequest(request *RpcRequest) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if request.bLocalRequest == false {
|
if request.bLocalRequest == false {
|
||||||
err = request.rpcProcessor.Unmarshal(request.RpcRequestData.GetInParam(),iParam)
|
if iParam == nil {
|
||||||
if err!=nil {
|
iParam = request.RpcRequestData.GetInParam()
|
||||||
rErr := "Call Rpc "+request.RpcRequestData.GetServiceMethod()+" Param error "+err.Error()
|
}else{
|
||||||
log.Error(rErr)
|
err = request.rpcProcessor.Unmarshal(request.RpcRequestData.GetInParam(),iParam)
|
||||||
if request.requestHandle!=nil {
|
if err!=nil {
|
||||||
request.requestHandle(nil, RpcError(rErr))
|
rErr := "Call Rpc "+request.RpcRequestData.GetServiceMethod()+" Param error "+err.Error()
|
||||||
|
log.Error(rErr)
|
||||||
|
if request.requestHandle!=nil {
|
||||||
|
request.requestHandle(nil, RpcError(rErr))
|
||||||
|
}
|
||||||
|
return
|
||||||
}
|
}
|
||||||
return
|
|
||||||
}
|
}
|
||||||
}else {
|
}else {
|
||||||
if request.inputArgs!=nil {
|
if iParam == nil {
|
||||||
|
iParam = request.inputArgs.GetRawData()
|
||||||
|
}else if request.inputArgs!=nil {
|
||||||
err = request.rpcProcessor.Unmarshal(request.inputArgs.GetRawData(),iParam)
|
err = request.rpcProcessor.Unmarshal(request.inputArgs.GetRawData(),iParam)
|
||||||
if err!=nil {
|
if err!=nil {
|
||||||
rErr := "Call Rpc "+request.RpcRequestData.GetServiceMethod()+" Param error "+err.Error()
|
rErr := "Call Rpc "+request.RpcRequestData.GetServiceMethod()+" Param error "+err.Error()
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"github.com/duanhf2012/origin/node"
|
"github.com/duanhf2012/origin/node"
|
||||||
"github.com/duanhf2012/origin/rpc"
|
"github.com/duanhf2012/origin/rpc"
|
||||||
"github.com/duanhf2012/origin/sysservice/tcpservice"
|
"github.com/duanhf2012/origin/sysservice/tcpservice"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -88,11 +89,41 @@ func (r *Router) loadCfg(cfg interface{}){
|
|||||||
//error ...
|
//error ...
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
msgId,ok := iMsgId.(float64)
|
msgIdList,ok := iMsgId.(string)
|
||||||
if ok == false {
|
if ok == false {
|
||||||
//error ...
|
//error ...
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
var startMsgId int
|
||||||
|
var endMsgId int
|
||||||
|
var err error
|
||||||
|
sliceId := strings.Split(msgIdList,"-")
|
||||||
|
if len(sliceId) == 1 {
|
||||||
|
startMsgId,err = strconv.Atoi(msgIdList)
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("TcpGateService %s config is error!",iRpc.(string))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
endMsgId = startMsgId
|
||||||
|
} else if len(sliceId) == 2 {
|
||||||
|
startMsgId,err = strconv.Atoi(sliceId[0])
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("TcpGateService %s config is error!",iRpc.(string))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
endMsgId,err = strconv.Atoi(sliceId[1])
|
||||||
|
if err != nil {
|
||||||
|
log.Fatal("TcpGateService %s config is error!",iRpc.(string))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if startMsgId>endMsgId {
|
||||||
|
log.Fatal("TcpGateService %s config is error!",iRpc.(string))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
}else {
|
||||||
|
log.Fatal("TcpGateService %s config is error!",iRpc.(string))
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
strService := strings.Split(iRpc.(string),".")
|
strService := strings.Split(iRpc.(string),".")
|
||||||
if len(strService)!=2 {
|
if len(strService)!=2 {
|
||||||
@@ -100,7 +131,10 @@ func (r *Router) loadCfg(cfg interface{}){
|
|||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
r.mapMsgRouterInfo[uint16(msgId)] = &MsgRouterInfo{ServiceName: strService[0],Rpc: iRpc.(string),LoadBalanceType: iLoadBalanceType.(string)}
|
msgInfo := &MsgRouterInfo{ServiceName: strService[0],Rpc: iRpc.(string),LoadBalanceType: iLoadBalanceType.(string)}
|
||||||
|
for i:=startMsgId;i<=endMsgId;i++{
|
||||||
|
r.mapMsgRouterInfo[uint16(i)] = msgInfo
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//parse EventRouter
|
//parse EventRouter
|
||||||
|
|||||||
Reference in New Issue
Block a user