Beispiel #1
0
 private static void Eval(TestScriptStatementList statements, TestScriptEvalContext context)
 {
     foreach (TestScriptStatement statement in statements)
     {
         Eval(statement.Expression, context);
     }
 }
Beispiel #2
0
        private static TestScriptValue Eval(TestScriptExpression expression, TestScriptEvalContext context)
        {
            switch (expression.Type)
            {
            case TestScriptExpressionType.String:
                return(new TestScriptValue(((TestScriptString)expression).Text, TestScriptValueType.String));

            case TestScriptExpressionType.Integer:
                return(new TestScriptValue(((TestScriptInteger)expression).Value, TestScriptValueType.Long));

            case TestScriptExpressionType.EnumValue:
            {
                TestScriptEnumValue enumExp = (TestScriptEnumValue)expression;

                ProtoSpecModule module = context.ProtoSpec.Modules.GetByName(enumExp.Module, true);

                if (module == null)
                {
                    Error("上下文中并不包含模块“" + module + "”的定义");
                }

                long value = module.EnumValues.GetValueByName(enumExp.Name, true);

                if (value < 0)
                {
                    Error("模块“" + module + "”中并不包含枚举值“" + enumExp.Name + "”的定义");
                }

                return(TestScriptValue.CreateEnum(enumExp.Module, enumExp.Name, value));
            }

            case TestScriptExpressionType.FunctionCall:
            {
                TestScriptFunctionCall funExp = (TestScriptFunctionCall)expression;

                ProtoSpecModule module = context.ProtoSpec.Modules.GetByName(funExp.Module, true);

                if (module == null)
                {
                    Error("上下文中不存在模块“" + module + "”");
                }

                ProtoSpecAction action = module.Actions.GetByName(funExp.Function, true);

                if (action == null)
                {
                    Error("模块“" + module + "”中不存在操作“" + funExp.Function + "”");
                }

                return(SendData(context, action, funExp.ParamList));
            }
            }

            return(null);
        }
Beispiel #3
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);
        }
Beispiel #4
0
        public static void Clean(TestScriptEvalContext context, int delayMilliSecond)
        {
            System.Threading.Thread.Sleep(delayMilliSecond);

            byte[] buffer = new byte[1024];

            Socket socket = context.Sock;

            while (socket.Available > 0)
            {
                socket.Receive(buffer);
            }
        }
Beispiel #5
0
        private static TestScriptValue SendData(TestScriptEvalContext context, ProtoSpecAction action, TestScriptExpressionList paramList)
        {
            Socket socket = context.Sock;

            MemoryStream stream = new MemoryStream();

            stream.WriteByte(0);
            stream.WriteByte(0);
            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);

            int i = 0;

            #region Generate Data

            foreach (ProtoSpecColumn column in action.Input.Columns)
            {
                TestScriptValue param = Eval(paramList[i], context);

                i++;

                switch (column.ColumnType)
                {
                case ProtoSpecColumnType.Enum:
                    stream.WriteByte((byte)(long)param.Value);
                    break;

                case ProtoSpecColumnType.Byte:
                    stream.WriteByte((byte)(long)param.Value);
                    break;

                case ProtoSpecColumnType.Short:
                {
                    short value = (short)(long)param.Value;

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

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

                case ProtoSpecColumnType.Int:
                {
                    int value = (int)(long)param.Value;

                    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)param.Value;

                    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((string)param.Value);

                    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;
                }
            }

            #endregion

            byte[] buffer = stream.ToArray();

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

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

            socket.Send(buffer);

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

            return(null);
        }
Beispiel #6
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);
        }
Beispiel #7
0
        private static TestScriptValue Eval(TestScriptExpression expression, TestScriptEvalContext context)
        {
            switch (expression.Type)
            {
            case TestScriptExpressionType.String:
                return(new TestScriptValue(((TestScriptString)expression).Text, TestScriptValueType.String));

            case TestScriptExpressionType.Integer:
                return(new TestScriptValue(((TestScriptInteger)expression).Value, TestScriptValueType.Long));

            case TestScriptExpressionType.EnumValue:
            {
                TestScriptEnumValue enumExp = (TestScriptEnumValue)expression;

                ProtoSpecModule module = context.ProtoSpec.Modules.GetByName(enumExp.Module, true);

                if (module == null)
                {
                    Error("上下文中并不包含模块“" + module + "”的定义");
                }

                long value = module.EnumValues.GetValueByName(enumExp.Name, true);

                if (value < 0)
                {
                    Error("模块“" + module + "”中并不包含枚举值“" + enumExp.Name + "”的定义");
                }

                return(TestScriptValue.CreateEnum(enumExp.Module, enumExp.Name, value));
            }

            case TestScriptExpressionType.FunctionCall:
            {
                TestScriptFunctionCall funExp = (TestScriptFunctionCall)expression;

                if (funExp.Module == "proto")
                {
                    switch (funExp.Function)
                    {
                    case "clean":
                        if (funExp.ParamList.Count == 0)
                        {
                            Clean(context, 1000);
                        }
                        else if (funExp.ParamList.Count == 1 && funExp.ParamList[0].Type == TestScriptExpressionType.Integer)
                        {
                            Clean(context, (int)((TestScriptInteger)funExp.ParamList[0]).Value);
                        }
                        else
                        {
                            Error("函数“proto:clean()”的参数个数应该为0个或1个");
                        }
                        break;
                    }

                    return(null);
                }
                else
                {
                    ProtoSpecModule module = context.ProtoSpec.Modules.GetByName(funExp.Module, true);

                    if (module == null)
                    {
                        Error("上下文中不存在模块“" + module + "”");
                    }

                    ProtoSpecAction action = module.Actions.GetByName(funExp.Function, true);

                    if (action == null)
                    {
                        Error("模块“" + module + "”中不存在操作“" + funExp.Function + "”");
                    }

                    return(SendData(context, action, funExp.ParamList));
                }
            }
            }

            return(null);
        }
Beispiel #8
0
        public static void Run(TestScriptDocument script, TestScriptEvalContext context)
        {
            Eval(script.Statements, context);

            #region OLD

            /*
             * Socket socket = context.Sock;
             *
             * TestScriptValue list = TestScriptValue.CreateList();
             *
             * System.Threading.Thread.Sleep(1000);
             *
             * while (socket.Available > 0)
             * {
             *  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);
             *  }
             *
             *  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;
             */
            #endregion
        }