コード例 #1
0
ファイル: UdpSession.cs プロジェクト: blockspacer/niveum
        private void OnExecute(StreamedVirtualTransportServerHandleResult 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();
            }
        }
コード例 #2
0
        public StreamedVirtualTransportServerHandleResult Handle(int Count)
        {
            var ret = StreamedVirtualTransportServerHandleResult.CreateContinue();

            var Buffer        = c.ReadBuffer.Array;
            var FirstPosition = c.ReadBuffer.Offset;
            var BufferLength  = c.ReadBuffer.Offset + c.ReadBuffer.Count;
            var CheckPosition = FirstPosition;

            if (Transformer != null)
            {
                Transformer.Inverse(Buffer, BufferLength, Count);
            }
            BufferLength += Count;

            var LineFeedPosition = -1;

            for (int i = CheckPosition; i < BufferLength; i += 1)
            {
                Byte b = Buffer[i];
                if (b == '\n')
                {
                    LineFeedPosition = i;
                    break;
                }
            }
            if (LineFeedPosition >= FirstPosition)
            {
                var LineBytes = Buffer.Skip(FirstPosition).Take(LineFeedPosition - FirstPosition).Where(b => b != '\r').ToArray();
                var Line      = TextEncoding.UTF8.GetString(LineBytes, 0, LineBytes.Length);
                var cmd       = ParseCommand(Line);
                if (cmd.OnSome)
                {
                    var CommandName = cmd.Some.CommandName;
                    var CommandHash = cmd.Some.CommandHash;
                    var Parameters  = cmd.Some.Parameters;
                    if (InputByteLengthReport != null)
                    {
                        InputByteLengthReport(CommandName, LineBytes.Length);
                    }
                    if ((CommandHash.OnSome ? ss.HasCommand(CommandName, CommandHash.Some) : ss.HasCommand(CommandName)) && (CheckCommandAllowed != null ? CheckCommandAllowed(CommandName) : true))
                    {
                        if (CommandHash.OnSome)
                        {
                            ret = StreamedVirtualTransportServerHandleResult.CreateCommand(new StreamedVirtualTransportServerHandleResultCommand
                            {
                                CommandName    = CommandName,
                                ExecuteCommand = (OnSuccess, OnFailure) =>
                                {
                                    Action <String> OnSuccessInner = OutputParameters =>
                                    {
                                        var Bytes = TextEncoding.UTF8.GetBytes(String.Format(@"/svr {0} {1}" + "\r\n", CommandName + "@" + CommandHash.Some.ToString("X8", System.Globalization.CultureInfo.InvariantCulture), OutputParameters));
                                        lock (c.WriteBufferLockee)
                                        {
                                            if (Transformer != null)
                                            {
                                                Transformer.Transform(Bytes, 0, Bytes.Length);
                                            }
                                            c.WriteBuffer.Add(Bytes);
                                        }
                                        if (OutputByteLengthReport != null)
                                        {
                                            OutputByteLengthReport(CommandName, Bytes.Length);
                                        }
                                        OnSuccess();
                                    };
                                    ss.ExecuteCommand(CommandName, CommandHash.Some, Parameters, OnSuccessInner, OnFailure);
                                }
                            });
                        }
                        else
                        {
                            ret = StreamedVirtualTransportServerHandleResult.CreateCommand(new StreamedVirtualTransportServerHandleResultCommand
                            {
                                CommandName    = CommandName,
                                ExecuteCommand = (OnSuccess, OnFailure) =>
                                {
                                    Action <String> OnSuccessInner = OutputParameters =>
                                    {
                                        var Bytes = TextEncoding.UTF8.GetBytes(String.Format(@"/svr {0} {1}" + "\r\n", CommandName, OutputParameters));
                                        lock (c.WriteBufferLockee)
                                        {
                                            c.WriteBuffer.Add(Bytes);
                                        }
                                        if (OutputByteLengthReport != null)
                                        {
                                            OutputByteLengthReport(CommandName, Bytes.Length);
                                        }
                                        OnSuccess();
                                    };
                                    ss.ExecuteCommand(CommandName, Parameters, OnSuccessInner, OnFailure);
                                }
                            });
                        }
                    }
                    else
                    {
                        ret = StreamedVirtualTransportServerHandleResult.CreateBadCommand(new StreamedVirtualTransportServerHandleResultBadCommand {
                            CommandName = CommandName
                        });
                    }
                }
                else if (cmd.OnNone)
                {
                    ret = StreamedVirtualTransportServerHandleResult.CreateBadCommandLine(new StreamedVirtualTransportServerHandleResultBadCommandLine {
                        CommandLine = Line
                    });
                }
                else
                {
                    throw new InvalidOperationException();
                }
                FirstPosition = LineFeedPosition + 1;
                CheckPosition = FirstPosition;
            }

            if (BufferLength >= Buffer.Length && FirstPosition > 0)
            {
                var CopyLength = BufferLength - FirstPosition;
                for (int i = 0; i < CopyLength; i += 1)
                {
                    Buffer[i] = Buffer[FirstPosition + i];
                }
                BufferLength  = CopyLength;
                FirstPosition = 0;
            }
            if (FirstPosition >= BufferLength)
            {
                c.ReadBuffer = new ArraySegment <Byte>(Buffer, 0, 0);
            }
            else
            {
                c.ReadBuffer = new ArraySegment <Byte>(Buffer, FirstPosition, BufferLength - FirstPosition);
            }

            return(ret);
        }
コード例 #3
0
        public StreamedVirtualTransportServerHandleResult Handle(int Count)
        {
            var ret = StreamedVirtualTransportServerHandleResult.CreateContinue();

            var Buffer        = c.ReadBuffer.Array;
            var FirstPosition = c.ReadBuffer.Offset;
            var BufferLength  = c.ReadBuffer.Offset + c.ReadBuffer.Count;

            if (Transformer != null)
            {
                Transformer.Inverse(Buffer, BufferLength, Count);
            }
            BufferLength += Count;

            while (true)
            {
                var r = TryShift(c, Buffer, FirstPosition, BufferLength - FirstPosition);
                if (r == null)
                {
                    break;
                }
                FirstPosition = r.Position;

                if (r.Command != null)
                {
                    var CommandName = r.Command.CommandName;
                    var CommandHash = r.Command.CommandHash;
                    var Parameters  = r.Command.Parameters;
                    if (InputByteLengthReport != null)
                    {
                        InputByteLengthReport(CommandName, r.Command.ByteLength);
                    }
                    if (ss.HasCommand(CommandName, CommandHash) && (CheckCommandAllowed != null ? CheckCommandAllowed(CommandName) : true))
                    {
                        ret = StreamedVirtualTransportServerHandleResult.CreateCommand(new StreamedVirtualTransportServerHandleResultCommand
                        {
                            CommandName    = CommandName,
                            ExecuteCommand = (OnSuccess, OnFailure) =>
                            {
                                Action <Byte[]> OnSuccessInner = OutputParameters =>
                                {
                                    var CommandNameBytes = TextEncoding.UTF16.GetBytes(CommandName);
                                    Byte[] Bytes;
                                    using (var ms = Streams.CreateMemoryStream())
                                    {
                                        ms.WriteInt32(CommandNameBytes.Length);
                                        ms.Write(CommandNameBytes);
                                        ms.WriteUInt32(CommandHash);
                                        ms.WriteInt32(OutputParameters.Length);
                                        ms.Write(OutputParameters);
                                        ms.Position = 0;
                                        Bytes       = ms.Read((int)(ms.Length));
                                    }
                                    var BytesLength = Bytes.Length;
                                    lock (c.WriteBufferLockee)
                                    {
                                        if (Transformer != null)
                                        {
                                            Transformer.Transform(Bytes, 0, BytesLength);
                                        }
                                        c.WriteBuffer.Add(Bytes);
                                    }
                                    if (OutputByteLengthReport != null)
                                    {
                                        OutputByteLengthReport(CommandName, BytesLength);
                                    }
                                    OnSuccess();
                                };
                                ss.ExecuteCommand(CommandName, CommandHash, Parameters, OnSuccessInner, OnFailure);
                            }
                        });
                    }
                    else
                    {
                        ret = StreamedVirtualTransportServerHandleResult.CreateBadCommand(new StreamedVirtualTransportServerHandleResultBadCommand {
                            CommandName = CommandName
                        });
                    }
                    break;
                }
            }

            if (BufferLength >= Buffer.Length && FirstPosition > 0)
            {
                var CopyLength = BufferLength - FirstPosition;
                for (int i = 0; i < CopyLength; i += 1)
                {
                    Buffer[i] = Buffer[FirstPosition + i];
                }
                BufferLength  = CopyLength;
                FirstPosition = 0;
            }
            if (FirstPosition >= BufferLength)
            {
                c.ReadBuffer = new ArraySegment <Byte>(Buffer, 0, 0);
            }
            else
            {
                c.ReadBuffer = new ArraySegment <Byte>(Buffer, FirstPosition, BufferLength - FirstPosition);
            }

            return(ret);
        }