Beispiel #1
0
        public void Reset(string traceId, bool isIn, byte[] packet)
        {
            this.isIn = isIn;

            this.traceId = traceId;

            string moduleId = packet[0].ToString();
            string actionId = packet[1].ToString();

            foreach (ProtoSpecModule module in ProtoSpecModules)
            {
                if (module.ModuleId == moduleId)
                {
                    ProtoSpecAction action = module.Actions.GetById(actionId);

                    if (action != null)
                    {
                        pModule = module;
                        pAction = action;
                    }
                }
            }

            this.Text       = traceId + " " + (pModule != null ? pModule.Name : moduleId.ToString()) + ":" + (pAction != null ? pAction.Name : actionId.ToString());
            this.ImageIndex = isIn ? 0 : 1;

            this.data = packet;
        }
Beispiel #2
0
        void ToolStripButton2Click(object sender, EventArgs e)
        {
            TestScriptParser parser = new TestScriptParser(scintilla1.Text, 0);

            TestScriptDocument document = parser.Parse();

            if (OpenConnection())
            {
                TestScriptEvalContext context = new TestScriptEvalContext(protoSpecDocument, socket);

                TestScriptValue result = TestScriptEval.Do(document, context);

                treeView4.Nodes.Clear();

                foreach (TestScriptValue item in (TestScriptValueList)result.Value)
                {
                    TreeNode node = treeView4.Nodes.Add(item.ModuleName + ":" + item.ActionName);

                    TestScriptValue value = (TestScriptValue)item.Value;

                    ProtoSpecModule module = protoSpecDocument.Modules.GetByName(item.ModuleName, true);

                    ProtoSpecAction action = module.Actions.GetByName(item.ActionName, true);

                    ParseResponse2(value, action.Output, node.Nodes);
                }
            }
        }
Beispiel #3
0
        void ProtoSpecSubsetTree(ProtoSpecModule module, ProtoSpecAction action, TreeNode parent, ProtoSpecSubset subset)
        {
            foreach (ProtoSpecColumn column in subset.Columns)
            {
                string columnType = ColumnTypeToString(column.ColumnType);

                string className = "";

                if (column.ClassName != null)
                {
                    if (column.ClassModule != null)
                    {
                        className = "<" + column.ClassModule + "." + column.ClassName + ">";
                    }
                    else
                    {
                        className = "<" + column.ClassName + ">";
                    }
                }

                TreeNode columnNode = parent.Nodes.Add(column.Name + " : " + columnType + className);

                if (column.Format != null)
                {
                    ProtoSpecSubsetTree(module, action, columnNode, column.Format);
                }
                else if (column.Values != null)
                {
                    foreach (ProtoSpecEnumValue value in column.Values)
                    {
                        columnNode.Nodes.Add(value.Name + " = " + value.Value);
                    }
                }
            }
        }
Beispiel #4
0
        void SendData(ProtoSpecAction action)
        {
            if (OpenConnection() == false)
            {
                return;
            }

            treeView2.Nodes.Clear();

            MemoryStream stream = new MemoryStream();

            stream.WriteByte(0);
            stream.WriteByte(0);

            byte moduleId = byte.Parse(action.ParentModule.ModuleId);
            byte actionId = byte.Parse(action.ActionId);

            stream.WriteByte(moduleId);
            stream.WriteByte(actionId);

            foreach (ProtoSpecColumn column in action.Input.Columns)
            {
                TextBox textbox = (TextBox)panel1.Controls["tb_" + column.Name];

                switch (column.ColumnType)
                {
                case ProtoSpecColumnType.Enum:
                case ProtoSpecColumnType.Byte:
                    stream.WriteByte(byte.Parse(textbox.Text));
                    break;

                case ProtoSpecColumnType.Short:
                {
                    short value = short.Parse(textbox.Text);

                    byte[] bytes = BitConverter.GetBytes(value);

                    stream.WriteByte(bytes[1]);
                    stream.WriteByte(bytes[0]);
                }
                break;

                case ProtoSpecColumnType.Int:
                {
                    int value = int.Parse(textbox.Text);

                    byte[] bytes = BitConverter.GetBytes(value);

                    stream.WriteByte(bytes[3]);
                    stream.WriteByte(bytes[2]);
                    stream.WriteByte(bytes[1]);
                    stream.WriteByte(bytes[0]);
                }
                break;

                case ProtoSpecColumnType.Long:
                {
                    long value = long.Parse(textbox.Text);

                    byte[] bytes = BitConverter.GetBytes(value);

                    stream.WriteByte(bytes[7]);
                    stream.WriteByte(bytes[6]);
                    stream.WriteByte(bytes[5]);
                    stream.WriteByte(bytes[4]);
                    stream.WriteByte(bytes[3]);
                    stream.WriteByte(bytes[2]);
                    stream.WriteByte(bytes[1]);
                    stream.WriteByte(bytes[0]);
                }
                break;

                case ProtoSpecColumnType.String:
                {
                    byte[] bytes = Encoding.UTF8.GetBytes(textbox.Text);

                    short length = (short)bytes.Length;

                    byte[] head = BitConverter.GetBytes(length);

                    stream.WriteByte(head[1]);
                    stream.WriteByte(head[0]);

                    stream.Write(bytes, 0, bytes.Length);
                }
                break;

                case ProtoSpecColumnType.List:
                    MessageBox.Show("请求参数中不支持list类型的数据");
                    break;
                }
            }

            byte[] buffer = stream.ToArray();

            byte[] packHead = BitConverter.GetBytes((short)buffer.Length - 2);

            buffer[0] = packHead[1];
            buffer[1] = packHead[0];

            socket.Send(buffer);

            stream.Close();
            stream.Dispose();

            if (action.Output.Columns.Count > 0)
            {
RECV:
                byte[] head = new byte[2];

                int received = 0;

                while (received != 2)
                {
                    received += socket.Receive(head);
                }

                byte temp = head[0];

                head[0] = head[1];
                head[1] = temp;

                short length = BitConverter.ToInt16(head, 0);

                received = 0;

                byte[] data = new byte[length];

                while (received != length)
                {
                    received += socket.Receive(data, received, length - received, SocketFlags.None);
                }

                if (data[0] == moduleId && data[1] == actionId)
                {
                    int parseOffset = 2;

                    ParseResponse(data, ref parseOffset, action.Output, treeView2.Nodes);
                }
                else
                {
                    goto RECV;
                }
            }
        }
Beispiel #5
0
        void TreeView1NodeMouseDoubleClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            if (e.Node.Level != 1)
            {
                return;
            }

            ProtoSpecAction action = (ProtoSpecAction)e.Node.Tag;             //actionDict[e.Node.FullPath];

            panel1.Controls.Clear();

            int label_x  = 10;
            int label_y  = 14;
            int submit_y = label_y;

            foreach (ProtoSpecColumn column in action.Input.Columns)
            {
                Label label = new Label();

                label.Text   = column.Name;
                label.Left   = label_x;
                label.Top    = label_y;
                label.Height = 18;
                label.Width  = panel1.Width - 20;
                label.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;

                panel1.Controls.Add(label);

                TextBox textbox = new TextBox();

                textbox.Name   = "tb_" + column.Name;
                textbox.Left   = label_x;
                textbox.Top    = label.Top + label.Height;
                textbox.Width  = panel1.Width - 20;
                textbox.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;

                label_y += label.Height + textbox.Height + 10;

                panel1.Controls.Add(textbox);

                submit_y = textbox.Top + textbox.Height + 20;
            }

            Button submit = new Button();

            submit.Text   = "发送请求";
            submit.Left   = label_x;
            submit.Top    = submit_y;
            submit.Width  = panel1.Width - 20;
            submit.Height = 34;
            submit.Anchor = AnchorStyles.Left | AnchorStyles.Top | AnchorStyles.Right;

            panel1.Controls.Add(submit);

            submit.Click += delegate(object sender2, EventArgs e2)
            {
                try
                {
                    SendData(action);
                }
                catch (SocketException socketExp)
                {
                    CloseConnection();

                    MessageBox.Show(socketExp.Message, "出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, "出错", MessageBoxButtons.OK, MessageBoxIcon.Error);
                }
            };
        }
Beispiel #6
0
        public static bool GenerateErlangCode2(bool isConsole, bool developMode)
        {
            List <ProtoSpecModule> moduleList = GetModuleList(isConsole);

            if (moduleList == null)
            {
                return(false);
            }

#if DEBUG
            string includeDir2 = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, "..\\..\\..\\..\\..\\server-new\\include"));
            string serverDir   = Path.GetFullPath(Path.Combine(Environment.CurrentDirectory, "..\\..\\..\\..\\..\\server-new"));
#else
            string includeDir2 = Path.Combine(Environment.CurrentDirectory, "server-new\\include");
            string serverDir   = Path.Combine(Environment.CurrentDirectory, "server-new");
#endif
            includeDir2 = FixPath(includeDir2);
            serverDir   = FixPath(serverDir);

            StringBuilder router = new StringBuilder();

            router.Append("-module(game_router).").AppendLine();
            router.Append("-export([route_request/2]).").AppendLine();
            router.Append("-include(\"game.hrl\").").AppendLine().AppendLine();

            router.Append("route_request(<<Module:8/unsigned, Action:8/unsigned, Args/binary>>, State) -> ").AppendLine();

            if (developMode)
            {
                //router.Append("    io:format(\"Call ~p --> ~p~n\", [Module, Action]),").AppendLine();
                router.Append("    {Time1, _} = statistics(runtime),").AppendLine();
                router.Append("    {Time2, _} = statistics(wall_clock),").AppendLine();
                router.Append("    {M, A, NewState} =");
            }

            router.Append("    route_request(Module, Action, Args, State)");

            if (developMode)
            {
                router.Append(",").AppendLine();
                router.Append("    {Time3, _} = statistics(runtime),").AppendLine();
                router.Append("    {Time4, _} = statistics(wall_clock),").AppendLine();
                router.Append("    Sec1 = (Time3 - Time1) / 1000.0,").AppendLine();
                router.Append("    Sec2 = (Time4 - Time2) / 1000.0,").AppendLine();
                router.Append("    game_prof_srv:set_info(M, A, Sec1, Sec2),").AppendLine();
                router.Append("    NewState.").AppendLine();
            }
            else
            {
                router.Append(".").AppendLine();
            }

            int n = 0;

            int headIndex = 0;

            int listParserCount = 0;
            int typeParserCount = 0;

            List <string> listParsers = new List <string>();
            List <string> typeParsers = new List <string>();

            foreach (ProtoSpecModule module in moduleList)
            {
                router.Append("route_request(").Append(module.ModuleId).Append(", _Action, _Args0, _State) -> ").AppendLine();

                if (!developMode && Convert.ToInt16(module.ModuleId) == 99)
                {
                    //允许访问后台接口IP
                    string admin_ips = "[\n" +
                                       "        {10, 182, 1, 71}, {10, 182, 1, 72}, {10,190,233,245}, {10,190,233,235}, %srv \n" +
                                       "        {10, 182, 0, 38}, {10, 182, 0, 39}, {10,207,251,82}  %web \n" +
                                       "    ]";

                    router.Append("    {ok, {Address, _Port}} = inet:peername(_State #client_state.sock),").AppendLine();
                    router.Append("    case lists:member(Address, ").Append(admin_ips).Append(") of").AppendLine();
                    router.Append("        true -> ok;\n        _ -> exit({invalid_ip, Address})\n    end,").AppendLine().AppendLine();
                }

                router.Append("    case _Action of").AppendLine();

                for (int i = 0; i < module.Actions.Count; i++)
                {
                    ProtoSpecAction action = module.Actions[i];

                    router.Append("        ").Append(action.ActionId).Append(" -> ").AppendLine();

                    if (action.Input.Columns.Count > 0)
                    {
                        int n2        = 0;
                        int argsCount = 0;
                        int lenCount  = 1;
                        int sizeCount = 1;

ParseInput:

                        router.Append("            <<");

                        bool            inList     = false;
                        bool            inType     = false;
                        ProtoSpecColumn listColumn = null;
                        ProtoSpecColumn typeColumn = null;

                        for (int j = n2; j < action.Input.Columns.Count; j++)
                        {
                            ProtoSpecColumn column = action.Input.Columns[j];

                            switch (column.ColumnType)
                            {
                            case ProtoSpecColumnType.Byte:
                                router.Append(FormatName(column.Name)).Append(":8/signed");
                                break;

                            case ProtoSpecColumnType.Enum:
                                router.Append(FormatName(column.Name)).Append(":8/unsigned");
                                break;

                            case ProtoSpecColumnType.Short:
                                router.Append(FormatName(column.Name)).Append(":16/signed");
                                break;

                            case ProtoSpecColumnType.Int:
                                router.Append(FormatName(column.Name)).Append(":32/signed");
                                break;

                            case ProtoSpecColumnType.Long:
                                router.Append(FormatName(column.Name)).Append(":64/signed");
                                break;

                            case ProtoSpecColumnType.String:
                                router.Append("Len").Append(lenCount).Append(":16/unsigned, ").Append(FormatName(column.Name)).Append(":Len").Append(lenCount).Append("/binary");
                                lenCount += 1;
                                break;

                            case ProtoSpecColumnType.List:
                                router.Append("Size").Append(sizeCount).Append(":16/unsigned, ").Append(FormatName(column.Name)).Append("Bin/binary");
                                sizeCount += 1;
                                inList     = true;
                                listColumn = column;
                                break;

                            case ProtoSpecColumnType.TypeOf:
                                router.Append(FormatName(column.Name)).Append("Bin/binary");
                                sizeCount += 1;
                                inType     = true;
                                typeColumn = column;
                                break;
                            }

                            n2 += 1;

                            if (inList || inType)
                            {
                                break;
                            }

                            if (n2 < action.Input.Columns.Count)
                            {
                                router.Append(", ");
                            }
                        }

                        router.Append(">> = _Args").Append(argsCount).Append(",").AppendLine();

                        if (inList)
                        {
                            argsCount += 1;

                            int newParserId = GenerateListParser(ref listParserCount, ref typeParserCount, listParsers, typeParsers, listColumn.Format);

                            router.Append("            {").Append(FormatName(listColumn.Name)).Append(", _Args").Append(argsCount).Append("}").Append(" = list_parser_").Append(newParserId).Append("(").Append("Size").Append(sizeCount - 1).Append(", ").Append(FormatName(listColumn.Name)).Append("Bin, []),").AppendLine();

                            if (n2 < action.Input.Columns.Count)
                            {
                                goto ParseInput;
                            }
                        }
                        else if (inType)
                        {
                            argsCount += 1;

                            int newParserId = GenerateTypeParser(ref listParserCount, ref typeParserCount, listParsers, typeParsers, typeColumn.Format);

                            router.Append("            {").Append(FormatName(typeColumn.Name)).Append(", _Args").Append(argsCount).Append("}").Append(" = type_parser_").Append(newParserId).Append("(").Append(FormatName(typeColumn.Name)).Append("Bin),").AppendLine();

                            if (n2 < action.Input.Columns.Count)
                            {
                                goto ParseInput;
                            }
                        }
                    }

                    if (developMode)
                    {
                        router.Append("            NewState =");
                    }

                    router.Append("            api_").Append(module.Name).Append(":").Append(action.Name).Append("(");

                    foreach (ProtoSpecColumn column in action.Input.Columns)
                    {
                        if (column.ColumnType == ProtoSpecColumnType.String)
                        {
                            router.Append("binary_to_list(").Append(FormatName(column.Name)).Append(")");
                        }
                        else
                        {
                            router.Append(FormatName(column.Name));
                        }

                        router.Append(", ");
                    }

                    router.Append("_State)");

                    if (developMode)
                    {
                        router.Append(",").AppendLine();
                        //router.Append("            io:format(\"~p : ~p~n\", [").Append(module.Name).Append(", ").Append(action.Name).Append("]),").AppendLine();
                        router.Append("            {").Append(module.Name).Append(", ").Append(action.Name).Append(", NewState}");
                    }

                    if (i < module.Actions.Count - 1)
                    {
                        router.AppendLine(";");
                    }
                    else
                    {
                        router.AppendLine();
                    }
                }
                router.Append("    end");

                if (n < moduleList.Count - 1)
                {
                    router.AppendLine(";");
                }
                else
                {
                    router.AppendLine(".");
                }

                router.AppendLine();


                StringBuilder head = new StringBuilder();

                foreach (ProtoSpecEnumValue value in module.EnumValues)
                {
                    head.AppendFormat(
                        "-define({0}, {1}).", value.Name.ToUpper(), value.Value
                        ).AppendLine();
                }

                StringBuilder code = new StringBuilder();

                code.AppendLine();

                int autoFunCount = 0;

                List <string> autoFunList = new List <string>();

                int level = 1;

                code.Append("-module(api_" + module.Name + "_out).").AppendLine();

                List <ProtoSpecAction> outputActions = new List <ProtoSpecAction>();

                for (int i = 0; i < module.Actions.Count; i++)
                {
                    if (module.Actions[i].Output.Columns.Count == 0)
                    {
                        continue;
                    }

                    outputActions.Add(module.Actions[i]);
                }

                code.Append("-export([").AppendLine();

                for (int i = 0; i < outputActions.Count; i++)
                {
                    code.Append("    ").Append(outputActions[i].Name).Append("/1");

                    if (i < outputActions.Count - 1)
                    {
                        code.AppendLine(",");
                    }
                    else
                    {
                        code.AppendLine();
                    }
                }

                code.Append("]).").AppendLine();
                code.AppendLine();

                for (int i = 0; i < outputActions.Count; i++)
                {
                    code.Append(
                        GenerateErlangFunction2(
                            module.ModuleId,
                            outputActions[i].ActionId,
                            outputActions[i].Name,
                            outputActions[i].Output,
                            ref autoFunCount,
                            ref autoFunList,
                            ref level,
                            false,
                            developMode
                            )
                        );

                    code.AppendLine();
                }

                code.AppendLine();
                code.AppendLine();

                for (int i = 0; i < autoFunList.Count; i++)
                {
                    code.Append(autoFunList[i]);
                }

                using (StreamWriter writer = new StreamWriter(FixPath(Path.Combine(includeDir2, "gen\\api_" + module.Name + ".hrl")), false))
                {
                    writer.Write(head.ToString());

                    writer.WriteLine();
                }

                using (StreamWriter writer = new StreamWriter(FixPath(Path.Combine(serverDir, "src\\gen\\api_" + module.Name + "_out.erl")), false))
                {
                    writer.Write(code.ToString().Substring(headIndex));
                }

                n += 1;
            }

            for (int i = listParsers.Count - 1; i >= 0; i--)
            {
                router.AppendLine(listParsers[i]);
            }

            for (int i = typeParsers.Count - 1; i >= 0; i--)
            {
                router.AppendLine(typeParsers[i]);
            }

            using (StreamWriter writer = new StreamWriter(FixPath(Path.Combine(serverDir, "src\\gen\\game_router.erl")), false))
            {
                writer.Write(router.ToString());
            }

            Info("服务端代码生成完毕");

            return(true);
        }