File: System\ServiceModel\Channels\NamedPipeTransportBindingElement.cs
Web Access
Project: ..\..\..\src\System.ServiceModel.NetNamedPipe\src\System.ServiceModel.NetNamedPipe.csproj (System.ServiceModel.NetNamedPipe)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
 
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.Versioning;
using System.Security.Principal;
 
namespace System.ServiceModel.Channels
{
    [SupportedOSPlatform("windows")]
    public class NamedPipeTransportBindingElement : ConnectionOrientedTransportBindingElement
    {
        private readonly List<SecurityIdentifier> _allowedUsers = new List<SecurityIdentifier>();
        private readonly NamedPipeConnectionPoolSettings _connectionPoolSettings = new NamedPipeConnectionPoolSettings();
        private readonly NamedPipeSettings _namedPipeSettings = new NamedPipeSettings();
 
        public NamedPipeTransportBindingElement() : base()
        {
            if (!RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
            {
                throw ExceptionHelper.PlatformNotSupported(SR.Format(SR.PlatformNotSupported_NetNamedPipe));
            }
        }
 
        protected NamedPipeTransportBindingElement(NamedPipeTransportBindingElement elementToBeCloned)
            : base(elementToBeCloned)
        {
            if (elementToBeCloned._allowedUsers != null)
            {
                foreach (SecurityIdentifier id in elementToBeCloned._allowedUsers)
                {
                    _allowedUsers.Add(id);
                }
            }
 
            _connectionPoolSettings = elementToBeCloned._connectionPoolSettings.Clone();
            _namedPipeSettings = elementToBeCloned._namedPipeSettings.Clone();
        }
 
        public NamedPipeConnectionPoolSettings ConnectionPoolSettings => _connectionPoolSettings;
 
        public NamedPipeSettings PipeSettings => _namedPipeSettings;
 
        public override string Scheme => Uri.UriSchemeNetPipe;
 
        public override BindingElement Clone() => new NamedPipeTransportBindingElement(this);
 
        public override IChannelFactory<TChannel> BuildChannelFactory<TChannel>(BindingContext context)
        {
            if (context == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(context));
            }
 
            if (!CanBuildChannelFactory<TChannel>(context))
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgument(nameof(TChannel), SR.Format(SR.ChannelTypeNotSupported, typeof(TChannel)));
            }
 
            return (IChannelFactory<TChannel>)(object)new NamedPipeChannelFactory<TChannel>(this, context);
        }
 
        public override T GetProperty<T>(BindingContext context)
        {
            if (context == null)
            {
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperArgumentNull(nameof(context));
            }
            if (typeof(T) == typeof(IBindingDeliveryCapabilities))
            {
                return (T)(object)new BindingDeliveryCapabilitiesHelper();
            }
            if (typeof(T) == typeof(NamedPipeSettings))
            {
                return (T)(object)PipeSettings;
            }
            else
            {
                return base.GetProperty<T>(context);
            }
        }
 
        internal override bool IsMatch(BindingElement b)
        {
            if (!base.IsMatch(b))
            {
                return false;
            }
 
            if (b is not NamedPipeTransportBindingElement namedPipe)
            {
                return false;
            }
 
            return ConnectionPoolSettings.IsMatch(namedPipe.ConnectionPoolSettings)
                && PipeSettings.IsMatch(namedPipe.PipeSettings);
        }
 
        private class BindingDeliveryCapabilitiesHelper : IBindingDeliveryCapabilities
        {
            internal BindingDeliveryCapabilitiesHelper()
            {
            }
            bool IBindingDeliveryCapabilities.AssuresOrderedDelivery => true;
 
            bool IBindingDeliveryCapabilities.QueuedDelivery => false;
        }
    }
}