Пример #1
0
        public static string CreateServerInterfaceCode(FunctionAttr functionAttrInterface, string inerfaceName)
        {
            string strs = "func (slf *" + StringTo.ToUpper(inerfaceName) + "Interface)  " + functionAttrInterface.FuncName + "Interface(context *rpcb.Context, req *rpcs.Request) error{\n";

            strs += "\t" + functionAttrInterface.FuncArgMap.VarName + "FB := GetRootAs" + functionAttrInterface.FuncArgMap.TypeName + "FB(req.Pop(), 0)\n";
            strs += "\t" + functionAttrInterface.FuncArgMap.VarName + " := " + functionAttrInterface.FuncArgMap.TypeName + "{}\n";

            strs += "\t//input flatbuffer code for " + functionAttrInterface.FuncArgMap.VarName + "FB class\n";

            strs += "" + ParseStruct.CreateDeserializeCodeForFlatbuffer((ParseStruct)Vars.GetStruct(functionAttrInterface.FuncArgMap.TypeName),
                                                                        functionAttrInterface.FuncArgMap.VarName, functionAttrInterface.FuncArgMap.VarName + "FB") + "\n\n";

            strs += "\tdeleageService := " + StringTo.ToUpper(inerfaceName) + "Service{_implcontext:context}\n";
            strs += "\t" + functionAttrInterface.FuncReturn.VarName + " := deleageService." + functionAttrInterface.FuncName + "(" + functionAttrInterface.FuncArgMap.VarName + ")\n";


            strs += "\tbuilder := slf._implsrv.MallocFlatBuilder()\n";
            strs += "\tdefer slf._implsrv.FreeFlatBuilder(builder)\n";
            strs += "\t//input flatbuffer code for " + functionAttrInterface.FuncReturn.TypeName + "\n";
            strs += "\t" + ParseStruct.CreateSerializeCodeForFlatbuffer((ParseStruct)Vars.GetStruct(functionAttrInterface.FuncReturn.TypeName),
                                                                        functionAttrInterface.FuncReturn.VarName);
            strs += "\tbuilder.Finish(" + functionAttrInterface.FuncReturn.VarName + "Pos)\n";

            strs += "\tresp := rpcs.NewReponse(req, builder.FinishedBytes())\n";
            strs += "\tif err := slf._implsrv.Response(resp, rpcc.MRPC_PT_FLATBUFF); err != nil {\n";
            strs += "\t\tcontext.Error(\"response fail:%v\", err)\n";
            strs += "\t}\n";


            strs += "\treturn nil\n}\n\n";

            return(strs);
        }
Пример #2
0
        public static string CreateServerInterfaceCode(ParseInterface parse)
        {
            string interfaceClassName = StringTo.ToUpper(parse.InterfaceName) + "Interface";
            string interfaceClassVar  = parse.InterfaceName[0] +
                                        Md5Helper.Md5(interfaceClassName).ToLower().Replace("-", "");

            interfaceClassVar = interfaceClassVar.ToLower();

            string strs = "var (\n\t" + interfaceClassVar + " *" + interfaceClassName + "\n)\n\n";

            strs += "func Fasten" + parse.InterfaceName + "(psrv *rpcs.Server) {\n";
            strs += "\t" + interfaceClassVar + " = &" + interfaceClassName + "{_implsrv:psrv}\n";
            foreach (FunctionAttr funAttr in parse.Functions)
            {
                strs += "\t" + interfaceClassVar + "._implsrv.BindDelegate(" + funAttr.FuncHash + "," +
                        interfaceClassVar + "." + funAttr.FuncName + "Interface," +
                        "\"" + parse.InterfaceName + "Service." + funAttr.FuncName + "\")\n";
            }
            strs += "}\n\n";


            strs += "type " + StringTo.ToUpper(parse.InterfaceName) + "Interface struct {\n";
            strs += "\t_implsrv *rpcs.Server\n";
            strs += "}\n\n";

            foreach (FunctionAttr funAttr in parse.Functions)
            {
                strs += functionAttrCode.CreateServerInterfaceCode(funAttr, parse.InterfaceName);
            }

            return(strs);
        }
Пример #3
0
        public static string CreateClientInterfaceCode(ParseInterface parse)
        {
            string interfaceClassName = StringTo.ToUpper(parse.InterfaceName, 0) + "Interface";
            string strs = "type " + interfaceClassName + " struct {\n";

            strs += "}\n\n";
            foreach (FunctionAttr funAttr in parse.Functions)
            {
                strs += functionAttrCode.CreateClientInterfaceCode(funAttr, parse.InterfaceName);
            }

            return(strs);
        }
Пример #4
0
        public static string CreateServerServiceCode(ParseInterface parse)
        {
            string serviceName = StringTo.ToUpper(parse.InterfaceName) + "Service";
            string strs        = "type " + serviceName + " struct {\n";

            strs += "\t_implcontext *rpcb.Context\n";
            strs += "}\n\n";

            foreach (FunctionAttr funAttr in parse.Functions)
            {
                strs += functionAttrCode.CreateServerServiceCode(funAttr, parse.InterfaceName);
            }

            return(strs);
        }
Пример #5
0
        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);
        }
Пример #6
0
        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);
        }
Пример #7
0
        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);
        }
Пример #8
0
        public static string CreateClientServiceCode(ParseInterface parse)
        {
            string serviceName = StringTo.ToUpper(parse.InterfaceName) + "Service";
            string strs        = "type " + serviceName + " struct {\n";

            strs += "\t" + "_brokerImpl *rpclient.Broker\n";
            strs += "\t" + "_compressType rpcc.MRPC_PACKAGE_COMPRESS\n";
            strs += "}\n\n";
            strs += "func (" + serviceName[0].ToString().ToLower() + " *" + serviceName + ") WithBroker(broker *rpclient.Broker) {\n";
            strs += "\t" + serviceName[0].ToString().ToLower() + "._brokerImpl = broker\n";
            strs += "}\n";

            strs += "func (" + serviceName[0].ToString().ToLower() + " *" + serviceName + ") WithCommpressType(compressType rpcc.MRPC_PACKAGE_COMPRESS) {\n";
            strs += "\t" + serviceName[0].ToString().ToLower() + "._compressType = compressType\n";
            strs += "}\n";


            foreach (FunctionAttr funAttr in parse.Functions)
            {
                strs += functionAttrCode.CreateClientServiceCode(funAttr, parse.InterfaceName);
            }

            return(strs);
        }
Пример #9
0
        public static string CreateSerializeCodeForFlatbuffer(ParseStruct structInfo, string varName)
        {
            string            classStr       = "";
            string            stringStr      = "";
            string            argsStr        = "";
            string            spacesStr      = "";
            List <MemberAttr> memberAttrInfo = structInfo.MemberAttrs;
            string            structName     = structInfo.StructName;

            serializeRecursive++;
            for (int j = 0; j <= serializeRecursive; j++)
            {
                spacesStr += "\t";
            }



            for (int i = 0; i < memberAttrInfo.Count; i++)
            {
                string iterName = memberAttrInfo[i].VarName;
                string iterType = memberAttrInfo[i].TypeName;

                if (memberAttrInfo[i].IsArray)
                {
                    if (memberAttrInfo[i].IsClass)
                    {
                        classStr += "\n";
                        classStr += spacesStr + "//class " + iterType + " vector serialize\n";
                        classStr += spacesStr + iterName + "PosArray := make([]flatbuffers.UOffsetT, len(" + varName + "." + iterName + "))\n";
                        classStr += spacesStr + "for i," + iterName + " := range " + varName + "." + iterName + " {\n";
                        classStr += spacesStr + ParseStruct.CreateSerializeCodeForFlatbuffer((ParseStruct)Vars.GetStruct(iterType), iterName);
                        classStr += spacesStr + "\t" + iterName + "PosArray[i] = " + iterName + "Pos\n";
                    }
                    else if (memberAttrInfo[i].IsString)
                    {
                        classStr += "\n";
                        classStr += spacesStr + "//string " + iterName + " vector serialize\n";
                        classStr += spacesStr + iterName + "PosArray := make([]flatbuffers.UOffsetT, len(" + varName + "." + iterName + "))\n";
                        classStr += spacesStr + "for i," + iterName + " := range " + varName + "." + iterName + " {\n";
                        classStr += spacesStr + "\t" + iterName + "PosArray[i] = builder.CreateString(" + iterName + ")\n";
                    }
                    else
                    {
                        string endOffset = varName + StringTo.ToUpper(iterName) + "EndOffset";
                        classStr += "\n";
                        classStr += spacesStr + "//" + iterType + " " + iterName + " vector serialize\n";
                        classStr += spacesStr + structName + "FBStart" + StringTo.ToUpper(iterName) + "Vector(builder, len(" + varName + "." + iterName + "))\n";
                        classStr += spacesStr + "for _," + iterName + " := range " + varName + "." + iterName + " {\n";
                        classStr += spacesStr + "\tbuilder.Prepend" + StringTo.ToUpper(iterType) + "(" + iterName + ")\n";
                        classStr += spacesStr + "}\n";
                        classStr += spacesStr + endOffset + " := builder.EndVector(len(" + varName + "." + iterName + "))\n\n";
                        argsStr  += spacesStr + structName + "FBAdd" + StringTo.ToUpper(iterName) + "(builder," + endOffset + ")\n";
                    }

                    if (memberAttrInfo[i].IsClass || memberAttrInfo[i].IsString)
                    {
                        classStr += spacesStr + "}\n";

                        string endOffset = varName + StringTo.ToUpper(iterName) + "EndOffset";

                        classStr += spacesStr + structName + "FBStart" + StringTo.ToUpper(iterName) + "Vector(builder, len(" + iterName + "PosArray))\n";
                        classStr += spacesStr + "for _," + iterName + "Offset := range " + iterName + "PosArray {\n";
                        classStr += "\t\tbuilder.PrependUOffsetT(" + iterName + "Offset)\n";
                        classStr += "\t}\n";
                        classStr += spacesStr + endOffset + " := builder.EndVector(len(" + iterName + "PosArray))\n\n";
                        argsStr  += spacesStr + structName + "FBAdd" + StringTo.ToUpper(iterName) + "(builder," +
                                    varName + StringTo.ToUpper(iterName) + "EndOffset)\n";
                    }
                }
                else if (memberAttrInfo[i].IsClass)
                {
                    classStr += "\n";
                    classStr += "//class " + iterType + " " + iterName + " serialize\n";
                    classStr += ParseStruct.CreateSerializeCodeForFlatbuffer((ParseStruct)Vars.GetStruct(iterType), iterName);
                    argsStr  += spacesStr + structName + "FBAdd" + StringTo.ToUpper(iterName) + "(builder," + iterName + "Pos" + ")\n";
                }
                else if (memberAttrInfo[i].IsString)
                {
                    string stringPos = varName + StringTo.ToUpper(iterName) + "StringPos";
                    stringStr += "\n";
                    stringStr += spacesStr + stringPos + " := builder.CreateString(" + varName + "." + iterName + ")\n";
                    argsStr   += spacesStr + structName + "FBAdd" + StringTo.ToUpper(iterName) + "(builder," + stringPos + ")\n";
                }
                else
                {
                    argsStr += spacesStr + structName + "FBAdd" + StringTo.ToUpper(iterName) + "(builder," + varName + "." + iterName + ")\n";
                }
            }


            classStr += spacesStr + "//class " + structName + " serialize start\n";
            classStr += stringStr;
            classStr += spacesStr + structName + "FBStart(builder)\n";
            classStr += argsStr;
            classStr += spacesStr + varName + "Pos := " + structName + "FBEnd(builder)\n";
            classStr += spacesStr + "//class " + structName + " serialize end\n\n";
            serializeRecursive--;
            return(classStr);
        }
Пример #10
0
        public static string CreateDeserializeCodeForFlatbuffer(ParseStruct structInfo, string varName, string fbName)
        {
            string            strs           = "";
            string            spacesStr      = "";
            List <MemberAttr> memberAttrInfo = structInfo.MemberAttrs;
            string            structName     = structInfo.StructName;

            deserializeRecursive++;
            for (int j = 0; j <= deserializeRecursive; j++)
            {
                spacesStr += "\t";
            }

            for (int i = 0; i < memberAttrInfo.Count; i++)
            {
                string iterName = memberAttrInfo[i].VarName;
                string iterType = memberAttrInfo[i].TypeName;

                //for (int j = 0; j <= deserializeRecursive; j++)
                //     speces += "\t";

                if (memberAttrInfo[i].IsArray)
                {
                    if (memberAttrInfo[i].IsClass)
                    {
                        string tmpName = iterType.ToLower() + "Tmp";
                        strs += spacesStr + "for i := 0;i < " + fbName + "." + StringTo.ToUpper(iterName) + "Length();i++ {\n";
                        strs += spacesStr + "\tvar " + tmpName + " " + iterType + "\n";
                        strs += spacesStr + "\tvar " + iterName + " " + iterType + "FB\n";

                        strs += spacesStr + "\tif ok := " + fbName + "." + StringTo.ToUpper(iterName) + "(&" + iterName + ",i);!ok {\n";
                        strs += spacesStr + "\t\treturn nil, errors.New(\"deserialize " + fbName + "=>" + StringTo.ToUpper(iterName) + " fail\")\n";
                        strs += spacesStr + "\t}\n\n";
                        strs += ParseStruct.CreateDeserializeCodeForFlatbuffer((ParseStruct)Vars.GetStruct(iterType), tmpName, iterName);
                        strs += spacesStr + "\t" + varName + "." + iterName + " = append(" + varName + "." + iterName + "," + tmpName + ")\n";
                    }
                    else
                    {
                        strs += spacesStr + "for i := 0;i < " + fbName + "." + StringTo.ToUpper(iterName) + "Length();i++ {\n";
                        strs += spacesStr + "\t" + varName + "." + iterName + " = append(" + varName + "." + iterName + ",";
                        if (memberAttrInfo[i].IsString)
                        {
                            strs += spacesStr + "string(" + fbName + "." + StringTo.ToUpper(iterName) + "(i)))\n";
                        }
                        else
                        {
                            strs += spacesStr + fbName + "." + StringTo.ToUpper(iterName) + "(i))\n";
                        }
                    }
                    strs += spacesStr + "}\n\n";
                }
                else if (memberAttrInfo[i].IsClass)
                {
                    strs += ParseStruct.CreateDeserializeCodeForFlatbuffer((ParseStruct)Vars.GetStruct(iterType), varName + "." + iterName,
                                                                           fbName + "." + StringTo.ToUpper(iterName) + "()");
                }
                else
                {
                    if (memberAttrInfo[i].IsString)
                    {
                        strs += spacesStr + varName + "." + iterName + " = string(" + fbName + "." + StringTo.ToUpper(iterName) + "())\n";
                    }
                    else
                    {
                        strs += spacesStr + varName + "." + iterName + " = " + fbName + "." + StringTo.ToUpper(iterName) + "()\n";
                    }
                }
            }

            deserializeRecursive--;
            return(strs);
        }