public FunctionAttr(string fileRoutePath, string functlp) { string fromatFunctlp = Regex.Replace(functlp, @"[\(\)]", " "); fromatFunctlp = Regex.Replace(fromatFunctlp, @"\,", ""); fromatFunctlp = Regex.Replace(fromatFunctlp, @"^\s*", ""); fromatFunctlp = Regex.Replace(fromatFunctlp, @"\s*$", ""); string[] funcTlpList = fromatFunctlp.Split(" "); if (funcTlpList.Length < 4 && funcTlpList.Length % 2 == 0) { throw new System.Exception("parse function arguments is failed, " + functlp); } m_retValue = new FunctionArg(funcTlpList[0], "ret" + funcTlpList[0]); m_funcName = funcTlpList[1]; m_funcHash = (size_t)HashEncoder.Hash(System.Text.Encoding.UTF8.GetBytes(fileRoutePath + m_funcName)); for (int i = 2; i < funcTlpList.Length; i += 2) { m_funcArgMap = new FunctionArg(funcTlpList[i], funcTlpList[i + 1]); } }
public static string CreateClientServiceCode(FunctionAttr functionAttrInterface, string inerfaceName) { string serviceName = StringTo.ToUpper(inerfaceName, 0) + "Service"; string interfaceName = StringTo.ToUpper(inerfaceName, 0) + "Interface"; string selfName = inerfaceName[0].ToString().ToLower(); FunctionArg v = functionAttrInterface.FuncArgMap; string funcArgsStr = v.VarName + " " + v.TypeName; string strs = "func (" + selfName + " *" + serviceName + ") " + StringTo.ToUpper(functionAttrInterface.FuncName, 0) + "("; strs += funcArgsStr + ", timeout uint32" + ") (*" + functionAttrInterface.FuncReturn.TypeName + ", error) {\n"; strs += "\ts := " + interfaceName + "{}\n"; strs += "\treturn s." + StringTo.ToLower(functionAttrInterface.FuncName, 0) + "Interface(" + selfName + "._brokerImpl" + ", " + v.VarName + ", " + selfName + "._compressType, timeout)\n"; strs += "}\n\n"; return(strs); }
public static string CreateClientInterfaceCode(FunctionAttr functionAttrInterface, string inerfaceName) { string strs = ""; string funcArgsStr = ""; string funcArgsStructStr = ""; string funcInterfaceName = StringTo.ToUpper(inerfaceName, 0) + "Interface"; FunctionArg v = functionAttrInterface.FuncArgMap; funcArgsStr = v.VarName + " " + v.TypeName; funcArgsStructStr = v.VarName; strs += "func (" + inerfaceName[0].ToString().ToLower() + " *" + funcInterfaceName + ") " + StringTo.ToLower(functionAttrInterface.FuncName, 0) + "Interface(broker" + " *rpclient.Broker, " + funcArgsStr + ", compressType rpcc.MRPC_PACKAGE_COMPRESS, timeout uint32) (*" + functionAttrInterface.FuncReturn.TypeName + ", error) {\n"; strs += "\tconst funcID = " + functionAttrInterface.FuncHash + "\n"; strs += "\tbuilder := flatbuffers.NewBuilder(512)\n"; strs += "\t//input flatbuffer code for " + v.TypeName + "FB class\n"; strs += ParseStruct.CreateSerializeCodeForFlatbuffer((ParseStruct)Vars.GetStruct(v.TypeName), v.VarName); strs += "\tbuilder.Finish(" + v.VarName + "Pos)\n"; strs += "\treq := rpclient.NewRequest(funcID, compressType,timeout, builder.FinishedBytes())\n"; strs += "\tresp, err := broker.SyncCall(req, rpcc.MRPC_PT_FLATBUFF)\n"; strs += "\tif err != nil{\n"; strs += "\t\treturn nil, err\n"; strs += "\t}\n\n"; strs += "\tif resp.GetStatus() != rpcc.RS_OK {\n"; strs += "\t\treturn nil, errors.New(\"rpc sync call error, function:" + functionAttrInterface.FuncName + "\")\n"; strs += "\t}\n\n"; strs += "\t" + functionAttrInterface.FuncReturn.VarName + "FB := GetRootAs" + functionAttrInterface.FuncReturn.TypeName + "FB(resp.Pop(), 0)\n"; strs += "\tvar " + functionAttrInterface.FuncReturn.VarName + " " + functionAttrInterface.FuncReturn.TypeName + "\n"; strs += "\t//input flatbuffer code for %" + functionAttrInterface.FuncReturn.TypeName + "FB class\n\n"; strs += ParseStruct.CreateDeserializeCodeForFlatbuffer((ParseStruct)Vars.GetStruct(functionAttrInterface.FuncReturn.TypeName), functionAttrInterface.FuncReturn.VarName, functionAttrInterface.FuncReturn.VarName + "FB") + "\n"; strs += "\treturn &" + functionAttrInterface.FuncReturn.VarName + ", nil\n"; strs += "}\n"; return(strs); }
public static string CreateServerServiceCode(FunctionAttr functionAttrInterface, string inerfaceName) { string strs = ""; string funcArgsStrs = ""; string returnStrs = StringTo.ToLower(functionAttrInterface.FuncReturn.TypeName) + "Ret"; string serviceName = StringTo.ToUpper(inerfaceName) + "Service"; FunctionArg v = functionAttrInterface.FuncArgMap; funcArgsStrs += v.VarName + " " + v.TypeName; strs += "func (" + StringTo.ToLower(functionAttrInterface.FuncReturn.TypeName)[0] + " *" + serviceName + ") " + functionAttrInterface.FuncName + "(" + funcArgsStrs + ") " + functionAttrInterface.FuncReturn.TypeName + " {\n"; strs += "\tvar " + returnStrs + " " + functionAttrInterface.FuncReturn.TypeName + "\n"; strs += "\t//TODO: 编写函数代码\n\n"; strs += "\treturn " + returnStrs + "\n"; strs += "}\n"; return(strs); }