Пример #1
0
        private static TestScriptValue Eval(TestScriptExpressionList expList, TestScriptEvalContext context)
        {
            TestScriptValue list = TestScriptValue.CreateList();

            foreach (TestScriptExpression expression in expList)
            {
                list.AddItem(Eval(expression, context));
            }

            return(list);
        }
Пример #2
0
        private static void ParseResponse(string module, byte[] data, ref int parseOffset, ProtoSpecSubset format, TestScriptValue recvValue)
        {
            foreach (ProtoSpecColumn column in format.Columns)
            {
                switch (column.ColumnType)
                {
                case ProtoSpecColumnType.Byte:
                {
                    byte value = data[parseOffset++];

                    recvValue.AddProperty(
                        column.Name,
                        new TestScriptValue(value, TestScriptValueType.Byte)
                        );
                }
                break;

                case ProtoSpecColumnType.Short:
                {
                    byte[] bytes = new byte[2];

                    bytes[1] = data[parseOffset++];
                    bytes[0] = data[parseOffset++];

                    short value = BitConverter.ToInt16(bytes, 0);

                    recvValue.AddProperty(
                        column.Name,
                        new TestScriptValue(value, TestScriptValueType.Short)
                        );
                }
                break;

                case ProtoSpecColumnType.Int:
                {
                    byte[] bytes = new byte[4];

                    bytes[3] = data[parseOffset++];
                    bytes[2] = data[parseOffset++];
                    bytes[1] = data[parseOffset++];
                    bytes[0] = data[parseOffset++];

                    int value = BitConverter.ToInt32(bytes, 0);

                    recvValue.AddProperty(
                        column.Name,
                        new TestScriptValue(value, TestScriptValueType.Integer)
                        );
                }
                break;

                case ProtoSpecColumnType.Long:
                {
                    byte[] bytes = new byte[8];

                    bytes[7] = data[parseOffset++];
                    bytes[6] = data[parseOffset++];
                    bytes[5] = data[parseOffset++];
                    bytes[4] = data[parseOffset++];
                    bytes[3] = data[parseOffset++];
                    bytes[2] = data[parseOffset++];
                    bytes[1] = data[parseOffset++];
                    bytes[0] = data[parseOffset++];

                    long value = BitConverter.ToInt64(bytes, 0);

                    recvValue.AddProperty(
                        column.Name,
                        new TestScriptValue(value, TestScriptValueType.Long)
                        );
                }
                break;

                case ProtoSpecColumnType.String:
                {
                    byte[] packhead = new byte[2];

                    packhead[1] = data[parseOffset++];
                    packhead[0] = data[parseOffset++];

                    short len = BitConverter.ToInt16(packhead, 0);

                    string text = Encoding.UTF8.GetString(data, parseOffset, len);

                    parseOffset += len;

                    recvValue.AddProperty(
                        column.Name,
                        new TestScriptValue(text, TestScriptValueType.String)
                        );
                }
                break;

                case ProtoSpecColumnType.List:
                {
                    byte[] packhead = new byte[2];

                    packhead[1] = data[parseOffset++];
                    packhead[0] = data[parseOffset++];

                    short len = BitConverter.ToInt16(packhead, 0);

                    TestScriptValue list = TestScriptValue.CreateList();

                    for (int i = 0; i < len; i++)
                    {
                        TestScriptValue item = TestScriptValue.CreateObject();

                        ParseResponse(module, data, ref parseOffset, column.Format, item);

                        list.AddItem(item);
                    }

                    recvValue.AddProperty(column.Name, list);
                }
                break;

                case ProtoSpecColumnType.Enum:
                {
                    byte value = data[parseOffset++];

                    recvValue.AddProperty(
                        column.Name,
                        TestScriptValue.CreateEnum(module, column.Values.GetNameByValue(value), value)
                        );
                }
                break;
                }
            }
        }
Пример #3
0
        public static TestScriptValue Do(TestScriptDocument script, TestScriptEvalContext context)
        {
            Eval(script.Statements, context);

            Socket socket = context.Sock;

            TestScriptValue list = TestScriptValue.CreateList();

            System.Threading.Thread.Sleep(1000);

            while (socket.Available > 0)
            {
                byte[] head = new byte[4];

                int received = 0;

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

                Array.Reverse(head);

                int length = BitConverter.ToInt32(head, 0);

                received = 0;

                byte[] data = new byte[length];

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

                ProtoSpecModule recvModule = context.ProtoSpec.Modules.GetById(data[0].ToString());

                if (recvModule == null)
                {
                    Error("数据接收失败,无法找到ID为“" + data[0] + "”的模块");
                }

                ProtoSpecAction recvAction = recvModule.Actions.GetById(data[1].ToString());

                if (recvAction == null)
                {
                    Error("数据接收失败,在模块“" + recvModule.Name + "”中无法找到ID位“" + data[1] + "”的操作");
                }

                if (recvAction.Output.Columns.Count > 0)
                {
                    int parseOffset = 2;

                    TestScriptValue recvValue = TestScriptValue.CreateObject();

                    ParseResponse(recvModule.Name, data, ref parseOffset, recvAction.Output, recvValue);

                    TestScriptValue actionResult = TestScriptValue.CreateActionResult(recvModule.Name, recvAction.Name, recvValue);

                    list.AddItem(actionResult);
                }
            }

            return(list);
        }