private async Task Close()
        {
            if (Interlocked.Exchange(ref _state, _disposed) == _disposed)
            {
                return;
            }

            try
            {
                if (_keepAliveTracker != null)
                {
                    _keepAliveTracker.Dispose();
                }
                if (_keepAliveTimeoutTimer != null)
                {
                    _keepAliveTimeoutTimer.Dispose();
                }
                if (_closingTimeoutTimer != null)
                {
                    _closingTimeoutTimer.Dispose();
                }
                if (_stream != null)
                {
                    _stream.Dispose();
                    _stream = null;
                }
                if (_tcpClient != null && _tcpClient.Connected)
                {
                    _tcpClient.Dispose();
                    _tcpClient = null;
                }
            }
            catch (Exception) { }

            if (_receiveBuffer != null)
            {
                _bufferManager.ReturnBuffer(_receiveBuffer);
            }
            if (_sessionBuffer != null)
            {
                _bufferManager.ReturnBuffer(_sessionBuffer);
            }

            _log.DebugFormat("Session closed for [{0}] on [{1}] in dispatcher [{2}] with session count [{3}].",
                             this.RemoteEndPoint,
                             DateTime.UtcNow.ToString(@"yyyy-MM-dd HH:mm:ss.fffffff"),
                             _module.GetType().Name,
                             this.Server.SessionCount - 1);
            try
            {
                await _module.OnSessionClosed(this);
            }
            catch (Exception ex)
            {
                HandleUserSideError(ex);
            }
        }
Example #2
0
        private void InternalClose(bool shallNotifyUserSide)
        {
            if (Interlocked.Exchange(ref _state, _disposed) == _disposed)
            {
                return;
            }

            try
            {
                if (_keepAliveTracker != null)
                {
                    _keepAliveTracker.Dispose();
                }
                if (_keepAliveTimeoutTimer != null)
                {
                    _keepAliveTimeoutTimer.Dispose();
                }
                if (_closingTimeoutTimer != null)
                {
                    _closingTimeoutTimer.Dispose();
                }
                if (_stream != null)
                {
                    _stream.Dispose();
                    _stream = null;
                }
                if (_tcpClient != null && _tcpClient.Connected)
                {
                    _tcpClient.Close();
                    _tcpClient = null;
                }
            }
            catch (Exception) { }

            if (_receiveBuffer != null)
            {
                _bufferManager.ReturnBuffer(_receiveBuffer);
            }
            _receiveBufferOffset = 0;

            if (shallNotifyUserSide)
            {
                _log(string.Format("Disconnected from server [{0}] on [{1}].",
                                   this.RemoteEndPoint, DateTime.UtcNow.ToString(@"yyyy-MM-dd HH:mm:ss.fffffff")));
                try
                {
                    RaiseServerDisconnected();
                }
                catch (Exception ex)
                {
                    HandleUserSideError(ex);
                }
            }
        }
Example #3
0
        private void Clean()
        {
            try
            {
                try
                {
                    if (_keepAliveTracker != null)
                    {
                        _keepAliveTracker.StopTimer();
                        _keepAliveTracker.Dispose();
                    }
                }
                catch { }
                try
                {
                    if (_keepAliveTimeoutTimer != null)
                    {
                        _keepAliveTimeoutTimer.Change(Timeout.Infinite, Timeout.Infinite);
                        _keepAliveTimeoutTimer.Dispose();
                    }
                }
                catch { }
                try
                {
                    if (_closingTimeoutTimer != null)
                    {
                        _closingTimeoutTimer.Change(Timeout.Infinite, Timeout.Infinite);
                        _closingTimeoutTimer.Dispose();
                    }
                }
                catch { }
                try
                {
                    if (_stream != null)
                    {
                        _stream.Dispose();
                    }
                }
                catch { }
                try
                {
                    if (_tcpClient != null)
                    {
                        _tcpClient.Dispose();
                    }
                }
                catch { }
            }
            catch { }
            finally
            {
                _keepAliveTracker      = null;
                _keepAliveTimeoutTimer = null;
                _closingTimeoutTimer   = null;
                _stream    = null;
                _tcpClient = null;
            }

            if (_receiveBuffer != default(ArraySegment <byte>))
            {
                _bufferManager.ReturnBuffer(_receiveBuffer);
            }
            _receiveBuffer       = default(ArraySegment <byte>);
            _receiveBufferOffset = 0;
        }