public bool OnSessionEstablished(Session session)
        {
            if (session == null || session.SessionType != SessionTypes.Client || session.Connection == null)
            {
                return(false);
            }

            var key = MiscUtil.GetAddressInfo(session.IP, session.RemotePort);

            if (_clientSessions.ContainsKey(key))
            {
                //session.Socket.Disconnect(false);
                _clientSessions[key].ServerChannel.Disconnect();

                lock (_clientSessions)
                    _clientSessions[key].ServerChannel = new ServerChannel(session.Connection, session.IP.ToString(), session.RemotePort, this._localAddress.ToString(), session.SessionType, _traceProvider, _channelFormatter);

                try
                {
                    return(_clientSessions[key].ServerChannel.Connect(true));
                }
                catch (ChannelException ex)
                {
                    //RTD: Replace shardLogger with the respective module logger name
                    if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsErrorEnabled)
                    {
                        LoggerManager.Instance.ShardLogger.Error("Error: ClientSessionMgr.OnSessionEstd()", ex.ToString());
                    }
                    return(false);
                }
            }
            var clientSession = new ClientSession(_databaseEngineFactory, this, ShardName)
            {
                ServerChannel =
                    new ServerChannel(session.Connection, session.IP.ToString(), session.RemotePort,
                                      _localAddress.ToString(), session.SessionType, _traceProvider, _channelFormatter)
            };

            if (!clientSession.ServerChannel.Connect(true))
            {
                return(false);
            }
            lock (_clientSessions)
            {
                _clientSessions[key] = clientSession;
            }
            return(true);
        }
Example #2
0
        public void ChannelDisconnected(IRequestResponseChannel channel, string reason)
        {
            if (channel == null)
            {
                return;
            }
            try
            {
                foreach (ReaderInfo readerInfo in _readersList)
                {
                    var operation = new DisposeReaderOperation
                    {
                        Database      = readerInfo.DatabaseName,
                        Collection    = readerInfo.CollectionName,
                        ReaderUID     = readerInfo.ReaderId,
                        OperationType = DatabaseOperationType.DisposeReader,
                        SessionId     = CsSessionId
                    };
                    try
                    {
                        _dbEngine.DiposeReader(operation);
                    }
                    catch (SecurityException exc)
                    {
                        if (LoggerManager.Instance.SecurityLogger != null &&
                            LoggerManager.Instance.SecurityLogger.IsErrorEnabled)
                        {
                            LoggerManager.Instance.SecurityLogger.Error("ClientSession.ChannelDisconected() ", exc);
                        }
                    }
                }
                _readersList.Clear();

                _conmmandInfos.Clear();

                _clientDisconnection.DisconnectClient(MiscUtil.GetAddressInfo(channel.PeerAddress.IpAddress,
                                                                              channel.PeerAddress.Port));
                _databaseEngineFactory.Dispose(_noSConnectionString);
                try
                {
                    channel.Disconnect();
                }
                catch (ThreadAbortException e)
                {
                    //Todo: maybe log in future or something.
                }

                // Log Client Disconnected.
                if (LoggerManager.Instance.ServerLogger.IsInfoEnabled)
                {
                    LoggerManager.Instance.ServerLogger.Info("ClientSession.ChannelDisconnected",
                                                             "Client [" + MiscUtil.GetAddressInfo(channel.PeerAddress.IpAddress,
                                                                                                  channel.PeerAddress.Port) + "] with sessionId'" + CsSessionId.SessionId + "' disconnected.");
                }

                LoggerManager.Instance.SetThreadContext(new LoggerContext()
                {
                    ShardName = "", DatabaseName = ""
                });
            }
            catch (Exception e)
            {
                try
                {
                    //Remove client session from session manager.
                    _clientDisconnection.DisconnectClient(MiscUtil.GetAddressInfo(channel.PeerAddress.IpAddress, channel.PeerAddress.Port));
                }
                catch (Exception ex) { }

                if (LoggerManager.Instance.ShardLogger != null && LoggerManager.Instance.ShardLogger.IsErrorEnabled)
                {
                    LoggerManager.Instance.ShardLogger.Error("ClientSessionDisconnect",
                                                             e.Message + " StackTrace: " + e.StackTrace);
                }
            }
        }