Exemplo n.º 1
0
        public SessionId CreateSession(ConnectionTicket ticket)
        {
            ArgumentValidator.IsNotNull("ticket", ticket, "Ticket cannot be null!");

            using (ClientProxy clientProxy = CreateClientProxy(ticket))
            {
                try
                {
                    SessionId sessionId = CreateSessionId(ticket);

                    if (_sessions.ContainsKey(sessionId))
                    {
                        return(sessionId);
                    }

                    string validationHash = clientProxy.ValidateConnection(Constants.ClientConnectionHash);
                    if (string.Compare(validationHash, Constants.ClientValidResultHash) != 0)
                    {
                        throw new ClientNotValidException(GetType());
                    }

                    User user;
                    if (!CheckUser(ticket.Username, ticket.SecurityToken, out user))
                    {
                        throw new UserIsNotValidException(GetType(), ticket.Username);
                    }

                    if (!_sessions.TryAdd(sessionId, new SessionData(sessionId, ticket)))
                    {
                        throw new CannotCreateSessionException(GetType());
                    }
                    SaveSessionToDatabase(sessionId, user, ticket);
                    InitCheckTimer();
                    Logger.Info("User {0} has been connected (session: {1}).", ticket.Username, sessionId);
                    return(sessionId);
                }
                catch (CommunicationException e)
                {
                    ApplicationContext.Log.Error(GetType(), e);
                }
                catch (ClientNotValidException e)
                {
                    ApplicationContext.Log.Error(GetType(), e);
                    throw;
                }
                catch (Exception e)
                {
                    ApplicationContext.Log.Error(GetType(), e);
                }
            }
            return(null);
        }
Exemplo n.º 2
0
        private void CheckConnectedUsersCallback(object state)
        {
            Logger.Info("Checking of connected users.");
            foreach (SessionData sessionData in _sessions.Values)
            {
                bool      testFailed = false;
                Exception exc        = null;
                try
                {
                    using (ClientProxy proxy = CreateClientProxy(sessionData.Ticket))
                    {
                        if (!proxy.IsActive())
                        {
                            testFailed = true;
                        }
                    }
                }
                catch (CommunicationException e)
                {
                    testFailed = true;
                    exc        = e;
                }
                catch (Exception e)
                {
                    testFailed = true;
                    exc        = e;
                }

                if (testFailed)
                {
                    if (exc != null)
                    {
                        Logger.Error(exc);
                    }
                    Logger.Info("Closing session to not available client computer '{0}'.", sessionData.Ticket.ClientComputer);
                    CloseSession(sessionData.SessionId);
                }
            }
        }
Exemplo n.º 3
0
        public void ReconnectSessions()
        {
            using (ServerDataContext context = new ServerDataContext())
            {
                List <SessionId> toRemove = new List <SessionId>();
                foreach (ActiveSession session in context.ActiveSessions)
                {
                    SessionId   sessionId = new SessionId(session.SessionId);
                    SessionData data      = new SessionData(sessionId, context.CreateFakeTicket(session));
                    _sessions.TryAdd(sessionId, data);
                    try
                    {
                        using (ClientProxy proxy = CreateClientProxy(data.Ticket))
                        {
                            proxy.ConnectionRestarted();
                            Logger.Info("Restarted session to computer {0}.", data.Ticket.ClientComputer);
                        }
                    }
                    catch (Exception e)
                    {
                        toRemove.Add(sessionId);
                        ApplicationContext.Log.Error(GetType(), e);
                        ApplicationContext.Log.Info(GetType(), "Removed session to: {0}", data.Ticket.ClientComputer);
                    }
                }

                if (toRemove.Count > 0)
                {
                    foreach (SessionId sessionId in toRemove)
                    {
                        SessionData tmpData;
                        context.RemoveSession(sessionId);
                        _sessions.TryRemove(sessionId, out tmpData);
                    }
                    context.SaveChanges();
                }
            }
        }