示例#1
0
        private async Task <GetOrCreateClientSessionResult> GetOrCreateClientSessionAsync(MqttConnectPacket connectPacket)
        {
            await _sessionsSemaphore.WaitAsync().ConfigureAwait(false);

            try
            {
                var isSessionPresent = _sessions.TryGetValue(connectPacket.ClientId, out var clientSession);
                if (isSessionPresent)
                {
                    if (connectPacket.CleanSession)
                    {
                        _sessions.Remove(connectPacket.ClientId);
                        await clientSession.StopAsync();

                        clientSession = null;

                        _logger.LogTrace("Stopped existing session of client '{0}'.", connectPacket.ClientId);
                    }
                    else
                    {
                        _logger.LogTrace("Reusing existing session of client '{0}'.", connectPacket.ClientId);
                    }
                }

                var isExistingSession = true;
                if (clientSession == null)
                {
                    isExistingSession = false;

                    clientSession = _clientSesssionFactory.CreateClientSession(connectPacket.ClientId, this);
                    _sessions[connectPacket.ClientId] = clientSession;

                    _logger.LogTrace("Created a new session for client '{0}'.", connectPacket.ClientId);
                }

                return(new GetOrCreateClientSessionResult {
                    IsExistingSession = isExistingSession, Session = clientSession
                });
            }
            finally
            {
                _sessionsSemaphore.Release();
            }
        }
示例#2
0
        private GetOrCreateClientSessionResult GetOrCreateClientSession(MqttConnectPacket connectPacket)
        {
            lock (_clientSessions)
            {
                var isSessionPresent = _clientSessions.TryGetValue(connectPacket.ClientId, out var clientSession);
                if (isSessionPresent)
                {
                    if (connectPacket.CleanSession)
                    {
                        _clientSessions.Remove(connectPacket.ClientId);
                        clientSession.Stop();
                        clientSession = null;

                        _logger.LogTrace("Stopped existing session of client '{0}'.", connectPacket.ClientId);
                    }
                    else
                    {
                        _logger.LogTrace("Reusing existing session of client '{0}'.", connectPacket.ClientId);
                    }
                }

                var isExistingSession = true;
                if (clientSession == null)
                {
                    isExistingSession = false;

                    clientSession = _clientSesssionFactory.CreateClientSession(connectPacket.ClientId, this);
                    _clientSessions[connectPacket.ClientId] = clientSession;

                    _logger.LogTrace("Created a new session for client '{0}'.", connectPacket.ClientId);
                }

                return(new GetOrCreateClientSessionResult {
                    IsExistingSession = isExistingSession, Session = clientSession
                });
            }
        }