Prechádzať zdrojové kódy

Z991239-5240 #comment 同步WIN的SpGen

80374374 1 rok pred
rodič
commit
e3e5eecc05

+ 114 - 12
Tool/spgen/CPPGenerator.cs

@@ -7,7 +7,7 @@ namespace spgen
 {
     public class CPPGenerator : GeneratorBase
     {
-        static string[] CPP_TYPE_NAMES = new string[] 
+        static string[] CPP_TYPE_NAMES = new string[]
         {
             "bool",
             "short",
@@ -36,7 +36,7 @@ namespace spgen
             "CAutoArray<float>",
             "CAutoArray<CSimpleStringA>",
             "CAutoArray<CSimpleStringW>",
-            "CAutoArray<CBlob>", 
+            "CAutoArray<CBlob>",
         };
 
         static string GetCodeType(ParamType type)
@@ -44,9 +44,10 @@ namespace spgen
             return CPP_TYPE_NAMES[(int)type];
         }
 
-        public override void GenerateCode(string dir, Entity entity)
+        public override void GenerateCode(string dir, Entity entity, bool inSourceDir)
         {
-            dir = Path.Combine(dir, "generate");
+            if (!inSourceDir)
+                dir = Path.Combine(dir, "generate");
             if (!Directory.Exists(dir))
                 Directory.CreateDirectory(dir);
             string deffilename = string.Format("{0}_def_g.h", entity.Name);
@@ -94,13 +95,13 @@ namespace spgen
             sw.WriteLine();
         }
 
-        void WriteLineDefine(StreamWriter sw, Entity entity, string name, string num)
+        void WriteLineDefine(StreamWriter sw, Entity entity, string name, string num, bool tryReplace = true)
         {
             string value = "";
-            if(entity.defineDic.TryGetValue(name, out value))
+            if (tryReplace && entity.defineDic.TryGetValue(name, out value))
             {
                 //success ,judge if len > 3, len > 3, replace the new value; len <=3, must the same 
-                if(value.Length > 3)
+                if (value.Length > 3)
                     num = value;
                 else
                 {
@@ -133,6 +134,28 @@ namespace spgen
             sw.WriteLine();
         }
 
+        bool WriteLogCode(StreamWriter sw, Entity entity, EntityClass cls)
+        {
+            if (!Entity.entityDic.ContainsKey(entity.Name))
+                return false;
+            foreach (Method m in cls.MethodList)
+            {
+                if (typeof(Subscribe).IsInstanceOfType(m))
+                {
+                    continue;
+                }
+                else
+                {
+                    if (m.Id > 256) m.Id = 99;
+                    string nameStr = string.Format("{0}_LogCode_{1}", cls.Name, m.Name);
+                    string defineStr = string.Format("\"QLR0402{0}{1}\"", Entity.entityDic[entity.Name], m.Id.ToString("D2"));
+                    WriteLineDefine(sw, entity, nameStr, defineStr, false);
+                }
+            }
+            sw.WriteLine();
+            return true;
+        }
+
         void WriteMethodSignatureEnum(StreamWriter sw, Entity entity, EntityClass cls)
         {
             foreach (Method m in cls.MethodList)
@@ -259,6 +282,7 @@ namespace spgen
                 {
                     WriteMethodEnum(sw, entity, cls);
                     WriteMethodSignatureEnum(sw, entity, cls);
+                    WriteLogCode(sw, entity, cls);
                     WriteMethodParam(sw, entity, cls);
                     sw.WriteLine();
                     sw.WriteLine("///////////////////////////");
@@ -422,6 +446,7 @@ namespace spgen
             sw.WriteLine("\t\t\t\treturn;");
             sw.WriteLine("\t\t\t}");
             sw.WriteLine("#endif");
+            //sw.WriteLine("\t\tEntityResource::clearLink();");
             sw.WriteLine("\t\t\tswitch (dwMessageID) {");
             foreach (Method m in cls.MethodList)
             {
@@ -433,6 +458,8 @@ namespace spgen
                     sw.WriteLine("\t\t\t\t\t\tSpOnewayCallContext<{0}>::Pointer ctx;", GetInfoStructName(entity, cls, m));
                     sw.WriteLine("\t\t\t\t\t\tctx.Attach(new SpOnewayCallContext<{0}>());", GetInfoStructName(entity, cls, m));
                     sw.WriteLine("\t\t\t\t\t\tSpBuffer2Object(Buf, ctx->Info);");
+                    sw.WriteLine("\t\t\t\t\t\tpTransactionContext->GetLinkContext(ctx->link);");
+                    sw.WriteLine("\t\t\t\t\t\tEntityResource::setLink(ctx->link);");
                     sw.WriteLine("\t\t\t\t\t\tHandle_{0}(ctx);", m.Name);
                     sw.WriteLine("\t\t\t\t\t}");
                     sw.WriteLine("\t\t\t\t\tbreak;");
@@ -445,6 +472,8 @@ namespace spgen
                     sw.WriteLine("\t\t\t\t\t\tSpReqAnsContext<{0},{1}>::Pointer ctx;", GetReqStructName(entity, cls, m), GetAnsStructName(entity, cls, m));
                     sw.WriteLine("\t\t\t\t\t\tctx.Attach(new SpReqAnsContext<{0},{1}>(pTransactionContext));", GetReqStructName(entity, cls, m), GetAnsStructName(entity, cls, m));
                     sw.WriteLine("\t\t\t\t\t\tSpBuffer2Object(Buf, ctx->Req);");
+                    sw.WriteLine("\t\t\t\t\t\tpTransactionContext->GetLinkContext(ctx->link);");
+                    sw.WriteLine("\t\t\t\t\t\tEntityResource::setLink(ctx->link);");
                     sw.WriteLine("\t\t\t\t\t\tHandle_{0}(ctx);", m.Name);
                     sw.WriteLine("\t\t\t\t\t}");
                     sw.WriteLine("\t\t\t\t\tbreak;");
@@ -563,8 +592,17 @@ namespace spgen
                 sw.WriteLine("\tErrorCodeEnum {0}({1} &Info)", oneway.Name, iname);
                 sw.WriteLine("\t{");
                 sw.WriteLine("\t\tCSmartPointer<IClientSessionFunction> pFunc = GetFunction();");
+                sw.WriteLine("\t\tif (m_context.checkEmpty())");
+                sw.WriteLine("\t\t{");
+                sw.WriteLine("\t\t\tm_context.AutoGenerate();");
+                sw.WriteLine("\t\t\tDbgToBeidou(m_context, m_pEntityBase != NULL ? m_pEntityBase->GetEntityName() : \"\")();");
+                sw.WriteLine("\t\t\tm_context = m_context.upgradeLink();");
+                sw.WriteLine("\t\t}");
+
                 sw.WriteLine("\t\tCAutoBuffer Buf = SpObject2Buffer(Info);");
-                sw.WriteLine("\t\treturn pFunc->OnewayCall({0}, {1}, Buf);", GetMethodEnumName(entity, cls, oneway), GetMethodSignatureEnumName(entity, cls, oneway));
+                sw.WriteLine("\t\tauto ret = pFunc->OnewayCall({0}, {1}, Buf, m_context);", GetMethodEnumName(entity, cls, oneway), GetMethodSignatureEnumName(entity, cls, oneway));
+                sw.WriteLine("\t\tm_context.clear();");
+                sw.WriteLine("\t\treturn ret;");
                 sw.WriteLine("\t}");
             }
             else
@@ -572,7 +610,17 @@ namespace spgen
                 sw.WriteLine("\tErrorCodeEnum {0}()", oneway.Name);
                 sw.WriteLine("\t{");
                 sw.WriteLine("\t\tCSmartPointer<IClientSessionFunction> pFunc = GetFunction();");
-                sw.WriteLine("\t\treturn pFunc->OnewayCall({0}, {1});", GetMethodEnumName(entity, cls, oneway), GetMethodSignatureEnumName(entity, cls, oneway));
+                sw.WriteLine("\t\tif (m_context.checkEmpty())");
+                sw.WriteLine("\t\t{");
+                sw.WriteLine("\t\t\tm_context.AutoGenerate();");
+                sw.WriteLine("\t\t\tDbgToBeidou(m_context, m_pEntityBase != NULL ? m_pEntityBase->GetEntityName() : \"\")();");
+                sw.WriteLine("\t\t\tm_context = m_context.upgradeLink();");
+                sw.WriteLine("\t\t}");
+
+
+                sw.WriteLine("\t\tauto ret = pFunc->OnewayCall({0}, {1}, m_context);", GetMethodEnumName(entity, cls, oneway), GetMethodSignatureEnumName(entity, cls, oneway));
+                sw.WriteLine("\t\tm_context.clear();");
+                sw.WriteLine("\t\treturn ret;");
                 sw.WriteLine("\t}");
             }
             sw.WriteLine();
@@ -585,7 +633,17 @@ namespace spgen
             sw.WriteLine("\t{");
             sw.WriteLine("\t\tCSmartPointer<IClientSessionFunction> pFunc = GetFunction();");
             sw.WriteLine("\t\tCAutoBuffer Buf = SpObject2Buffer(Req);");
-            sw.WriteLine("\t\treturn pFunc->AsyncRequest({0}, {1}, Buf, spAsyncWait, dwTimeout);", GetMethodEnumName(entity, cls, twoway), GetMethodSignatureEnumName(entity, cls, twoway));
+            sw.WriteLine("\t\tif (m_context.checkEmpty())");
+            sw.WriteLine("\t\t{");
+            sw.WriteLine("\t\t\tm_context.AutoGenerate();");
+            sw.WriteLine("\t\t\tDbgToBeidou(m_context, m_pEntityBase != NULL ? m_pEntityBase->GetEntityName() : \"\")();");
+            sw.WriteLine("\t\t\tm_context = m_context.upgradeLink();");
+            sw.WriteLine("\t\t}");
+
+
+            sw.WriteLine("\t\tauto ret = pFunc->AsyncRequest({0}, {1}, Buf, spAsyncWait, m_context, dwTimeout);", GetMethodEnumName(entity, cls, twoway), GetMethodSignatureEnumName(entity, cls, twoway));
+            sw.WriteLine("\t\tm_context.clear();");
+            sw.WriteLine("\t\treturn ret;");
             sw.WriteLine("\t}");
 
             // sync
@@ -601,6 +659,19 @@ namespace spgen
             sw.WriteLine("\t\treturn Error;");
             sw.WriteLine("\t}");
 
+            // sync with user error + buf
+            sw.WriteLine("\tErrorCodeEnum {0}({1} &Req, {2} &Ans, DWORD dwTimeout, DWORD &dwUserError, CSimpleString &str)", twoway.Name, GetReqStructName(entity, cls, twoway), GetAnsStructName(entity, cls, twoway));
+            sw.WriteLine("\t{");
+            sw.WriteLine("\t\tCSmartPointer<IAsynWaitSp> spAsyncWait;");
+            sw.WriteLine("\t\tErrorCodeEnum Error = {0}(Req, spAsyncWait, dwTimeout);", twoway.Name);
+            sw.WriteLine("\t\tif (Error == Error_Succeed) {");
+            sw.WriteLine("\t\t\tbool bEnd = false;");
+            sw.WriteLine("\t\t\tError = SpWaitAnswerObject(spAsyncWait, Ans, bEnd, dwUserError, str, dwTimeout);");
+            sw.WriteLine("\t\t\tLOG_ASSERT(Error || bEnd);");
+            sw.WriteLine("\t\t}");
+            sw.WriteLine("\t\treturn Error;");
+            sw.WriteLine("\t}");
+
             // sync with user error
             sw.WriteLine("\tErrorCodeEnum {0}({1} &Req, {2} &Ans, DWORD dwTimeout, DWORD &dwUserError)", twoway.Name, GetReqStructName(entity, cls, twoway), GetAnsStructName(entity, cls, twoway));
             sw.WriteLine("\t{");
@@ -608,7 +679,8 @@ namespace spgen
             sw.WriteLine("\t\tErrorCodeEnum Error = {0}(Req, spAsyncWait, dwTimeout);", twoway.Name);
             sw.WriteLine("\t\tif (Error == Error_Succeed) {");
             sw.WriteLine("\t\t\tbool bEnd = false;");
-            sw.WriteLine("\t\t\tError = SpWaitAnswerObject(spAsyncWait, Ans, bEnd, dwUserError, dwTimeout);");
+            sw.WriteLine("\t\t\tCSimpleString str;");
+            sw.WriteLine("\t\t\tError = SpWaitAnswerObject(spAsyncWait, Ans, bEnd, dwUserError, str, dwTimeout);");
             sw.WriteLine("\t\t\tLOG_ASSERT(Error || bEnd);");
             sw.WriteLine("\t\t}");
             sw.WriteLine("\t\treturn Error;");
@@ -722,6 +794,16 @@ namespace spgen
             sw.WriteLine();
         }
 
+        void WriteOperatorContext(StreamWriter sw, Entity entity, EntityClass cls)
+        {
+            sw.WriteLine("\t{0}* operator () (const linkContext &curLink) ", GetClientClassName(entity, cls));
+            sw.WriteLine("\t{");
+            sw.WriteLine("\t\tm_context = curLink;");
+            sw.WriteLine("\t\treturn this;");
+            sw.WriteLine("\t}");
+            sw.WriteLine();
+        }
+
         void WriteClientDestructor(StreamWriter sw, Entity entity, EntityClass cls)
         {
             sw.WriteLine("protected:");
@@ -735,12 +817,29 @@ namespace spgen
 
         void WriteClientConnect(StreamWriter sw, Entity entity, EntityClass cls)
         {
+            sw.WriteLine("\tvoid OnConnectSucceed()");
+            sw.WriteLine("\t{");
+            sw.WriteLine("\t\tbSessionClosed = false;");
+            sw.WriteLine("\t}");
+
+            sw.WriteLine("\tvoid OnClose(ErrorCodeEnum)");
+            sw.WriteLine("\t{");
+            sw.WriteLine("\t\tDbg(\"session closed.\");");
+            sw.WriteLine("\t\tbSessionClosed = true;");
+            sw.WriteLine("\t}");
+
+            sw.WriteLine("\tbool QuerySessionClosed()");
+            sw.WriteLine("\t{");
+            sw.WriteLine("\t\treturn bSessionClosed;");
+            sw.WriteLine("\t}");
+
             sw.WriteLine("\tErrorCodeEnum Connect(CSmartPointer<IAsynWaitSp> &spAsyncWait)");
             sw.WriteLine("\t{");
             sw.WriteLine("\t\tCSmartPointer<IEntityFunction> pFunc = m_pEntityBase->GetFunction();");
             sw.WriteLine("\t\tErrorCodeEnum Error = pFunc->ConnectRemoteEntity(this, \"{0}\", \"{1}\", spAsyncWait);", entity.Name, cls.Name);
             sw.WriteLine("\t\tif (Error == Error_Succeed) {");
             sw.WriteLine("\t\t\tm_bSysManaged = true;");
+            sw.WriteLine("\t\t\tbSessionClosed = false;");
             sw.WriteLine("\t\t}");
             sw.WriteLine("\t\treturn Error;");
             sw.WriteLine("\t}");
@@ -784,6 +883,7 @@ namespace spgen
                 sw.WriteLine("class {0} : public CClientSessionBase {{", clsname);
             sw.WriteLine("public:");
             WriteClientConstructor(sw, entity, cls);
+            WriteOperatorContext(sw, entity, cls);
             WriteClientDestructor(sw, entity, cls);
             WriteClientConnect(sw, entity, cls);
             foreach (Method m in cls.MethodList)
@@ -810,9 +910,11 @@ namespace spgen
             {
                 WriteOnAnswer(sw, entity, cls);
             }
-            sw.WriteLine("private:");
+            sw.WriteLine("protected:");
             sw.WriteLine("\tbool m_bSysManaged;");
             sw.WriteLine("\tCEntityBase *m_pEntityBase;");
+            sw.WriteLine("\tlinkContext m_context;");
+            sw.WriteLine("\tbool bSessionClosed;");
             sw.WriteLine("};");
         }
 

+ 1 - 1
Tool/spgen/CSGenerator.cs

@@ -21,7 +21,7 @@ namespace spgen
             "string",
         };
 
-        public override void GenerateCode(string dir, Entity entity)
+        public override void GenerateCode(string dir, Entity entity, bool inSourceDir)
         {
             
         }

+ 30 - 4
Tool/spgen/Program.cs

@@ -4,6 +4,7 @@ using System.Linq;
 using System.Text;
 using System.Xml;
 using System.IO;
+using System.Text.RegularExpressions;
 
 namespace spgen
 {
@@ -167,6 +168,24 @@ namespace spgen
         public List<EntityMsg> MsgList = new List<EntityMsg>();
         public List<ConstItem> ConstList = new List<ConstItem>();
         public Dictionary<string, string> defineDic = new Dictionary<string, string>();
+        public static Dictionary<string, string> entityDic = new Dictionary<string, string>();
+
+        public static void ReadEntityInfoFromShellIni(string shellIniPath)
+        {
+            List<string> allLines = new List<string>();
+            if (File.Exists(shellIniPath))
+            {
+                string[] msgLines = File.ReadAllLines(shellIniPath, Encoding.GetEncoding("gb2312"));
+                Regex reg = new Regex("^(.+)=.+0x(.+)$");
+                foreach (string line in msgLines)
+                {
+                    Match match = reg.Match(line);
+                    if (match.Success && !entityDic.ContainsKey(match.Groups[1].Value))
+                        entityDic.Add(match.Groups[1].Value, match.Groups[2].Value);
+
+                }
+            }
+        }
 
         public static Entity Load(string xmlfile)
         {
@@ -501,7 +520,7 @@ namespace spgen
 
     public abstract class GeneratorBase
     {
-        public abstract void GenerateCode(string dir, Entity entity);
+        public abstract void GenerateCode(string dir, Entity entity, bool inSourceDir);
     }
 
     class Program
@@ -514,22 +533,29 @@ namespace spgen
         // spgen.exe print_entity.xml
         static void Main(string[] args)
         {
-            if (args.Length != 1)
+            if (args.Length != 1 && args.Length != 2)
             {
                 Usage();
                 return;
             }
 
+            String shellIniPath = "..\\..\\..\\..\\cfg\\shell.ini";
+            if (args.Length == 2)
+                shellIniPath = args[1];
             try
             {
+                Entity.ReadEntityInfoFromShellIni(shellIniPath);
                 string xmlfile = System.IO.Path.Combine(Environment.CurrentDirectory, args[0]);
                 Entity entity = Entity.Load(xmlfile);
 
+                int index = xmlfile.LastIndexOf('\\');
+                string curPath = xmlfile.Remove(index, xmlfile.Length - index);
+
                 CPPGenerator cppgen = new CPPGenerator();
-                cppgen.GenerateCode(Environment.CurrentDirectory, entity);
+                cppgen.GenerateCode(curPath, entity, true);
 
                 CSGenerator csgen = new CSGenerator();
-                csgen.GenerateCode(Environment.CurrentDirectory, entity);
+                csgen.GenerateCode(Environment.CurrentDirectory, entity, false);
 
                 Console.WriteLine("generated ok!");
             }

+ 3 - 0
Tool/spgen/app.config

@@ -0,0 +1,3 @@
+<?xml version="1.0" encoding="utf-8"?>
+<configuration>
+<startup><supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5.2"/></startup></configuration>

+ 8 - 2
Tool/spgen/spgen.csproj

@@ -1,5 +1,5 @@
 <?xml version="1.0" encoding="utf-8"?>
-<Project ToolsVersion="4.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
+<Project ToolsVersion="12.0" DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
   <PropertyGroup>
     <Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
     <Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
@@ -10,7 +10,7 @@
     <AppDesignerFolder>Properties</AppDesignerFolder>
     <RootNamespace>spgen</RootNamespace>
     <AssemblyName>spgen</AssemblyName>
-    <TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
+    <TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
     <FileAlignment>512</FileAlignment>
     <FileUpgradeFlags>
     </FileUpgradeFlags>
@@ -31,6 +31,7 @@
     <IsWebBootstrapper>true</IsWebBootstrapper>
     <UseApplicationTrust>false</UseApplicationTrust>
     <BootstrapperEnabled>true</BootstrapperEnabled>
+    <TargetFrameworkProfile />
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
     <DebugSymbols>true</DebugSymbols>
@@ -41,6 +42,7 @@
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
     <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
+    <Prefer32Bit>false</Prefer32Bit>
   </PropertyGroup>
   <PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
     <DebugType>pdbonly</DebugType>
@@ -50,6 +52,7 @@
     <ErrorReport>prompt</ErrorReport>
     <WarningLevel>4</WarningLevel>
     <CodeAnalysisRuleSet>AllRules.ruleset</CodeAnalysisRuleSet>
+    <Prefer32Bit>false</Prefer32Bit>
   </PropertyGroup>
   <ItemGroup>
     <Reference Include="System" />
@@ -88,6 +91,9 @@
       <Install>true</Install>
     </BootstrapperPackage>
   </ItemGroup>
+  <ItemGroup>
+    <None Include="app.config" />
+  </ItemGroup>
   <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
   <!-- To modify your build process, add your task inside one of the targets below and uncomment it. 
        Other similar extension points exist, see Microsoft.Common.targets.