Example #1
0
        public UdpSession(UdpServer Server, Socket ServerSocket, IPEndPoint RemoteEndPoint, Func <ISessionContext, IBinaryTransformer, KeyValuePair <IServerImplementation, IStreamedVirtualTransportServer> > VirtualTransportServerFactory, Action <Action> QueueUserWorkItem)
        {
            this.Server              = Server;
            this.ServerSocket        = ServerSocket;
            this.RemoteEndPoint      = RemoteEndPoint;
            this.LastActiveTimeValue = new LockedVariable <DateTime>(DateTime.UtcNow);
            ssm = new SessionStateMachine <StreamedVirtualTransportServerHandleResult, Unit>(ex => ex is SocketException, OnCriticalError, OnShutdownRead, OnShutdownWrite, OnWrite, OnExecute, OnStartRawRead, OnExit, QueueUserWorkItem);

            Context                = Server.ServerContext.CreateSessionContext();
            Context.Quit          += ssm.NotifyExit;
            Context.Authenticated += () => Server.NotifySessionAuthenticated(this);

            var rpst = new Rc4PacketServerTransformer();
            var Pair = VirtualTransportServerFactory(Context, rpst);

            si  = Pair.Key;
            vts = Pair.Value;
            Context.SecureConnectionRequired += c =>
            {
                rpst.SetSecureContext(c);
                NextSecureContext = c;
            };
            vts.ServerEvent           += () => ssm.NotifyWrite(new Unit());
            vts.InputByteLengthReport += (CommandName, ByteLength) => Server.ServerContext.RaiseSessionLog(new SessionLogEntry {
                Token = Context.SessionTokenString, RemoteEndPoint = RemoteEndPoint, Time = DateTime.UtcNow, Type = "InBytes", Name = CommandName, Message = ByteLength.ToInvariantString()
            });
            vts.OutputByteLengthReport += (CommandName, ByteLength) => Server.ServerContext.RaiseSessionLog(new SessionLogEntry {
                Token = Context.SessionTokenString, RemoteEndPoint = RemoteEndPoint, Time = DateTime.UtcNow, Type = "OutBytes", Name = CommandName, Message = ByteLength.ToInvariantString()
            });
        }
Example #2
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();
            }
        }