internal void Close() { lock (_connectionLock) { if (_state != MongoConnectionState.Closed) { if (_stream != null) { try { _stream.Close(); } catch { } // ignore exceptions _stream = null; } if (_tcpClient != null) { if (_tcpClient.Connected) { // even though MSDN says TcpClient.Close doesn't close the underlying socket // it actually does (as proven by disassembling TcpClient and by experimentation) try { _tcpClient.Close(); } catch { } // ignore exceptions } _tcpClient = null; } _state = MongoConnectionState.Closed; } } }
// constructors internal MongoConnection(MongoConnectionPool connectionPool) { _serverInstance = connectionPool.ServerInstance; _connectionPool = connectionPool; _generationId = connectionPool.GenerationId; _createdAt = DateTime.UtcNow; _state = MongoConnectionState.Initial; }
// constructors internal MongoConnection(MongoConnectionPool connectionPool) { this.serverInstance = connectionPool.ServerInstance; this.connectionPool = connectionPool; this.generationId = connectionPool.GenerationId; this.createdAt = DateTime.UtcNow; this.state = MongoConnectionState.Initial; }
internal MongoConnection( MongoConnectionPool connectionPool, IPEndPoint endPoint ) { this.connectionPool = connectionPool; this.endPoint = endPoint; this.createdAt = DateTime.UtcNow; this.state = MongoConnectionState.Initial; }
internal void Open() { if (_state != MongoConnectionState.Initial) { throw new InvalidOperationException("Open called more than once."); } var ipEndPoint = _serverInstance.GetIPEndPoint(); var tcpClient = new TcpClient(ipEndPoint.AddressFamily); tcpClient.NoDelay = true; // turn off Nagle tcpClient.ReceiveBufferSize = MongoDefaults.TcpReceiveBufferSize; tcpClient.SendBufferSize = MongoDefaults.TcpSendBufferSize; tcpClient.Connect(ipEndPoint); var stream = (Stream)tcpClient.GetStream(); if (_serverInstance.Settings.UseSsl) { SslStream sslStream; if (_serverInstance.Settings.VerifySslCertificate) { sslStream = new SslStream(stream, false); // don't leave inner stream open } else { sslStream = new SslStream(stream, false, AcceptAnyCertificate, null); // don't leave inner stream open } try { sslStream.AuthenticateAsClient(_serverInstance.Address.Host); } catch { try { stream.Close(); } catch { } // ignore exceptions try { tcpClient.Close(); } catch { } // ignore exceptions throw; } stream = sslStream; } _tcpClient = tcpClient; _stream = stream; _state = MongoConnectionState.Open; }
internal void Open() { if (state != MongoConnectionState.Initial) { throw new InvalidOperationException("Open called more than once"); } var tcpClient = new TcpClient(endPoint.AddressFamily); tcpClient.NoDelay = true; // turn off Nagle tcpClient.ReceiveBufferSize = MongoDefaults.TcpReceiveBufferSize; tcpClient.SendBufferSize = MongoDefaults.TcpSendBufferSize; tcpClient.Connect(endPoint); this.tcpClient = tcpClient; this.state = MongoConnectionState.Open; }
internal void Open() { if (_state != MongoConnectionState.Initial) { throw new InvalidOperationException("Open called more than once."); } var ipEndPoint = _serverInstance.GetIPEndPoint(); var tcpClient = new TcpClient(ipEndPoint.AddressFamily); tcpClient.NoDelay = true; // turn off Nagle tcpClient.ReceiveBufferSize = MongoDefaults.TcpReceiveBufferSize; tcpClient.SendBufferSize = MongoDefaults.TcpSendBufferSize; tcpClient.Connect(ipEndPoint); _tcpClient = tcpClient; _state = MongoConnectionState.Open; }
internal void Close() { lock (connectionLock) { if (state != MongoConnectionState.Closed) { if (tcpClient != null) { if (tcpClient.Connected) { // even though MSDN says TcpClient.Close doesn't close the underlying socket // it actually does (as proven by disassembling TcpClient and by experimentation) tcpClient.Close(); } tcpClient = null; } state = MongoConnectionState.Closed; } } }
private void HandleException( Exception ex ) { // TODO: figure out which exceptions are more serious than others // there are three possible situations: // 1. we can keep using the connection // 2. just this one connection needs to be discarded // 3. the whole connection pool needs to be discarded // for now the only exception we know affects only one connection is FileFormatException // and there are no cases where the connection can continue to be used state = MongoConnectionState.Damaged; if (!(ex is FileFormatException)) { try { serverInstance.Disconnect(); } catch { } // ignore any further exceptions } }
internal MongoConnection(MongoServerInstance serverInstance) { _serverInstance = serverInstance; _createdAt = DateTime.UtcNow; _state = MongoConnectionState.Initial; }
internal void Open() { if (_state != MongoConnectionState.Initial) { throw new InvalidOperationException("Open called more than once."); } var ipEndPoint = _serverInstance.GetIPEndPoint(); var tcpClient = new TcpClient(ipEndPoint.AddressFamily); tcpClient.NoDelay = true; // turn off Nagle tcpClient.ReceiveBufferSize = MongoDefaults.TcpReceiveBufferSize; tcpClient.SendBufferSize = MongoDefaults.TcpSendBufferSize; tcpClient.Connect(ipEndPoint); var stream = (Stream)tcpClient.GetStream(); if (_serverInstance.Settings.UseSsl) { var checkCertificateRevocation = true; var clientCertificateCollection = (X509CertificateCollection)null; var clientCertificateSelectionCallback = (LocalCertificateSelectionCallback)null; var enabledSslProtocols = SslProtocols.Default; var serverCertificateValidationCallback = (RemoteCertificateValidationCallback)null; var sslSettings = _serverInstance.Settings.SslSettings; if (sslSettings != null) { checkCertificateRevocation = sslSettings.CheckCertificateRevocation; clientCertificateCollection = sslSettings.ClientCertificateCollection; clientCertificateSelectionCallback = sslSettings.ClientCertificateSelectionCallback; enabledSslProtocols = sslSettings.EnabledSslProtocols; serverCertificateValidationCallback = sslSettings.ServerCertificateValidationCallback; } if (serverCertificateValidationCallback == null && !_serverInstance.Settings.VerifySslCertificate) { serverCertificateValidationCallback = AcceptAnyCertificate; } var sslStream = new SslStream(stream, false, serverCertificateValidationCallback, clientCertificateSelectionCallback); try { var targetHost = _serverInstance.Address.Host; sslStream.AuthenticateAsClient(targetHost, clientCertificateCollection, enabledSslProtocols, checkCertificateRevocation); } catch { try { stream.Close(); } catch { } // ignore exceptions try { tcpClient.Close(); } catch { } // ignore exceptions throw; } stream = sslStream; } _tcpClient = tcpClient; _stream = stream; _state = MongoConnectionState.Open; new Authenticator(this, _serverInstance.Settings.Credentials) .Authenticate(); }