internal static IListenerBinder GetBinder(IChannelListener listener, MessageVersion messageVersion)
 {
     IChannelListener<IInputChannel> listener2 = listener as IChannelListener<IInputChannel>;
     if (listener2 != null)
     {
         return new InputListenerBinder(listener2, messageVersion);
     }
     IChannelListener<IInputSessionChannel> listener3 = listener as IChannelListener<IInputSessionChannel>;
     if (listener3 != null)
     {
         return new InputSessionListenerBinder(listener3, messageVersion);
     }
     IChannelListener<IReplyChannel> listener4 = listener as IChannelListener<IReplyChannel>;
     if (listener4 != null)
     {
         return new ReplyListenerBinder(listener4, messageVersion);
     }
     IChannelListener<IReplySessionChannel> listener5 = listener as IChannelListener<IReplySessionChannel>;
     if (listener5 != null)
     {
         return new ReplySessionListenerBinder(listener5, messageVersion);
     }
     IChannelListener<IDuplexChannel> listener6 = listener as IChannelListener<IDuplexChannel>;
     if (listener6 != null)
     {
         return new DuplexListenerBinder(listener6, messageVersion);
     }
     IChannelListener<IDuplexSessionChannel> listener7 = listener as IChannelListener<IDuplexSessionChannel>;
     if (listener7 == null)
     {
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(System.ServiceModel.SR.GetString("UnknownListenerType1", new object[] { listener.Uri.AbsoluteUri })));
     }
     return new DuplexSessionListenerBinder(listener7, messageVersion);
 }
Example #2
0
        /// <summary>
        /// Creates a new <see cref="IDeviceListener"/> instance.
        /// </summary>
        /// <param name="channelListener">The channel listener that will be used to listen for events.</param>
        /// <returns>A new <see cref="IDeviceListener"/> instance.</returns>
        /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="channelListener"/> is <c>null</c>.</exception>
        public static IDeviceListener Create(IChannelListener channelListener)
        {
            Contract.Requires<ArgumentNullException>(channelListener != null, "channelListener");
            Contract.Ensures(Contract.Result<IDeviceListener>() != null);

            return new DeviceListener(channelListener);
        }
Example #3
0
 private static Task<IDuplexSessionChannel> AcceptChannelAsync(IChannelListener<IDuplexSessionChannel> listener)
 {
     return Task.Factory.FromAsync(
         (c, s) => ((IChannelListener<IDuplexSessionChannel>)s).BeginAcceptChannel(c, s),
         r => ((IChannelListener<IDuplexSessionChannel>)r.AsyncState).EndAcceptChannel(r),
         listener);
 }
        internal static IListenerBinder GetBinder(IChannelListener listener, MessageVersion messageVersion)
        {
            IChannelListener<IInputChannel> input = listener as IChannelListener<IInputChannel>;
            if (input != null)
                return new InputListenerBinder(input, messageVersion);

            IChannelListener<IInputSessionChannel> inputSession = listener as IChannelListener<IInputSessionChannel>;
            if (inputSession != null)
                return new InputSessionListenerBinder(inputSession, messageVersion);

            IChannelListener<IReplyChannel> reply = listener as IChannelListener<IReplyChannel>;
            if (reply != null)
                return new ReplyListenerBinder(reply, messageVersion);

            IChannelListener<IReplySessionChannel> replySession = listener as IChannelListener<IReplySessionChannel>;
            if (replySession != null)
                return new ReplySessionListenerBinder(replySession, messageVersion);

            IChannelListener<IDuplexChannel> duplex = listener as IChannelListener<IDuplexChannel>;
            if (duplex != null)
                return new DuplexListenerBinder(duplex, messageVersion);

            IChannelListener<IDuplexSessionChannel> duplexSession = listener as IChannelListener<IDuplexSessionChannel>;
            if (duplexSession != null)
                return new DuplexSessionListenerBinder(duplexSession, messageVersion);

            throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(new InvalidOperationException(SR.GetString(SR.UnknownListenerType1, listener.Uri.AbsoluteUri)));
        }
        public void Open()
        {
            HttpBinding binding = new HttpBinding();
            this.ChannelListener = binding.BuildChannelListener<IReplyChannel>(this.BaseAddress);
            this.ChannelListener.Open();

            IReplyChannel channnel = this.ChannelListener.AcceptChannel();
            channnel.Open();

            while (true)
            {
                RequestContext requestContext = channnel.ReceiveRequest(TimeSpan.MaxValue);
                Message message = requestContext.RequestMessage;
                MethodInfo method = message.GetType().GetMethod("GetHttpRequestMessage");
                HttpRequestMessage request = (HttpRequestMessage)method.Invoke(message, new object[] { true });
                Task<HttpResponseMessage> processResponse = base.SendAsync(request, new CancellationTokenSource().Token);
                processResponse.ContinueWith(task =>
                    {
                        string httpMessageTypeName = "System.Web.Http.SelfHost.Channels.HttpMessage, System.Web.Http.SelfHost";
                        Type httpMessageType = Type.GetType(httpMessageTypeName);
                        Message reply = (Message)Activator.CreateInstance(httpMessageType, new object[] { task.Result });
                        requestContext.Reply(reply);
                    });
            }
        }
Example #6
0
        /// <summary>
        /// Initializes a new instance of the <see cref="DeviceListener"/> class.
        /// </summary>
        /// <param name="channelListener">The channel listener.</param>
        /// <exception cref="System.ArgumentNullException">Thrown when <paramref name="channelListener"/> is <c>null</c>.</exception>
        protected DeviceListener(IChannelListener channelListener)
        {
            Contract.Requires<ArgumentNullException>(channelListener != null, "channelListener");

            _channelListener = channelListener;
            _channelListener.EventReceived += Channel_EventReceived;
        }
Example #7
0
        public CustomChannelDispatcher(MyServiceManager serviceManager, IChannelListener<IReplyChannel> listener)
        {
            this.serviceManager = serviceManager;
            this.listener = listener;

            this.onReceive = new AsyncCallback(this.OnReceive);
            this.acceptCallback = new WaitCallback(this.AcceptCallback);
        }
 public SecurityListenerSettingsLifetimeManager(SecurityProtocolFactory securityProtocolFactory, SecuritySessionServerSettings sessionSettings, bool sessionMode, IChannelListener innerListener)
 {
     this.securityProtocolFactory = securityProtocolFactory;
     this.sessionSettings = sessionSettings;
     this.sessionMode = sessionMode;
     this.innerListener = innerListener;
     this.referenceCount = 1;
 }
 public SecurityListenerSettingsLifetimeManager(SecurityProtocolFactory securityProtocolFactory, SecuritySessionServerSettings sessionSettings, bool sessionMode, IChannelListener innerListener)
 {
     this.securityProtocolFactory = securityProtocolFactory;
     this.sessionSettings = sessionSettings;
     this.sessionMode = sessionMode;
     this.innerListener = innerListener;
     // have a reference right from the start so that the state can be aborted before open
     referenceCount = 1;
 }
Example #10
0
        public void Load(string category, string hash, IChannelListener listener)
        {
            /* Load data in a separate thread, because we're an asynchronous load method. */

            new Thread(delegate() {
                Channel channel = new Channel("Cached-Substream-Channel", ChannelType.TYPE_SUBSTREAM, null);
                listener.ChannelHeader(channel, null);
                listener.ChannelData(channel, Load(category, hash));
                listener.ChannelEnd(channel);
            }).Start();
        }
Example #11
0
        public Channel(string name, ChannelType type, IChannelListener listener)
        {
            this._id = Channel._nextId++;
            this._name = name + "-" + this.Id;
            this._state = ChannelState.STATE_HEADER;
            this._type = type;
            this._headerLength = 0;
            this._dataLength = 0;
            this._listener = listener;

            /* Force data state for AES key channel. */
            if (this._type == ChannelType.TYPE_AESKEY)
                this._state = ChannelState.STATE_DATA;
        }
Example #12
0
        protected void WaitForChannel(IChannelListener listener, bool async, TimeSpan timeout)
        {
            bool ret = false;

            if (async)
            {
                IAsyncResult result = listener.BeginWaitForChannel(timeout, null, null);
                ret = listener.EndWaitForChannel(result);
            }
            else
            {
                ret = listener.WaitForChannel(timeout);
            }

            this.Results.Add(String.Format("WaitForChannel returned {0}", ret));
        }
        public async Task StartAsync()
        {
            if (listener != null)
            {
                throw new InvalidOperationException(ExceptionMessageListenerHasAlreadyBeenStarted);
            }

            try
            {
                var tcpRelayTransportBindingElement =
                    new TcpRelayTransportBindingElement(RelayClientAuthenticationType.RelayAccessToken)
                    {
                        TransferMode = TransferMode.Buffered,
                        ConnectionMode = TcpRelayConnectionMode.Relayed,
                        IsDynamic = (relayAddressType == RelayAddressType.Dynamic),
                        ManualAddressing = true
                    };
                tcpRelayTransportBindingElement.GetType()
                    .GetProperty("TransportProtectionEnabled",
                        BindingFlags.GetProperty | BindingFlags.Instance | BindingFlags.NonPublic)
                    .SetValue(tcpRelayTransportBindingElement, true);

                var tb = new TransportClientEndpointBehavior(tokenProvider);
                this.listenerBinding = new CustomBinding(
                    new BinaryMessageEncodingBindingElement(),
                    tcpRelayTransportBindingElement);
                
                listener = listenerBinding.BuildChannelListener<IDuplexSessionChannel>(new Uri(address), tb);
                await Task.Factory.FromAsync(listener.BeginOpen, listener.EndOpen, null);
            }
            catch
            {
                listener = null;
                throw;
            }
        }
        Type BuildChannelListener(StuffPerListenUriInfo stuff,
                                  ServiceHostBase serviceHost,
                                  Uri listenUri,
                                  ListenUriMode listenUriMode,
                                  bool supportContextSession,
                                  out IChannelListener result)
        {
            Binding originalBinding = stuff.Endpoints[0].Binding;
            CustomBinding binding = new CustomBinding(originalBinding);
            BindingParameterCollection parameters = stuff.Parameters;

            Uri listenUriBaseAddress;
            string listenUriRelativeAddress;
            GetBaseAndRelativeAddresses(serviceHost, listenUri, binding.Scheme, out listenUriBaseAddress, out listenUriRelativeAddress);

            InternalDuplexBindingElement internalDuplex = null;
            InternalDuplexBindingElement.AddDuplexListenerSupport(binding, ref internalDuplex);

            // All types are supported to start
            bool reply = true;
            bool replySession = true;
            bool input = true;
            bool inputSession = true;
            bool duplex = true;
            bool duplexSession = true;
            string sessionContractName = null;
            string datagramContractName = null;
            // each endpoint adds constraints
            for (int i = 0; i < stuff.Endpoints.Count; ++i)
            {
                ContractDescription contract = stuff.Endpoints[i].Contract;
                if (contract.SessionMode == SessionMode.Required)
                {
                    sessionContractName = contract.Name;
                }
                if (contract.SessionMode == SessionMode.NotAllowed)
                {
                    datagramContractName = contract.Name;
                }

                System.Collections.IList endpointTypes = GetSupportedChannelTypes(contract);
                if (!endpointTypes.Contains(typeof(IReplyChannel)))
                {
                    reply = false;
                }
                if (!endpointTypes.Contains(typeof(IReplySessionChannel)))
                {
                    replySession = false;
                }
                if (!endpointTypes.Contains(typeof(IInputChannel)))
                {
                    input = false;
                }
                if (!endpointTypes.Contains(typeof(IInputSessionChannel)))
                {
                    inputSession = false;
                }
                if (!endpointTypes.Contains(typeof(IDuplexChannel)))
                {
                    duplex = false;
                }
                if (!endpointTypes.Contains(typeof(IDuplexSessionChannel)))
                {
                    duplexSession = false;
                }
            }

            if ((sessionContractName != null) && (datagramContractName != null))
            {
                string text = SR.GetString(SR.SFxCannotRequireBothSessionAndDatagram3, datagramContractName, sessionContractName, binding.Name);
                Exception error = new InvalidOperationException(text);
                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(error);
            }

            List<Type> supportedChannelTypes = new List<Type>();
            if (input)
            {
                supportedChannelTypes.Add(typeof(IInputChannel));
            }
            if (inputSession)
            {
                supportedChannelTypes.Add(typeof(IInputSessionChannel));
            }
            if (reply)
            {
                supportedChannelTypes.Add(typeof(IReplyChannel));
            }
            if (replySession)
            {
                supportedChannelTypes.Add(typeof(IReplySessionChannel));
            }
            if (duplex)
            {
                supportedChannelTypes.Add(typeof(IDuplexChannel));
            }
            if (duplexSession)
            {
                supportedChannelTypes.Add(typeof(IDuplexSessionChannel));
            }
            // now we know what channel types we can use to support the contracts at this ListenUri
            Type returnValue = DispatcherBuilder.MaybeCreateListener(true, supportedChannelTypes.ToArray(), binding, parameters,
                                                                     listenUriBaseAddress, listenUriRelativeAddress, listenUriMode, serviceHost.ServiceThrottle, out result,
                                                                     supportContextSession && sessionContractName != null);
            if (result == null)
            {
                // we put a lot of work into creating a good error message, as this is a common case
                Dictionary<Type, byte> setOfChannelTypesSupportedByBinding = new Dictionary<Type, byte>();
                if (binding.CanBuildChannelListener<IInputChannel>())
                {
                    setOfChannelTypesSupportedByBinding.Add(typeof(IInputChannel), 0);
                }
                if (binding.CanBuildChannelListener<IReplyChannel>())
                {
                    setOfChannelTypesSupportedByBinding.Add(typeof(IReplyChannel), 0);
                }
                if (binding.CanBuildChannelListener<IDuplexChannel>())
                {
                    setOfChannelTypesSupportedByBinding.Add(typeof(IDuplexChannel), 0);
                }
                if (binding.CanBuildChannelListener<IInputSessionChannel>())
                {
                    setOfChannelTypesSupportedByBinding.Add(typeof(IInputSessionChannel), 0);
                }
                if (binding.CanBuildChannelListener<IReplySessionChannel>())
                {
                    setOfChannelTypesSupportedByBinding.Add(typeof(IReplySessionChannel), 0);
                }
                if (binding.CanBuildChannelListener<IDuplexSessionChannel>())
                {
                    setOfChannelTypesSupportedByBinding.Add(typeof(IDuplexSessionChannel), 0);
                }

                throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ChannelRequirements.CantCreateListenerException(
                                                                              setOfChannelTypesSupportedByBinding.Keys, supportedChannelTypes, originalBinding.Name));
            }
            return returnValue;
        }
 static internal Type MaybeCreateListener(bool actuallyCreate, Type[] supportedChannels,
                                          Binding binding, BindingParameterCollection parameters,
                                          Uri listenUriBaseAddress, string listenUriRelativeAddress,
                                          ListenUriMode listenUriMode, ServiceThrottle throttle,
                                          out IChannelListener result)
 {
     return MaybeCreateListener(actuallyCreate, supportedChannels, binding, parameters, listenUriBaseAddress, listenUriRelativeAddress, listenUriMode, throttle,
         out result, false);
 }
 public uint Subscribe(IChannelListener listener)
 {
     //when a message comes in over udp, we want to fire the listener.MessageArrived function
     uint token = this.tokenCount++;
     Console.WriteLine("Subscribe req for udp channel. Token: " + token.ToString());
     if (this.sock == null)
         BuildSocket();
     lock (this.listeners)
     {
         this.listeners.Add(token, listener);
     }
     return token;
 }
Example #17
0
 public ServiceHost(IChannelListener channelListener)
     : base(channelListener)
 {
 }
 internal ReplySessionListenerBinder(IChannelListener<IReplySessionChannel> listener, MessageVersion messageVersion)
 {
     this.listener = listener;
     this.messageVersion = messageVersion;
 }
 internal DuplexSessionListenerBinder(IChannelListener<IDuplexSessionChannel> listener, MessageVersion messageVersion)
 {
     this.correlator = new RequestReplyCorrelator();
     this.listener = listener;
     this.messageVersion = messageVersion;
 }
 private System.Type BuildChannelListener(StuffPerListenUriInfo stuff, ServiceHostBase serviceHost, Uri listenUri, ListenUriMode listenUriMode, bool supportContextSession, out IChannelListener result)
 {
     Uri uri;
     string str;
     Binding binding = stuff.Endpoints[0].Binding;
     CustomBinding binding2 = new CustomBinding(binding);
     BindingParameterCollection parameters = stuff.Parameters;
     this.GetBaseAndRelativeAddresses(serviceHost, listenUri, binding2.Scheme, out uri, out str);
     InternalDuplexBindingElement internalDuplexBindingElement = null;
     InternalDuplexBindingElement.AddDuplexListenerSupport(binding2, ref internalDuplexBindingElement);
     bool flag = true;
     bool flag2 = true;
     bool flag3 = true;
     bool flag4 = true;
     bool flag5 = true;
     bool flag6 = true;
     string name = null;
     string str3 = null;
     for (int i = 0; i < stuff.Endpoints.Count; i++)
     {
         ContractDescription contract = stuff.Endpoints[i].Contract;
         if (contract.SessionMode == SessionMode.Required)
         {
             name = contract.Name;
         }
         if (contract.SessionMode == SessionMode.NotAllowed)
         {
             str3 = contract.Name;
         }
         IList supportedChannelTypes = GetSupportedChannelTypes(contract);
         if (!supportedChannelTypes.Contains(typeof(IReplyChannel)))
         {
             flag = false;
         }
         if (!supportedChannelTypes.Contains(typeof(IReplySessionChannel)))
         {
             flag2 = false;
         }
         if (!supportedChannelTypes.Contains(typeof(IInputChannel)))
         {
             flag3 = false;
         }
         if (!supportedChannelTypes.Contains(typeof(IInputSessionChannel)))
         {
             flag4 = false;
         }
         if (!supportedChannelTypes.Contains(typeof(IDuplexChannel)))
         {
             flag5 = false;
         }
         if (!supportedChannelTypes.Contains(typeof(IDuplexSessionChannel)))
         {
             flag6 = false;
         }
     }
     if ((name != null) && (str3 != null))
     {
         Exception exception = new InvalidOperationException(System.ServiceModel.SR.GetString("SFxCannotRequireBothSessionAndDatagram3", new object[] { str3, name, binding2.Name }));
         throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(exception);
     }
     List<System.Type> requiredChannels = new List<System.Type>();
     if (flag3)
     {
         requiredChannels.Add(typeof(IInputChannel));
     }
     if (flag4)
     {
         requiredChannels.Add(typeof(IInputSessionChannel));
     }
     if (flag)
     {
         requiredChannels.Add(typeof(IReplyChannel));
     }
     if (flag2)
     {
         requiredChannels.Add(typeof(IReplySessionChannel));
     }
     if (flag5)
     {
         requiredChannels.Add(typeof(IDuplexChannel));
     }
     if (flag6)
     {
         requiredChannels.Add(typeof(IDuplexSessionChannel));
     }
     System.Type type = MaybeCreateListener(true, requiredChannels.ToArray(), binding2, parameters, uri, str, listenUriMode, serviceHost.ServiceThrottle, out result, supportContextSession && (name != null));
     if (result != null)
     {
         return type;
     }
     Dictionary<System.Type, byte> dictionary = new Dictionary<System.Type, byte>();
     if (binding2.CanBuildChannelListener<IInputChannel>(new object[0]))
     {
         dictionary.Add(typeof(IInputChannel), 0);
     }
     if (binding2.CanBuildChannelListener<IReplyChannel>(new object[0]))
     {
         dictionary.Add(typeof(IReplyChannel), 0);
     }
     if (binding2.CanBuildChannelListener<IDuplexChannel>(new object[0]))
     {
         dictionary.Add(typeof(IDuplexChannel), 0);
     }
     if (binding2.CanBuildChannelListener<IInputSessionChannel>(new object[0]))
     {
         dictionary.Add(typeof(IInputSessionChannel), 0);
     }
     if (binding2.CanBuildChannelListener<IReplySessionChannel>(new object[0]))
     {
         dictionary.Add(typeof(IReplySessionChannel), 0);
     }
     if (binding2.CanBuildChannelListener<IDuplexSessionChannel>(new object[0]))
     {
         dictionary.Add(typeof(IDuplexSessionChannel), 0);
     }
     throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(ChannelRequirements.CantCreateListenerException(dictionary.Keys, requiredChannels, binding.Name));
 }
 public void Open()
 {
     this.replyChannelListener = this.upstreamBinding.BuildChannelListener<IReplyChannel>(this.upstreamUri, credentials);
     this.replyChannelListener.Open();
     this.replyChannelListener.BeginAcceptChannel(ChannelAccepted, replyChannelListener);
 }
 private static System.Type MaybeCreateListener(bool actuallyCreate, System.Type[] supportedChannels, Binding binding, BindingParameterCollection parameters, Uri listenUriBaseAddress, string listenUriRelativeAddress, ListenUriMode listenUriMode, ServiceThrottle throttle, out IChannelListener result, bool supportContextSession)
 {
     result = null;
     for (int i = 0; i < supportedChannels.Length; i++)
     {
         System.Type type = supportedChannels[i];
         if ((type == typeof(IInputChannel)) && binding.CanBuildChannelListener<IInputChannel>(parameters))
         {
             if (actuallyCreate)
             {
                 result = binding.BuildChannelListener<IInputChannel>(listenUriBaseAddress, listenUriRelativeAddress, listenUriMode, parameters);
             }
             return typeof(IInputChannel);
         }
         if ((type == typeof(IReplyChannel)) && binding.CanBuildChannelListener<IReplyChannel>(parameters))
         {
             if (actuallyCreate)
             {
                 result = binding.BuildChannelListener<IReplyChannel>(listenUriBaseAddress, listenUriRelativeAddress, listenUriMode, parameters);
             }
             return typeof(IReplyChannel);
         }
         if ((type == typeof(IDuplexChannel)) && binding.CanBuildChannelListener<IDuplexChannel>(parameters))
         {
             if (actuallyCreate)
             {
                 result = binding.BuildChannelListener<IDuplexChannel>(listenUriBaseAddress, listenUriRelativeAddress, listenUriMode, parameters);
             }
             return typeof(IDuplexChannel);
         }
         if ((type == typeof(IInputSessionChannel)) && binding.CanBuildChannelListener<IInputSessionChannel>(parameters))
         {
             if (actuallyCreate)
             {
                 result = binding.BuildChannelListener<IInputSessionChannel>(listenUriBaseAddress, listenUriRelativeAddress, listenUriMode, parameters);
             }
             return typeof(IInputSessionChannel);
         }
         if ((type == typeof(IReplySessionChannel)) && binding.CanBuildChannelListener<IReplySessionChannel>(parameters))
         {
             if (actuallyCreate)
             {
                 result = binding.BuildChannelListener<IReplySessionChannel>(listenUriBaseAddress, listenUriRelativeAddress, listenUriMode, parameters);
             }
             return typeof(IReplySessionChannel);
         }
         if ((type == typeof(IDuplexSessionChannel)) && binding.CanBuildChannelListener<IDuplexSessionChannel>(parameters))
         {
             if (actuallyCreate)
             {
                 result = binding.BuildChannelListener<IDuplexSessionChannel>(listenUriBaseAddress, listenUriRelativeAddress, listenUriMode, parameters);
             }
             return typeof(IDuplexSessionChannel);
         }
     }
     for (int j = 0; j < supportedChannels.Length; j++)
     {
         System.Type type2 = supportedChannels[j];
         if ((type2 == typeof(IInputChannel)) && binding.CanBuildChannelListener<IInputSessionChannel>(parameters))
         {
             if (actuallyCreate)
             {
                 IChannelListener<IInputSessionChannel> inner = binding.BuildChannelListener<IInputSessionChannel>(listenUriBaseAddress, listenUriRelativeAddress, listenUriMode, parameters);
                 result = DatagramAdapter.GetInputListener(inner, throttle, binding);
             }
             return typeof(IInputSessionChannel);
         }
         if ((type2 == typeof(IReplyChannel)) && binding.CanBuildChannelListener<IReplySessionChannel>(parameters))
         {
             if (actuallyCreate)
             {
                 IChannelListener<IReplySessionChannel> listener2 = binding.BuildChannelListener<IReplySessionChannel>(listenUriBaseAddress, listenUriRelativeAddress, listenUriMode, parameters);
                 result = DatagramAdapter.GetReplyListener(listener2, throttle, binding);
             }
             return typeof(IReplySessionChannel);
         }
         if ((supportContextSession && (type2 == typeof(IReplySessionChannel))) && (binding.CanBuildChannelListener<IReplyChannel>(parameters) && (binding.GetProperty<IContextSessionProvider>(parameters) != null)))
         {
             if (actuallyCreate)
             {
                 result = binding.BuildChannelListener<IReplyChannel>(listenUriBaseAddress, listenUriRelativeAddress, listenUriMode, parameters);
             }
             return typeof(IReplyChannel);
         }
     }
     return null;
 }
Example #23
0
        private void AcceptChannelAndReceive(IChannelListener<IInputChannel> listener)
        {
            IInputChannel channel;
            TransactionScope transactionToAbortOnAccept = null;

            if (this.Parameters.AbortTxDatagramAccept)
            {
                transactionToAbortOnAccept = new TransactionScope(TransactionScopeOption.RequiresNew);
            }

            if (this.Parameters.AsyncAccept)
            {
                IAsyncResult result = listener.BeginAcceptChannel(null, null);
                channel = listener.EndAcceptChannel(result);
            }
            else
            {
                channel = listener.AcceptChannel();
            }

            if (this.Parameters.AbortTxDatagramAccept)
            {
                transactionToAbortOnAccept.Dispose();
            }

            channel.Open();
            Message message;

            if (this.Parameters.CloseListenerEarly)
            {
                listener.Close();
            }

            try
            {
                using (TransactionScope ts = new TransactionScope(TransactionScopeOption.RequiresNew))
                {
                    Message firstMessage = channel.Receive(this.Parameters.ReceiveTimeout);

                    lock (this.Results)
                    {
                        this.Results.Add(String.Format("Received message with Action '{0}'", firstMessage.Headers.Action));
                    }

                    ts.Complete();
                }
            }
            catch (TimeoutException)
            {
                lock (this.Results)
                {
                    this.Results.Add("Receive timed out.");
                }

                channel.Abort();
                return;
            }

            AutoResetEvent doneReceiving = new AutoResetEvent(false);
            int threadsCompleted = 0;

            for (int i = 0; i < this.Parameters.NumberOfThreads; ++i)
            {
                ThreadPool.QueueUserWorkItem(new WaitCallback(delegate(object unused)
                {
                    do
                    {
                        if (this.Parameters.ReceiverShouldAbort)
                        {
                            this.ReceiveMessage(channel, false);
                            Thread.Sleep(200);
                        }

                        message = this.ReceiveMessage(channel, true);
                    }
                    while (message != null);

                    if (Interlocked.Increment(ref threadsCompleted) == this.Parameters.NumberOfThreads)
                    {
                        doneReceiving.Set();
                    }
                }));
            }

            TimeSpan threadTimeout = TimeSpan.FromMinutes(2.0);
            if (!doneReceiving.WaitOne(threadTimeout, false))
            {
                this.Results.Add(String.Format("Threads did not complete within {0}.", threadTimeout));
            }

            channel.Close();
        }
 internal DuplexListenerBinder(IChannelListener<IDuplexChannel> listener, System.ServiceModel.Channels.MessageVersion messageVersion)
 {
     this.listener = listener;
     this.messageVersion = messageVersion;
 }
 internal ReplySessionListenerBinder(IChannelListener<IReplySessionChannel> listener, System.ServiceModel.Channels.MessageVersion messageVersion)
 {
     this.listener = listener;
     this.messageVersion = messageVersion;
 }
		public static SvcHttpHandler GetHandlerForListener (IChannelListener listener)
		{
			return handlers.Values.First (h => h.Host.ChannelDispatchers.Any (cd => cd.Listener == listener));
		}
        static Type MaybeCreateListener(bool actuallyCreate, Type[] supportedChannels,
                                                 Binding binding, BindingParameterCollection parameters,
                                                 Uri listenUriBaseAddress, string listenUriRelativeAddress,
                                                 ListenUriMode listenUriMode, ServiceThrottle throttle,
                                                 out IChannelListener result, bool supportContextSession)
        {
            // if actuallyCreate is true, then this behaves like CreateListener()
            // else this behaves like CanCreateListener()
            // result is channel type that was (would be) created, null if can't create
            // 
            // Ugly API helps refactor common code in these two similar-but-different methods

            result = null;

            for (int i = 0; i < supportedChannels.Length; i++)
            {
                Type channelType = supportedChannels[i];

                if (channelType == typeof(IInputChannel))
                {
                    if (binding.CanBuildChannelListener<IInputChannel>(parameters))
                    {
                        if (actuallyCreate)
                        {
                            result = binding.BuildChannelListener<IInputChannel>(listenUriBaseAddress, listenUriRelativeAddress, listenUriMode, parameters);
                        }
                        return typeof(IInputChannel);
                    }
                }
                if (channelType == typeof(IReplyChannel))
                {
                    if (binding.CanBuildChannelListener<IReplyChannel>(parameters))
                    {
                        if (actuallyCreate)
                        {
                            result = binding.BuildChannelListener<IReplyChannel>(listenUriBaseAddress, listenUriRelativeAddress, listenUriMode, parameters);
                        }
                        return typeof(IReplyChannel);
                    }
                }
                if (channelType == typeof(IDuplexChannel))
                {
                    if (binding.CanBuildChannelListener<IDuplexChannel>(parameters))
                    {
                        if (actuallyCreate)
                        {
                            result = binding.BuildChannelListener<IDuplexChannel>(listenUriBaseAddress, listenUriRelativeAddress, listenUriMode, parameters);
                        }
                        return typeof(IDuplexChannel);
                    }
                }
                if (channelType == typeof(IInputSessionChannel))
                {
                    if (binding.CanBuildChannelListener<IInputSessionChannel>(parameters))
                    {
                        if (actuallyCreate)
                        {
                            result = binding.BuildChannelListener<IInputSessionChannel>(listenUriBaseAddress, listenUriRelativeAddress, listenUriMode, parameters);
                        }
                        return typeof(IInputSessionChannel);
                    }
                }
                if (channelType == typeof(IReplySessionChannel))
                {
                    if (binding.CanBuildChannelListener<IReplySessionChannel>(parameters))
                    {
                        if (actuallyCreate)
                        {
                            result = binding.BuildChannelListener<IReplySessionChannel>(listenUriBaseAddress, listenUriRelativeAddress, listenUriMode, parameters);
                        }
                        return typeof(IReplySessionChannel);
                    }
                }
                if (channelType == typeof(IDuplexSessionChannel))
                {
                    if (binding.CanBuildChannelListener<IDuplexSessionChannel>(parameters))
                    {
                        if (actuallyCreate)
                        {
                            result = binding.BuildChannelListener<IDuplexSessionChannel>(listenUriBaseAddress, listenUriRelativeAddress, listenUriMode, parameters);
                        }
                        return typeof(IDuplexSessionChannel);
                    }
                }
            }

            // If the binding does not support the type natively, try to adapt
            for (int i = 0; i < supportedChannels.Length; i++)
            {
                Type channelType = supportedChannels[i];

                // For SessionMode.Allowed or SessionMode.NotAllowed we will accept session-ful variants as well and adapt them
                if (channelType == typeof(IInputChannel))
                {
                    if (binding.CanBuildChannelListener<IInputSessionChannel>(parameters))
                    {
                        if (actuallyCreate)
                        {
                            IChannelListener<IInputSessionChannel> temp = binding.BuildChannelListener<IInputSessionChannel>(listenUriBaseAddress, listenUriRelativeAddress, listenUriMode, parameters);
                            result = DatagramAdapter.GetInputListener(temp, throttle, binding);
                        }
                        return typeof(IInputSessionChannel);
                    }
                }

                if (channelType == typeof(IReplyChannel))
                {
                    if (binding.CanBuildChannelListener<IReplySessionChannel>(parameters))
                    {
                        if (actuallyCreate)
                        {
                            IChannelListener<IReplySessionChannel> temp = binding.BuildChannelListener<IReplySessionChannel>(listenUriBaseAddress, listenUriRelativeAddress, listenUriMode, parameters);
                            result = DatagramAdapter.GetReplyListener(temp, throttle, binding);
                        }
                        return typeof(IReplySessionChannel);
                    }
                }

                if (supportContextSession)
                {
                    // and for SessionMode.Required, it is possible that the InstanceContextProvider is handling the session management, so 
                    // accept datagram variants if that is the case
                    if (channelType == typeof(IReplySessionChannel))
                    {
                        if (binding.CanBuildChannelListener<IReplyChannel>(parameters)
                            && binding.GetProperty<IContextSessionProvider>(parameters) != null)
                        {
                            if (actuallyCreate)
                            {
                                result = binding.BuildChannelListener<IReplyChannel>(listenUriBaseAddress, listenUriRelativeAddress, listenUriMode, parameters);
                            }
                            return typeof(IReplyChannel);
                        }
                    }
                }
            }

            return null;
        }
 internal InputListenerBinder(IChannelListener<IInputChannel> listener, MessageVersion messageVersion)
 {
     this.listener = listener;
     this.messageVersion = messageVersion;
 }
        void ChannelAccepted(IAsyncResult result)
        {
            try
            {
                IReplyChannel replyChannel = replyChannelListener.EndAcceptChannel(result);
                if (replyChannel != null)
                {
                    try
                    {
                        replyChannel.Open();
                        replyChannel.BeginReceiveRequest(RequestAccepted, replyChannel);
                    }
                    catch
                    {
                        replyChannel.Abort();
                    }

                    if (replyChannelListener.State == CommunicationState.Opened)
                    {
                        this.replyChannelListener.BeginAcceptChannel(ChannelAccepted, replyChannelListener);
                    }
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e);
                this.replyChannelListener.Abort();
                this.replyChannelListener = null;
            }
        }
 /// <summary>Add a connection listener.</summary>
 /// <param name="channelListener">The listener.</param>
 public virtual void AddChannelListener(IChannelListener channelListener) { this.channelListener.AddDelegate(channelListener); }