Пример #1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="SecureTransport"/> class.
        /// </summary>
        /// <param name="configuration">Configuration of the transport</param>
        /// <param name="instrumentation">Instrumentation object for getting notification of the internal state</param>
        /// <param name="cancellationToken">Cancellation token</param>
        public SecureTransport(Configuration configuration, ISecureTransportInstrumentation instrumentation, CancellationToken cancellationToken)
        {
            this.transportId = Interlocked.Increment(ref SecureTransport.lastAssignedTransportId);

            if (configuration == null)
            {
                throw new ArgumentNullException(nameof(configuration));
            }

            if (configuration.CommunicationProtocolVersion == 0)
            {
                throw new ArgumentException("CommunicationProtocolVersion must be specified");
            }

            this.configuration = configuration;

            if (configuration.UseSecureConnection)
            {
                var sslConfiguration = new SslConnection.Configuration()
                {
                    ClientCertificates = configuration.ClientCertificates,
                    ServerCertificates = configuration.ServerCertificates,
                    RemoteCertificateValidationCallback = configuration.RemoteCertificateValidationCallback,
                    LocalCertificateSelectionCallback   = configuration.LocalCertificateSelectionCallback,
                    IsClientCertificateRequired         = configuration.IsClientCertificateRequired,
                    MustCheckCertificateRevocation      = configuration.MustCheckCertificateRevocation,
                    MustCheckCertificateTrustChain      = configuration.MustCheckCertificateTrustChain,
                    ExplicitRule           = configuration.ExplicitRule,
                    Identities             = configuration.Identities,
                    SubjectValidations     = configuration.SubjectValidations,
                    StartAsClient          = configuration.AuthAsClient,
                    BlacklistedThumbprints = configuration.BlacklistedThumbprints,
                };

                this.secureConnectionPolicy = new SslConnection(this.transportId, sslConfiguration);
            }
            else
            {
                this.secureConnectionPolicy = new NoSslConnection();
            }

            this.acceptConnectionsSemaphore = new SemaphoreSlim(this.configuration.MaxConnections, this.configuration.MaxConnections);
            this.MaxConnectionLifeSpan      = configuration.MaxConnectionLifespan;
            this.rootCancellationToken      = cancellationToken;
            this.instrumentation            = instrumentation ?? NoInstrumentation.Instance;
        }