2 // ChannelListenerBase.cs
4 // Author: Atsushi Enomoto (atsushi@ximian.com)
6 // Copyright (C) 2005 Novell, Inc (http://www.novell.com)
8 // Permission is hereby granted, free of charge, to any person obtaining
9 // a copy of this software and associated documentation files (the
10 // "Software"), to deal in the Software without restriction, including
11 // without limitation the rights to use, copy, modify, merge, publish,
12 // distribute, sublicense, and/or sell copies of the Software, and to
13 // permit persons to whom the Software is furnished to do so, subject to
14 // the following conditions:
16 // The above copyright notice and this permission notice shall be
17 // included in all copies or substantial portions of the Software.
19 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
20 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
21 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
22 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
23 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
24 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
25 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 using System.Collections.Generic;
29 using System.ServiceModel;
31 namespace System.ServiceModel.Channels
33 internal abstract class InternalChannelListenerBase<TChannel>
34 : ChannelListenerBase<TChannel>
35 where TChannel : class, IChannel
37 protected InternalChannelListenerBase ()
42 protected InternalChannelListenerBase (IDefaultCommunicationTimeouts timeouts)
47 Func<TimeSpan,TChannel> accept_channel_delegate;
48 Func<TimeSpan,bool> wait_delegate;
49 Action<TimeSpan> open_delegate, close_delegate;
51 protected override IAsyncResult OnBeginAcceptChannel (
52 TimeSpan timeout, AsyncCallback callback,
55 if (accept_channel_delegate == null)
56 accept_channel_delegate = new Func<TimeSpan,TChannel> (OnAcceptChannel);
57 return accept_channel_delegate.BeginInvoke (timeout, callback, asyncState);
60 protected override TChannel OnEndAcceptChannel (IAsyncResult result)
62 if (accept_channel_delegate == null)
63 throw new InvalidOperationException ("Async AcceptChannel operation has not started");
64 return accept_channel_delegate.EndInvoke (result);
67 protected override IAsyncResult OnBeginWaitForChannel (
68 TimeSpan timeout, AsyncCallback callback, object state)
70 if (wait_delegate == null)
71 wait_delegate = new Func<TimeSpan,bool> (OnWaitForChannel);
72 return wait_delegate.BeginInvoke (timeout, callback, state);
75 protected override bool OnEndWaitForChannel (IAsyncResult result)
77 if (wait_delegate == null)
78 throw new InvalidOperationException ("Async WaitForChannel operation has not started");
79 return wait_delegate.EndInvoke (result);
82 protected override IAsyncResult OnBeginOpen (TimeSpan timeout,
83 AsyncCallback callback, object state)
85 if (open_delegate == null)
86 open_delegate = new Action<TimeSpan> (OnOpen);
87 return open_delegate.BeginInvoke (timeout, callback, state);
90 protected override void OnEndOpen (IAsyncResult result)
92 if (open_delegate == null)
93 throw new InvalidOperationException ("Async Open operation has not started");
94 open_delegate.EndInvoke (result);
97 protected override IAsyncResult OnBeginClose (TimeSpan timeout,
98 AsyncCallback callback, object state)
100 if (close_delegate == null)
101 close_delegate = new Action<TimeSpan> (OnClose);
102 return close_delegate.BeginInvoke (timeout, callback, state);
105 protected override void OnEndClose (IAsyncResult result)
107 if (close_delegate == null)
108 throw new InvalidOperationException ("Async Close operation has not started");
109 close_delegate.EndInvoke (result);
113 public abstract class ChannelListenerBase<TChannel>
114 : ChannelListenerBase, IChannelListener<TChannel>,
115 IChannelListener, ICommunicationObject
116 where TChannel : class, IChannel
118 IDefaultCommunicationTimeouts timeouts;
120 protected ChannelListenerBase ()
121 : this (DefaultCommunicationTimeouts.Instance)
125 protected ChannelListenerBase (
126 IDefaultCommunicationTimeouts timeouts)
128 if (timeouts == null)
129 throw new ArgumentNullException ("timeouts");
130 this.timeouts = timeouts;
133 public TChannel AcceptChannel ()
135 return AcceptChannel (timeouts.ReceiveTimeout);
138 public TChannel AcceptChannel (TimeSpan timeout)
140 ThrowIfDisposedOrNotOpen ();
141 return OnAcceptChannel (timeout);
144 public IAsyncResult BeginAcceptChannel (
145 AsyncCallback callback, object asyncState)
147 return BeginAcceptChannel (
148 timeouts.ReceiveTimeout, callback, asyncState);
151 public IAsyncResult BeginAcceptChannel (TimeSpan timeout,
152 AsyncCallback callback, object asyncState)
154 return OnBeginAcceptChannel (timeout, callback, asyncState);
157 public TChannel EndAcceptChannel (IAsyncResult result)
159 return OnEndAcceptChannel (result);
162 protected abstract TChannel OnAcceptChannel (TimeSpan timeout);
164 protected abstract IAsyncResult OnBeginAcceptChannel (TimeSpan timeout,
165 AsyncCallback callback, object asyncState);
167 protected abstract TChannel OnEndAcceptChannel (IAsyncResult result);