예제 #1
0
        public override object HandleMethodCall(IMethodCallMessage mcm)
        {
            if (!serverCache.TryGetValue(mcm.MethodBase, out var methodId))
            {
                methodId = serverCache[mcm.MethodBase] = Guid.NewGuid().ToString();
            }
            var methodCallCommand = new MethodCallCommand {
                MethodName = mcm.MethodName, Arguments = mcm.Args, MethodId = methodId
            };

            var task = _callResultAwaiter.Where(it => it.AnswerTo == methodCallCommand.Number).ToTask();

            _messagePublisher.Publish(methodCallCommand);
            var result = task.Result;

            if (result == null)
            {
                throw new SandboxTerminatedException();
            }
            if (result.Exception != null)
            {
                throw result.Exception;
            }
            return(result.Result);
        }
예제 #2
0
        public void Evaluate(IEnumerable <object> statements)
        {
            BeginScope();

            foreach (var statement in statements)
            {
                if (statement is Branch)
                {
                    Branch branch = (Branch)statement;

                    var branchCommand = new BranchCommand(branch.Left, branch.Right);
                    branchCommand.Run(this);
                }
                else if (statement is EndBranch)
                {
                    var endBranchCommand = new EndBranchCommand();
                    endBranchCommand.Run(this);
                }

                /* Todo: This breaks the pattern because we can't roll
                 *  up these commands into a list before looping through to run them.
                 */
                else if (SkipLines)
                {
                    var doNothingCommand = new DoNothingCommand();
                    doNothingCommand.Run(this);
                }

                else if (statement is CreateVariable)
                {
                    var createVariable = (CreateVariable)statement;

                    var createVariableCommand = new CreateVariableCommand(createVariable.Name,
                                                                          createVariable.Value);

                    createVariableCommand.Run(this);
                }
                else if (statement is Assignment)
                {
                    var assignment = (Assignment)statement;

                    var assignmentCommand = new AssignmentCommand(assignment.Name,
                                                                  assignment.Value);

                    assignmentCommand.Run(this);
                }
                else if (statement is MethodCall)
                {
                    var methodCall = (MethodCall)statement;

                    var methodCallCommand = new MethodCallCommand("print",
                                                                  methodCall.Arguments);

                    methodCallCommand.Run(this);
                }
            }

            EndScope();
        }
예제 #3
0
        public Message Deserialize(byte[] bytes)
        {
            using (var stream = new MemoryStream(bytes))
            {
                switch (stream.ReadByte())
                {
                case 1:
                    var createObjectOfTypeCommand = new CreateObjectOfTypeCommand(stream.ReadInt());
                    createObjectOfTypeCommand.AssemblyPath = stream.ReadString();
                    createObjectOfTypeCommand.TypeFullName = stream.ReadString();
                    return(createObjectOfTypeCommand);

                case 2:
                    var invokeCommand = new EventInvokeCommand(stream.ReadInt());
                    invokeCommand.EventName = stream.ReadString();
                    invokeCommand.Arguments = ( object[] )stream.ReadObject(_formatter);
                    return(invokeCommand);

                case 3:
                    var methodCallCommand = new MethodCallCommand(stream.ReadInt());
                    methodCallCommand.MethodName = stream.ReadString();
                    methodCallCommand.MethodId   = stream.ReadString();
                    methodCallCommand.Arguments  = ( object[] )stream.ReadObject(_formatter);
                    return(methodCallCommand);

                case 4:
                    var methodCallResultAnswer = new MethodCallResultAnswer(stream.ReadInt());
                    methodCallResultAnswer.AnswerTo  = stream.ReadInt();
                    methodCallResultAnswer.Result    = stream.ReadObject(_formatter);
                    methodCallResultAnswer.Exception = ( Exception )stream.ReadObject(_formatter);
                    return(methodCallResultAnswer);

                case 5:
                    var subscribeToEventCommand = new SubscribeToEventCommand(stream.ReadInt());
                    subscribeToEventCommand.EventName = stream.ReadString();
                    return(subscribeToEventCommand);

                case 6:
                    return(new UnexpectedExceptionMessage(stream.ReadInt())
                    {
                        Exception = ( Exception )stream.ReadObject(_formatter)
                    });

                case 7:
                    return(new UnsubscribeFromEventCommand(stream.ReadInt())
                    {
                        EventName = stream.ReadString()
                    });

                default:
                    throw new NotImplementedException();
                }
            }
        }