1 //------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //------------------------------------------------------------
5 namespace System.ServiceModel.Channels
7 using System.Collections.Generic;
9 using System.ServiceModel;
11 class InputChannel : InputQueueChannel<Message>, IInputChannel
13 EndpointAddress localAddress;
15 public InputChannel(ChannelManagerBase channelManager, EndpointAddress localAddress)
16 : base(channelManager)
18 this.localAddress = localAddress;
21 public EndpointAddress LocalAddress
23 get { return localAddress; }
26 public override T GetProperty<T>()
28 if (typeof(T) == typeof(IInputChannel))
30 return (T)(object)this;
33 T baseProperty = base.GetProperty<T>();
34 if (baseProperty != null)
42 protected override IAsyncResult OnBeginOpen(TimeSpan timeout, AsyncCallback callback, object state)
44 return new CompletedAsyncResult(callback, state);
47 protected override void OnEndOpen(IAsyncResult result)
49 CompletedAsyncResult.End(result);
52 protected override void OnOpen(TimeSpan timeout)
56 public virtual Message Receive()
58 return this.Receive(this.DefaultReceiveTimeout);
61 public virtual Message Receive(TimeSpan timeout)
63 if (timeout < TimeSpan.Zero)
64 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
65 new ArgumentOutOfRangeException("timeout", timeout, SR.GetString(SR.SFxTimeoutOutOfRange0)));
69 return InputChannel.HelpReceive(this, timeout);
72 public virtual IAsyncResult BeginReceive(AsyncCallback callback, object state)
74 return this.BeginReceive(this.DefaultReceiveTimeout, callback, state);
77 public virtual IAsyncResult BeginReceive(TimeSpan timeout, AsyncCallback callback, object state)
79 if (timeout < TimeSpan.Zero)
81 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
82 new ArgumentOutOfRangeException("timeout", timeout, SR.GetString(SR.SFxTimeoutOutOfRange0)));
87 return InputChannel.HelpBeginReceive(this, timeout, callback, state);
90 public Message EndReceive(IAsyncResult result)
92 return InputChannel.HelpEndReceive(result);
95 public virtual bool TryReceive(TimeSpan timeout, out Message message)
97 if (timeout < TimeSpan.Zero)
99 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
100 new ArgumentOutOfRangeException("timeout", timeout, SR.GetString(SR.SFxTimeoutOutOfRange0)));
104 return base.Dequeue(timeout, out message);
107 public virtual IAsyncResult BeginTryReceive(TimeSpan timeout, AsyncCallback callback, object state)
109 if (timeout < TimeSpan.Zero)
111 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
112 new ArgumentOutOfRangeException("timeout", timeout, SR.GetString(SR.SFxTimeoutOutOfRange0)));
116 return base.BeginDequeue(timeout, callback, state);
119 public virtual bool EndTryReceive(IAsyncResult result, out Message message)
121 return base.EndDequeue(result, out message);
124 public bool WaitForMessage(TimeSpan timeout)
126 if (timeout < TimeSpan.Zero)
128 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
129 new ArgumentOutOfRangeException("timeout", timeout, SR.GetString(SR.SFxTimeoutOutOfRange0)));
133 return base.WaitForItem(timeout);
136 public IAsyncResult BeginWaitForMessage(TimeSpan timeout, AsyncCallback callback, object state)
138 if (timeout < TimeSpan.Zero)
139 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
140 new ArgumentOutOfRangeException("timeout", timeout, SR.GetString(SR.SFxTimeoutOutOfRange0)));
143 return base.BeginWaitForItem(timeout, callback, state);
146 public bool EndWaitForMessage(IAsyncResult result)
148 return base.EndWaitForItem(result);
151 #region static Helpers to convert TryReceive to Receive
152 internal static Message HelpReceive(IInputChannel channel, TimeSpan timeout)
155 if (channel.TryReceive(timeout, out message))
161 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(CreateReceiveTimedOutException(channel, timeout));
165 internal static IAsyncResult HelpBeginReceive(IInputChannel channel, TimeSpan timeout, AsyncCallback callback, object state)
167 return new HelpReceiveAsyncResult(channel, timeout, callback, state);
170 internal static Message HelpEndReceive(IAsyncResult result)
172 return HelpReceiveAsyncResult.End(result);
175 class HelpReceiveAsyncResult : AsyncResult
177 IInputChannel channel;
179 static AsyncCallback onReceive = Fx.ThunkCallback(new AsyncCallback(OnReceive));
182 public HelpReceiveAsyncResult(IInputChannel channel, TimeSpan timeout, AsyncCallback callback, object state)
183 : base(callback, state)
185 this.channel = channel;
186 this.timeout = timeout;
187 IAsyncResult result = channel.BeginTryReceive(timeout, onReceive, this);
189 if (!result.CompletedSynchronously)
194 HandleReceiveComplete(result);
198 public static Message End(IAsyncResult result)
200 HelpReceiveAsyncResult thisPtr = AsyncResult.End<HelpReceiveAsyncResult>(result);
201 return thisPtr.message;
204 void HandleReceiveComplete(IAsyncResult result)
206 if (!this.channel.EndTryReceive(result, out this.message))
208 throw DiagnosticUtility.ExceptionUtility.ThrowHelperError(
209 InputChannel.CreateReceiveTimedOutException(this.channel, this.timeout));
213 static void OnReceive(IAsyncResult result)
215 if (result.CompletedSynchronously)
220 HelpReceiveAsyncResult thisPtr = (HelpReceiveAsyncResult)result.AsyncState;
221 Exception completionException = null;
224 thisPtr.HandleReceiveComplete(result);
226 #pragma warning suppress 56500 // Microsoft, transferring exception to another thread
234 completionException = e;
237 thisPtr.Complete(false, completionException);
241 static Exception CreateReceiveTimedOutException(IInputChannel channel, TimeSpan timeout)
243 if (channel.LocalAddress != null)
245 return new TimeoutException(SR.GetString(SR.ReceiveTimedOut, channel.LocalAddress.Uri.AbsoluteUri, timeout));
249 return new TimeoutException(SR.GetString(SR.ReceiveTimedOutNoLocalAddress, timeout));