1 //-----------------------------------------------------------------------------
2 // Copyright (c) Microsoft Corporation. All rights reserved.
3 //-----------------------------------------------------------------------------
5 namespace System.ServiceModel.Dispatcher
9 using System.ServiceModel;
11 class ErrorHandlingAcceptor
13 readonly ChannelDispatcher dispatcher;
14 readonly IListenerBinder binder;
16 internal ErrorHandlingAcceptor(IListenerBinder binder, ChannelDispatcher dispatcher)
20 Fx.Assert("binder is null");
22 if (dispatcher == null)
24 Fx.Assert("dispatcher is null");
28 this.dispatcher = dispatcher;
35 this.binder.Listener.Close();
47 void HandleError(Exception e)
49 if (this.dispatcher != null)
51 this.dispatcher.HandleError(e);
55 void HandleErrorOrAbort(Exception e)
57 if ((this.dispatcher == null) || !this.dispatcher.HandleError(e))
59 // We only stop if the listener faults. It is a bug
60 // if the listener is in an invalid state and does not
61 // fault. So there are no cases today where this aborts.
65 internal bool TryAccept(TimeSpan timeout, out IChannelBinder channelBinder)
69 channelBinder = this.binder.Accept(timeout);
70 if (channelBinder != null)
72 this.dispatcher.PendingChannels.Add(channelBinder.Channel);
76 catch (CommunicationObjectAbortedException)
81 catch (CommunicationObjectFaultedException)
86 catch (TimeoutException)
91 catch (CommunicationException e)
103 this.HandleErrorOrAbort(e);
104 channelBinder = null;
109 internal IAsyncResult BeginTryAccept(TimeSpan timeout, AsyncCallback callback, object state)
113 return this.binder.BeginAccept(timeout, callback, state);
115 catch (CommunicationObjectAbortedException)
117 return new ErrorHandlingCompletedAsyncResult(true, callback, state);
119 catch (CommunicationObjectFaultedException)
121 return new ErrorHandlingCompletedAsyncResult(true, callback, state);
123 catch (TimeoutException)
125 return new ErrorHandlingCompletedAsyncResult(false, callback, state);
127 catch (CommunicationException e)
130 return new ErrorHandlingCompletedAsyncResult(false, callback, state);
138 this.HandleErrorOrAbort(e);
139 return new ErrorHandlingCompletedAsyncResult(false, callback, state);
143 internal bool EndTryAccept(IAsyncResult result, out IChannelBinder channelBinder)
145 ErrorHandlingCompletedAsyncResult handlerResult = result as ErrorHandlingCompletedAsyncResult;
146 if (handlerResult != null)
148 channelBinder = null;
149 return ErrorHandlingCompletedAsyncResult.End(handlerResult);
155 channelBinder = this.binder.EndAccept(result);
156 if (channelBinder != null)
158 this.dispatcher.PendingChannels.Add(channelBinder.Channel);
162 catch (CommunicationObjectAbortedException)
164 channelBinder = null;
167 catch (CommunicationObjectFaultedException)
169 channelBinder = null;
172 catch (TimeoutException)
174 channelBinder = null;
177 catch (CommunicationException e)
180 channelBinder = null;
189 this.HandleErrorOrAbort(e);
190 channelBinder = null;
196 internal void WaitForChannel()
200 this.binder.Listener.WaitForChannel(TimeSpan.MaxValue);
202 catch (CommunicationObjectAbortedException) { }
203 catch (CommunicationObjectFaultedException) { }
204 catch (CommunicationException e)
214 this.HandleErrorOrAbort(e);
218 internal IAsyncResult BeginWaitForChannel(AsyncCallback callback, object state)
222 return this.binder.Listener.BeginWaitForChannel(TimeSpan.MaxValue, callback, state);
224 catch (CommunicationObjectAbortedException)
226 return new WaitCompletedAsyncResult(callback, state);
228 catch (CommunicationObjectFaultedException)
230 return new WaitCompletedAsyncResult(callback, state);
232 catch (CommunicationException e)
235 return new WaitCompletedAsyncResult(callback, state);
243 this.HandleErrorOrAbort(e);
244 return new WaitCompletedAsyncResult(callback, state);
248 internal void EndWaitForChannel(IAsyncResult result)
250 WaitCompletedAsyncResult handlerResult = result as WaitCompletedAsyncResult;
251 if (handlerResult != null)
253 WaitCompletedAsyncResult.End(handlerResult);
259 this.binder.Listener.EndWaitForChannel(result);
261 catch (CommunicationObjectAbortedException) { }
262 catch (CommunicationObjectFaultedException) { }
263 catch (CommunicationException e)
273 this.HandleErrorOrAbort(e);
278 class ErrorHandlingCompletedAsyncResult : CompletedAsyncResult<bool>
280 internal ErrorHandlingCompletedAsyncResult(bool data, AsyncCallback callback, object state)
281 : base(data, callback, state)
286 class WaitCompletedAsyncResult : CompletedAsyncResult
288 internal WaitCompletedAsyncResult(AsyncCallback callback, object state)
289 : base(callback, state)