示例#1
0
        private void OnExecute(HttpVirtualTransportServerHandleResult r, Action OnSuccess, Action OnFailure)
        {
            if (r.OnCommand)
            {
                var CommandName = r.Command.CommandName;

                Action a = () =>
                {
                    var CurrentTime = DateTime.UtcNow;
                    Context.RequestTime = CurrentTime;
                    if (Server.ServerContext.EnableLogPerformance)
                    {
                        var sw = new Stopwatch();
                        sw.Start();
                        Action OnSuccessInner = () =>
                        {
                            sw.Stop();
                            Server.ServerContext.RaiseSessionLog(new SessionLogEntry {
                                Token = Context.SessionTokenString, RemoteEndPoint = RemoteEndPoint, Time = DateTime.UtcNow, Type = "Time", Name = CommandName, Message = String.Format("{0}μs", (sw.ElapsedTicks * 1000000) / Stopwatch.Frequency)
                            });
                            ssm.NotifyWrite(new Unit());
                            OnSuccess();
                        };
                        Action <Exception> OnFailureInner = ex =>
                        {
                            RaiseUnknownError(CommandName, ex, new StackTrace(true));
                            OnSuccess();
                        };
                        r.Command.ExecuteCommand(OnSuccessInner, OnFailureInner);
                    }
                    else
                    {
                        Action OnSuccessInner = () =>
                        {
                            ssm.NotifyWrite(new Unit());
                            OnSuccess();
                        };
                        Action <Exception> OnFailureInner = ex =>
                        {
                            RaiseUnknownError(CommandName, ex, new StackTrace(true));
                            OnSuccess();
                        };
                        r.Command.ExecuteCommand(OnSuccessInner, OnFailureInner);
                    }
                };

                ssm.AddToActionQueue(a);
            }
            else if (r.OnBadCommand)
            {
                var CommandName = r.BadCommand.CommandName;

                NumBadCommands += 1;

                // Maximum allowed bad commands exceeded.
                if (Server.MaxBadCommands != 0 && NumBadCommands > Server.MaxBadCommands)
                {
                    RaiseError(CommandName, "Too many bad commands, closing transmission channel.");
                    OnFailure();
                }
                else
                {
                    RaiseError(CommandName, "Not recognized.");
                    OnSuccess();
                }
            }
            else if (r.OnBadCommandLine)
            {
                var CommandLine = r.BadCommandLine.CommandLine;

                NumBadCommands += 1;

                // Maximum allowed bad commands exceeded.
                if (Server.MaxBadCommands != 0 && NumBadCommands > Server.MaxBadCommands)
                {
                    RaiseError("", String.Format(@"""{0}"": Too many bad commands, closing transmission channel.", CommandLine));
                    OnFailure();
                }
                else
                {
                    RaiseError("", String.Format(@"""{0}"":  recognized.", CommandLine));
                    OnSuccess();
                }
            }
            else
            {
                throw new InvalidOperationException();
            }
        }
        public HttpVirtualTransportServerHandleResult Handle(JObject CommandObject)
        {
            var jo = CommandObject;

            if (jo["commandName"] == null || jo["commandName"].Type != JTokenType.String || (jo["commandHash"] != null && jo["commandHash"].Type != JTokenType.String) || jo["parameters"] == null || jo["parameters"].Type != JTokenType.String)
            {
                return(HttpVirtualTransportServerHandleResult.CreateBadCommandLine(new HttpVirtualTransportServerHandleResultBadCommandLine {
                    CommandLine = jo.ToString(Formatting.None)
                }));
            }
            var CommandName = StringFromJson(jo["commandName"]);
            var CommandHash = Optional <UInt32> .Empty;

            if (jo["commandHash"] != null)
            {
                UInt32 ch;
                if (!UInt32.TryParse(StringFromJson(jo["commandHash"]), System.Globalization.NumberStyles.HexNumber, System.Globalization.CultureInfo.InvariantCulture, out ch))
                {
                    return(HttpVirtualTransportServerHandleResult.CreateBadCommandLine(new HttpVirtualTransportServerHandleResultBadCommandLine {
                        CommandLine = jo.ToString(Formatting.None)
                    }));
                }
                CommandHash = ch;
            }
            var Parameters = StringFromJson(jo["parameters"]);

            HttpVirtualTransportServerHandleResult ret;

            if ((CommandHash.OnSome ? ss.HasCommand(CommandName, CommandHash.Some) : ss.HasCommand(CommandName)) && (CheckCommandAllowed != null ? CheckCommandAllowed(CommandName) : true))
            {
                if (CommandHash.OnSome)
                {
                    ret = HttpVirtualTransportServerHandleResult.CreateCommand(new HttpVirtualTransportServerHandleResultCommand
                    {
                        CommandName    = CommandName,
                        ExecuteCommand = (OnSuccess, OnFailure) =>
                        {
                            Action <String> OnSuccessInner = OutputParameters =>
                            {
                                var rjo            = new JObject();
                                rjo["commandName"] = new JValue(CommandName);
                                rjo["commandHash"] = new JValue(CommandHash.Some.ToString("X8", System.Globalization.CultureInfo.InvariantCulture));
                                rjo["parameters"]  = new JValue(OutputParameters);
                                lock (c.WriteBufferLockee)
                                {
                                    c.WriteBuffer.Add(rjo);
                                }
                                OnSuccess();
                            };
                            ss.ExecuteCommand(CommandName, CommandHash.Some, Parameters, OnSuccessInner, OnFailure);
                        }
                    });
                }
                else
                {
                    ret = HttpVirtualTransportServerHandleResult.CreateCommand(new HttpVirtualTransportServerHandleResultCommand
                    {
                        CommandName    = CommandName,
                        ExecuteCommand = (OnSuccess, OnFailure) =>
                        {
                            Action <String> OnSuccessInner = OutputParameters =>
                            {
                                var rjo            = new JObject();
                                rjo["commandName"] = new JValue(CommandName);
                                rjo["parameters"]  = new JValue(OutputParameters);
                                lock (c.WriteBufferLockee)
                                {
                                    c.WriteBuffer.Add(rjo);
                                }
                                OnSuccess();
                            };
                            ss.ExecuteCommand(CommandName, Parameters, OnSuccessInner, OnFailure);
                        }
                    });
                }
            }
            else
            {
                ret = HttpVirtualTransportServerHandleResult.CreateBadCommand(new HttpVirtualTransportServerHandleResultBadCommand {
                    CommandName = CommandName
                });
            }
            return(ret);
        }