internal CassandraConnection(Session owner, IPAddress serverAddress, ProtocolOptions protocolOptions,
                                     SocketOptions socketOptions, ClientOptions clientOptions,
                                     IAuthInfoProvider authInfoProvider)
        {
            this._owner = owner;
            _bufferingMode = null;
            switch (protocolOptions.Compression)
            {
                case CompressionType.Snappy:
                    _bufferingMode = new FrameBuffering();
                    break;
                case CompressionType.NoCompression:
                    _bufferingMode = clientOptions.WithoutRowSetBuffering ? new NoBuffering() : new FrameBuffering();
                    break;
                default:
                    throw new ArgumentException();
            }

            this._authInfoProvider = authInfoProvider;
            if (protocolOptions.Compression == CompressionType.Snappy)
            {
                _startupOptions.Add("COMPRESSION", "snappy");
                _compressor = new SnappyProtoBufCompressor();
            }
            this._serverAddress = serverAddress;
            this._port = protocolOptions.Port;
            this._queryAbortTimeout = clientOptions.QueryAbortTimeout;
            this._asyncCallAbortTimeout = clientOptions.AsyncCallAbortTimeout;

            this._socketOptions = socketOptions;

            CreateConnection();
            if (IsHealthy)
                BeginReading();
        }
        internal CassandraConnection(Session owner, IPAddress serverAddress, ProtocolOptions protocolOptions,
                                     SocketOptions socketOptions, ClientOptions clientOptions,
                                     IAuthInfoProvider authInfoProvider)
        {
            this.Guid = Guid.NewGuid();
            this._owner = owner;
            _bufferingMode = null;
            switch (protocolOptions.Compression)
            {
                case CompressionType.Snappy:
                    _bufferingMode = new FrameBuffering();
                    break;
                case CompressionType.NoCompression:
                    _bufferingMode = clientOptions.WithoutRowSetBuffering ? new NoBuffering() : new FrameBuffering();
                    break;
                default:
                    throw new ArgumentException();
            }

            this._authInfoProvider = authInfoProvider;
            if (protocolOptions.Compression == CompressionType.Snappy)
            {
                _startupOptions.Add("COMPRESSION", "snappy");
                _compressor = new SnappyProtoBufCompressor();
            }
            this._serverAddress = serverAddress;
            this._port = protocolOptions.Port;
            this._queryAbortTimeout = clientOptions.QueryAbortTimeout;

            this._socketOptions = socketOptions;

            for (int i = 0; i <= sbyte.MaxValue; i++)
                _freeStreamIDs.Push(i);

            _protocolErrorHandlerAction = new Action<ErrorActionParam>((param) =>
               {
                   if (param.AbstractResponse is ErrorResponse)
                       JobFinished(
                           param.Jar,
                           (param.AbstractResponse as ErrorResponse).Output);
               });

            _frameEventCallback.Value = new Action<ResponseFrame>(EventOccured);

            _buffer = new byte[][] {
                    new byte[_bufferingMode.PreferedBufferSize()],
                    new byte[_bufferingMode.PreferedBufferSize()] };

            var newSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            if (_socketOptions.KeepAlive != null)
                newSock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive,
                                        _socketOptions.KeepAlive.Value);

            newSock.SendTimeout = _socketOptions.ConnectTimeoutMillis;

            if (_socketOptions.SoLinger != null)
                newSock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger,
                                        new LingerOption(true, _socketOptions.SoLinger.Value));

            if (_socketOptions.ReceiveBufferSize != null)
                newSock.ReceiveBufferSize = _socketOptions.ReceiveBufferSize.Value;

            if (_socketOptions.SendBufferSize != null)
                newSock.ReceiveBufferSize = _socketOptions.SendBufferSize.Value;

            if (_socketOptions.TcpNoDelay != null)
                newSock.NoDelay = _socketOptions.TcpNoDelay.Value;

            newSock.Connect(new IPEndPoint(_serverAddress, _port));
            _socket = newSock;
            _bufferingMode.Reset();

            if (protocolOptions.SslOptions == null)
                _socketStream = new NetworkStream(_socket);
            else
            {
                string targetHost;
                try
                {
                    targetHost = Dns.GetHostEntry(_serverAddress).HostName;
                }
                catch (SocketException ex)
                {
                    targetHost = serverAddress.ToString();
                    _logger.Error(string.Format("SSL connection: Can not resolve {0} address. Using IP address instead of hostname. This may cause RemoteCertificateNameMismatch error during Cassandra host authentication. Note that Cassandra node SSL certificate's CN(Common Name) must match the Cassandra node hostname.", _serverAddress.ToString()), ex);
                }

                _socketStream = new SslStream(new NetworkStream(_socket), false, new RemoteCertificateValidationCallback(protocolOptions.SslOptions.RemoteCertValidationCallback), null);
                (_socketStream as SslStream).AuthenticateAsClient(targetHost, new X509CertificateCollection(), protocolOptions.SslOptions.SslProtocol, false);
            }

            if (IsHealthy)
                BeginReading();
        }
Example #3
0
        internal CassandraConnection(ISession owner, IPAddress serverAddress, ProtocolOptions protocolOptions,
                                     SocketOptions socketOptions, ClientOptions clientOptions,
                                     IAuthProvider authProvider, IAuthInfoProvider authInfoProvider, int protocolVersion)
        {
            if (protocolVersion == 1)
            {
                _binaryProtocolRequestVersionByte  = RequestFrame.ProtocolV1RequestVersionByte;
                _binaryProtocolResponseVersionByte = ResponseFrame.ProtocolV1ResponseVersionByte;
            }

            Guid           = Guid.NewGuid();
            _owner         = owner;
            _bufferingMode = null;
            switch (protocolOptions.Compression)
            {
            case CompressionType.LZ4:
                _bufferingMode = new FrameBuffering();
                break;

            case CompressionType.Snappy:
                _bufferingMode = new FrameBuffering();
                break;

            case CompressionType.NoCompression:
                _bufferingMode = clientOptions.WithoutRowSetBuffering ? new NoBuffering() : new FrameBuffering();
                break;

            default:
                throw new ArgumentException();
            }

            _authProvider     = authProvider;
            _authInfoProvider = authInfoProvider;
            if (protocolOptions.Compression == CompressionType.Snappy)
            {
                _startupOptions.Add("COMPRESSION", "snappy");
                _compressor = new SnappyProtoBufCompressor();
            }
            else if (protocolOptions.Compression == CompressionType.LZ4)
            {
                _startupOptions.Add("COMPRESSION", "lz4");
                _compressor = new LZ4ProtoBufCompressor();
            }

            _serverAddress     = serverAddress;
            _port              = protocolOptions.Port;
            _queryAbortTimeout = clientOptions.QueryAbortTimeout;

            _socketOptions = socketOptions;

            for (int i = 0; i <= sbyte.MaxValue; i++)
            {
                _freeStreamIDs.Push(i);
            }

            _protocolErrorHandlerAction = param =>
            {
                if (param.AbstractResponse is ErrorResponse)
                {
                    JobFinished(
                        param.Jar,
                        (param.AbstractResponse as ErrorResponse).Output);
                }
            };

            _frameEventCallback.Value = EventOccured;

            _buffer = new[]
            {
                new byte[_bufferingMode.PreferedBufferSize()],
                new byte[_bufferingMode.PreferedBufferSize()]
            };

            var newSock = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);

            if (_socketOptions.KeepAlive != null)
            {
                newSock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.KeepAlive,
                                        _socketOptions.KeepAlive.Value);
            }

            newSock.SendTimeout = _socketOptions.ConnectTimeoutMillis;

            if (_socketOptions.SoLinger != null)
            {
                newSock.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.Linger,
                                        new LingerOption(true, _socketOptions.SoLinger.Value));
            }

            if (_socketOptions.ReceiveBufferSize != null)
            {
                newSock.ReceiveBufferSize = _socketOptions.ReceiveBufferSize.Value;
            }

            if (_socketOptions.SendBufferSize != null)
            {
                newSock.ReceiveBufferSize = _socketOptions.SendBufferSize.Value;
            }

            if (_socketOptions.TcpNoDelay != null)
            {
                newSock.NoDelay = _socketOptions.TcpNoDelay.Value;
            }


            var connectionResult = newSock.BeginConnect(_serverAddress, _port, null, null);

            connectionResult.AsyncWaitHandle.WaitOne(_socketOptions.ConnectTimeoutMillis);

            if (!newSock.Connected)
            {
                //The socket is still open
                newSock.Close();
                throw new SocketException((int)SocketError.TimedOut);
            }

            _socket = newSock;
            _bufferingMode.Reset();

            if (protocolOptions.SslOptions == null)
            {
                _socketStream = new NetworkStream(_socket);
            }
            else
            {
                string targetHost;
                try
                {
                    targetHost = Dns.GetHostEntry(_serverAddress).HostName;
                }
                catch (SocketException ex)
                {
                    targetHost = serverAddress.ToString();
                    Logger.Error(
                        String.Format(
                            "SSL connection: Can not resolve address {0}. Using the IP address instead of the hostname. This may cause RemoteCertificateNameMismatch error during Cassandra host authentication. Note that the Cassandra node SSL certificate's CN(Common Name) must match the Cassandra node hostname.",
                            _serverAddress), ex);
                }

                _socketStream = new SslStream(new NetworkStream(_socket), false, protocolOptions.SslOptions.RemoteCertValidationCallback, null);
                (_socketStream as SslStream).AuthenticateAsClient(targetHost, new X509CertificateCollection(), protocolOptions.SslOptions.SslProtocol,
                                                                  false);
            }

            if (IsHealthy)
            {
                BeginReading();
            }
        }