/// <summary> /// Génère le code pour un wrapper de fonction à distance (pour les clients). /// </summary> /// <param name="instruction"></param> /// <returns></returns> string GenerateRemoteFunctionWrapper(Model.Language.Macros.RemoteFunctionWrapper instruction) { Function func = instruction.Func.Func; StringBuilder builder = new StringBuilder(); string returnTypeName = GenerateTypeInstanceName(func.ReturnType); builder.Append("public " + returnTypeName + " " + func.Name + "("); // Arg list foreach (var arg in func.Arguments) { builder.Append(GenerateTypeInstanceName(arg.ArgType) + " " + arg.ArgName + (arg == func.Arguments.Last() ? "" : ",")); } builder.Append(")\r\n{\r\n"); // Corps de la fonction. // --- Send builder.AppendLine("\t// Send"); builder.AppendLine("\tArrayList<Object> args = new ArrayList<Object>(); "); // Arg list foreach (var arg in func.Arguments) { builder.AppendLine("\targs.add(" + arg.ArgName + ");"); } // Func id builder.AppendLine("\tint funcId = " + instruction.Id + ";"); builder.AppendLine("\tArrayList<Object> obj = new ArrayList<Object>();"); builder.AppendLine("\tobj.add(funcId);"); builder.AppendLine("\tobj.add(args);"); // Sending builder.AppendLine("\tTCPHelper.send(JsonWriter.objectToJson(obj));"); // --- Receive builder.AppendLine("\t// Receive"); builder.AppendLine("\tString str = TCPHelper.receive();"); builder.AppendLine("\tJSONArray arr = (JSONArray)JSONReader.jsonToJava(str);"); // Désérialisation de ce qu'on a reçu. Variable srcVar = new Variable() { Type = func.ReturnType, }; string dstVar = "_ret"; string srcJsonObj = "arr"; string srcJsonKey = "0"; builder.AppendLine(Tools.StringUtils.Indent( GenerateDeserializerInstruction(srcVar, dstVar, srcJsonObj, srcJsonKey))); builder.AppendLine("\treturn " + dstVar + ";"); builder.AppendLine("}\r\n\r\n"); return(builder.ToString()); }
/// <summary> /// Génère le code pour un wrapper de fonction à distance (pour les clients). /// </summary> /// <param name="instruction"></param> /// <returns></returns> string GenerateRemoteFunctionWrapper(Model.Language.Macros.RemoteFunctionWrapper instruction) { Function func = instruction.Func.Func; StringBuilder builder = new StringBuilder(); string returnTypeName = GenerateTypeInstanceName(func.ReturnType); builder.Append(returnTypeName + " State::" + func.Name + "("); // Arg list foreach (var arg in func.Arguments) { builder.Append(GenerateTypeInstanceName(arg.ArgType) + " " + arg.ArgName + (arg == func.Arguments.Last() ? "" : ",")); } builder.Append(")\r\n{\r\n"); /* * if (PRINT_DEBUG) * { * builder.AppendLine("\tConsole.WriteLine(\"[" + instruction.Func.Func.Name + "]\");"); * }*/ builder.AppendLine("\tstd::ostringstream output = std::ostringstream();"); // Sérialise le numéro de la fonction. builder.AppendLine(Tools.StringUtils.Indent(GenerateSerializationInstruction(new Variable() { Name = instruction.Id.ToString(), Type = m_project.Types.TypeInstances["int"] }))); // Sérialise les arguments. for (int i = 0; i < func.Arguments.Count; i++) { builder.AppendLine(Tools.StringUtils.Indent(GenerateSerializationInstruction(new Variable() { Name = func.Arguments[i].ArgName, Type = func.Arguments[i].ArgType }))); } builder.AppendLine("\toutput.flush();"); builder.AppendLine("\tTCPHelper::tcpsend(output);"); // Récupère la réponse du serveur. builder.AppendLine("\tstd::istringstream input;"); builder.AppendLine("\tTCPHelper::tcpreceive(input);"); // Récupère l'objet dans le stream. builder.AppendLine(Tools.StringUtils.Indent(GenerateDeserializationInstruction( new Variable() { Type = func.ReturnType }, "returnValue"))); builder.AppendLine("\treturn (" + GenerateTypeInstanceName(func.ReturnType) + ")returnValue;"); builder.AppendLine("}\r\n\r\n"); return(builder.ToString()); }
/// <summary> /// Génère le code pour un wrapper de fonction à distance (pour les clients). /// </summary> /// <param name="instruction"></param> /// <returns></returns> string GenerateRemoteFunctionWrapper(Model.Language.Macros.RemoteFunctionWrapper instruction) { Function func = instruction.Func.Func; StringBuilder builder = new StringBuilder(); string returnTypeName = GenerateTypeInstanceName(func.ReturnType); builder.Append("public " + returnTypeName + " " + func.Name + "("); // Arg list foreach (var arg in func.Arguments) { builder.Append(GenerateTypeInstanceName(arg.ArgType) + " " + arg.ArgName + (arg == func.Arguments.Last() ? "" : ",")); } builder.Append(")\r\n{\r\n"); // Corps de la fonction. // --- Send builder.AppendLine("\t// Send"); builder.Append("\tList<object> args = new List<object>() { "); // Arg list foreach (var arg in func.Arguments) { builder.Append(arg.ArgName + (arg == func.Arguments.Last() ? "" : ",")); } builder.AppendLine("};"); // Func id builder.AppendLine("\tint funcId = " + instruction.Id + ";"); builder.AppendLine("\tList<object> obj = new List<object>() { funcId, args };"); // Sending builder.AppendLine("\tTCPHelper.Send(Newtonsoft.Json.JsonConvert.SerializeObject(obj));"); // --- Receive builder.AppendLine("\t// Receive"); builder.AppendLine("\tstring str = TCPHelper.Receive();"); builder.AppendLine("\tNewtonsoft.Json.Linq.JArray o = (Newtonsoft.Json.Linq.JArray)Newtonsoft.Json.JsonConvert.DeserializeObject(str);"); // Object if (func.ReturnType.BaseType.JType == JSONType.Object || func.ReturnType.BaseType.JType == JSONType.Array) { builder.Append("\treturn (" + returnTypeName + ")o[0].ToObject(typeof(" + returnTypeName + "));\r\n"); } else // Value { builder.Append("\treturn o.Value<" + returnTypeName + ">(0);\r\n"); } builder.AppendLine("}\r\n\r\n"); return(builder.ToString()); }
/// <summary> /// Génère le code pour un wrapper de fonction à distance (pour les clients). /// </summary> /// <param name="instruction"></param> /// <returns></returns> string GenerateRemoteFunctionWrapper(Model.Language.Macros.RemoteFunctionWrapper instruction) { Function func = instruction.Func.Func; StringBuilder builder = new StringBuilder(); string returnTypeName = GenerateTypeInstanceName(func.ReturnType); builder.Append(returnTypeName + " " + func.Name + "("); // Arg list foreach (var arg in func.Arguments) { builder.Append(GenerateTypeInstanceName(arg.ArgType) + " " + arg.ArgName + (arg == func.Arguments.Last() ? "" : ",")); } builder.AppendLine(");"); return(builder.ToString()); }